GY-63_MS5611/libraries/ACS712/ACS712.h

94 lines
2.6 KiB
C
Raw Normal View History

2020-03-19 10:16:52 -04:00
#pragma once
//
// FILE: ACS712.h
2021-12-01 08:20:22 -05:00
// AUTHOR: Rob Tillaart, Pete Thompson
2021-12-09 09:42:38 -05:00
// VERSION: 0.2.6
2020-11-27 05:10:47 -05:00
// DATE: 2020-08-02
2020-03-19 10:16:52 -04:00
// PURPOSE: ACS712 library - current measurement
//
// Tested with a RobotDyn ACS712 20A breakout + UNO.
//
2021-10-16 05:40:09 -04:00
2020-03-19 10:16:52 -04:00
#include "Arduino.h"
2021-12-09 09:42:38 -05:00
#define ACS712_LIB_VERSION (F("0.2.6"))
2021-10-16 05:40:09 -04:00
2021-06-24 08:41:36 -04:00
// ACS712_FF_SINUS == 1.0/sqrt(2) == 0.5 * sqrt(2)
// should be smaller in practice 0.5 ?
#define ACS712_FF_SINUS (1.0/sqrt(2))
#define ACS712_FF_SQUARE (1.0)
#define ACS712_FF_TRIANGLE (1.0/sqrt(3))
2020-03-19 10:16:52 -04:00
class ACS712
{
public:
// NOTE:
// One can quite precisely tune the value of the sensor
// (1) the milliVolt per Ampere and
// (2) the volts parameter.
//
// TYPE mV per Ampere
// 5A 185
// 20A 100
// 30A 66
ACS712(uint8_t analogPin, float volts = 5.0, uint16_t maxADC = 1023, uint8_t mVperA = 100);
2021-06-24 08:41:36 -04:00
2020-03-19 10:16:52 -04:00
// returns mA
2020-11-27 05:10:47 -05:00
// blocks 20-21 ms to sample a whole 50 or 60 Hz period.
2021-06-24 08:41:36 -04:00
// lower frequencies block longer.
2021-10-16 05:40:09 -04:00
int mA_AC(float freq = 50);
2020-03-19 10:16:52 -04:00
2021-06-24 08:41:36 -04:00
2020-03-19 10:16:52 -04:00
// returns mA
// blocks < 1 ms
int mA_DC();
2021-06-24 08:41:36 -04:00
2020-03-19 10:16:52 -04:00
// midpoint ADC for DC only
2021-01-29 06:31:58 -05:00
inline void setMidPoint(uint16_t mp) { _midPoint = mp; };
2020-03-19 10:16:52 -04:00
inline uint16_t getMidPoint() { return _midPoint; };
inline void incMidPoint() { _midPoint++; };
inline void decMidPoint() { _midPoint--; };
2020-11-27 05:10:47 -05:00
// Auto midPoint, assuming zero DC current or any AC current
2021-10-16 05:40:09 -04:00
void autoMidPoint(float freq = 50);
2020-03-19 10:16:52 -04:00
2021-06-24 08:41:36 -04:00
// also known as crest factor; affects mA_AC() only
// default sinus.
inline void setFormFactor(float ff = ACS712_FF_SINUS) { _formFactor = ff; };
2020-03-19 10:16:52 -04:00
inline float getFormFactor() { return _formFactor; };
2021-06-24 08:41:36 -04:00
// noise defaults 21
inline void setNoisemV(uint8_t noisemV = 21) { _noisemV = noisemV; };
2020-11-27 05:10:47 -05:00
inline uint8_t getNoisemV() { return _noisemV; };
2021-06-24 08:41:36 -04:00
2020-03-19 10:16:52 -04:00
// AC and DC
inline void setmVperAmp(uint8_t mva) { _mVperAmpere = mva; };
inline uint8_t getmVperAmp() { return _mVperAmpere; };
2021-06-24 08:41:36 -04:00
2021-12-01 08:20:22 -05:00
// Experimental frequency detection.
// the minimal frequency determines the time to sample.
2021-12-02 13:29:13 -05:00
float detectFrequency(float minimalFrequency = 40);
void setMicrosAdjust(float factor = 1.0) { _microsAdjust = factor; };
2021-12-01 08:20:22 -05:00
float getMicrosAdjust() { return _microsAdjust; };
2020-03-19 10:16:52 -04:00
private:
uint8_t _pin;
2021-01-29 06:31:58 -05:00
float _mVpstep; // millivolt per step
float _formFactor; // point2point -> RMS
2020-03-19 10:16:52 -04:00
uint8_t _mVperAmpere;
uint16_t _midPoint;
2020-11-27 05:10:47 -05:00
uint8_t _noisemV;
2021-12-01 08:20:22 -05:00
float _microsAdjust = 1.0; // 0.9986
2020-03-19 10:16:52 -04:00
};
2021-06-24 08:41:36 -04:00
// -- END OF FILE --