Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7043

Troubleshooting • MCP3208 Raspi-4 interface sends 0V output data after working for a while.

$
0
0
I'm tapping into a board that has multiple MCP3208s to read voltage data through SPI. It works perfectly fine and sends the correct data for a while and then it suddenly starts to send 0V readings as the output. Hard rebooting the raspi fixes this issue and correct voltages are printed out when the code is run, until it stops working. I checked the input channel pins with a multi-meter on the MCP3208, it is showing the correct voltages, nothing wrong with the ADC. I have the code attached below. What am I resetting with a hard reboot that is temporarily fixing this issue? I also tried 'rmmod' and 'modprobe' spidev and bcm_2835, it did not fix anything. Any help is appreciated. Thanks!


Code:

import spidevimport timeimport RPi.GPIO as GPIOGPIO.setmode(GPIO.BCM)# SPI Setupspi = spidev.SpiDev()spi.open(0, 0)spi.max_speed_hz = 1000000# The chip select pin is connected to GPIO 12, depending on it the data will be read or not readCS_ADC = 12GPIO.setup(CS_ADC, GPIO.OUT)# Function to read analog data from a specific channeldef analogRead(channel):    adc = spi.xfer2([6 | (channel >> 2), channel << 6, 0])  # 0 0 0 0  0 1 1 D2, D1 D0 0 0  0000, 00000000    data = ((adc[1] & 15) << 8) + adc[2]    adc_value = 3.3 * (data / (2 ** 12 - 1))  # vref*(value/(2**bitdepth-1))    return data# Delay in reading datadelay = 0.5# List of channels to read fromchannels = [0, 1, 2, 3, 4, 5, 6, 7]  # Modify this list as per your requirementstry:    while True:        for channel in channels:            GPIO.output(CS_ADC, GPIO.LOW)            value = analogRead(channel)            GPIO.output(CS_ADC, GPIO.HIGH)            print(f"Channel {channel}: {value:.3f}")            time.sleep(delay)except KeyboardInterrupt:    # Clean up GPIO on Ctrl+C exit    GPIO.cleanup()

Statistics: Posted by Samuel_Ranjith — Thu May 09, 2024 8:30 pm — Replies 0 — Views 27



Viewing all articles
Browse latest Browse all 7043

Trending Articles