I am trying to make the following system:
An ADC (specifically the LTC 1407) will recieve a signal an Arduino will acquire the data using the SPI protocol. (At first i was using the Arduino UNO R4 Minima but it didnt show any result so i used the Arduino UNO R3).
(PS1: The end game is to sample the signal buta i am starting simple so i want to take 1 value enery 1s. I decided to make it easier so instead of any other signal i am supplying the ADC with a DC Voltage (from 0,1V to 1,1V))
(PS2: the ADC (LTC1407) has 2 differential channels, and for what i understood i have to always supply both of them with signal/signals, but thats not the main problem here)
The problem: I have connected everything as i should ( a) ADC to Arduino, b) the ADC with capacitpors, according to the datasheet ets) but the result in the monitor doesnt correspond to anaything and doesnt make any sense. I provide below the code i have constructed, with some pictures of the results.
I check with an oscillator that the clock, the DC, other connections were ok, i changed the wires to make sure it wasnt any of that the problem
also when i pulled out the data cable (from the ADC to the Arduino, MISO) all returning values returned zero. So far so good. But when i pullet out either the Channel 0 cables, or the Channel 1 cables, or the cable 3,3V (from arduino) the results werent affected at all, they were still coming and the same values. Also the values didnt change from 0,1V to 1,1V of signal i was sending.
Does anyone have any opinion about what might be the problem?
Thank you in advance
I am at your disposal
(i understand that the code i am providing may have some minor errors but it is my understanding that my problem is something else. Maybe i am wrong)
CODE BELOW:
#include <SPI.h>
// SPI - Arduino Pins
const int clockPin = 13;
const int slavePin = 10;
const int dataPin = 12;
// ADC's reference voltage
const float referenceVoltage = 2.5;
// Stating the data variables
int bytesChannel0; // 2 bytes data from Channel 0
int bytesChannel1; // 2 bytes data from Channel 1
// Stating frequency variables
float frequencyChannel0;
float frequencyChannel1;
// Variables to store time for frequency calculation
unsigned long startTimeChannel0;
unsigned long startTimeChannel1;
void setup()
{
// Set up Serial communication
Serial.begin(9600);
// Set up SPI communication
SPI.begin();
// Set SPI pins
pinMode(clockPin, OUTPUT);
pinMode(slavePin, OUTPUT);
pinMode(dataPin, INPUT);
// Set ADC chip slave select pin high
digitalWrite(slavePin, HIGH);
// Initialize start time for frequency calculation
startTimeChannel0 = micros();
startTimeChannel1 = micros();
}
void loop()
{
// Variables to store Voltage
float voltageValueChannel0;
float voltageValueChannel1;
// Reading Channel 0, Channel 1
readADC(bytesChannel0, bytesChannel1);
// Convert bits to Voltage
voltageValueChannel0 = convertToVoltage(bytesChannel0);
voltageValueChannel1 = convertToVoltage(bytesChannel1);
// Calculate frequency of signals
frequencyChannel0 = calculateFrequency(startTimeChannel0);
frequencyChannel1 = calculateFrequency(startTimeChannel1);
// Print results for Channel 0
Serial.print("Byte data from channel 0:");
Serial.println(bytesChannel0);
Serial.print("Channel 0: Voltage = ");
Serial.print(voltageValueChannel0);
Serial.print(" V, Frequency = ");
Serial.print(frequencyChannel0);
Serial.println(" Hz");
// Print results for Channel 1
Serial.print("Byte data from channel 1:");
Serial.println(bytesChannel1);
Serial.print("Channel 1: Voltage = ");
Serial.print(voltageValueChannel1);
Serial.print(" V, Frequency = ");
Serial.print(frequencyChannel1);
Serial.println(" Hz");
delay(1000); // Wait for 1 second before taking the next set of samples
}
// Function to read ADC's data
void readADC (int &bytesChannel0, int &bytesChannel1)
{
// Begin SPI transaction between Arduino and ADC LTC1407
SPI.beginTransaction(SPISettings(200000, LSBFIRST, SPI_MODE0));
// Select ADC LTC1407 by setting it low
digitalWrite(slavePin, LOW);
// Tranfer bytes to variables
bytesChannel0=SPI.transfer(0x00);
bytesChannel1=SPI.transfer(0x00);
// De-select ADC LTC1407 by setting it high
digitalWrite(slavePin, HIGH);
// End SPI transaction between Arduino and ADC LTC1407
SPI.endTransaction();
}
// Function to convert ADC's bit data to voltage data
float convertToVoltage(int bitData)
{
float voltage = (bitData * referenceVoltage) / 4096.0; // 2^32
return voltage;
}
// Function to calculate frequency using micros() function
float calculateFrequency(unsigned long &startTime)
{
unsigned long currentTime = micros();
unsigned long period = currentTime - startTime;
// Convert period to frequency in Hz
float frequency = 1000000.0 / period;
// Update start time for the next cycle
startTime = currentTime;
return frequency;
}