-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add example **estimateMidPointAC.ino** #37 - update readme.md (sampling trick #38). - minor edits.
- Loading branch information
1 parent
d7beb2d
commit c56d3ce
Showing
9 changed files
with
223 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
examples/ACS712_20_AC_simulation/ACS712_20_AC_simulation.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// FILE: ACS712_20_AC_simulation.ino | ||
// AUTHOR: Rob Tillaart | ||
// PURPOSE: demo AC measurement with point to point | ||
// URL: https://github.com/RobTillaart/ACS712 | ||
|
||
|
||
#include "ACS712.h" | ||
|
||
|
||
// Arduino UNO has 5.0 volt with a max ADC value of 1023 steps | ||
// ACS712 5A uses 185 mV per A | ||
// ACS712 20A uses 100 mV per A | ||
// ACS712 30A uses 66 mV per A | ||
|
||
|
||
ACS712 ACS(A0, 5.0, 1023, 100); | ||
// ESP 32 example (might requires resistors to step down the logic voltage) | ||
// ACS712 ACS(25, 3.3, 4095, 185); | ||
|
||
|
||
uint32_t start, stop; | ||
|
||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial); | ||
Serial.println(__FILE__); | ||
Serial.print("ACS712_LIB_VERSION: "); | ||
Serial.println(ACS712_LIB_VERSION); | ||
|
||
// select simulated signal | ||
ACS.setADC(signal, 5, 1024); | ||
|
||
ACS.autoMidPoint(); | ||
Serial.print("MidPoint: "); | ||
Serial.print(ACS.getMidPoint()); | ||
Serial.print(". Noise mV: "); | ||
Serial.println(ACS.getNoisemV()); | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
delay(100); | ||
start = micros(); | ||
// int mA = ACS.mA_AC(); | ||
int mA = ACS.mA_AC_sampling(50); | ||
stop = micros(); | ||
Serial.print("mA: "); | ||
Serial.print(mA); | ||
Serial.print(" time: "); | ||
Serial.println(stop - start); | ||
delay(5000); | ||
} | ||
|
||
|
||
// simulation. | ||
uint16_t signal(uint8_t p) | ||
{ | ||
return round(512 + 400 * sin((micros() % 1000000) * (TWO_PI * 50 / 1e6))); | ||
} | ||
|
||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// FILE: estimateMidPointAC.ino | ||
// AUTHOR: Rob Tillaart | ||
// PURPOSE: experimental | ||
// URL: https://github.com/RobTillaart/ACS712/issues/37 | ||
// | ||
// Estimates the midpoint by taking many (short-blocking) samples | ||
// instead of many samples in a long blocking period. | ||
// The function adjusts the confidence (or quality) of the midpoint | ||
// depending on the value read. | ||
// This code is experimental and meant to investigate a non-blocking | ||
// way to find the midPoint for the ACS712 when measuring AC currents. | ||
// | ||
// It will not be included in the library | ||
// | ||
// Use with care. | ||
|
||
|
||
#include "Arduino.h" | ||
|
||
|
||
uint32_t start, stop; | ||
int _pin = A0; | ||
uint32_t count = 0; | ||
volatile uint16_t mp; | ||
float conf = 0; | ||
|
||
|
||
uint16_t estimateMidPointAC(float &confidence, bool reset = false) | ||
{ | ||
static bool _firstCall = true; | ||
static float _minimum, _maximum, _confidence; | ||
|
||
int value = analogRead(_pin); | ||
if (_firstCall || reset) | ||
{ | ||
_firstCall = false; | ||
_minimum = _maximum = value; | ||
_confidence = 0; | ||
confidence = _confidence; | ||
return _minimum; | ||
} | ||
if (value > _maximum) | ||
{ | ||
_maximum = value; | ||
_confidence /= 2; | ||
} | ||
else if (value < _minimum) | ||
{ | ||
_minimum = value; | ||
_confidence /= 2; | ||
} | ||
else if (_confidence < 100) | ||
{ | ||
_confidence += 1; | ||
} | ||
confidence = _confidence; | ||
return (_minimum + _maximum) / 2; | ||
} | ||
|
||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
while (!Serial); | ||
Serial.println(__FILE__); | ||
} | ||
|
||
|
||
void loop() | ||
{ | ||
count++; | ||
start = micros(); | ||
mp = estimateMidPointAC(conf, true); | ||
stop = micros(); | ||
Serial.print(millis()); | ||
Serial.print("\t"); | ||
Serial.print(count); | ||
Serial.print("\t"); | ||
Serial.print(conf); | ||
Serial.print("\t"); | ||
Serial.print(mp); | ||
Serial.print("\t"); | ||
Serial.print(stop - start); | ||
Serial.println(); | ||
delay(random(100)); | ||
} | ||
|
||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=ACS712 | ||
version=0.3.6 | ||
version=0.3.7 | ||
author=Rob Tillaart <[email protected]>, Pete Thompson <[email protected]> | ||
maintainer=Rob Tillaart <[email protected]> | ||
sentence=ACS712 library for Arduino. | ||
|