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

MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 10:19 am

Hi All, I've been following this https://github.com/meyskens/raspi-ir-distance, but keeps coming up with this error, the reading has no bearing of what is in front of the sensor, please see below, can anyone help?

Code: Select all

Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> 
================= RESTART: /home/pi/Documents/IR-Distance.py =================
4.54963942308

Traceback (most recent call last):
  File "/home/pi/Documents/IR-Distance.py", line 26, in <module>
    distance = (1.0 / (v / 13.15)) - 0.35
ZeroDivisionError: float division by zero
>>> 
This is the original code below

Code: Select all

import cgitb
import spidev
import RPi.GPIO as GPIO
import time
import sys

cgitb.enable()


spi = spidev.SpiDev()  # create spi object
spi.open(0, 0)  # open spi port 0, device (CS) 0, for the MCP8008


def readadc(adcnum):  # read out the ADC
    if ((adcnum > 7) or (adcnum < 0)):
        return -1
    r = spi.xfer2([1, (8 + adcnum) << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout


while True:
    v = readadc(0) * (3.3 / 1023.0)  # Convert value to voltage

    # Thanks to
    # http://davstott.me.uk/index.php/2013/06/02/raspberry-pi-sharp-infrared/
    distance = (1.0 / (v / 13.15)) - 0.35

    print distance
    time.sleep(1)
Not sure what is happening with this?

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 11:46 am

Ghwana wrote:
Tue Apr 24, 2018 10:19 am
Hi All, I've been following this https://github.com/meyskens/raspi-ir-distance, but keeps coming up with this error, the reading has no bearing of what is in front of the sensor, please see below, can anyone help?

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Documents/IR-Distance.py", line 26, in <module>
    distance = (1.0 / (v / 13.15)) - 0.35
ZeroDivisionError: float division by zero
>>> 
This is the original code below

Code: Select all

while True:
    v = readadc(0) * (3.3 / 1023.0)  # Convert value to voltage

    distance = (1.0 / (v / 13.15)) - 0.35

    
Not sure what is happening with this?
I'd say the line "# Convert value to voltage" is returning a 0 and this code is failing "distance = (1.0 / (v / 13.15)) - 0.35" because of the zero. Try printing the 'v' to see if 'v' being a 0 is the culprit.
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.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 1:16 pm

Hi the 0 is the MCP3008 first analog input, tried what you said, but still coming up with same error

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 1:23 pm

Ghwana wrote:
Tue Apr 24, 2018 1:16 pm
Hi the 0 is the MCP3008 first analog input, tried what you said, but still coming up with same error

So can you confirm that the value of v returned by readadc(0) * (3.3 / 1023.0) was non-zero?

Edit: 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?
Last edited by B.Goode on Tue Apr 24, 2018 1:35 pm, edited 1 time in total.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 1:29 pm

Ghwana wrote:
Tue Apr 24, 2018 1:16 pm
Hi the 0 is the MCP3008 first analog input, tried what you said, but still coming up with same error
When you tried what I wrote, a print statement of v (

Code: Select all

print(str(v))
), what value did your routine show v to be? The print of v should be right after the value is received and calculated from channel (0) of the A to D converter.

I understood that part of the code that indicated that the channel to use (read from) of the A/D converter is channel 0.

v is the only variable, that I saw, that would make the routine bomb at line 26 of the routine.
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.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 1:46 pm

Try this code.

Code: Select all

import cgitb
import spidev
import RPi.GPIO as GPIO
import time
import sys

cgitb.enable()


spi = spidev.SpiDev()  # create spi object
spi.open(0, 0)  # open spi port 0, device (CS) 0, for the MCP8008


def readadc(adcnum):  # read out the ADC
    if ((adcnum > 7) or (adcnum < 0)):
        return -1
    r = spi.xfer2([1, (8 + adcnum) << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout


while True:

    data = readadc(0)
    
    print " Return value form MCP" , data

    v = data * (3.3 / 1023.0)  # Convert value to voltage
    
    print " Converted to voltage ", v

    if v > 0:
        distance = (1.0 / (v / 13.15)) - 0.35
        print " Distance ",  distance
        
    else:
        print " v was zero so no distance calculation."
    time.sleep(1)
    
not tested so there may be errors.

The code should print the result of each stage of data collection and conversion, if v =0 then the distance part of the code is ignored so allowing the code to loop and try again.
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

Tue Apr 24, 2018 2:06 pm

Hi, have tried this and this is what is coming back, will try another MCP3008 chip, just incase this ones not working properly, looks like its only good for 1 read, then it reverts back to zero

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Documents/IR-Distance.py", line 30, in <module>
    print " Converted to voltage ", v
  File "/usr/lib/python2.7/idlelib/PyShell.py", line 1356, in write
    return self.shell.write(s, self.tags)
KeyboardInterrupt
>>> 
================= RESTART: /home/pi/Documents/IR-Distance.py =================
 Return value form MCP 12
 Converted to voltage  0.0387096774194
 Distance  339.358333333
 Return value form MCP 0
 Converted to voltage  0.0
 v was zero so no distance calculation.
 Return value form MCP 0
 Converted to voltage  0.0
 v was zero so no distance calculation.
 Return value form MCP 0
 Converted to voltage  0.0
 v was zero so no distance calculation.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 2:13 pm

looks like its only good for 1 read, then it reverts back to zero

Yes.

Now you need to find out why.

Maybe do more debugging by putting print statements like the ones @pcmanbob used into your readadc() function.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 2:19 pm

Hi looks like there is no power going to the sensor, I'm supplying it with 5V from rpi, changed sensor and chip now and still doing the same thing, just tested 5v from rpi, it is constant so not rpi, not sure, could be breadboard, maybe?

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 2:23 pm

just tried adc 0 through to 7 and all are doing the same thing, 1 read the rest are zero!!

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 2:33 pm

An idea to help you separate concerns about cabling and power from uncertainty about your code.

The gpiozero Python library module installed by default with the Raspbian Operating system has built-in support for mcp3008 adc convertor chips. It also has a number of very simple 'recipes' to demonstrate basic usage.

Perhaps you could test your hardware with one of the gpiozero samples?

http://gpiozero.readthedocs.io/en/stabl ... entiometer

Also see the following section 2.26

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 2:40 pm

So you need to eliminate the sensor as the cause of the problem.

so replace the sensor with a 10K variable resistor connected like this.

Image

use a meter to set the output to between 1 - 2V

then try running your code you should see the same value returned time after time, if not then its the mcp3008 or the software that's the problem, if you do get the same value returned all the time then its the sensor that's the problem.

As with all problems you have to eliminate possible causes one at a time.

edit.....
beaten to it but will leave post as example any way.
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

Tue Apr 24, 2018 3:07 pm

Hi I think it's the software, tried 3 other mcp3008 chips, still doing the same thing with the 10k resistor, I've tried installing spidev and software has already been installed, also run raspi-config and switched on spi and also enabled spi in pi configuration, not sure what else to do, maybe rpi is faulty

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 3:15 pm

not sure what else to do,

You could try reading the mcp3008 adc chip with a software package supported by the Raspberry Pi Foundation. Details in an earlier post...

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

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 3:31 pm

As you have not said what instructions you followed to connect the MCP3008 to the gpio.

just checking you have it connected correctly

VDD (Pin 16) wire this to 3.3V
VREF (Pin 15) wire this to 3.3V
AGND (Pin 14) wire this to ground
CLK (Pin 13) wire this to GPIO11 (Pin 23/SCLK)
DOUT (Pin 12) wire this to GPIO9 (Pin 21/MISO)
DIN (Pin 11) wire this to GPIO10 (Pin 19/MOSI)
CS (Pin 10) wire this to GPIO8 (Pin 24/CE0)
DGND (Pin 9) wire this to GROUND
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

jbudd
Posts: 655
Joined: Mon Dec 16, 2013 10:23 am

Re: MCP3008 Chip and Sharp ir distance sensor

Tue Apr 24, 2018 3:35 pm

This doesn't help with your 0 voltage issue but "distance = (1.0 / (v / 13.15)) - 0.35" hurts my eyes.
Isn't it the same sum as "13.15/v - 0.35" ?

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 7:42 am

Hi, yes went through the MCP3008 wiring with a fine tooth comb, have made sure that is absolutely correct, I'm going to try another RPI 3b and see what happens, might be a software issue, not sure until I try!

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 8:26 am

OK, so connected up new RPi with mcp 3008 chip, enabled spi on both sudo raspi-config and on preferences rpi configuration, am i missing anything, do I need to install any other software, still doing the same thing, fictious reading and then just 0s!!

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 8:32 am

Ghwana wrote:
Wed Apr 25, 2018 8:26 am
OK, so connected up new RPi with mcp 3008 chip, enabled spi on both sudo raspi-config and on preferences rpi configuration, am i missing anything, do I need to install any other software, still doing the same thing, fictious reading and then just 0s!!

Third and final attempt: what happens if you use the alternative implementation, which includes a very clear wiring diagram, provided by the Raspberry Pi Foundation to support this adc convertor? http://gpiozero.readthedocs.io/en/stabl ... entiometer

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 9:26 am

Hi, I don't have a potentiometer, so unable to do this project, ius there anyway I can incorporate the IR Sensor in this code?

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 9:59 am

Ghwana wrote:
Wed Apr 25, 2018 9:26 am
Hi, I don't have a potentiometer, so unable to do this project, ius there anyway I can incorporate the IR Sensor in this code?

Surely the point of your project is to read the variable voltage returned by the sensor and scale that value to represent a distance?

In the gpiozero recipe the potentiometer simply provides an alternative source for the varying voltage: it is not an essential component - you can substitute your own sensor. Without either you could test the two limiting values from the ADC convertor simply by using Ground and Vcc as two reference voltages.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 10:06 am

OK, do you have an example of what this would look like?

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 10:10 am

Is there specific software I'm supposed to use for the MCP3008 chip, as I've tried everything, went onto several forums, looked up drivers for MCP3008 without success, there must be a way to get this MCP3008 chip to work surely!!

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 11:45 am

I have nothing further to contribute to this discussion.

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

Re: MCP3008 Chip and Sharp ir distance sensor

Wed Apr 25, 2018 12:38 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.

Code: Select all

#!/usr/bin/python
import spidev
import RPi.GPIO as GPIO
import time
import sys

spi = spidev.SpiDev()  # create spi object
spi.open(0, 0)  # open spi port 0, device (CS) 0, for the MCP8008
spi.max_speed_hz=1000000

def readadc(adcnum):  # read out the ADC
    if ((adcnum > 7) or (adcnum < 0)):
        return -1
    r = spi.xfer2([1, (8 + adcnum) << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout


while True:
    v = readadc(0) * (3.3 / 1023.0)  # Convert value to voltage

    # Thanks to
    # http://davstott.me.uk/index.php/2013/06/02/raspberry-pi-sharp-infrared/
    distance = (1.0 / (v / 13.15)) - 0.35

    print distance
    time.sleep(1)
    
and just in case some one finding this wants to read all channels

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)
    
    
test both and working using pi3B+ running raspbian stretch lite
programs saved in /home/pi and run from command line.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Troubleshooting”

Who is online

Users browsing this forum: T-EGWZ34 and 48 guests