Other Parts Discussed in Thread: ADS1258
Tool/software:
I am trying to connect an ADS1258 ADC converter to an Arduino Mega 2560 microcontroller. My goal is to apply a signal to ADS1258, connect ADS1258 to Arduino and Arduino to a computer, and read the time-variant voltage on the computer. fixed mode I am doing. The connection as follow:
#include <SPI.h>
const int DRDY=2;
const int DOUT=50;
const int DIN=51;
const int SCLK=52;
const int CS=53;
/* ADS1258 minimal FIXED MODE driver (single‑ended AIN0…AIN7)*/
#pragma once
#include <SPI.h>
class ADS1258 {
public:
ADS1258(uint8_t cs, uint8_t drdy, uint8_t start, uint8_t reset)
: _cs(cs), _drdy(drdy), _start(start), _reset(reset) {}
void begin() {
pinMode(_cs, OUTPUT); digitalWrite(_cs, HIGH);
pinMode(_start, OUTPUT); digitalWrite(_start, LOW);
pinMode(_reset, OUTPUT); digitalWrite(_reset, HIGH);
pinMode(_drdy, INPUT);
SPI.begin();
SPI.beginTransaction(SPISettings(4000000UL, MSBFIRST, SPI_MODE0));
_hardwareReset(); // step 1
_writeRegisters(); // step 3
digitalWrite(_start, HIGH); // step 4
}
/* Blocks until DRDY falls, then returns raw 24‑bit sample and channel id */
bool readSample(int32_t &code, uint8_t &channelId) {
if (digitalRead(_drdy)) return false; // not ready yet
digitalWrite(_cs, LOW);
_spiTransfer(CMD_RDATA); // 0x30
uint8_t status = _spiTransfer(0x00);
uint32_t d = 0;
d |= (uint32_t)_spiTransfer(0x00) << 16;
d |= (uint32_t)_spiTransfer(0x00) << 8;
d |= (uint32_t)_spiTransfer(0x00);
digitalWrite(_cs, HIGH);
channelId = status & 0x08; // CHID[4:0] :contentReference[oaicite:8]{index=8}
/* sign‑extend 24‑bit two’s‑complement to 32 bit */
if (d & 0x800000) d |= 0xFF000000;
code = (int32_t)d;
return true;
}
private:
/* -------- low‑level helpers -------- */
static constexpr uint8_t CMD_RESET = 0xC0; // C[2:0]=110, MUL/A=0
static constexpr uint8_t CMD_WREG = 0x60; // C[2:0]=011, MUL=1, A=0000
static constexpr uint8_t CMD_RDATA = 0x30; // C[2:0]=001, MUL=1
void _hardwareReset() {
digitalWrite(_reset, LOW); delayMicroseconds(10);
digitalWrite(_reset, HIGH); delay(2); // ≥218 fCLK cycles :contentReference[oaicite:9]{index=9}
}
void _writeRegisters() {
static const uint8_t regImage[] = {
0x22, // CONFIG0 – status enabled, auto‑scan, STAT=1 :contentReference[oaicite:10]{index=10}
0x80, // CONFIG1 – DRATE[1:0]=11 (23.7 kSPS/ch) :contentReference[oaicite:11]{index=11}
//0x00, // MUXDIF – no differential pairs
0x08, // MUXCH – enable AIN0…7
// 0x00 // MUXSG1 – no AIN8…15
};
digitalWrite(_cs, LOW);
_spiTransfer(CMD_WREG);
for (uint8_t b : regImage) _spiTransfer(b);
digitalWrite(_cs, HIGH);
}
uint8_t _spiTransfer(uint8_t val) { return SPI.transfer(val); }
uint8_t _cs, _drdy, _start, _reset;
};
#include "ADS1258.h"
ADS1258 adc(53, 2, 4, 54); // CS=10, DRDY=2, START=3, RESET=4
void setup() {
Serial.begin(115200);
adc.begin();
}
void loop() {
int32_t code; uint8_t channelId;
if (adc.readSample(code,channelId)) {
float v = (code / 8388608.0) * 5.0; // example for VREF = 2.5 V unipolar
Serial.print("CH"); Serial.print(channelId);
Serial.print(": "); Serial.println(v, 6);
}
}
but it is not measuring the AIN0 instead on DOUT random number is appearing. Can you please help