Arduino Forum

Using Arduino => Sensors => Topic started by: arazajr on Mar 19, 2019, 09:10 am

Title: Need help on ACS712 sensor
Post by: arazajr on Mar 19, 2019, 09:10 am
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.
Title: Re: Need help on ACS712 sensor
Post by: pylon on Mar 19, 2019, 04:29 pm
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?
Title: Re: Need help on ACS712 sensor
Post by: MarkT on Mar 19, 2019, 07:06 pm
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.
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 20, 2019, 07:08 am
how much is the error percentage if there is a bit of noise and a slight of inaccurate reading of the current?
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 20, 2019, 07:19 am
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.
Title: Re: Need help on ACS712 sensor
Post by: freaklabs on Mar 20, 2019, 08:51 am
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.
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 20, 2019, 08:58 am
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.
Title: Re: Need help on ACS712 sensor
Post by: MarkT on Mar 20, 2019, 12:02 pm
The basics would be something like
Code: [Select]

if (value > threshold)
{
  light_led();
}
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 20, 2019, 09:47 pm
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;
}
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 21, 2019, 01:41 am
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?
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 21, 2019, 01:51 am
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..
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 21, 2019, 02:05 am
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.
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 21, 2019, 02:15 am
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..
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 21, 2019, 02:26 am
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
}
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 21, 2019, 02:55 am
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
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 22, 2019, 04:52 am
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
}

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
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 22, 2019, 05:53 am
Serial.println(); // is a common combination of print and newline
Which Arduino are you using?
Did you copy/paste, or re-type it.
Leo..
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 22, 2019, 07:46 am
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
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 22, 2019, 10:56 am
Should work on an Uno.
Must have done/typed something wrong.
Reference page here. (https://www.arduino.cc/en/Serial/Println)
Leo..
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 26, 2019, 03:52 am
Should work on an Uno.
Must have done/typed something wrong.
Reference page here. (https://www.arduino.cc/en/Serial/Println)
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..
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 26, 2019, 05:00 am
You probably have typed a capital "I" instead of a lowercase "L".

The "Serial.println" should turn orange when properly entered.
Leo..
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 26, 2019, 08:22 am
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" ....
Title: Re: Need help on ACS712 sensor
Post by: Wawa on Mar 26, 2019, 11:30 am
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..
Title: Re: Need help on ACS712 sensor
Post by: arazajr on Mar 27, 2019, 04:35 am
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..