Need help on ACS712 sensor

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

You probably have typed a capital "I" instead of a lowercase "L".

The "Serial.println" should turn orange when properly entered.
Leo..

Wawa:
You probably have typed a capital "I" instead of a lowercase "L".

The "Serial.println" should turn orange when properly entered.
Leo..

I have rechecked all capital sensitive code..it still didn't turn orange...instead its just plain black text like this "Serial.printIn" ....

The printIn you typed in post#15 and post#21 is with a capital "i".
You should type a lowercase "L".
The letter that is used in the word linefeed.
Leo..

Wawa:
The printIn you typed in post#15 and post#21 is with a capital "i".
You should type a lowercase "L".
The letter that is used in the word linefeed.
Leo..

oh..okay
I will try and change it..