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.


#!/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: 4401
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: 4401
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: 1788
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

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

Return to “Beginners”

Who is online

Users browsing this forum: Google [Bot], nunocv and 34 guests