mcp3008 read error

Moderators: grovkillen, Stuntteam, TD-er, enesbcs

Post Reply
Message
Author
User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

mcp3008 read error

#1 Post by uxhamby » 21 Nov 2023, 05:39

HI,

I have a fairly vanilla RPiEasy system (RPIEasy 2.3.273) running on an older RPi.
Type: RPI-Linux-apt (supported)
OS: linux 6.1.0-rpi4-rpi-v6
OS full name: Raspbian GNU/Linux 12 (bookworm)
Hardware: Pi 1 Model B 512MB
I have an MCP3008 ADC connected as follows:
RaspberryPi MCP3008
Pin 1 (3.3V) Pin 16 (VDD)
Pin 1 (3.3V) Pin 15 (VREF)
Pin 6 (GND) Pin 14 (AGND)
Pin 23 (SCLK) Pin 13 (CLK)
Pin 21 (MISO) Pin 12 (DOUT)
Pin 19 (MOSI) Pin 11 (DIN)
Pin 24 (CE0) Pin 10 (CS/SHDN)
Pin 6 (GND) Pin 9 (DGND)
I have run raspi-config and confirmed that SPI bus is enabled on the RPi system itself.

I have created & enabled RPiEASY "Device / Tasks" for channel 0 and channel 1 of the MCP3008 as
Device: Analog input - MCP3008/3208
Name: GarageWestDoor
Enabled: chk
SPI line: SPI0
SPI device num: CE0
ADC type: MCP3008
Channel number: CH0

(and similar for GarageEastDoor on Channel number: CH1)

From the console of the RPi, I have run python test routine:

Code: Select all

from spidev import SpiDev

class MCP3008:
    def __init__(self, bus = 0, device = 0):
        self.bus, self.device = bus, device
        self.spi = SpiDev()
        self.open()
        self.spi.max_speed_hz = 1000000 # 1MHz

    def open(self):
        self.spi.open(self.bus, self.device)
        self.spi.max_speed_hz = 1000000 # 1MHz
    
    def read(self, channel = 0):
        adc = self.spi.xfer2([1, (8 + channel) << 4, 0])
        data = ((adc[1] & 3) << 8) + adc[2]
        return data
            
    def close(self):
        self.spi.close()

Code: Select all

from MCP3008 import MCP3008

adc = MCP3008()
value = adc.read( channel = 0 ) 
value1 = adc.read( channel = 1 )
print("Applied voltage: %.2f" % (value / 1023.0 * 3.3) )
print("Applied voltage1: %.2f" % (value1 / 1023.0 * 3.3) )

. . . . . . which reports believable readings for voltages prevailing at the inputs of the MCP3008 as measured with DVM.

I have dutifully rebooted the entire system, after effecting setup changes.

I only get error messages however, when attempting to read from the MCP3008, from within the RPIEASY application, as follows:
21:33:03 Webserver starting at http://192.168.55.151:80
21:33:43 MCP3008 read failed!
22:37:32 MCP3008 read failed!
22:42:33 MCP3008 read failed!
Obviously some facet of the black arts are eluding me.

Thoughts and advice from experience, would be welcome.

Thanks,
Brian H.

User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Re: mcp3008 read error

#2 Post by uxhamby » 25 Nov 2023, 18:10

Still stumped by this. Thoughts anyone?

Thanks,

Brian H.

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests