i am using this code to work with external ADC with arduino mega and it is work fine. However, the total Sample rate is lower than i need.
I achieved 10 KSPS with Arduino mega. But ad7606 has the capablity of 100KSPS with SPI.
therefore, i tried to use esp32 instead in order to get faster conversion time, However, i could not get reading from ad7606 maybe because i need to set the right spi (HSPI/VSPI) to the esp32
i tried out some codes but it does not yet worked for me.
could anyone help
thanks in advance
#include <SPI.h>
uint32_t startTime, elapsedTime, old_res;
/* D14 and D15 should be grounded if you use this ad7606 board
* https://www.amazon.de/RELAND-SUN-Multi-Channel-Datenerfassungsmodul-Synchronisation/dp/B0B4HKSK8B/ref=sr_1_2?keywords=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 SCALE_FACTOR 0.000152587890625 // (2*5V(volt range) / 2^16 bit)
#define BUSY 32 // you can use any digital pin
#define RESET 30 // you can use any digital pin
#define START_CONVERSION 34 // you can use any digital pin
#define CHIP_SELECT 53 // SPI CS
#define D7_out 50 // SPI MISO there is no need to use MOSI port with the ad7606
#define RD 52 // SPI SCLK
#define TOTAL_RAW_BYTES 16
SPISettings _spiSettings;
int bytesToRead = TOTAL_RAW_BYTES;
byte raw[TOTAL_RAW_BYTES];
uint16_t parsed[8];
//--------------SETUP-UP---------------------------
void setup()
{
initial();
Serial.begin(115200);
while(!Serial){} // wait for usb connection
SPI.begin();
}
//---------------LOOP--------------------------
void loop()
{
startTime = micros();
readData();
elapsedTime = micros() - startTime;
if (elapsedTime != old_res)
{
Serial.print("Conversion time:\t");
Serial.print(elapsedTime);
Serial.println(" microseconds");
old_res = elapsedTime;
}
parseRawBytes();
for (int i = 0; i < 8; i++) {
Serial.print((float)parsed[i] * SCALE_FACTOR, 5);
Serial.print(",");
}
Serial.print("\r\n");
}
//-----------------------------------------
void initial()
{
pinMode(BUSY, INPUT);
pinMode(RESET, OUTPUT);
pinMode(START_CONVERSION, OUTPUT);
pinMode(CHIP_SELECT, OUTPUT);
pinMode(D7_out, OUTPUT);
pinMode(RD, OUTPUT);
digitalWrite(START_CONVERSION, HIGH);
digitalWrite(CHIP_SELECT, HIGH);
reset(RESET);
}
//-----------------------------------------
void parseRawBytes() {
for(int i = 0; i<(sizeof(parsed) / sizeof(int));i++)
{
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(8000000, MSBFIRST, SPI_MODE2));
digitalWrite(CHIP_SELECT, LOW);
while (bytesToRead > 0) {
raw[TOTAL_RAW_BYTES - bytesToRead] = SPI.transfer(0x00);
bytesToRead--;
}
digitalWrite(CHIP_SELECT, HIGH);
SPI.endTransaction();
bytesToRead = TOTAL_RAW_BYTES;
}
//-----------------------------------------
Why not post the code you are using with the ESP32 that you are trying to use? Or are you wanting someone just to write the ESP32 code from your posted MEGA code?
ADC conversion time for a sample is: 4 us.
Allowable sampling: 200x1000 samples/sec
Time allocation for each sample is: 5 us (1/200000)
After issuing conversion command to the ADC, there must be a delay of 4 us and then the 16-bit ADC data is to read in the next 1 us before taking the next sample (assume 200 ksps).
The SPI data transfer rate stands as: 16 MBits/sec which can be handled by MEGA.
What is the sampling rate (number of samples are being taken in 1-sec)?