Gerymake
Posts: 41
Joined: Sun Jan 06, 2019 11:38 pm

Reading LDR with MCP3008

Fri Jun 07, 2019 3:11 pm

Hello guys,

I am trying to read a photoresistor via an ADC (MCP3008) in a Raspberry Pi Zero WH. I have followed this wiring diagram.
Image

When it comes to the code, I came across two different snippets:
1.- With the gpizero library. It is working, I am getting coherent readings.

Code: Select all

#!/usr/bin/python
import time
from gpiozero import MCP3008
 
divider = MCP3008(0)
 
while True:
    print(divider.value)
    time.sleep(1.0)
2.- With the spidev library. It is showing wrong readings, not logical, I cover the sensor and it outputs the same value when using a flashlight. Furthermore, most of the time I am getting "0". Example:

Code: Select all

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
880
888
888
888
888
784
848
784
784
784
856
784
888

Code: Select all

import spidev
import time
 
#Define Variables
delay = 2
ldr_channel = 0
 
#Create SPI
spi = spidev.SpiDev()
spi.open(0, 0)
 
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
 
while True:
    ldr_value = readadc(ldr_channel)
    print("LDR Value: %d" % ldr_value)
    time.sleep(delay)
What might be the problem?

Thanks in advance,

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

Re: Reading LDR with MCP3008

Fri Jun 07, 2019 4:22 pm

In case it helps, the source code for the SPI device support implemented by gpiozero is here: https://gpiozero.readthedocs.io/en/stab ... vices.html

Maybe comparison of the two implementations might reveal what is wrong?

ghp
Posts: 1350
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Reading LDR with MCP3008

Fri Jun 07, 2019 4:55 pm

Perhaps you try
r = spi.xfer2([1, (8 + adcnum) << 4, 0])
instead of
r = spi.xfer2([1, 8 + adcnum << 4, 0])

User avatar
DougieLawson
Posts: 35353
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Reading LDR with MCP3008

Fri Jun 07, 2019 5:05 pm

Rather than showing a Fritzing diagram of what you think the wiring should look like, show a photo of your actual wiring.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Reading LDR with MCP3008

Fri Jun 07, 2019 6:00 pm

The reason your second lot of posted code does not return a value is because you are missing the line " spi.max_speed_hz=1000000 " and your " r = " line is missing some brackets.

So adding the line and brackets to your code results in this

Code: Select all

import spidev
import time
 
#Define Variables
delay = 2
ldr_channel = 0
 
#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
 
while True:
    ldr_value = readadc(ldr_channel)
    print("LDR Value: %d" % ldr_value)
    time.sleep(delay)
    
I have not tested this code but the SPI setup and the readadc parts now match my known working code.

A simple way to test your MCP3008 is to connect 3.3v to input 0 then you should get a maximum reading from the MCP3008, so you will then know the MCP3008 and your code is working.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Gerymake
Posts: 41
Joined: Sun Jan 06, 2019 11:38 pm

Re: Reading LDR with MCP3008

Sat Jun 15, 2019 3:33 pm

As it has already been mentioned, the problem was the frequency, it had to be limited. Anyway, I am a newbie at electronics and I do not fully understand what frequency are we limiting? I am familiar with this concept but I cannot relate to this case.

Thanks,

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

Re: Reading LDR with MCP3008

Sat Jun 15, 2019 3:45 pm

Gerymake wrote:
Sat Jun 15, 2019 3:33 pm
As it has already been mentioned, the problem was the frequency, it had to be limited. Anyway, I am a newbie at electronics and I do not fully understand what frequency are we limiting? I am familiar with this concept but I cannot relate to this case.

Thanks,

I don't think it is what most people would regard as 'electronics'. Integration of electronic components maybe..

If I understand correctly this option is limiting how often the RPi processor should go back to the the external interface to get a new reading, because the external ADC chip might not be returning valid data if you ask for another reading too quickly. The maximum 'refresh' rate you can hope for is somewhere in the datasheets for the microchip MCP series chips.

Return to “Python”