Old Wollies
Posts: 7
Joined: Fri Sep 08, 2017 7:52 pm

mcp3008 readings

Sun Sep 17, 2017 1:02 pm

Hi All,
I am trying to get this script working but failed despite days of grappling blind, without enough knowledge.
The input of CH1) of the mcp3008 varies between 3,3 and 1,6V by 10k +10k potmeter divider.
Please be assured that there is no wiring fault, grant me that much after 50 years in ICT.

Code: Select all

#!/usr/bin/python
 
# Import Libraries
import spidev
import time
 
# SPI intialisation
spi = spidev.SpiDev()
spi.open(0,0)
 
# Function to get the readings from the mcp3008
  def read_spi(channel):
  spi.max_speed_hz = 1000000
  spidata = spi.xfer2([1,(8+channel)<<4,0])
  print("Raw ADC:      {}".format(spidata))
  data = ((spidata[1] & 3) << 8) + spidata[2]
  return data
 
try:
  while True:
    channeldata = read_spi(7)
    voltage = round(((channeldata * 3300) / 1024),0)
    print("Data (dec)    {}".format(channeldata))
    print("Data (bin)    {}".format(('{0:010b}'.format(channeldata))))
    print("Voltage (mV): {}".format(voltage))
    print("--------------------")
    time.sleep(1)
 
except KeyboardInterrupt:
  spi.close()
The script runs ok but the returned readings are erratic and do in no way reflect the input.
The output seems to rise and fall cyclically. The valeus rise to 265 and then falls to 0 and rises again and so on.
Varying the potmeter has no visible effect on the reading.

Any suggestions?
Thanks

B.Goode
Posts: 5560
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: mcp3008 readings

Sun Sep 17, 2017 2:33 pm

Any suggestions?
A couple of things.

Python syntax relies heavily on indentation, and despite your efforts the forum software removes indentation from plain text.

The [ Code] button, 5th from the left at the top of the message edit screen, will insert markup that will maintain the indentation of your script. Your message can be modified retrospectively.

The gpiozero Python library module has built-in support for mcp3008 ADC convertors. http://gpiozero.readthedocs.io/en/stable/api_spi.html

Even if you want to persist and write your own code, maybe a quick comparison using this library would show whether your OS has been set up for SPI.

Old Wollies
Posts: 7
Joined: Fri Sep 08, 2017 7:52 pm

Re: mcp3008 readings

Mon Sep 18, 2017 8:52 am

Hi,
Tks for the reply. It's very helpfull for a newby.
I will check out the site you gave.

btw is there on the forum something of a user guide or tutorial?

B.Goode
Posts: 5560
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: mcp3008 readings

Mon Sep 18, 2017 9:04 am

is there on the forum something of a user guide or tutorial?
Probably thousands of them! Guides to do what in particular?

Maybe start with the resources made available by the Raspberry Pi Foundation, for instance:

https://www.raspberrypi.org/help/

https://www.raspberrypi.org/resources/

pcmanbob
Posts: 3673
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: mcp3008 readings

Mon Sep 18, 2017 1:51 pm

Old Wollies wrote:
Sun Sep 17, 2017 1:02 pm


The script runs ok but the returned readings are erratic and do in no way reflect the input.
The output seems to rise and fall cyclically. The valeus rise to 265 and then falls to 0 and rises again and so on.
Varying the potmeter has no visible effect on the reading.

Any suggestions?
Thanks
Have you actually put a meter between the input pin and ground and measured the voltage you never know you could have a faulty component in your potential divider circuit.

once you have proved the potential divider circuit try connecting the input to a ground and see if you get a reliable zero result then, you never know you could have a bad mcp3008 or just a bad connection.

never assume just because you think its right it is, prove the hardware first.
Please only ask questions in the forum I will not answer questions sent to me directly via PM
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Old Wollies
Posts: 7
Joined: Fri Sep 08, 2017 7:52 pm

Re: mcp3008 readings

Tue Sep 19, 2017 9:26 am

Good idea, will try that first

Nauti
Posts: 8
Joined: Fri Dec 09, 2016 12:20 am

Re: mcp3008 readings

Thu Mar 01, 2018 11:53 pm

I have the same issue. Can't figure out what it is. Channel 1 to 7 on my MCP3008 reads fine. Channel 0 shows a nonsensical value. When I have my transistor switch on all the time that powers the sensors, the readings are fine. When I power them up 5 - 30 seconds before taking the readings, channel 0 shows nonsense.

Did you figure your issue out?

Idahowalker
Posts: 201
Joined: Wed Jan 03, 2018 5:43 pm

Re: mcp3008 readings

Fri Mar 02, 2018 12:56 am

Put a voltage divider as the input, instead of a pot, to see if the voltage divider produces a steady signal; like 1 2 !)K resistors from Vcc to gnd.
Being a programmer: She says go to the store get 1 bottle of milk, if they have eggs bring back 6. She gets mad cause I brought back 6 bottles of milk.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 10477
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: mcp3008 readings

Fri Mar 02, 2018 2:18 pm

added code tags.

Return to “Beginners”

Who is online

Users browsing this forum: piglet, scotty101 and 12 guests