Go Down

Topic: Need help on ACS712 sensor (Read 606 times) previous topic - next topic

arazajr

Can the ACS712 sensor be used for detecting current instead of measuring the current?
I'm currently working on a project that needs to light up an LED once the current reach a certain higher amount while it also light up another LED once the current dropped to a certain amount.

The following are my code that I build while looking around on the internet.

pylon

I'm not a native speaker. Please define the difference between detecting current and measuring current, except that the former usually means a binary decision but you're writing about amounts of current, so what is the difference in your sentence?

MarkT

So long as you don't expect the noise and accuracy performance of the ACS712 to be any different,
yes you can detect a current.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

arazajr

how much is the error percentage if there is a bit of noise and a slight of inaccurate reading of the current?

arazajr

I'm not a native speaker. Please define the difference between detecting current and measuring current, except that the former usually means a binary decision but you're writing about amounts of current, so what is the difference in your sentence?
What I mean is...instead of measuring the current that is flowing through the sensor, I want it to detect a certain amount of current at activate an LED once that amount is reached and also do the same if the current dropped to a certain amount, it also activate another LED.

freaklabs

Hi arazajr.
Yes, its possible. You just continuously measure the current and convert it on the ADC of the Arduino. Once it hits your threshold value, you turn on the LED.
Hope that helps.
FreakLabs
Wireless Technology for the environment, wildlife, and agriculture
https://freaklabs.org
https://hackerfarm.jp
Twitter: @freaklabs

arazajr

Hi arazajr.
Yes, its possible. You just continuously measure the current and convert it on the ADC of the Arduino. Once it hits your threshold value, you turn on the LED.
Hope that helps.
Is there any code that I can use as reference? All I need left is a code that can specify the amount that it should light the LED and a code that can be use as a limiter to the amount of current that are going through the sensor.
As for an example : If the current that is going through the sensor is at 20mV, LED 1 will light up / active. If the current that is going through the sensor is below than 10mV, LED 2 will light up / active.

MarkT

The basics would be something like
Code: [Select]

if (value > threshold)
{
  light_led();
}
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Wawa

Are you looking for a "window comparator", with an if/if else/else statement.
Something like this (sudo code).
Leo..
Code: [Select]
//measure current first, only once.
// then compare that value to two thresholds

if (value < lowerThreshold)
{
  light_too low_LEDs;
}

else if (value > upperThreshold)
{
  light_too high_LEDs;
}

else
{
  light ok LEDs;
}

arazajr

#9
Mar 21, 2019, 01:41 am Last Edit: Mar 21, 2019, 01:51 am by arazajr
Are you looking for a "window comparator", with an if/if else/else statement.
Something like this (sudo code).
Leo..
Code: [Select]
//measure current first, only once.
// then compare that value to two thresholds

if (value < lowerThreshold)
{
  light_too low_LEDs;
}

else if (value > upperThreshold)
{
  light_too high_LEDs;
}

else
{
  light ok LEDs;
}

Is the code that you write going to be under void(loop) and is there anything else i should add to measure the current?
Is the current measurement going to be a separate void loop or just need to be under same void loop?

Wawa

So is a "window comparator" what you want?
I only posted the general idea to write the code, not the actual code.
Writing code is up to you.

Ask if you don't understand, or post your best attempt if you want help.

I would first start with a sketch that measures current, and displays the current on the serial monitor.
If that works, then think of adding conditions to light LEDs.
Leo..

arazajr

So is a "window comparator" what you want?
I only posted the general idea to write the code, not the actual code.
Writing code is up to you.

Ask if you don't understand, or post your best attempt if you want help.

I would first start with a sketch that measures current, and displays the current on the serial monitor.
If that works, then think of adding conditions to light LEDs.
Leo..
I can't say for sure that "window comparator" is what I wanted..but I simply combining codes to make a code that can detect a certain amount of mA and activate an LED.

The basic idea of what I'm trying to make is, when the ACS712 sensor detect 20mA and above, LED 1 will active.
If the sensor detect 10mA and below, LED 2 will active.

Wawa

I'm affraid the ACS712/5Amp can't reliably detect low currents like that.
Resolution (detection steps) of that sensor, with a 10-bit A/D, is about 12.5mA per A/D value.

An INA219 breakout board would be better suited for those low currents.
Leo..

Wawa

Cleaned up your code (untested), so you can try.
Leo..
Code: [Select]
#include "ACS712.h"
ACS712 sensor(ACS712_05B, A1);
int mVperAmp = 185;
byte LED1 = 2;
byte LED2 = 3;
float current;

void setup() {
  Serial.begin (9600);
  sensor.calibrate();
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
  // measure
  float current = sensor.getCurrentDC();
  // print
  Serial.print("Current is ");
  Serial.print(current);
  Serial.println(" Amps");
  // control LEDs
  if (current < 0.01) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }
  else if (current > 0.02) {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }
  else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
  }
  delay(1000); // some delay, so we can read it
}

arazajr

Cleaned up your code (untested), so you can try.
Leo..
Code: [Select]
#include "ACS712.h"
ACS712 sensor(ACS712_05B, A1);
int mVperAmp = 185;
byte LED1 = 2;
byte LED2 = 3;
float current;

void setup() {
  Serial.begin (9600);
  sensor.calibrate();
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop() {
  // measure
  float current = sensor.getCurrentDC();
  // print
  Serial.print("Current is ");
  Serial.print(current);
  Serial.println(" Amps");
  // control LEDs
  if (current < 0.01) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
  }
  else if (current > 0.02) {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
  }
  else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
  }
  delay(1000); // some delay, so we can read it
}

Thank you very much. I will try it along with some other code that I have created previously

Go Up