Skip to content

Commit a19ec7f

Browse files
committed
update libraries A-D
1 parent 2b97b8d commit a19ec7f

File tree

228 files changed

+6098
-1674
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

228 files changed

+6098
-1674
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# Arduino libraries
22

3+
**NOTE** these libraries will all get their own repo, so this repository
4+
does not have the latest version of all libs anymore. That said I will update
5+
this bulk repo on request or if time permit.
6+
7+
38
### Introduction
49
This repository contains several Arduino libraries I have written to be used in applications.
510
Most of them include example code how the libraries can be used.
611
Furthermore this repository contains a few stand alone applications.
712

8-
**NOTE** these libraries will all get their own repo, so this repository
9-
does not have the latest version of all libs anymore. That said I will update
10-
this bulk repo on request or if time permit.
1113

1214
### Questions
1315
For questions about the usage of the libraries, please post a question on the Arduino

libraries/ACS712/ACS712.cpp

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
//
22
// FILE: ACS712.cpp
3-
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.1
5-
// DATE: 2020-03-17
3+
// AUTHOR: Rob Tillaart, Pete Thompson
4+
// VERSION: 0.2.0
5+
// DATE: 2020-08-02
66
// PURPOSE: ACS712 library - current measurement
77
//
88
// HISTORY:
99
// 0.1.0 2020-03-17 initial version
1010
// 0.1.1 2020-03-18 first release version
11-
//
12-
// Released to the public domain
13-
//
11+
// 0.1.2 2020-03-21 automatic formfactor test
12+
// 0.1.3 2020-05-27 fix library.json
13+
// 0.1.4 2020-08-02 Allow for faster processors
14+
// 0.2.0 2020-08-02 Add autoMidPoint
1415

1516
#include "ACS712.h"
1617

@@ -22,26 +23,43 @@ ACS712::ACS712(uint8_t analogPin, float volts, uint16_t maxADC, uint8_t mVperA)
2223
_mVperAmpere = mVperA;
2324
_formFactor = 0.70710678119; // 0.5 * sqrt(2); TODO: should be smaller in practice 0.5 ?
2425
_midPoint = maxADC / 2;
26+
_noisemV = 21; // Noise is 21mV according to datasheet
2527
}
2628

27-
int ACS712::mA_AC()
29+
int ACS712::mA_AC(uint8_t freq)
2830
{
2931
uint32_t start = micros();
32+
uint16_t period = ((freq == 60) ? 16670 : 20000);
33+
uint16_t samples = 0;
34+
uint16_t zeros = 0;
3035
int _min, _max;
3136
_min = _max = analogRead(_pin);
32-
while (micros() - start < 20000) // UNO ~180 samples...
37+
while (micros() - start < period) // UNO ~180 samples...
3338
{
39+
samples++;
3440
int val = analogRead(_pin);
3541
if (val < _min) _min = val;
3642
if (val > _max) _max = val;
43+
if (abs(val - _midPoint) <= (_noisemV/_mVpstep)) zeros++;
3744
}
3845
int p2p = (_max - _min);
39-
// TODO determine _formFactor
40-
// two counters, a threshold and math is needed
41-
// D = (1.0 - #samples close to zero ) / #samples
42-
// FF = D * 0.5 * sqrt(2);
43-
44-
// math could be partially precalculated
46+
47+
// automatic determine _formFactor / crest factor
48+
float D = 0;
49+
float FF = 0;
50+
if (zeros > samples * 0.025)
51+
{
52+
D = 1.0 - (1.0 * zeros) / samples; // % SAMPLES NONE ZERO
53+
FF = sqrt(D) * 0.5 * sqrt(2); // ASSUME NON ZERO PART ~ SINUS
54+
}
55+
else // # zeros is small => D --> 1 --> sqrt(D) --> 1
56+
{
57+
FF = 0.5 * sqrt(2);
58+
}
59+
_formFactor = FF;
60+
61+
// math could be partially precalculated: C = 1000.0 * 0.5 * _mVpstep / _mVperAmpere;
62+
// rounding?
4563
return 1000.0 * 0.5 * p2p * _mVpstep * _formFactor / _mVperAmpere;
4664
}
4765

@@ -53,4 +71,22 @@ int ACS712::mA_DC()
5371
return 1000.0 * steps * _mVpstep / _mVperAmpere;
5472
}
5573

74+
// configure by sampling for 2 cycles of AC
75+
// Also works for DC as long as no current flowing
76+
void ACS712::autoMidPoint(uint8_t freq)
77+
{
78+
uint32_t start = micros();
79+
uint16_t twoPeriods = ((freq == 60) ? 16670 : 20000) * 2;
80+
uint32_t total = 0;
81+
uint32_t samples = 0;
82+
while (micros() - start < twoPeriods) {
83+
uint16_t reading = analogRead(_pin);
84+
total += reading;
85+
samples ++;
86+
// Delaying ensures we won't overflow since we'll perform a maximum of 40,000 reads
87+
delayMicroseconds(1);
88+
}
89+
_midPoint = total / samples;
90+
}
91+
5692
// END OF FILE

libraries/ACS712/ACS712.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
//
33
// FILE: ACS712.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.1
6-
// DATE: 2020-03-17
5+
// VERSION: 0.2.0
6+
// DATE: 2020-08-02
77
// PURPOSE: ACS712 library - current measurement
88
//
9-
// Released to the public domain
10-
//
119
// Tested with a RobotDyn ACS712 20A breakout + UNO.
1210
//
1311

1412
#include "Arduino.h"
1513

16-
#define ACS712_LIB_VERSION "0.1.1"
14+
#define ACS712_LIB_VERSION "0.1.3"
1715

1816

1917
class ACS712
@@ -31,8 +29,8 @@ class ACS712
3129
ACS712(uint8_t analogPin, float volts = 5.0, uint16_t maxADC = 1023, uint8_t mVperA = 100);
3230

3331
// returns mA
34-
// blocks 20-21 ms to sample a whole 50 or 60 Hz period. // TODO HZ as param ??
35-
int mA_AC();
32+
// blocks 20-21 ms to sample a whole 50 or 60 Hz period.
33+
int mA_AC(uint8_t freq = 50);
3634

3735
// returns mA
3836
// blocks < 1 ms
@@ -43,11 +41,17 @@ class ACS712
4341
inline uint16_t getMidPoint() { return _midPoint; };
4442
inline void incMidPoint() { _midPoint++; };
4543
inline void decMidPoint() { _midPoint--; };
44+
// Auto midPoint, assuming zero DC current or any AC current
45+
void autoMidPoint(uint8_t freq = 50);
4646

4747
// also known as crest factor; affects AC only
4848
inline void setFormFactor(float ff) { _formFactor = ff; };
4949
inline float getFormFactor() { return _formFactor; };
5050

51+
// noise
52+
inline void setNoisemV(uint8_t noisemV) { _noisemV = noisemV; };
53+
inline uint8_t getNoisemV() { return _noisemV; };
54+
5155
// AC and DC
5256
inline void setmVperAmp(uint8_t mva) { _mVperAmpere = mva; };
5357
inline uint8_t getmVperAmp() { return _mVperAmpere; };
@@ -58,6 +62,7 @@ class ACS712
5862
float _formFactor; // P2P -> RMS
5963
uint8_t _mVperAmpere;
6064
uint16_t _midPoint;
65+
uint8_t _noisemV;
6166
};
6267

6368
// END OF FILE

libraries/ACS712/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Rob Tillaart
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

libraries/ACS712/examples/ACS712_20_AC/ACS712_20_AC.ino

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,28 @@
1515
// ACS712 30A uses 66 mV per A
1616

1717
ACS712 ACS(A0, 5.0, 1023, 100);
18+
// ESP 32 example (requires resistors to step down the logic voltage)
19+
//ACS712 ACS(25, 5.0, 4095, 185);
1820

1921
void setup()
2022
{
2123
Serial.begin(115200);
2224
Serial.println(__FILE__);
25+
26+
ACS.autoMidPoint();
27+
Serial.print("MidPoint: ");
28+
Serial.print(ACS.getMidPoint());
29+
Serial.print(". Noise mV: ");
30+
Serial.println(ACS.getNoisemV());
2331
}
2432

2533
void loop()
2634
{
2735
int mA = ACS.mA_AC();
28-
Serial.println(mA);
36+
Serial.print("mA: ");
37+
Serial.print(mA);
38+
Serial.print(". Form factor: ");
39+
Serial.println(ACS.getFormFactor());
2940
}
3041

3142
// END OF FILE

libraries/ACS712/examples/ACS712_20_AC_DEMO/ACS712_20_AC_DEMO.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
// ACS712 30A uses 66 mV per A
1616

1717
ACS712 ACS(A0, 5.0, 1023, 100);
18+
// ESP 32 example (requires resistors to step down the logic voltage)
19+
//ACS712 ACS(25, 5.0, 4095, 185);
1820

1921
void setup()
2022
{
2123
Serial.begin(115200);
2224
Serial.println(__FILE__);
25+
ACS.autoMidPoint();
2326
}
2427

2528
void loop()

libraries/ACS712/examples/ACS712_20_DC/ACS712_20_DC.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
// ACS712 30A uses 66 mV per A
1616

1717
ACS712 ACS(A0, 5.0, 1023, 100);
18+
// ESP 32 example (requires resistors to step down the logic voltage)
19+
//ACS712 ACS(25, 5.0, 4095, 185);
1820

1921
void setup()
2022
{
2123
Serial.begin(115200);
2224
Serial.println(__FILE__);
25+
ACS.autoMidPoint();
2326
}
2427

2528
void loop()

libraries/ACS712/examples/ACS712_20_DC_DEMO/ACS712_20_DC_DEMO.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
// ACS712 30A uses 66 mV per A
1616

1717
ACS712 ACS(A0, 5.0, 1023, 100);
18+
// ESP 32 example (requires resistors to step down the logic voltage)
19+
//ACS712 ACS(25, 5.0, 4095, 185);
1820

1921
void setup()
2022
{
2123
Serial.begin(115200);
2224
Serial.println(__FILE__);
25+
ACS.autoMidPoint();
2326
}
2427

2528
void loop()

libraries/ACS712/library.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
"name": "Rob Tillaart",
99
"email": "Rob.Tillaart@gmail.com",
1010
"maintainer": true
11+
},
12+
{
13+
"name": "Pete Thompson",
14+
"email": "pete.thompson@yahoo.com",
15+
"maintainer": false
1116
}
17+
1218
],
1319
"repository":
1420
{
1521
"type": "git",
16-
"url": "https://github.com/RobTillaart/Arduino.git"
22+
"url": "https://github.com/RobTillaart/ACS712"
1723
},
18-
"version":"0.1.1",
24+
"version":"0.2.0",
1925
"frameworks": "arduino",
20-
"platforms": "*",
21-
"export": {
22-
"include": "libraries/ACS712"
23-
}
26+
"platforms": "*"
2427
}

libraries/ACS712/library.properties

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name=ACS712
2-
version=0.1.1
3-
author=Rob Tillaart <rob.tillaart@gmail.com>
2+
version=0.2.0
3+
author=Rob Tillaart <rob.tillaart@gmail.com>, Pete Thompson <pete.thompson@yahoo.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
5-
sentence=ACS712 library for Arduino.
5+
sentence=ACS712 library for Arduino.
66
paragraph=Current measurement, tested with RobotDyn ACDC 20A Module.
77
category=Signal Input/Output
8-
url=https://github.com/RobTillaart/Arduino.git
9-
architectures=*
8+
url=https://github.com/RobTillaart/ACS712
9+
architectures=*
10+
includes=ACS712.h
11+
depends=

0 commit comments

Comments
 (0)