Skip to content

Commit 891cd90

Browse files
authored
fix #26 DC measurements (#28)
* fix #26 midPoint data type * fix incrMidpoint
1 parent e9d55d0 commit 891cd90

File tree

8 files changed

+107
-53
lines changed

8 files changed

+107
-53
lines changed

.arduino-ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ compile:
2424
- esp32
2525
- esp8266
2626
# - mega2560
27-
- rpipico
27+
- rpipico
28+

ACS712.cpp

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,9 @@
11
//
22
// FILE: ACS712.cpp
33
// AUTHOR: Rob Tillaart, Pete Thompson
4-
// VERSION: 0.3.1
4+
// VERSION: 0.3.2
55
// DATE: 2020-08-02
66
// PURPOSE: ACS712 library - current measurement
7-
//
8-
// HISTORY:
9-
// 0.1.0 2020-03-17 initial version
10-
// 0.1.1 2020-03-18 first release version
11-
// 0.1.2 2020-03-21 automatic form factor test
12-
// 0.1.3 2020-05-27 fix library.json
13-
// 0.1.4 2020-08-02 Allow for faster processors
14-
//
15-
// 0.2.0 2020-08-02 Add autoMidPoint
16-
// 0.2.1 2020-12-06 Add Arduino-CI + readme + unit test + refactor
17-
// 0.2.2 2021-06-23 support for more frequencies.
18-
// 0.2.3 2021-10-15 changed frequencies to float, for optimal tuning.
19-
// updated build CI, readme.md
20-
// 0.2.4 2021-11-22 add experimental detectFrequency()
21-
// 0.2.5 2021-12-03 add timeout to detectFrequency()
22-
// 0.2.6 2021-12-09 update readme.md + license
23-
// 0.2.7 2022-08-10 change mVperAmp to float
24-
// add ACS712_FF_SAWTOOTH
25-
// update readme.md + unit test + minor edits
26-
// 0.2.8 2022-08-19 prepare for 0.3.0
27-
// Fix #21 FormFactor
28-
// add mA_AC_sampling() as method to determine
29-
// current when FormFactor is unknown.
30-
// added float _AmperePerStep cached value.
31-
// added getAmperePerStep();
32-
// moved several functions to .cpp
33-
// improve documentation
34-
//
35-
// 0.3.0 2022-09-01 return midPoint value in MP functions.
36-
// float return type for mA() functions
37-
// add float mA_peak2peak(freq, cycles)
38-
// add debug getMinimum(), getmaximum();
39-
// update Readme.md
40-
// 0.3.1 2022-09-xx add float mVNoiseLevel(frequency, cycles)
41-
// add void suppressNoise(bool flag)
42-
// experimental suppression by averaging two samples.
43-
// update readme.md
44-
// improve midPoint functions
45-
// add resetMidPoint()
46-
// add RP2040 pico in build-ci
477

488

499
#include "ACS712.h"
@@ -184,6 +144,7 @@ float ACS712::mA_AC_sampling(float frequency, uint16_t cycles)
184144
}
185145
float current = value - _midPoint;
186146
sumSquared += (current * current);
147+
// not adding noise squared might be more correct for small currents.
187148
// if (abs(current) > noiseLevel)
188149
// {
189150
// sumSquared += (current * current);
@@ -223,7 +184,7 @@ float ACS712::mA_DC(uint16_t cycles)
223184
// CALIBRATION MIDPOINT
224185
uint16_t ACS712::setMidPoint(uint16_t midPoint)
225186
{
226-
if (midPoint <= _maxADC) _midPoint = midPoint;
187+
if (midPoint <= _maxADC) _midPoint = (int) midPoint;
227188
return _midPoint;
228189
};
229190

@@ -236,7 +197,7 @@ uint16_t ACS712::getMidPoint()
236197

237198
uint16_t ACS712::incMidPoint()
238199
{
239-
if (_midPoint < _maxADC) _midPoint += 1;
200+
if (_midPoint < (int)(_maxADC)) _midPoint += 1;
240201
return _midPoint;
241202
};
242203

ACS712.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: ACS712.h
44
// AUTHOR: Rob Tillaart, Pete Thompson
5-
// VERSION: 0.3.1
5+
// VERSION: 0.3.2
66
// DATE: 2020-08-02
77
// PURPOSE: ACS712 library - current measurement
88
//
@@ -12,7 +12,7 @@
1212

1313
#include "Arduino.h"
1414

15-
#define ACS712_LIB_VERSION (F("0.3.1"))
15+
#define ACS712_LIB_VERSION (F("0.3.2"))
1616

1717

1818
// ACS712_FF_SINUS == 1.0/sqrt(2) == 0.5 * sqrt(2)
@@ -111,7 +111,7 @@ class ACS712
111111
float _formFactor; // peak2peak -> RMS
112112
float _mVperAmpere;
113113
float _mAPerStep;
114-
uint16_t _midPoint;
114+
int _midPoint;
115115
uint8_t _noisemV;
116116
float _microsAdjust = 1.0; // 0.9986
117117
bool _suppresNoise = false;

CHANGELOG.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Change Log AD520X
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
9+
## [0.3.2] - 2022-11-18
10+
- fix #26 revert data type \_midPoint to int
11+
- Add CHANGELOG.md
12+
13+
## [0.3.1 2022-09-xx
14+
- add float mVNoiseLevel(frequency, cycles)
15+
- add void suppressNoise(bool flag) - experimental suppression by averaging two samples.
16+
- update readme.md
17+
- improve midPoint functions
18+
- add resetMidPoint()
19+
- add RP2040 pico in build-ci
20+
21+
## [0.3.0] - 2022-09-01
22+
- return midPoint value in MP functions.
23+
- float return type for mA() functions
24+
- add float mA_peak2peak(freq, cycles)
25+
- add debug getMinimum(), getmaximum();
26+
- update Readme.md
27+
28+
----
29+
30+
## [0.2.8] - 2022-08-19 prepare for 0.3.0
31+
- Fix #21 FormFactor
32+
- add mA_AC_sampling() as method to determine
33+
- current when FormFactor is unknown.
34+
- added float _AmperePerStep cached value.
35+
- added getAmperePerStep();
36+
- moved several functions to .cpp
37+
- improve documentation
38+
39+
## [0.2.7] - 2022-08-10
40+
- change mVperAmp to float
41+
- add ACS712_FF_SAWTOOTH
42+
- update readme.md + unit test + minor edits
43+
44+
## [0.2.6] - 2021-12-09
45+
- update readme.md
46+
- update license
47+
48+
## [0.2.5] - 2021-12-03
49+
- add timeout to detectFrequency()
50+
51+
## [0.2.4] - 2021-11-22
52+
- add experimental detectFrequency()
53+
54+
## [0.2.3] - 2021-10-15
55+
- change frequencies to float, for optimal tuning.
56+
- update build CI
57+
- update readme.md
58+
59+
## [0.2.2] - 2021-06-23
60+
- support for more frequencies
61+
62+
## [0.2.1] - 2020-12-06
63+
- Add Arduino-CI + unit test
64+
- update readme
65+
- refactor
66+
67+
## [0.2.0] - 2020-08-02
68+
- Add autoMidPoint()
69+
70+
----
71+
72+
## [0.1.4] - 2020-08-02
73+
- Allow for faster processors
74+
75+
## [0.1.3] - 2020-05-27
76+
- fix library.json
77+
78+
## [0.1.2] - 2020-03-21
79+
- automatic form factor test
80+
81+
## [0.1.1] - 2020-03-18
82+
- first release version
83+
84+
## [0.1.0] - 2020-03-17
85+
- initial version
86+
87+
88+

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ The midpoint is the (raw) zero-reference for all current measurements.
147147
It is defined in steps of the ADC and is typical around half the **maxADC** value defined
148148
in the constructor. So for a 10 bit ADC a number between 500..525 is most likely.
149149

150-
Since 0.3.0 all midpoint functions return actual midPoint.
150+
Since 0.3.0 all midpoint functions return the actual midPoint.
151151

152152
- **uint16_t setMidPoint(uint16_t midPoint)** sets midpoint for the ADC conversion.
153-
Parameter must be between 0 and maxADC, otherwise midpoint is not changed.
153+
Parameter must be between 0 and maxADC/2, otherwise midpoint is not changed.
154154
- **uint16_t autoMidPoint(float frequency = 50, uint16_t cycles = 1)** Auto midPoint,
155155
assuming zero DC current or any AC current.
156156
The function takes the average of many measurements during one or more full cycles.
@@ -176,7 +176,7 @@ One can use the two debug functions.
176176
and take the average of these two values. In code:
177177

178178
```cpp
179-
uint16_t midpnt = ACS.setMidPoint((ACS.getMinimum(20) + ACS.getMaximum(20)) / 2);
179+
uint16_t midpoint = ACS.setMidPoint(ACS.getMinimum(20)/2 + ACS.getMaximum(20)/ 2);
180180
```
181181
See - ACS712_20_AC_midPoint_compare.ino
182182

@@ -343,7 +343,7 @@ The examples show the basic working of the functions.
343343
#### Should - 0.3.x
344344

345345
- investigate noise suppression #21 (0.3.1 and later)
346-
- external history file = changelog.md
346+
- add external history file = changelog.md
347347

348348

349349
#### Could

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"type": "git",
2222
"url": "https://github.com/RobTillaart/ACS712.git"
2323
},
24-
"version": "0.3.1",
24+
"version": "0.3.2",
2525
"license": "MIT",
2626
"frameworks": "arduino",
2727
"platforms": "*",

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ACS712
2-
version=0.3.1
2+
version=0.3.2
33
author=Rob Tillaart <rob.tillaart@gmail.com>, Pete Thompson <pete.thompson@yahoo.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=ACS712 library for Arduino.

test/unit_test_001.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ unittest(test_midPoint)
123123
amp = ACS.getMidPoint();
124124
assertEqual(1000, amp);
125125

126+
ACS.decMidPoint();
127+
amp = ACS.getMidPoint();
128+
assertEqual(999, amp);
129+
126130
ACS.resetMidPoint();
127131
amp = ACS.getMidPoint();
128132
assertEqual(511, amp);

0 commit comments

Comments
 (0)