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
|
2023-09-20 13:55:34 -04:00
|
|
|
// VERSION: 0.3.8
|
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
|
2023-01-03 14:19:20 -05:00
|
|
|
// URL: https://github.com/RobTillaart/ACS712
|
2020-03-19 10:16:52 -04:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2023-09-20 13:55:34 -04:00
|
|
|
|
|
|
|
#define ACS712_LIB_VERSION (F("0.3.8"))
|
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))
|
2022-08-12 04:47:41 -04:00
|
|
|
#define ACS712_FF_SAWTOOTH (1.0/sqrt(3))
|
2020-03-19 10:16:52 -04:00
|
|
|
|
2022-08-28 03:44:41 -04:00
|
|
|
#define ACS712_DEFAULT_FREQ 50
|
|
|
|
#define ACS712_DEFAULT_NOISE 21
|
|
|
|
|
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.
|
|
|
|
//
|
2022-08-12 04:47:41 -04:00
|
|
|
// TYPE mV per Ampere
|
|
|
|
// 5A 185.0
|
|
|
|
// 20A 100.0
|
|
|
|
// 30A 66.0
|
|
|
|
ACS712(uint8_t analogPin, float volts = 5.0, uint16_t maxADC = 1023, float mVperAmpere = 100);
|
2020-03-19 10:16:52 -04:00
|
|
|
|
2022-09-01 05:19:21 -04:00
|
|
|
// returns mA peak2peak current.
|
|
|
|
float mA_peak2peak(float frequency = ACS712_DEFAULT_FREQ, uint16_t cycles = 1);
|
2021-06-24 08:41:36 -04:00
|
|
|
|
2022-08-12 04:47:41 -04:00
|
|
|
// returns mA
|
|
|
|
// blocks 20-21 ms to sample a whole 50 or 60 Hz period.
|
2022-09-01 05:19:21 -04:00
|
|
|
// works with peak2peak level and (crest) Form Factor.
|
2022-08-12 04:47:41 -04:00
|
|
|
// lower frequencies block longer.
|
2022-09-01 05:19:21 -04:00
|
|
|
float mA_AC(float frequency = ACS712_DEFAULT_FREQ, uint16_t cycles = 1);
|
2020-03-19 10:16:52 -04:00
|
|
|
|
2022-08-28 03:44:41 -04:00
|
|
|
// returns mA
|
|
|
|
// blocks 20-21 ms to sample a whole 50 or 60 Hz period.
|
|
|
|
// works with sampling.
|
|
|
|
// lower frequencies block longer.
|
|
|
|
float mA_AC_sampling(float frequency = ACS712_DEFAULT_FREQ, uint16_t cycles = 1);
|
2021-06-24 08:41:36 -04:00
|
|
|
|
2022-08-12 04:47:41 -04:00
|
|
|
// returns mA
|
|
|
|
// blocks < 1 ms
|
2022-09-01 05:19:21 -04:00
|
|
|
float mA_DC(uint16_t samples = 1);
|
2020-03-19 10:16:52 -04:00
|
|
|
|
2021-06-24 08:41:36 -04:00
|
|
|
|
2022-08-28 03:44:41 -04:00
|
|
|
// midPoint functions
|
2022-09-01 05:19:21 -04:00
|
|
|
// set reference point (raw ADC) for both DC and AC
|
|
|
|
uint16_t setMidPoint(uint16_t midPoint);
|
2022-08-28 03:44:41 -04:00
|
|
|
uint16_t getMidPoint();
|
2022-09-01 05:19:21 -04:00
|
|
|
uint16_t incMidPoint();
|
|
|
|
uint16_t decMidPoint();
|
2022-08-12 04:47:41 -04:00
|
|
|
// Auto midPoint, assuming zero DC current or any AC current
|
2023-04-20 06:03:58 -04:00
|
|
|
// For DC current set the frequency to 1000 or so to reduce blocking.
|
2022-09-01 05:19:21 -04:00
|
|
|
uint16_t autoMidPoint(float frequency = ACS712_DEFAULT_FREQ, uint16_t cycles = 1);
|
2023-04-20 06:03:58 -04:00
|
|
|
// Auto midPoint, dedicated for zero DC current (much faster ==> less blocking)
|
|
|
|
uint16_t autoMidPointDC(uint16_t cycles = 1);
|
2022-10-10 06:21:13 -04:00
|
|
|
// resets to half maxADC
|
|
|
|
uint16_t resetMidPoint();
|
2020-03-19 10:16:52 -04:00
|
|
|
|
2021-06-24 08:41:36 -04:00
|
|
|
|
2022-08-12 04:47:41 -04:00
|
|
|
// Form Factor is also known as crest factor;
|
|
|
|
// affects mA_AC() only, default sinus.
|
2022-08-28 03:44:41 -04:00
|
|
|
void setFormFactor(float formFactor = ACS712_FF_SINUS);
|
|
|
|
float getFormFactor();
|
2020-03-19 10:16:52 -04:00
|
|
|
|
2021-06-24 08:41:36 -04:00
|
|
|
|
2022-08-28 03:44:41 -04:00
|
|
|
// noise defaults 21 datasheet
|
|
|
|
void setNoisemV(uint8_t noisemV = ACS712_DEFAULT_NOISE);
|
|
|
|
uint8_t getNoisemV();
|
2022-10-10 06:21:13 -04:00
|
|
|
// enable/disable noiseSuppression for this measurement as needed.
|
|
|
|
float mVNoiseLevel(float frequency = ACS712_DEFAULT_FREQ, uint16_t cycles = 1); // uses mA_peak2peak()
|
|
|
|
void suppressNoise(bool flag);
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2021-06-24 08:41:36 -04:00
|
|
|
|
2022-08-28 03:44:41 -04:00
|
|
|
// Adjusting resolution AC and DC
|
|
|
|
void setmVperAmp(float mVperAmpere);
|
|
|
|
float getmVperAmp();
|
2022-09-01 05:19:21 -04:00
|
|
|
float getmAPerStep();
|
2022-08-28 03:44:41 -04:00
|
|
|
float getAmperePerStep();
|
2020-03-19 10:16:52 -04:00
|
|
|
|
2021-06-24 08:41:36 -04:00
|
|
|
|
2022-08-12 04:47:41 -04:00
|
|
|
// Frequency detection.
|
|
|
|
// the minimal frequency determines the time to sample.
|
|
|
|
float detectFrequency(float minimalFrequency = 40);
|
2022-08-28 03:44:41 -04:00
|
|
|
void setMicrosAdjust(float factor = 1.0);
|
|
|
|
float getMicrosAdjust();
|
2021-12-01 08:20:22 -05:00
|
|
|
|
2022-09-01 05:19:21 -04:00
|
|
|
// DEBUG
|
|
|
|
uint16_t getMinimum(uint16_t milliSeconds = 20);
|
|
|
|
uint16_t getMaximum(uint16_t milliSeconds = 20);
|
|
|
|
|
2021-12-01 08:20:22 -05:00
|
|
|
|
2023-01-15 14:39:54 -05:00
|
|
|
// EXPERIMENTAL 0.3.4
|
2023-04-20 06:03:58 -04:00
|
|
|
// function returning 16 bit max, with pin or channel as parameter
|
2023-01-15 14:39:54 -05:00
|
|
|
void setADC(uint16_t (*)(uint8_t), float volts, uint16_t maxADC);
|
|
|
|
|
2023-04-20 06:03:58 -04:00
|
|
|
|
2020-03-19 10:16:52 -04:00
|
|
|
private:
|
|
|
|
uint8_t _pin;
|
2022-10-10 06:21:13 -04:00
|
|
|
uint16_t _maxADC;
|
2022-08-12 04:47:41 -04:00
|
|
|
float _mVperStep;
|
2022-08-28 03:44:41 -04:00
|
|
|
float _formFactor; // peak2peak -> RMS
|
2022-08-12 04:47:41 -04:00
|
|
|
float _mVperAmpere;
|
2022-09-01 05:19:21 -04:00
|
|
|
float _mAPerStep;
|
2022-11-21 14:44:08 -05:00
|
|
|
int _midPoint;
|
2020-11-27 05:10:47 -05:00
|
|
|
uint8_t _noisemV;
|
2022-10-10 06:21:13 -04:00
|
|
|
float _microsAdjust = 1.0; // 0.9986
|
|
|
|
bool _suppresNoise = false;
|
2023-01-15 14:39:54 -05:00
|
|
|
|
|
|
|
// EXPERIMENTAL 0.3.4
|
|
|
|
// supports up to 16 bits ADC.
|
|
|
|
uint16_t (* _readADC)(uint8_t);
|
2023-01-18 07:43:31 -05:00
|
|
|
uint16_t _analogRead(uint8_t pin);
|
|
|
|
|
2020-03-19 10:16:52 -04:00
|
|
|
};
|
|
|
|
|
2022-08-12 04:47:41 -04:00
|
|
|
|
2023-05-20 09:38:39 -04:00
|
|
|
// -- END OF FILE --
|
2022-08-12 04:47:41 -04:00
|
|
|
|