ADS131M02-q1 ADC与树莓派SPI通信读值始终为0问题排查
Your code has several critical issues causing the zero readings:
1. Unnecessary Repeated Reset
Sending the reset command (0x06) every loop iteration keeps the ADC stuck in a reset state—it never gets a chance to run conversions. Move the reset to the initialization phase only, then add a short delay for the ADC to boot up.
2. Incorrect SPI Data Retrieval
The ADS131M02-Q1 doesn’t support raw readbytes() calls. It uses a command-based SPI protocol. To get conversion data, you need to send the RDATA command (0x10). This command triggers the ADC to send back the latest conversion results for all channels (plus a status byte).
3. Wrong Data Format Handling
The ADC outputs 24-bit two's complement values, not 32-bit unsigned integers. Your current code misinterprets the data, which would lead to incorrect values even if you received valid data.
Fixed Code Example
import spidev import time # Initialize SPI bus spi = spidev.SpiDev() spi.open(0, 0) spi.mode = 0b01 # Correct mode for ADS131M02-Q1 (CPOL=0, CPHA=1) spi.max_speed_hz = 1000000 # 1MHz is safe, can go up to 20MHz if needed # Reset ADC once at startup spi.xfer([0x06]) time.sleep(0.1) # Wait for ADC to complete reset (datasheet specifies ~1ms, 0.1s is safe) while True: # Send RDATA command followed by 7 dummy bytes to receive response # Response structure: [status_byte, ch0_byte1, ch0_byte2, ch0_byte3, ch1_byte1, ch1_byte2, ch1_byte3] response = spi.xfer([0x10] + [0x00]*7) # Parse Channel 0 24-bit two's complement data ch0_raw = (response[1] << 16) | (response[2] << 8) | response[3] # Convert to signed integer if ch0_raw & 0x800000: ch0_raw -= 0x1000000 # Parse Channel 1 (if you're using it) ch1_raw = (response[4] << 16) | (response[5] << 8) | response[6] if ch1_raw & 0x800000: ch1_raw -= 0x1000000 # Calculate voltage (assuming VREF=2.5V, gain=1) vref = 2.5 gain = 1 ch0_voltage = ch0_raw * vref / (2**23 - 1) / gain ch1_voltage = ch1_raw * vref / (2**23 - 1) / gain print(f"Ch0 ADC Value: {ch0_raw}, Voltage: {ch0_voltage:.4f} V") print(f"Ch1 ADC Value: {ch1_raw}, Voltage: {ch1_voltage:.4f} V") time.sleep(0.1)
Even with fixed code, double-check these:
- Channel Enable: The ADC might have channels disabled by default. Use the WREG command to write to register 0x00 (CONFIG0) to enable your target channel(s).
- Input Configuration: Ensure your function generator's signal is connected to the correct ADC input (e.g., CH0INP/CH0INN) and that the input is configured for single-ended or differential mode as needed.
- Power Supply: Confirm the ADC's AVDD and DVDD pins are receiving the correct voltage (typically 3.3V) and that power rails are stable.
- Grounding: All devices (Raspberry Pi, ADC, oscillator, function generator) must share a common ground to avoid signal loss or noise.
内容的提问来源于stack exchange,提问作者Michael Parsons

