Hi all,
I am using an interrupt to capture the values of 3 analogue inputs at a specific point in time, triggered by an external reference signal. These values are then simply sent over serial to the PC.
These 3 signals are 400Hz AC waveforms. I wish to capture their peak amplitude using the interrupt, triggered by a 4th reference signal. I have built an optocoupler circuit for the external interrupt to ensure the signal is as clean and noise-free as, checked it on the oscilloscope, and can confirm it's a nice square wave, with no noise visible (20MHz scope) that would cause unwanted triggering of the interrupt.
It is important that the amplitudes are captured when the interrupt is triggered by the reference signal so that they are all sampled at the same time - understanding that there may be a slight delay between samples due to the UNO analogue inputs having to take turns with the ADC.
My end result is I want to see three "pseudo DC" signals by capturing the same point of their waveforms each sample, these signals will reflect the amplitude at that point in time. However, I;m getting a bit of a mess:
The image above shows how the UNO is sampling the waves. They look like AC waves, but in reality are likely the result of the analogue reads sampling at odd times and capturing various parts of the AC waves.
const byte interruptPin = 2;
const int inPin0 = A0;
const int inPin1 = A1;
const int inPin2 = A2;
volatile int inVal0 = 0;
volatile int inVal1 = 0;
volatile int inVal2 = 0;
void setup() {
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), readIn, FALLING);
Serial.begin(9600);
}
void loop() { // loop seems to be required for UNO to run
}
void readIn() {
inVal0 = analogRead(inPin0);
inVal1 = analogRead(inPin1);
inVal2 = analogRead(inPin2);
Serial.print(inVal0);
Serial.print(",");
Serial.print(inVal1);
Serial.print(",");
Serial.print(inVal2);
Serial.print(",");
Serial.print("1023"); // only used to stop the serial plotter auto-scaling
Serial.print("\n");
}
I cannot use a bridge rectifier, and smooth these to DC signals as they can be in phase or in anti-phase, which is why I need to sample all three AC signals at the same time, when triggered by the reference signal. The AC signals are decoupled and resistor biased to +2.5V and swing roughly between ~0.5V and ~4.5V.
I did think that as this is all at 400Hz, could I be interrupting my interrupt? Or once an interrupt is busy does it ignore further triggers?
I have tried to eliminate noise issues causing false triggering of the ISR: I am using the internal pullup on the digital pin, with an optocoupler switching down to 0V, triggering on the falling edge which is nice and sharp on my oscilloscope. I have added some decoupling capacitors to the Arduino board 5V power supply rail too just to try and rule out noise on the power supply.
I have tried placing the serial printing within the loop instead of the ISR, which does produce different looking results, but the issue still exists of not sampling the same point of the waveform as I had expected.
Any ideas please? This is my first Arduino interrupt program, so I may have made some blunder somewhere.
Thanks, Scott.