Need help on ACS712 sensor

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.

ACS712_5B.ino (701 Bytes)

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?

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.

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

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?

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.

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:
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.

The basics would be something like

if (value > threshold)
{
  light_led();
}

Are you looking for a "window comparator", with an if/if else/else statement.
Something like this (sudo code).
Leo..

//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;
}

Wawa:
Are you looking for a "window comparator", with an if/if else/else statement.
Something like this (sudo code).
Leo..

//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?

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..

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..

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.

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..

Cleaned up your code (untested), so you can try.
Leo..

#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
}

Wawa:
Cleaned up your code (untested), so you can try.
Leo..

#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

Wawa:
Cleaned up your code (untested), so you can try.
Leo..

#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
}

I tried the code you wrote but apparently my Arduino couldn't detect the Serial.printIn code and state that "class HardwareSerial" has no member named printIn

Serial.println(); // is a common combination of print and newline
Which Arduino are you using?
Did you copy/paste, or re-type it.
Leo..

Wawa:
Serial.println(); // is a common combination of print and newline
Which Arduino are you using?
Did you copy/paste, or re-type it.
Leo..

At first, I copy and paste it and then retype it..and I'm using Arduino Uno

Should work on an Uno.
Must have done/typed something wrong.
Reference page here.
Leo..

Wawa:
Should work on an Uno.
Must have done/typed something wrong.
Reference page here.
Leo..

I have rechecked many times and I even check it lines by lines..
I also read the reference before and I know it should be fine yet it still doesn't work..