Review my Arduino code for ACS712 current sensor

Thread Starter

rahul411

Joined Feb 19, 2018
260
I wrote a code for ACS712 sensor and i want someone to review it. I'm getting pretty accurate reading when compared to my multimeters.

https://github.com/deligent4/ac8712/blob/main/ac8712_rev2.ino

Link to the code. I'm new to coding started learning it recently.

I know there are codes available online fro but all they are doing is converting the analog voltage to current with some formulas (i have done the same). But the thing is they don't output stable reading, i mean when i connect sensor to Arduino at zero current, those codes always showed something is flowing through sensor but in reality there isn't any current Ofcourse by changing some constant values i can get 0 current value when no current flows, but as soon as I change the power source say mobile charger or power bank the values get randon. Not what i needed. Also there is library for this sensor too but that is also based on assuming that I'm having spot on 5V on 5v pin and taking it as reference.
 

djsfantasi

Joined Apr 11, 2010
9,156
You really need to host the code on here and include the code in your post.

You can attach it as a text file (use “Attach files” button below). Or Copy and Paste it inline, between “code tags”. Code tags are CODE and /CODE inserted between square brackets. Then, you actual code is inserted between these tags, between ][.
 

KeithWalker

Joined Jul 10, 2017
3,063
I wrote a code for ACS712 sensor and i want someone to review it. I'm getting pretty accurate reading when compared to my multimeters.

https://github.com/deligent4/ac8712/blob/main/ac8712_rev2.ino

Link to the code. I'm new to coding started learning it recently.

I know there are codes available online fro but all they are doing is converting the analog voltage to current with some formulas (i have done the same). But the thing is they don't output stable reading, i mean when i connect sensor to Arduino at zero current, those codes always showed something is flowing through sensor but in reality there isn't any current Ofcourse by changing some constant values i can get 0 current value when no current flows, but as soon as I change the power source say mobile charger or power bank the values get randon. Not what i needed. Also there is library for this sensor too but that is also based on assuming that I'm having spot on 5V on 5v pin and taking it as reference.
The zero offset from the inexpensive current sensor you are using is quite normal and as you mention, can be corrected for in the software.
The stability of the current reading will depend on the stability of the current you are measuring. A mobile charger and power bank are not designed to be particularly stable and noise free. A well smoothed lab power supply should give you stable readings.
The accuracy of the readings from an A/D always depends on the accuracy and stability of the reference used. Any inaccuracy can be compensated for in the software as long as the reference is stable.
The library for the sensor is just there to save you the effort of sampling the A/D and scaling the result yourself.
Regards,
Keith
 

nsaspook

Joined Aug 27, 2009
13,079
I wrote a code for ACS712 sensor and i want someone to review it. I'm getting pretty accurate reading when compared to my multimeters.

https://github.com/deligent4/ac8712/blob/main/ac8712_rev2.ino

Link to the code. I'm new to coding started learning it recently.

I know there are codes available online fro but all they are doing is converting the analog voltage to current with some formulas (i have done the same). But the thing is they don't output stable reading, i mean when i connect sensor to Arduino at zero current, those codes always showed something is flowing through sensor but in reality there isn't any current Ofcourse by changing some constant values i can get 0 current value when no current flows, but as soon as I change the power source say mobile charger or power bank the values get randon. Not what i needed. Also there is library for this sensor too but that is also based on assuming that I'm having spot on 5V on 5v pin and taking it as reference.
The sensor has Ratiometric output from supply voltage. If you provide a stable reference voltage (separate from the controller and logic Vdd) to both the sensor and the ADC ref pin your measurement stability will improve without extra averaging processing (phase shifts and slow measurement updates) and internal controller Vref offsets (drifts and scaling offset across the measurement range).
https://www.analog.com/media/en/technical-documentation/data-sheets/ADR01_02_03_06.pdf
 
Top