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

Other projects • ADS1220, read register goes wrong, CS and DRDY remain high

$
0
0
Hi everyone!

I hope you're well and ready to help me ;-)

I'm trying to connect a ADS1220 (CJMCU-1220 brand) to a Raspberry Pi 4B, and then testing it (in differential A0A1) thanks to a programmable power supply. I can check the real tension thanks to a multimeter Keithley 2701.

My wiring is as follow:

Raspberry Pi ADS1220
----------------- -------------
3.3V (Pin 1) ---> AVDD (Pin 8), DVDD (Pin 6)
GND (Pin 6) ---> AGND (Pin 9), DGND (Pin 7)
MOSI (Pin 19) ---> MOSI (Pin 3)
MISO (Pin 21) ---> MISO (Pin 2)
SCLK (Pin 23) ---> SCLK (Pin 4)
CE0 (Pin 24) ---> CS (Pin 5)
GPIO17 (Pin 11) ---> DRDY (Pin 1)

And the python code I'm using is:

Code:

import spidevimport RPi.GPIO as GPIOimport timeimport csvimport osfrom datetime import datetime# Raspberry Pi pins connected to the ADS1220CS_PIN = 8  # Chip Select (GPIO8/CE0)DRDY_PIN = 17  # Data Ready (GPIO17)# SPI configurationSPI_BUS = 0SPI_DEVICE = 0SPI_SPEED_HZ = 1000000  # 1 MHz# ADS1220 commandsCMD_RESET = 0x06CMD_START = 0x08CMD_POWERDOWN = 0x02CMD_RDATA = 0x10CMD_WREG = 0x40CMD_RREG = 0x20# ADS1220 registersREG_CONF0 = 0x00REG_CONF1 = 0x01REG_CONF2 = 0x02REG_CONF3 = 0x03# Conversion parametersVREF = 2.048  # Volts (internal reference of ADS1220)FSR = (2**23) # Full scale range (24 bits with 1 sign bit)CSV_FILENAME = "ads1220_data.csv"def ask_gain():    print("Choose the gain for the ADS1220:")    print(" 1 : Gain = 1")    print(" 2 : Gain = 2")    print(" 4 : Gain = 4")    print(" 8 : Gain = 8")    print("16 : Gain = 16")    print("32 : Gain = 32")    print("64 : Gain = 64")    print("128: Gain = 128")    valid_gains = [1,2,4,8,16,32,64,128]    while True:        try:            g = int(input("Enter the desired gain value: "))            if g in valid_gains:                return g            else:                print("Invalid value. Try again.")        except Exception:            print("Invalid input. Try again.")def wait_drdy():    cpt = 0    while GPIO.input(DRDY_PIN):        time.sleep(0.001)        cpt += 1        if cpt % 1000 == 0:            print(f"[DEBUG] Still waiting for DRDY ({cpt} ms)")def ads1220_write_reg(spi, reg, value):    spi.xfer2([CMD_WREG | (reg << 2), 0x00, value])def ads1220_read_reg(spi, reg):    resp = spi.xfer2([CMD_RREG | (reg << 2), 0x00, 0x00])    return resp[2]def ads1220_read_data(spi):    resp = spi.xfer2([CMD_RDATA, 0x00, 0x00, 0x00])    raw = (resp[1] << 16) | (resp[2] << 8) | resp[3]    if raw & 0x800000:        raw -= 1 << 24    return rawdef ads1220_init(spi, gain):    # Converter reset    spi.xfer2([CMD_RESET])    time.sleep(0.1)    # MUX[3:0]=0000 (A0-A1), PGA enabled, user-selected gain    mux_bits = 0b0000 << 4    pga_enable = 0 << 3    # GAIN bits (2:0), according to table    gain_map = {        1: 0b000,        2: 0b001,        4: 0b010,        8: 0b011,        16: 0b100,        32: 0b101,        64: 0b110,        128: 0b111    }    gain_bits = gain_map[gain]    reg0 = mux_bits | pga_enable | gain_bits    reg1 = 0b00000000  # Data rate=20SPS, normal mode, single-shot conversion    reg2 = 0b00010000  # Internal VREF, 50/60Hz rejection    reg3 = 0x00        # Disable IDACs    ads1220_write_reg(spi, REG_CONF0, reg0)    ads1220_write_reg(spi, REG_CONF1, reg1)    ads1220_write_reg(spi, REG_CONF2, reg2)    ads1220_write_reg(spi, REG_CONF3, reg3)    # Register debug    val0 = ads1220_read_reg(spi, REG_CONF0)    print(f"[DEBUG] REG0 after init : {bin(val0)} (0x{val0:02X}), gain={gain}")def raw_to_voltage(raw):    v = (raw / float(FSR)) * (VREF)    return vdef append_to_csv(filename, row):    file_exists = os.path.isfile(filename)    with open(filename, 'a', newline='') as csvfile:        writer = csv.writer(csvfile, delimiter=';')        if not file_exists:            writer.writerow(['Timestamp', 'Raw_value', 'Diff_voltage_V', 'Gain'])        writer.writerow(row)def get_timestamp():    return datetime.now().strftime("%d/%m/%y - %H:%M:%S")def main():    print("[DEBUG] GPIO initialization")    GPIO.setmode(GPIO.BCM)    GPIO.setup(DRDY_PIN, GPIO.IN)    gain = ask_gain()    print(f"[DEBUG] SPI initialization (bus={SPI_BUS}, device={SPI_DEVICE}, speed={SPI_SPEED_HZ} Hz)")    spi = spidev.SpiDev()    spi.open(SPI_BUS, SPI_DEVICE)    spi.max_speed_hz = SPI_SPEED_HZ    spi.mode = 1    ads1220_init(spi, gain)    try:        while True:            spi.xfer2([CMD_START])            wait_drdy()            value_raw = ads1220_read_data(spi)            value_volts = raw_to_voltage(value_raw)            timestamp = get_timestamp()            print(f"{timestamp} ; {value_raw} ; {value_volts:.6f} ; {gain}")            append_to_csv(CSV_FILENAME, [timestamp, value_raw, f"{value_volts:.6f}", gain])            time.sleep(1)    finally:        print("[DEBUG] Closing SPI and cleaning up GPIO")        spi.close()        GPIO.cleanup()if __name__ == '__main__':    main()
The problem is that the read register function (line 112 : val0 = ads1220_read_reg(spi, REG_CONF0) ) always returns "0b11111111 (0xFF)" instead of something like "0b00000000".

I already check the SPI communication of my Raspberry Pi and it's working well.

I have to admit that I don't know if it's an hardware issue or software or both...

I hope you'll be able to help me!

Thank you in advance!

Su

Statistics: Posted by Suasua44 — Wed Jun 11, 2025 3:42 pm



Viewing all articles
Browse latest Browse all 8609

Trending Articles