ACS712 Sensor High/Low

Hello Everyone,

I have several ACS712 sensors and what I would like to do is just use the sensor to turn on a LED and turn off a LED and expand from there. I do not have a need to measure any of the current I just need to use the sensor to provide a High/Low state. All the sketches that I have come across so far measures the current. Thanks in advance for pointing me in the right direction.

Maybe i didn t understand, you just want to turn a led on when the current is enough high? If yes do an analogRead and when the value from the sensor reach a certain number (for example 750,since 512 is the minimum) with an if turn the led on

That’s almost it. I have posted code below maybe I am headed in the right direction if not let me know:

int ledPin    = 13;             
int inputPin = A0;                         
int val = 0;                    
 
void setup() {
  pinMode(ledPin, OUTPUT);     
  pinMode(inputPin, INPUT_PULLUP);
 
  Serial.begin(9600);
}
 
void loop(){
  val = analogRead(inputPin);  
  if (val < 750) {            
    digitalWrite(ledPin, HIGH);
   }
  
 if (val > 750) {
    digitalWrite(ledPin, LOW);
  }
}

ACS712 sensors come in different current ratings.
What is current you want to detect (ratio, percentage).
Is it AC or DC.

Don’t pull up the analogue pin.
The ACS712 has a fixed VCC/2 output without current (A/D ~512).

If you’re using the same value to test, you could use the if/else statement.
Leo…

int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (analogRead(A0) < 525) { // threshold, sensitivity
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

I am measuring....or I should say detecting DC. I am just looking for a 0 = off and 1 = on type of thing.....not looking for any particular range....any current detected LED goes ON and no current LED goes OFF.

PilotinControl:
That’s almost it. I have posted code below maybe I am headed in the right direction if not let me know:

int ledPin    = 13;             

int inputPin = A0;                       
int val = 0;

void setup() {
  pinMode(ledPin, OUTPUT);   
  pinMode(inputPin, INPUT_PULLUP);

Serial.begin(9600);
}

void loop(){
  val = analogRead(inputPin); 
  if (val < 750) {           
    digitalWrite(ledPin, HIGH);
  }
 
if (val > 750) {
    digitalWrite(ledPin, LOW);
  }
}

Good you are headed to the right direction :slight_smile:
You was almost there on your own, but you should invert the condition. <600 off else the led is on
To determine the right value print on the serial val than choose a value that is good for your project
And use the suggestion of waka (don t pull up the analog input)

Please post the model ACS712 you have (current rating), and the current you want to detect.

The first sketch I posted only detects uni-directional current.
You could also change the if statement to detect current in both directions.
Example code attached.
Leo…

int ledPin = 13; // indicator LED
int value; // holds A/D value

void setup() {
  Serial.begin(9600); // start serial monitor for debugging
  pinMode(ledPin, OUTPUT); // declare LED pin as output
}

void loop() {
  value = analogRead(A0); // read analogue input
  if (value < 504 || value > 520) { // "or" with two thresholds
    digitalWrite(ledPin, HIGH); // current above thresholds
    Serial.print(value); // some info to adjust thresholds 
    delay(1000); // delay time for humans to see what happened
  }
  else {
    digitalWrite(ledPin, LOW); // LED off when current is below thresholds
  }
}

That code worked like a charm....I will be testing with several others and see how they work. I was indeed close in comparing 2 thresh holds. Thanks again! I will report back on how the others work.

Hello,

Thanks for the help earlier on the sketch and measuring the current. I have a bi-color LED hooked up so that when current is "sensed" the color turns red and when there is no current being sensed the color turns green. Now I get a bit of flickering due to the instability of the current flow. My question is this: is there a way to smooth that out? I did find the section on smoothing which takes a bunch of readings and then using the average from that. I am a bit confused. How do I implement that in the code posted above? I tried several ways and I think I have burned my brain out. Thanks again for the help.

You didn't answer all my questions, so I guessed the threshold values.

Zero current should return an A/D value of about 512, but could be different for your ACS712. I entered threshold values of 504 and 520, eight values away from "zero current". If this is too sensitive, change the values. Leo..

I am using this sensor: http://www.allegromicro.com/en/Products/Current-Sensor-ICs/Zero-To-Fifty-Amp-Integrated-Conductor-Sensor-ICs/ACS712.aspx

When the DC current is sensed the LED does change color...it flickers a bit...I am looking for a way to minimize or eliminate that if at all possible. Thanks.

Yes, I know what an ACS712 is. But there is a 5Amp, a 20Amp, and a 30Amp version of that sensor. So which one are you using, and how much current are you detecting.

The flickering LED suggests you're detecting a load that doesn't draw a constant current. It might also help if we know what you're detecting. Leo..

I am using the 5amp version and detecting anything from 0-12 volts.....and the threshholds that the serial monitor is showing is anywhere from 495-550 and these are the specific specs on this version of the ACS712:

Vcc Voltage: up to5V Output: Analog: 0 to Vcc Resolution: 185 mV / 1 Amp Calculation: 1/2 Vcc + 185 mV / 1 Amp Bandwidth: 80 kHz

Thanks again.

PilotinControl: I am using the 5amp version and detecting anything from 0-12 volts

These sensors measure current, not voltage.

So you're using a 12volt supply, for what. A motor? Is this motor powered continuously, or with PWM? Leo..

I am monitoring DC motors as small as hobby motors NOT controlled by the arduino...these are 12 volt or less motors and one side of the lead wire is connected to the sensor and the other lead is connected directly to the motor from the power source.

If the LED flickers a bit, you could increase the delay() time a bit. Say to 3000. Or make the detection window smaller. e.g. 508 and 516. Leo..

The delay does work…however it delays the whole sketch while multiple sensors are reading multiple motors this creates too much of a delay in reaction time with other functions. So I need to implement the blink without delay type of thing. Which is starting to complicate my knowledge of coding. Learning curves aren’t anything new to me.

I didn't see anything else (motors) in the sketch you posted.

Delay() is blocking. It is important to understand that BlinkWithoutDelay sketch. Leo..

The motors aren't being controlled by the arduino they are being controlled by a separate DC power source....the current sensors are monitoring the current from the motors...I will have a total of 5 current sensors which need to be read and then the LEDS will change based on the current thresholds...which we have already established....the flickering of the LEDS should be fixed once we get rid of the delay....if each sensor uses delay then the whole sketch will be blocked.....I've been reading about using millis()...still a bit greek to me.

I think I finally got it. The code below eliminates all LED flickering and I switched to tracking the voltage now each ACS712 5amp sensor instead of the current itself. As you can see I am monitoring 2 sensors and I will be adding at least 3 more. Please take a look at my code and see if there is any place I can “clean” it up or condense even more. I will be experimenting with millis() later as I will be adding other functions. Thanks for the help so far :slight_smile:

int ledPin1 = 12;              
int ledPin2 = 11;              
int ledPin3 = 10;             
int ledPin4 =  9;              

int sensorPin1 = A0;           
int sensorPin2 = A1;           

void setup() {
Serial.begin(9600);
pinMode(ledPin1,    OUTPUT);  
pinMode(ledPin2,    OUTPUT);  
pinMode(ledPin3,    OUTPUT);   
pinMode(ledPin4,    OUTPUT);   
pinMode(sensorPin1, INPUT);    
pinMode(sensorPin2, INPUT);    
}
 
void loop() {
float average  = 0;
float average2 = 0;
for(int i = 0; i < 1000; i++) {
average  = average  + (.0264 * analogRead(sensorPin1) -13.51) / 1000;
average2 = average2 + (.0264 * analogRead(sensorPin2) -13.51) / 1000;
}

if (average < -0.01 || average > 0.12) {   
digitalWrite(ledPin1, HIGH);                     
digitalWrite(ledPin2, LOW);                
} else {
digitalWrite(ledPin1, LOW);                
digitalWrite(ledPin2, HIGH);       
} 

if (average2 < -0.05 || average2 > 0.05) { 
digitalWrite(ledPin3, HIGH);              
digitalWrite(ledPin4, LOW);                
} else {
digitalWrite(ledPin3, LOW);               
digitalWrite(ledPin4, HIGH);       
}

}