Hello everyone. I was trying to interface AD7606 from analog device with the ESP32S3, but i get odd readings. I've read through and followed instruction/sample from https://forum.arduino.cc/t/ad7606-with-arduino-via-spi/1116739 , managed to pick up the readings from AD7606 via SPI communication, but it's weird that the reading shows around 65533 (16-bit) when analog input into 0V/GND, and i get 32562 when connect analog input into 5V. I've set the RANGE to +-5V mode, but yet i get reading only 32562 instead of max 16bit value 65536.
Meanwhile, when i try to adjust the analog input using potentiometer, when reached 0V/GND, its value fluctuating between 0 and 65535, looks like the value overshoot back to FFFF.
I don't understand that why analog input connected to 0V, the readings show ~65533 (FFFD).
And when connected to 5V, i get ~32562. As I know, it can read +-5V range, so if 0V, shouldn't it be around half of the 16bit value = 32,768, and when 5V, it should be max value = 65536??
Below is my code:
#include <SPI.h>
#include <Arduino.h>
//#include "digitalWriteFast.h"
uint32_t startTime, elapsedTime, old_res;
#define SCALE_FACTOR 0.000152587890625 // 10v / 2^16 = 0.000152587890625
/* D14 and D15 should be grounded if you use this ad7606 boaSCK
* https://www.amazon.de/RELAND-SUN-Multi-Channel-Datenerfassungsmodul-Synchronisation/dp/B0B4HKSK8B/ref=sr_1_2?keywoSCKs=ad7606&qid=1681803018&sr=8-2
* i do not use oversampling in this example
* i set range to the ground in this example so the range is +/-5V
*/
#define BUSY 16
#define RESET 17
#define START_CONVERSION 18
#define SS 10
#define MISO 13
#define SCK 12
//#define MOSI 11
#define TOTAL_RAW_BYTES 16
#define OS0 5
#define OS1 6
#define OS2 7
#define RANGE 15
int bytesToRead = TOTAL_RAW_BYTES;
byte raw[TOTAL_RAW_BYTES];
uint16_t parsed[8];
int send_counter = 0;
//--------------SETUP-UP---------------------------
void setup() {
Serial.println("setup start");
initial();
Serial.begin(115200);
while (!Serial) {} // wait for usb connection
// SPI = SPIClass((uint8_t)HSPI);
SPI.begin(SCK, MISO, MOSI, SS);
// SPI.setHwCs(true);
startTime = micros();
Serial.println("setup end");
}
//---------------LOOP--------------------------
void loop() {
// Serial.println("start loop");
static int loopCount = 0;
// startTime = micros();
readData();
if (loopCount % 1000 == 0 ) {
uint32_t newStartTime = micros();
elapsedTime = newStartTime - startTime;
startTime = newStartTime;
Serial.print("Conversion time:\t\t\t\t\t\t\t");
Serial.print(elapsedTime / 1000);
Serial.println(" microseconds");
}
parseRawBytes();
if (loopCount % 1000 == 0) {
for (int i = 0; i < 8; i++) {
//Serial.print((float)parsed[i] * SCALE_FACTOR, 5);
Serial.print((int)parsed[i]); //print decimal value
Serial.print(",");
}
Serial.print("\r\n");
}
loopCount++;
// Serial.println("loop end");
}
//-----------------------------------------
void initial() {
pinMode(RANGE,OUTPUT);
pinMode(OS0,OUTPUT);
pinMode(OS1,OUTPUT);
pinMode(OS2,OUTPUT);
pinMode(BUSY, INPUT);
pinMode(RESET, OUTPUT);
pinMode(START_CONVERSION, OUTPUT);
pinMode(SS, OUTPUT);
pinMode(MISO, OUTPUT);
pinMode(SCK, OUTPUT);
digitalWrite(START_CONVERSION, LOW);
digitalWrite(SS, HIGH);
digitalWrite(RANGE,LOW);
digitalWrite(OS0,LOW); //设置为1次平均采样
digitalWrite(OS1,LOW);
digitalWrite(OS2,LOW);
reset(RESET);
}
//-----------------------------------------
void parseRawBytes() {
for (int i = 0; i < 8; i++) { //use 8 for ESP32
parsed[i] = (raw[i * 2] << 8) + raw[(i * 2) + 1];
}
}
//-----------------------------------------
/*reset signal*/
void reset(uint8_t port) {
digitalWrite(port, HIGH);
// delayMicroseconds(1);
digitalWrite(port, LOW);
// delayMicroseconds(1);
}
//-----------------------------------------
void conversionPulse(uint8_t port) {
digitalWrite(port, LOW);
// delayMicroseconds(1);
digitalWrite(port, HIGH);
}
//-----------------------------------------
/*
1- start conversion START_CONVERSION HIGH
2- wait until the busy is LOW again
3- put the CS to LOW
4- Read a byte from the SPI
*/
void readData()
{
conversionPulse(START_CONVERSION);
while (digitalRead(BUSY) == HIGH) {
// delayMicroseconds(1);
}
//SPI.beginTransaction(_spiSettings);
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE2));
digitalWrite(SS, LOW);
while (bytesToRead > 0) {
raw[TOTAL_RAW_BYTES - bytesToRead] = SPI.transfer(0x00);
bytesToRead--;
}
digitalWrite(SS, HIGH);
SPI.endTransaction();
bytesToRead = TOTAL_RAW_BYTES;
}