ACS712 Current Sensor Arduino Library 1.0.0
This is a library for the ACS712 current sensor.
Loading...
Searching...
No Matches
ACS712.h
Go to the documentation of this file.
1#pragma once
2//
3// FILE: _ACS712.h
4// AUTHOR: Rob Tillaart
5// VERSION: 0.2.2
6// DATE: 2020-08-02
7// PURPOSE: _ACS712 library - current measurement
8//
9// Tested with a RobotDyn _ACS712 20A breakout + UNO.
10//
11
12#include "Arduino.h"
13
14#define ACS712_LIB_VERSION (F("0.2.2"))
15
16// ACS712_FF_SINUS == 1.0/sqrt(2) == 0.5 * sqrt(2)
17// should be smaller in practice 0.5 ?
18#define ACS712_FF_SINUS (1.0 / sqrt(2))
19#define ACS712_FF_SQUARE (1.0)
20#define ACS712_FF_TRIANGLE (1.0 / sqrt(3))
21
22
24{
25 public:
26 // NOTE:
27 // One can quite precisely tune the value of the sensor
28 // (1) the milliVolt per Ampere and
29 // (2) the volts parameter.
30 //
31 // TYPE mV per Ampere
32 // 5A 185
33 // 20A 100
34 // 30A 66
35 _ACS712(uint8_t analogPin, float volts = 5.0, uint16_t maxADC = 1023, uint8_t mVperA = 100);
36
37
38 // returns mA
39 // blocks 20-21 ms to sample a whole 50 or 60 Hz period.
40 // lower frequencies block longer.
41 int mA_AC(uint8_t freq = 50);
42
43
44 // returns mA
45 // blocks < 1 ms
46 int mA_DC();
47
48
49 // midpoint ADC for DC only
50 inline void setMidPoint(uint16_t mp)
51 {
52 _midPoint = mp;
53 };
54 inline uint16_t getMidPoint()
55 {
56 return _midPoint;
57 };
58 inline void incMidPoint()
59 {
60 _midPoint++;
61 };
62 inline void decMidPoint()
63 {
64 _midPoint--;
65 };
66 // Auto midPoint, assuming zero DC current or any AC current
67 void autoMidPoint(uint8_t freq = 50);
68
69
70 // also known as crest factor; affects mA_AC() only
71 // default sinus.
72 inline void setFormFactor(float ff = ACS712_FF_SINUS)
73 {
74 _formFactor = ff;
75 };
76 inline float getFormFactor()
77 {
78 return _formFactor;
79 };
80
81
82 // noise defaults 21
83 inline void setNoisemV(uint8_t noisemV = 21)
84 {
85 _noisemV = noisemV;
86 };
87 inline uint8_t getNoisemV()
88 {
89 return _noisemV;
90 };
91
92
93 // AC and DC
94 inline void setmVperAmp(uint8_t mva)
95 {
96 _mVperAmpere = mva;
97 };
98 inline uint8_t getmVperAmp()
99 {
100 return _mVperAmpere;
101 };
102
103
104 private:
105 uint8_t _pin;
106 float _mVpstep; // millivolt per step
107 float _formFactor; // point2point -> RMS
109 uint16_t _midPoint;
110 uint8_t _noisemV;
111};
112
113// -- END OF FILE --
Definition ACS712.h:24
void setMidPoint(uint16_t mp)
Definition ACS712.h:50
int mA_DC()
Definition ACS712.cpp:76
uint8_t _noisemV
Definition ACS712.h:110
void setNoisemV(uint8_t noisemV=21)
Definition ACS712.h:83
float getFormFactor()
Definition ACS712.h:76
uint8_t _pin
Definition ACS712.h:105
float _formFactor
Definition ACS712.h:107
void setFormFactor(float ff=ACS712_FF_SINUS)
Definition ACS712.h:72
void autoMidPoint(uint8_t freq=50)
Definition ACS712.cpp:87
uint8_t _mVperAmpere
Definition ACS712.h:108
float _mVpstep
Definition ACS712.h:106
uint16_t _midPoint
Definition ACS712.h:109
void setmVperAmp(uint8_t mva)
Definition ACS712.h:94
uint8_t getNoisemV()
Definition ACS712.h:87
void incMidPoint()
Definition ACS712.h:58
uint8_t getmVperAmp()
Definition ACS712.h:98
int mA_AC(uint8_t freq=50)
Definition ACS712.cpp:33
void decMidPoint()
Definition ACS712.h:62
uint16_t getMidPoint()
Definition ACS712.h:54