I’ve been banging my head against the wall trying to figure this one out. I hope it’s something silly I’m just missing.
Basically, I’ve got a ACS714 hall effect sensor and I’m attempting to sample the sine wave (I’m wanting to monitor AC current) I’ve seen a few different ways to sample using Arduino code - but nothing that I can seem to properly translate.
I’ve been at this for two days (not a programmer on either platform - yet - and I like to figure these things out the hard way, but I’m lost here - any advice / direction / suggestions would be greatly appreciated. I’ve read over the forums, as well as the porting of code - swapping ‘int’ for ‘local’, ‘void’ for ‘function’ and as many of the other tips from the arduinoport pages.
I can sort out all the math, but I can’t manage to sample enough values in a 60Hz 110v signal to isolate the peaks - and thus have the amps. The logic simply escapes me. I posted the original code as opposed the the modification I’ve attempted to make with the hopes someone can make sense.
Thanks in advance!
Attempt #1
(original Arduino:)
int analogReadAmplitude = 0, min = 512, max = 0, filter = 4;
unsigned long start = millis();
do {
int val = 0;
for (int i = 0; i < filter; i++)
val += analogRead(currentAnalogInputPin);
val = (val / filter);
if (max < val) max = val;
if (val < min) min = val;
} while (millis() - start <= 1100/hz); //10% + to ensure p2p is acquired
analogReadAmplitude = (max - min) / 2;
<n\>
<n\>
My Translation:
function getPin1Voltage()
const local sensorPin = hardware.pin1.configure(ANALOG_IN); // pin that the sensor is attached to
const local hz = 60; // frequency of signal
const local filter = 4; // get average of several samples to reduce noize
// Current sensor (ACS714 5A)
local get_mA () {
local max = 0, min = 32767, cnt = 0;
unsigned long start = millis ();
do {
local val = 0;
for (local i = 0; i <filter; i ++)
val + = analogRead (sensorPin);
if (max <val) max = val;
if (min> val) min = val;
cnt ++;
} While (millis () - start <= 1100 / hz); // 10% + for ensure p2p is acquired
local mA = long ((max-min)) * 9333/1000 / filter;
return mA;
}
<n\>
<n\>
Attempt #2 (Arduino code)
int analogReadAmplitude = 0, min = 512, max = 0, filter = 4;
unsigned long start = millis();
do {
int val = 0;
for (int i = 0; i < filter; i++)
val += analogRead(currentAnalogInputPin);
val = (val / filter);
if (max < val) max = val;
if (val < min) min = val;
} while (millis() - start <= 1100/hz); //10% + to ensure p2p is acquired
analogReadAmplitude = (max - min) / 2;
<n\>
<n\>
Attempt #3 (This one I thought I almost had working!)
int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int reading = 0; // variable to store the value read
long maxVal = 0;
int samples = 10000; // how many samples per reading
void setup()
{
Serial1.begin(9600); // setup serial
pinMode(13, OUTPUT);
digitalWrite(2, HIGH);
}
void loop()
{
maxVal = 0;
// delay(500);
// digitalWrite(13, !digitalRead(13));
for (int counter = 1; counter < samples; counter++) {
reading = analogRead(analogPin); // read the input pin
if (reading > maxVal)
{
maxVal = reading;
}
}
Serial1.println(maxVal); // debug value
}