krisgambhir
New member
Hi,
I am trying to measure 4 analog signals in 2 pairs; 2 voltages and 2 currents, along with one accelerometer using I2C on the Teensy 4.1. In the end, the voltages will be measured using the LV25-P voltage transformer and the currents are measured using the LTS6-NP current transformer, while the accelerometer is the ADXL343. For test purposes, I am connecting a signal generator directly to the pins of my T4.1.
The measurements have to be done synchronously in pairs of voltages and currents, as the phase angle in between them has to be calculated for further calculations. I have adapted the synchronizedMeasurement code from the ADC library, where I start synchronized continuous readings on A8 and A9, read a value and print it, and repeat for pins A3 and A4. I delay by 100 microseconds at the end of the loop as well. The code is attached below.
When I run measurements with the same 50Hz 3Vpp sinusoidal signal from 0V:+3V, I get good measurements for a couple of seconds with approximately 110us between each measurement in the times columns t0 and t1, respectively. After the first couple of seconds, the time between measurements is increased by a huge amount, up to 6000us.
I haven't tried to add the accelerometer, as I need the voltages and currents to be measured properly first.
Does anyone have any idea on how to fix this issue, and if there is a more robust way of measuring the analog signal, for example using interrupts?
I am trying to measure 4 analog signals in 2 pairs; 2 voltages and 2 currents, along with one accelerometer using I2C on the Teensy 4.1. In the end, the voltages will be measured using the LV25-P voltage transformer and the currents are measured using the LTS6-NP current transformer, while the accelerometer is the ADXL343. For test purposes, I am connecting a signal generator directly to the pins of my T4.1.
The measurements have to be done synchronously in pairs of voltages and currents, as the phase angle in between them has to be calculated for further calculations. I have adapted the synchronizedMeasurement code from the ADC library, where I start synchronized continuous readings on A8 and A9, read a value and print it, and repeat for pins A3 and A4. I delay by 100 microseconds at the end of the loop as well. The code is attached below.
When I run measurements with the same 50Hz 3Vpp sinusoidal signal from 0V:+3V, I get good measurements for a couple of seconds with approximately 110us between each measurement in the times columns t0 and t1, respectively. After the first couple of seconds, the time between measurements is increased by a huge amount, up to 6000us.
I haven't tried to add the accelerometer, as I need the voltages and currents to be measured properly first.
Does anyone have any idea on how to fix this issue, and if there is a more robust way of measuring the analog signal, for example using interrupts?
Code:
#include <ADC.h>
#include <ADC_util.h>
const int P1 = A3; // Voltage 1
const int P2 = A4; // Current 1
const int P3 = A8; // Voltage 2
const int P4 = A9; // Current 2
ADC *adc = new ADC(); // adc object
elapsedMicros time;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
pinMode(P1, INPUT);
pinMode(P2, INPUT);
pinMode(P3, INPUT);
pinMode(P4, INPUT);
Serial.begin(9600);
//## ADC0 ##//
adc->adc0->setAveraging(1); // set number of averages
adc->adc0->setResolution(8); // set bits of resolution
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
//## ADC1 ##//
adc->adc1->setAveraging(1); // set number of averages
adc->adc1->setResolution(8); // set bits of resolution
adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
Serial.println("t0,A8,A9,t1,A3,A4");
}
ADC::Sync_result result;
void loop() {
adc->startSynchronizedContinuous(P1,P2);
result = adc->readSynchronizedContinuous();
result.result_adc0 = (uint16_t)result.result_adc0;
result.result_adc1 = (uint16_t)result.result_adc1;
Serial.print(time, DEC);
Serial.print(",");
Serial.print(result.result_adc0*3.3/adc->adc0->getMaxValue(), DEC);
Serial.print(",");
Serial.print(result.result_adc1*3.3/adc->adc1->getMaxValue(), DEC);
adc->startSynchronizedContinuous(P3,P4);
result = adc->readSynchronizedContinuous();
result.result_adc0 = (uint16_t)result.result_adc0;
result.result_adc1 = (uint16_t)result.result_adc1;
Serial.print(",");
Serial.print(time, DEC);
Serial.print(",");
Serial.print(result.result_adc0*3.3/adc->adc0->getMaxValue(), DEC);
Serial.print(",");
Serial.println(result.result_adc1*3.3/adc->adc1->getMaxValue(), DEC);
//digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
delayMicroseconds(100);
}