I have a two channel 24bit ADC(LTC2402- http://cds.linear.com/docs/en/datasheet/24012f.pdf) that I am communicating with via Arduino Leonardo(Leonardo because it was free!). If I attempt to do multiple reads the SPI becomes very slow while if I do a single read the time to send is what is expected. I have have integrated with python and timed the different transactions. A single read takes around 170ms( this is for both channels and the datasheet of the ADC indicates about 65ms per conversion(table 3 arduino as external sck etc.)) BUT if I do 5 reads the time taken is about 1.5s(expected about 0.65s) 10 reads take about 3s and 20 about 6.5s. Doing no ADC conversions and just serial comm to python is less than 2ms.
The relevant python code is as follows:
def getdata(ser,code):#ser is the arduino that has been connected, code is for writting to arduino
ser.write(code)
ch0=float(ser.readline().decode()[:-2])
ch1=float(ser.readline().decode()[:-2])
return ch0,ch1
code='1'
code=code.encode()
a=datetime.datetime.now()
ch0,ch1=getdata(ser,code)
b=datetime.datetime.now()
print(b-a)
The arduino code is as follows. The code has been simplified to just the necessary byte reads from the ADC, all other code has been removed to ensure the delays are coming from the SPI.transfer(0x00). The full code retrieves 4 bytes per channel and converts this to voltage and returns these values.
#include <SPI.h>
const int ltcs = 3; //active low ADC enable
float ch0=0.0;
float ch1=0.0;
byte b0;
void setup() {
SPI.begin();
SPI.beginTransaction(SPISettings(200000, MSBFIRST, SPI_MODE0));
Serial.begin(9600); // set the baud rate
pinMode(ltcs, OUTPUT);
digitalWrite(ltcs, LOW);
}
void loop() {
if(Serial.available()){
int data = Serial.read() - '0';
if(data>0){
getdata2(ch0,ch1);
//getdata2(ch0,ch1); //run multiple getdata2 to test multiple conversions
//getdata2(ch0,ch1);
//getdata2(ch0,ch1);
//getdata2(ch0,ch1);
Serial.println(0.2);
Serial.println(0.4);
}
}
}
void getdata2(float &ch0, float &ch1) {
ch0 = 0.0;
ch1 = 0.0;
for (int i = 0; i < 2; i++) { //Do twice to retrieve both Channels
while (digitalRead(MISO) == HIGH) {
}
b0 = SPI.transfer(0x00);
b0 = SPI.transfer(0x00);
b0 = SPI.transfer(0x00);
b0 = SPI.transfer(0x00);
}
}
Can anyone help me? I have tried different SPISettings with no change. Interestingly I also find that the Baud rate does not change either.
Thanks for the help if any
b0? You don't seem to be changingchexcept to set it to zero. I don't see whatgetdata2achieves except to slow things down.getdata2call in the above code. Can you please show the actual code that is slow?