├── ACS712.cpp ├── ACS712.h ├── README.md ├── examples ├── DetermineZeroPoint │ └── DetermineZeroPoint.ino ├── MeasureCurrentDC │ └── MeasureCurrentDC.ino ├── SetZeroPoint │ └── SetZeroPoint.ino └── Wattmeter │ └── Wattmeter.ino └── keywords.txt /ACS712.cpp: -------------------------------------------------------------------------------- 1 | #include "ACS712.h" 2 | int zero; 3 | ACS712::ACS712(ACS712_type type, uint8_t _pin) { 4 | switch (type) { 5 | case ACS712_05B: 6 | sensitivity = 0.185; 7 | break; 8 | case ACS712_20A: 9 | sensitivity = 0.100; 10 | break; 11 | case ACS712_30A: 12 | sensitivity = 0.066; 13 | break; 14 | default: 15 | sensitivity = 0.066; 16 | break; 17 | } 18 | pin = _pin; 19 | } 20 | 21 | int ACS712::calibrate() { 22 | int _zero = 0; 23 | for (int i = 0; i < 20; i++) { 24 | _zero += analogRead(pin); 25 | delay(1); 26 | } 27 | _zero /= 20; 28 | zero = _zero; 29 | return _zero; 30 | } 31 | 32 | void ACS712::setZeroPoint(int _zero) { 33 | zero = _zero; 34 | } 35 | 36 | void ACS712::setSensitivity(float sens) { 37 | sensitivity = sens; 38 | } 39 | 40 | float ACS712::getCurrentDC() { 41 | float I = (zero - analogRead(pin)) / ADC_SCALE * VREF / sensitivity; 42 | return I; 43 | } 44 | 45 | float ACS712::getCurrentAC() { 46 | return getCurrentAC(DEFAULT_FREQUENCY); 47 | } 48 | 49 | float ACS712::getCurrentAC(uint16_t frequency) { 50 | uint32_t period = 1000000 / frequency; 51 | uint32_t t_start = micros(); 52 | 53 | uint32_t Isum = 0, measurements_count = 0; 54 | int32_t Inow; 55 | 56 | while (micros() - t_start < period) { 57 | Inow = zero - analogRead(pin); 58 | Isum += Inow*Inow; 59 | measurements_count++; 60 | } 61 | 62 | float Irms = sqrt(Isum / measurements_count) / ADC_SCALE * VREF / sensitivity; 63 | return Irms; 64 | } 65 | -------------------------------------------------------------------------------- /ACS712.h: -------------------------------------------------------------------------------- 1 | #ifndef ACS712_h 2 | #define ACS712_h 3 | 4 | #include 5 | 6 | #define ADC_SCALE 1023.0 7 | #define VREF 5.0 8 | #define DEFAULT_FREQUENCY 50 9 | 10 | enum ACS712_type {ACS712_05B, ACS712_20A, ACS712_30A}; 11 | 12 | class ACS712 { 13 | public: 14 | ACS712(ACS712_type type, uint8_t _pin); 15 | int calibrate(); 16 | void setZeroPoint(int _zero); 17 | void setSensitivity(float sens); 18 | float getCurrentDC(); 19 | float getCurrentAC(); 20 | float getCurrentAC(uint16_t frequency); 21 | 22 | private: 23 | float zero = 512.0; 24 | float sensitivity; 25 | uint8_t pin; 26 | }; 27 | 28 | #endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ACS712 2 | ====== 3 | An Arduino library to interact with the ACS712 Hall effect-based linear current sensor. Includes DC and RMS AC current measuring. Supports ACS712-05B, ACS712-10A, ACS712-30A sensors. Typical applications include motor control, load detection and management, switch mode power supplies, and overcurrent fault protection. 4 | 5 | For more information see the datasheet: http://www.allegromicro.com/~/media/files/datasheets/acs712-datasheet.ashx 6 | 7 | Wiring 8 | ====== 9 | | ACS712 | Arduino | 10 | |:------:|:-------:| 11 | | GND | GND | 12 | | OUT | A1 | 13 | | VCC | 5V | 14 | 15 | Methods 16 | ======= 17 | ### Constructor: 18 | ### **ACS712(** *ACS712_type* type, *uint8_t* _pin **)** 19 | Constructor has two parameters: sensor model and analog input to which it is connected. Supported models: **ACS712_05B**, **ACS712_20A**, **ACS712_30A** 20 | 21 | ### *float* **getCurrentDC()** 22 | This method reads the value from the current sensor and returns it. 23 | 24 | ### *float* **getCurrentAC(** *uint16_t* frequency **)** 25 | This method allows you to measure AC voltage. Current frequency is measured in Hz. Method use the Root Mean Square technique for the measurement. The measurement itself takes time of one full period (1second / frequency). RMS method allow us to measure complex signals different from the perfect sine wave. 26 | 27 | ### *float* **getCurrentAC()** 28 | Does the same as the previous method, but frequency is equal to 50 Hz. 29 | 30 | ### *int* **calibrate()** 31 | This method reads the current value of the sensor and sets it as a reference point of measurement, and then returns this value. By default, this parameter is equal to half of the maximum value on analog input - 512; however, sometimes this value may vary. It depends on the concrete sensor, power issues etc… It is better to execute this method at the beginning of each program. Note that when performing this method, no current must flow through the sensor, and since this is not always possible - there is the following method: 32 | 33 | ### *void* **setZeroPoint(** *int* _zero **)** 34 | This method sets the obtained value as a zero point for measurements. You can use the previous method once, in order to find out zero point of your sensor and then use this method in your code. 35 | -------------------------------------------------------------------------------- /examples/DetermineZeroPoint/DetermineZeroPoint.ino: -------------------------------------------------------------------------------- 1 | #include "ACS712.h" 2 | 3 | /* 4 | This example shows how to determine zero point of your sensor 5 | */ 6 | 7 | // We have 30 amps version sensor connected to A1 pin of arduino 8 | // Replace with your version if necessary 9 | ACS712 sensor(ACS712_30A, A1); 10 | 11 | void setup() { 12 | Serial.begin(9600); 13 | 14 | float zero_point = sensor.calibrate(); 15 | 16 | Serial.print("Zero point for this sensor is "); 17 | Serial.println(zero_point); 18 | } 19 | 20 | void loop() {} 21 | -------------------------------------------------------------------------------- /examples/MeasureCurrentDC/MeasureCurrentDC.ino: -------------------------------------------------------------------------------- 1 | #include "ACS712.h" 2 | 3 | /* 4 | This example shows how to measure DC current 5 | */ 6 | 7 | // We have 30 amps version sensor connected to A1 pin of arduino 8 | // Replace with your version if necessary 9 | ACS712 sensor(ACS712_30A, A1); 10 | 11 | void setup() { 12 | Serial.begin(9600); 13 | 14 | // This method calibrates zero point of sensor, 15 | // It is not necessary, but may positively affect the accuracy 16 | // Ensure that no current flows through the sensor at this moment 17 | sensor.calibrate(); 18 | } 19 | 20 | void loop() { 21 | // Get current from sensor 22 | float I = sensor.getCurrentDC(); 23 | 24 | // Send it to serial 25 | Serial.println(String("I = ") + I + " A"); 26 | 27 | // Wait one second before the new cycle 28 | delay(1000); 29 | } 30 | -------------------------------------------------------------------------------- /examples/SetZeroPoint/SetZeroPoint.ino: -------------------------------------------------------------------------------- 1 | #include "ACS712.h" 2 | 3 | /* 4 | This example shows how to set zero point of your sensor 5 | */ 6 | 7 | // We have 30 amps version sensor connected to A1 pin of arduino 8 | // Replace with your version if necessary 9 | ACS712 sensor(ACS712_30A, A1); 10 | 11 | void setup() { 12 | Serial.begin(9600); 13 | 14 | // Value obtained using sensor.calibrate() when no current flows through the sensor 15 | sensor.setZeroPoint(438); 16 | } 17 | 18 | void loop() {} 19 | -------------------------------------------------------------------------------- /examples/Wattmeter/Wattmeter.ino: -------------------------------------------------------------------------------- 1 | #include "ACS712.h" 2 | 3 | /* 4 | This example shows how to measure the power consumption 5 | of devices in 230V electrical system 6 | or any other system with alternative current 7 | */ 8 | 9 | // We have 30 amps version sensor connected to A1 pin of arduino 10 | // Replace with your version if necessary 11 | ACS712 sensor(ACS712_30A, A1); 12 | 13 | void setup() { 14 | Serial.begin(9600); 15 | 16 | // This method calibrates zero point of sensor, 17 | // It is not necessary, but may positively affect the accuracy 18 | // Ensure that no current flows through the sensor at this moment 19 | sensor.calibrate(); 20 | } 21 | 22 | void loop() { 23 | // We use 230V because it is the common standard in European countries 24 | // Change to your local, if necessary 25 | float U = 230; 26 | 27 | // To measure current we need to know the frequency of current 28 | // By default 50Hz is used, but you can specify own, if necessary 29 | float I = sensor.getCurrentAC(); 30 | 31 | // To calculate the power we need voltage multiplied by current 32 | float P = U * I; 33 | 34 | Serial.println(String("P = ") + P + " Watts"); 35 | 36 | delay(1000); 37 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For ACS712 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ACS712 KEYWORD1 10 | ACS712_type KEYWORD1 11 | 12 | ####################################### 13 | # Methods and Functions (KEYWORD2) 14 | ####################################### 15 | 16 | begin KEYWORD2 17 | calibrate KEYWORD2 18 | setSensitivity KEYWORD2 19 | getCurrentDC KEYWORD2 20 | getCurrentAC KEYWORD2 21 | 22 | ####################################### 23 | # Constants (LITERAL1) 24 | ####################################### 25 | 26 | ACS712_05B LITERAL1 27 | ACS712_20A LITERAL1 28 | ACS712_30A LITERAL1 29 | --------------------------------------------------------------------------------