Go Down

Topic: ACS 712 sensor error in measuring current (Read 3387 times) previous topic - next topic

knightridar

Sep 19, 2017, 02:52 am Last Edit: Sep 19, 2017, 03:04 am by knightridar
Hi, I'm trying to use the ACS 712 sensors to measure current.

ACS712ELC-30A

I got the 30 amp version to make the current testing rig future proof.
Most of what I'm currently measuring though will be under 1 amp.
I didn't know that the analog values represent a certain set of mV values which are different based on each sensor model.

I changed the mVperAmp value to the value used for the 5 amp version of the sensor.
Although I don't think this is a practical fix I wanted to try it out.
Based on surfing the web and looking at other forum posts the values below are used to measure the current on the respective sensor models:

//use 185 for 5A Module, 100 for 20A Module, and 66 for 30A Module


I have 2 main issues:

1. My current ratings are not fluctuating on the serial monitor when the cycle is turning on.
    I've offset the ACSoffset value to get an initial current rating of ~ 0 amps, but even with the ports on
    and one solenoid attached to it, the current does not fluctuate.

2. I'm turning on and off 8 relays at the same time.
    With my current code setup when the cycle count is over the relays turn off.
    However, with  the code setup like shown below my cycle count is off by 1.

    If I start the cycle count with the port on then the count is right but it won't turn off after the cycle is
    over. I tried placing a port off code outside of the for loop and it still wouldn't turn off. So basically the
    relays stay on and I don't want that after the cycle is complete.
   
   
   
Code: [Select]

    PORTA = B00000000;
    delay(TOff); //delay between turning off each relay
    PORTA = B11111111;
    delay(TOn); //delay between turning on each relay
   






Code: [Select]

#include <LiquidCrystal.h>
#include <MenuBackend.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to

const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

// read the buttons
int read_LCD_buttons() {
  adc_key_in = analogRead(8);
  // read the value from the sensor
  // my buttons when read are centered at these values: 0, 144, 329, 504, 741
  // we add approx 50 to those values and check to see if we are close
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  return btnNONE;  // when all others fail, return this...
}

/****************************************************************************/

int TOn = 1000; //analogRead(1)*4;   // Adjust delay time in milliseconds to turn relay on/off
int TOff = 200; //analogRead(2)*4;

/* Measuring DC Current Using ACS712 */

double mVperAmp = 185; // use 185 for 5A Module, 100 for 20A Module, and 66 for 30A Module
float ACSoffset = 2470.345;

double RawValue1 = 0;
double RawValue2 = 0;
double RawValue3 = 0;
double RawValue4 = 0;
double RawValue5 = 0;
double RawValue6 = 0;
double RawValue7 = 0;
double RawValue8 = 0;

double V1 = 0;
double V2 = 0;
double V3 = 0;
double V4 = 0;
double V5 = 0;
double V6 = 0;
double V7 = 0;
double V8 = 0;

double Amps1 = 0;
double Amps2 = 0;
double Amps3 = 0;
double Amps4 = 0;
double Amps5 = 0;
double Amps6 = 0;
double Amps7 = 0;
double Amps8 = 0;

const int S1 = A0; // Analog input pin that sensor is attached to
const int S2 = A1;
const int S3 = A2;
const int S4 = A3;
const int S5 = A4;
const int S6 = A5;
const int S7 = A6;
const int S8 = A7;

void setup () {
  lcd.begin(16, 2);   // set up the LCD's number of columns and rows:
  lcd.setCursor(0, 0); // set printing position on lcd screen, currently that is row 0, column 0)
  lcd_key = read_LCD_buttons();  // read the buttons
  Serial.begin(9600);
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(S3, INPUT);
  pinMode(S4, INPUT);
  pinMode(S5, INPUT);
  pinMode(S6, INPUT);
  pinMode(S7, INPUT);
  pinMode(S8, INPUT);

  DDRA = B11111111; // set PORTA (digital 22-29) to outputs

  int maxnumber = 10; // set cycle count

  for (int count = 0; count < maxnumber;) //Counter will stop counting after certain amount of cycles
  {
    PORTA = B00000000;
    delay(TOff); //delay between turning off each relay
    PORTA = B11111111;
    delay(TOn); //delay between turning on each relay

    RawValue1 = analogRead(S1);
    RawValue2 = analogRead(S2);
    RawValue3 = analogRead(S3);
    RawValue4 = analogRead(S4);
    RawValue5 = analogRead(S5);
    RawValue6 = analogRead(S6);
    RawValue7 = analogRead(S7);
    RawValue8 = analogRead(S8);

    V1 = (RawValue1 / 1023.0) * 5000; // Gets you mV
    V2 = (RawValue2 / 1023.0) * 5000;
    V3 = (RawValue3 / 1023.0) * 5000;
    V4 = (RawValue4 / 1023.0) * 5000;
    V5 = (RawValue5 / 1023.0) * 5000;
    V6 = (RawValue6 / 1023.0) * 5000;
    V7 = (RawValue7 / 1023.0) * 5000;
    V8 = (RawValue8 / 1023.0) * 5000;

    Amps1 = ((V1 - ACSoffset) / mVperAmp);
    Amps2 = ((V2 - ACSoffset) / mVperAmp);
    Amps3 = ((V3 - ACSoffset) / mVperAmp);
    Amps4 = ((V4 - ACSoffset) / mVperAmp);
    Amps5 = ((V5 - ACSoffset) / mVperAmp);
    Amps6 = ((V6 - ACSoffset) / mVperAmp);
    Amps7 = ((V7 - ACSoffset) / mVperAmp);
    Amps8 = ((V8 - ACSoffset) / mVperAmp);

    Serial.println("Amps1 = "); // shows the voltage measured
    Serial.println(Amps1, 3); // the '2' after voltage allows you to display 2 digits after decimal point

    Serial.println("Amps2 = ");
    Serial.println(Amps2, 3);

    Serial.println("Amps3 = ");
    Serial.println(Amps3, 3);

    Serial.println("Amps4 = ");
    Serial.println(Amps4, 3);

    Serial.println("Amps5 = ");
    Serial.println(Amps5, 3);

    Serial.println("Amps6 = ");
    Serial.println(Amps6, 3);

    Serial.println("Amps7 = ");
    Serial.println(Amps7, 3);

    Serial.println("Amps8 = ");
    Serial.println(Amps8, 3);

    Serial.println("");

    lcd.clear(); //clears lcd before incrementing to next number
    count++;
    lcd.print(count);
  }
}

void loop() {
}
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

#1
Sep 19, 2017, 10:48 am Last Edit: Sep 19, 2017, 10:50 am by PaulS
Quote
Most of what I'm currently measuring though will be under 1 amp.
That's like trying to use a 30 pound scale to measure a few ounces. Using the proper sensor is a much better idea.

Quote
I didn't know that the analog values represent a certain set of mV values which are different based on each sensor model.
Then why would you comment, either way?

Quote
Based on surfing the web
So, you know what a link looks like, and yet you failed to post a link to your sensor. Try again.
The art of getting good answers lies in asking good questions.

knightridar

Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

Here is the link for the sensor:

https://www.amazon.com/DAOKI-Current-Sensor-Module-Arduino/dp/B00XT0PL20/ref=sr_1_1?ie=UTF8&qid=1505839180&sr=8-1&keywords=ARDUINO+CURRENT+SENSOR


So, amazon is making sensors these days. I did not know that. I, for sure, would not buy one from them since they have NO details about the sensor. All that they have is a pretty picture.

Pretty pictures are for porn magazines.
The art of getting good answers lies in asking good questions.

knightridar

here is the link to the pdf file of the sensor:

https://www.sparkfun.com/datasheets/BreakoutBoards/0712.pdf

Page 5 of 14 describes the mV/A setting used to calculate the current in the equations in the first post.
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

Code: [Select]
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(S3, INPUT);
  pinMode(S4, INPUT);
  pinMode(S5, INPUT);
  pinMode(S6, INPUT);
  pinMode(S7, INPUT);
  pinMode(S8, INPUT);

Why are you setting the mode of the digital pin that shares space with the analog pin, when using the pin as an analog pin. This CAN interfere with using the pin as an analog pin.

Arrays and for loops are your friends.

Reading from different analog pins usually requires that you read twice and throw away the first reading.

Code: [Select]
    Serial.println(Amps1, 3); // the '2' after voltage allows you to display 2 digits after decimal point

Useless comments MUST match the code!

Thinking that a 30 A sensor is going to read less than 1 A accurate to three decimal places is wishful thinking, at best.

The art of getting good answers lies in asking good questions.

knightridar

Ok got rid of pin mode.

I am breaking the problems down into pieces.

Issue 2 is resolved. I just had to turn the ports off at the end of the for loop.

Quote
2. I'm turning on and off 8 relays at the same time.
        With my current code setup when the cycle count is over the relays turn off.
        However, with  the code setup like shown below my cycle count is off by 1.

        If I start the cycle count with the port on then the count is right but it won't turn off after the cycle
        is over. I tried placing a port off code outside of the for loop and it still wouldn't turn off. So
        basically the relays stay on and I don't want that after the cycle is complete.
Code: [Select]

int TOn = 1000; //Adjust delay time in milliseconds to turn relay on/off
int TOff = 200;

void setup () {

  Serial.begin(9600);
  DDRA = B11111111; // set PORTA (digital 22-29) to outputs

  int maxnumber = 4; // set cycle count

  for (int count = 0; count < maxnumber;) //Counter will stop counting after certain amount of cycles
  {
    PORTA = B11111111;
    delay(TOn); //delay between turning on each relay
   
    PORTA = B00000000;
    delay(TOff); //delay between turning off each relay
   
    Serial.print(""); //clears serial port before incrementing to next number
    count++;
    Serial.print(count);
  }
  DDRA = B00000000;
}
void loop() {
}
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

knightridar

#7
Sep 19, 2017, 11:43 pm Last Edit: Sep 19, 2017, 11:51 pm by knightridar
I've modified the code to print on the lcd.

It's really bizarre this wasn't happening before but now I am getting weird characters on the lcd screen.
Like an infinity symbol, forward slash, blank block, etc... and sometimes the count displays.

I will double check the pin connections.
I am using the D1 Robot LCD keypad shield.
https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)

Code: [Select]

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to

const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

/**************************************************************************************/

int TOn = 1000; //Adjust delay time in milliseconds to turn relay on/off
int TOff = 200;

void setup () {

  lcd.begin(16, 2);   // set up the LCD's number of columns and rows:
  lcd.setCursor(0, 0); // set printing position on lcd screen, currently that is row 0, column 0)
  Serial.begin(9600);
  DDRA = B11111111; // set PORTA (digital 22-29) to outputs

  int maxnumber = 10; // set cycle count

  for (int count = 0; count < maxnumber;) //Counter will stop counting after certain amount of cycles
  {
    PORTA = B11111111;
    delay(TOn); //delay between turning on each relay

    PORTA = B00000000;
    delay(TOff); //delay between turning off each relay

    lcd.clear(); //clears lcd before incrementing to next number
    count++;
    lcd.print(count);
  }
  DDRA = B00000000;
}
void loop() {
}
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

knightridar

#8
Sep 20, 2017, 02:38 am Last Edit: Sep 20, 2017, 03:03 am by knightridar
I tested code with just text in it that remains static and it works without erroring out.

The screen only displays weird characters when the count function is displaying.

I didn't change the wiring either and I've double checked if the right wires are connected to the Arduino.

I'm wondering if it has anything to do with the relays turning on and off.
Even though I'm powering the screen with a separate 5V power supply it seems to flicker slightly as the relays turn on and off. Although I was using relays before and it was working fine with older versions of code. I tried it reuploading the older versions of code and I still get errors on the screen.

Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

knightridar

The code below works for counting and the display doesn't error out.
I'm beginning to wonder if it is the relays turning on and off.

Code: [Select]

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to

const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  lcd.begin(16, 2);   // set up the LCD's number of columns and rows:
  lcd.setCursor(0, 0); // set printing position on lcd screen, currently that is row 0, column 0)
}

void loop() {

  int maxnumber = 10; // set cycle count

  for (int count = 0; count < maxnumber;)
  {
    lcd.clear();
    lcd.print(count++);
    delay(1000);
  }
}
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

PaulS

Quote
I'm beginning to wonder if it is the relays turning on and off.
Unless you have snubber diodes on the relays, that is quite possible.
The art of getting good answers lies in asking good questions.

TomGeorge

Hi,
Can you post a complete picture of your project so we can see your component layout.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How are you powering the Arduino and the relays?

Thanks. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

MarkT

Quote
Hi, I'm trying to use the ACS 712 sensors to measure current.
...

I got the 30 amp version to make the current testing rig future proof.
Most of what I'm currently measuring though will be under 1 amp.
These are hall-effect sensors - they are very noisy and imprecise and only for high
current. But they are isolated from the load which can be handy. 

If you are measuring under 1A these are definitely not the sensors to use,
you want a shunt-resistor based sensor for that.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

knightridar

#13
Sep 20, 2017, 06:53 pm Last Edit: Sep 20, 2017, 07:02 pm by knightridar
I did think of getting one of those. Really liked the resolution on the sensor below and it'll work up to 3.2 amps. I'll look for some more.

https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/overview

Problem is I'm setting up a system using 8 relays.
The INA219 can only be used up to a maximum of 4 sensors because of the way it's setup and it uses the SDA and SCL ports on the arduino.

https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/assembly

Quote
Up to 4 boards may be connected. Addressing is as follows:
Board 0: Address = 0x40 Offset = binary 00000 (no jumpers required)
Board 1: Address = 0x41 Offset = binary 00001 (bridge A0 as in the photo above)
Board 2: Address = 0x44 Offset = binary 00100 (bridge A1)
Board 3: Address = 0x45 Offset = binary 00101 (bridge A0 & A1)
Useful CAD/ 3D model files for
Arduino, hobbyist, and engineering projects

https://grabcad.com/has-2

TomGeorge

Hi,
Please answer post #11.

How are you powering the project?
How are you driving the relays?
What are the relays?

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up