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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 1:32 pm

So after obtaining an MCP3008 and a little bit of testing and research I can read 1 or all 8 channels with out any problems.

So your original code for single channel modified to work.



So was it just the spi rate-limiting you tweaked?



Looking back at my first response in this thread I can see I asked -
it seems the first pass through the whileTrue: loop, and thus the first call to readadc(0) returns plausible, though not necessarily 'correct' values. Why doesn't it work on the second pass?

Looks like the answer may have been "because the ADC convertor can't keep up with the RPi?" Simple once you know. Well spotted.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 2:02 pm

Yes just tweaked the spi max speed, after reading the data sheet for the MCP3008 and many web pages, spi.max_speed_hz=1000000 seemed to be the correct speed and the bit that was missing from the posted code, testing proved this to be correct.

some times all it takes is a bit of research and some testing to get things working, Ghwana could have found this by doing the same research I did.

it did also remove the 2 lines

Code: Select all

import cgitb
cgitb.enable()
As I could not see what they were supposed to do in this particular program.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Ghwana
Posts: 70
Joined: Mon Nov 13, 2017 12:04 pm

Re: MCP3008 Chip and Sharp ir distance sensor

Thu Apr 26, 2018 8:09 am

Thank you pcmanbob, I did see this in a forum about max speed, but didn't twig, thank you for all your help. Cheers Gwana

Ghwana
Posts: 70
Joined: Mon Nov 13, 2017 12:04 pm

Re: MCP3008 Chip and Sharp ir distance sensor

Wed May 16, 2018 1:56 pm

Hi , I'm currently running 7 sharp GP2Y0A41SK0F distance sensors that have all been tested and they all work OK. I'm using a MCP3008 chip and using this script below.

Code: Select all

#!/usr/bin/python
 
import spidev
import time

#Define Variables
delay = 0.5


#Create SPI
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz=1000000
 
def readadc(adcnum):
    # read SPI data from the MCP3008, 8 channels in total
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1, 8 + adcnum << 4, 0])
    data = ((r[1] & 3) << 8) + r[2]
    return data





print('Reading MCP3008 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)
# Main program loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = readadc(i)
    # Print the ADC values.
    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
    # Pause for half a second.
    time.sleep(0.5)
I'm just getting random readings, not sure why, does the code need changing? Please see results below.

Code: Select all

================= RESTART: /home/pi/Documents/IR-Distance.py =================
Reading MCP3008 values, press Ctrl-C to quit...
|    0 |    1 |    2 |    3 |    4 |    5 |    6 |    7 |
---------------------------------------------------------
|  793 |  749 |  722 |  698 |  535 |  482 |  524 |   27 |
|  917 |  941 |  870 |  852 |  793 |  824 |  885 |  144 |
|  559 |  853 |  711 |  753 |  773 |  668 |  652 |   42 |
|    6 |   46 |    0 |    0 |    0 |    0 |    0 |  122 |
|  909 |  919 |  841 |  845 |  642 |  533 |  261 |  207 |
|  950 |  938 |  892 |  897 |  945 |  809 |  917 |  139 |
|  112 |   65 |   58 |    0 |    0 |    0 |   20 |    0 |
|  883 |  860 |  720 |  868 | 1023 |  934 |  933 |   73 |
|   75 |   33 |    0 |    0 |    0 |    0 |    0 |    0 |
|   30 |   30 |    0 |    0 |    0 |    0 |    0 |  139 |
|  916 |  893 |  816 |  874 |  869 |  688 |  824 |  273 |
|  936 |  901 |  960 |  900 |  979 |  810 |  841 |  152 |
|   83 |   89 |   19 |   74 |   91 |    0 |    0 |    0 |
|  391 |  185 |   18 |   47 |   51 |    0 |    0 |    0 |
| 1023 | 1023 |  937 | 1023 |  852 |  760 |  847 |   62 |
|  890 |  912 |  866 |  853 |  895 |  751 |  797 |  137 |
|  880 |  836 |  733 |  663 |  742 |  639 |  834 |   64 |
|   46 |  122 |   18 |   36 |   61 |    0 |    0 |    0 |
|  860 |  922 |  678 |  728 |  760 |  596 |  691 |   31 |
The reflective surfaces are all about 5 cms away, so should all be reading between 1010 and 890 constantly as did all the sensors when tested individually. Is there something I should do to refine the code so I get accurate readings? I've tried 4 different MCP3008 chips, but all getting similar random readings. I'm using channels 0-6, so channel 7 is not being used.

Any ideas at all?

Ghwana
Posts: 70
Joined: Mon Nov 13, 2017 12:04 pm

Re: MCP3008 Chip and Sharp ir distance sensor

Wed May 16, 2018 2:18 pm

Hi, its all sorted, changed voltage from 5v to 3v feeding the sensors and everything is OK, go figure, sensors are rated upto 7v, not sure why they don't like 5v when all together!!

Return to “Troubleshooting”

Who is online

Users browsing this forum: bluePiuser, TexasZeEdiot and 28 guests