Peter_n:
Did you tell which board you have ?
I hope you have an Arduino Uno or Leonardo or something like that. In the sketch I assume that the ADC is 10-bits and the board runs at 5V.
I would like to return a voltage as float and keep the extra information that was gained by averaging.
// Read voltage from a certain analog pin.
// AVG_NUM can be up to 60, more does not fit into a unsigned integer.
#define AVG_NUM 20
float read_volt(int pin)
{
unsigned int total = 0; // start with zero, this is used for the total value.
for (int i=0; i<AVG_NUM; i++) // loop through reading raw adc values AVG_NUM number of times
{
total += analogRead(pin); // read the input pin
// The delay is arbitrary.
// It can be 0 to 20ms.
// Test for your own circuit what is best.
delayMicroseconds(50); // pauses for 50 microseconds
}
// The voltage is raw_adc / 1023 * 5V and also divided by the amount of samples for average.
// Convert every integer to float before calculating something.
// This way, the extra bits gained by averaging are used in the calculation.
float volt = (float) total / 1023.0 * 5.0 / (float) AVG_NUM;
return(volt);
}
Converting the current can be done in the loop()
I prefer to explain every number and let the compiler or Arduino do the calculation.
const int currentPin = A0;
// The ASC712-30 has 66mV/A
// That is 1 amp for every 0.066V.
// The middle of 2.5V is zero amps.
float voltage = read_volt (currentPin);
float amps = (voltage - 2.5) / 0.066;
My ACS712 is soldered in a circuit, so I tested it without that.
I don't know if my calculation is right, you have to check that.
Can you also check with a multimeter what the actual current is and if that matches the Arduino output ?
This is a full test sketch with A0 as analog input:
// Tested with Arduino 1.5.8 without ACS712-30
const int currentPin = A0;
void setup()
{
Serial.begin(9600);
Serial.println(F("Current Sketch"));
}
void loop()
{
// The ASC712-30 has 66mV/A
// That is 1 amp for every 0.066V.
// The middle of 2.5V is zero amps.
float voltage = read_volt (currentPin);
float amps = (voltage - 2.5) / 0.066;
Serial.println( amps);
delay(500);
}
// Read voltage from a certain analog pin.
// AVG_NUM can be up to 60, more does not fit into a unsigned integer.
#define AVG_NUM 20
float read_volt(int pin)
{
unsigned int total = 0; // start with zero, this is used for the total value.
for (int i=0; i<AVG_NUM; i++) // loop through reading raw adc values AVG_NUM number of times
{
total += analogRead(pin); // read the input pin
// The delay is arbitrary.
// It can be 0 to 20ms.
// Test for your own circuit what is best.
delayMicroseconds(50); // pauses for 50 microseconds
}
// The voltage is raw_adc / 1023 * 5V and also divided by the amount of samples for average.
// Convert every integer to float before calculating something.
// This way, the extra bits gained by averaging are used in the calculation.
float volt = (float) total / 1023.0 * 5.0 / (float) AVG_NUM;
return(volt);
}
Try to increase the number of samples for the average to 5 or 60, and try to change that delay. It might not change anything, and sometimes it does help a little.
Hello
Thank You for the help and for a such comprehensive explanation!
I have Mega 2560 board. Tried your code and it works very well i increased AVG_NUM so the results are much steadier to read, without load i'm getting this:
0.08
0.10
0.10
0.10
0.08
0.09
0.08
0.08
0.10
0.09
0.09
0.11
0.10
0.10
0.09
I connected load of 100ma and tested with arduino, it matches almost identicaly as my digital multimeter!
Tomorow i will try some larger loads like 5-10A so i will see how it stands...
Best Regards!