first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,70 @@
//
// FILE: ACS712_20_AC.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);
// use simulation
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();
stop = micros();
Serial.print("mA: ");
Serial.print(mA);
Serial.print(". Form factor: ");
Serial.print(ACS.getFormFactor());
Serial.print(" time: ");
Serial.println(stop - start);
delay(5000);
}
// simulated 50 Hz signal
uint16_t signal(uint8_t p)
{
return 512 + 400 * sin((micros() % 1000000) * (TWO_PI * 50 / 1e6));
}
// -- END OF FILE --

View File

@@ -0,0 +1,58 @@
//
// FILE: ACS712_20_AC_DEMO.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to set the mVperAmpere and Form FActor.
// 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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
}
void loop()
{
int mA = ACS.mA_AC();
Serial.println(mA);
if (Serial.available() > 0)
{
char c = Serial.read();
if (c == '*') ACS.setmVperAmp(ACS.getmVperAmp() + 1);
if (c == '/') ACS.setmVperAmp(ACS.getmVperAmp() - 1);
Serial.print("mVperAmp:\t");
Serial.println(ACS.getmVperAmp());
if (c == '+') ACS.setFormFactor(ACS.getFormFactor() * 1.05);
if (c == '-') ACS.setFormFactor(ACS.getFormFactor() / 1.05);
Serial.print("formFactor:\t");
Serial.println(ACS.getFormFactor());
}
delay(1000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,52 @@
//
// FILE: ACS712_20_AC_SAMPLING_DEMO.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to sample AC current and set mVPerAmpere
// 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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
}
void loop()
{
float mA = ACS.mA_AC_sampling();
Serial.println(mA, 1);
while (Serial.available() > 0)
{
char c = Serial.read();
if (c == '*') ACS.setmVperAmp(ACS.getmVperAmp() + 1);
if (c == '/') ACS.setmVperAmp(ACS.getmVperAmp() - 1);
Serial.print("mVperAmp:\t");
Serial.println(ACS.getmVperAmp());
}
delay(250);
}
// -- END OF FILE --

View File

@@ -0,0 +1,59 @@
//
// FILE: ACS712_20_AC_average.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo AC measurement with point to point + averaging
// 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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
Serial.print("MidPoint: ");
Serial.println(ACS.getMidPoint());
Serial.print("Noise mV: ");
Serial.println(ACS.getNoisemV());
}
void loop()
{
float average = 0;
uint32_t start = millis();
for (int i = 0; i < 100; i++)
{
// select sppropriate function
// average += ACS.mA_AC_sampling();
average += ACS.mA_AC();
}
float mA = average / 100.0;
uint32_t duration = millis() - start;
Serial.print("Time: ");
Serial.print(duration);
Serial.print(" mA: ");
Serial.println(mA);
delay(1000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,50 @@
//
// FILE: ACS712_20_AC_compare.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo AC measurement comparison
// 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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
Serial.print("MidPoint: ");
Serial.print(ACS.getMidPoint());
Serial.print(". Noise mV: ");
Serial.println(ACS.getNoisemV());
}
void loop()
{
int m1 = ACS.mA_AC();
float m2 = ACS.mA_AC_sampling();
Serial.print("mA:\t");
Serial.print(m1);
Serial.print("\t\t");
Serial.println(m2);
delay(1000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,64 @@
//
// FILE: ACS712_20_AC_low_pass.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo AC measurement with point to point with low pass filter
// 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, 185);
// ESP 32 example (might requires resistors to step down the logic voltage)
// ACS712 ACS(25, 3.3, 4095, 185);
float value = 0;
float weight = 0.2;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
Serial.print("MidPoint: ");
Serial.println(ACS.getMidPoint());
Serial.print("Noise mV: ");
Serial.println(ACS.getNoisemV());
Serial.print("Amp/Step: ");
Serial.println(ACS.getAmperePerStep(), 4);
value = ACS.mA_AC(); // get good initial value
}
void loop()
{
// select sppropriate function
float mA = ACS.mA_AC_sampling();
// float mA = ACS.mA_AC();
value += weight * (mA - value); // low pass filtering
Serial.print("weight: ");
Serial.print(weight);
Serial.print(" value: ");
Serial.print(value, 0);
Serial.print(" mA: ");
Serial.print(mA);
Serial.println();
delay(1000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,54 @@
//
// FILE: ACS712_20_AC_midPoint_compare.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to compare different midPoint methods.
// 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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.println();
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
Serial.println("Compare different midPoint methods.\n");
Serial.print("Default: \t");
Serial.println(ACS.getMidPoint());
Serial.print("AutoMP: \t");
Serial.println(ACS.autoMidPoint());
uint16_t average = (ACS.getMinimum(20) + ACS.getMaximum(20)) / 2;
ACS.setMidPoint(average);
Serial.print("Average: \t");
Serial.println(average);
Serial.println();
Serial.println("\ndone...");
}
void loop()
{
delay(1000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,74 @@
//
// FILE: ACS712_20_AC_midPoint_performance.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to compare different midPoint methods.
// 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;
float mp = 0;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.println();
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
Serial.println("Compare different midPoint methods.\n");
start = micros();
mp = ACS.getMidPoint();
stop = micros();
Serial.print("Default: \t");
Serial.print(mp);
Serial.print("\t");
Serial.println(stop - start);
delay(10);
start = micros();
mp = ACS.autoMidPoint();
stop = micros();
Serial.print("AutoMP: \t");
Serial.print(mp);
Serial.print("\t");
Serial.println(stop - start);
delay(10);
start = micros();
uint16_t average = (ACS.getMinimum(20) + ACS.getMaximum(20)) / 2;
stop = micros();
Serial.print("Average: \t");
Serial.print(average);
Serial.print("\t");
Serial.println(stop - start);
delay(10);
Serial.println("\ndone...");
}
void loop()
{
delay(1000);
}
// -- END OF FILE --

View File

@@ -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 --

View File

@@ -0,0 +1,73 @@
//
// FILE: ACS712_20_AC_suppress_noise.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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
Serial.print("MidPoint: ");
Serial.print(ACS.getMidPoint());
Serial.print(". Noise mV: ");
Serial.println(ACS.getNoisemV());
}
void loop()
{
ACS.suppressNoise(false);
Serial.println("\nFALSE");
for (int i = 0; i < 4; i++)
{
Serial.print(ACS.mA_AC());
Serial.print("\t");
Serial.print(ACS.mA_AC_sampling());
Serial.print("\t");
Serial.print(ACS.mA_peak2peak());
Serial.print("\t");
Serial.println();
delay(1000);
}
ACS.suppressNoise(true);
Serial.println("\nTRUE");
for (int i = 0; i < 4; i++)
{
Serial.print(ACS.mA_AC());
Serial.print("\t");
Serial.print(ACS.mA_AC_sampling());
Serial.print("\t");
Serial.print(ACS.mA_peak2peak());
Serial.print("\t");
Serial.println();
delay(1000);
}
}
// -- END OF FILE --

View File

@@ -0,0 +1,44 @@
//
// FILE: ACS712_20_DC.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to measure mA DC
// URL: https://github.com/RobTillaart/ACS712
// use with Arduino Serial Plotter
#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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
// Serial.println(ACS.getMidPoint());
}
void loop()
{
int mA = ACS.mA_DC();
Serial.println(mA);
delay(100);
}
// -- END OF FILE --

View File

@@ -0,0 +1,56 @@
//
// FILE: ACS712_20_DC_DEMO.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to set the midPoint and the mVperAmpere.
// 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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
}
void loop()
{
int mA = ACS.mA_DC();
Serial.println(mA);
if (Serial.available() > 0)
{
char c = Serial.read();
if (c == '+') ACS.incMidPoint();
if (c == '-') ACS.decMidPoint();
if (c == '0') ACS.setMidPoint(512);
Serial.println(ACS.getMidPoint());
if (c == '*') ACS.setmVperAmp(ACS.getmVperAmp() * 1.05);
if (c == '/') ACS.setmVperAmp(ACS.getmVperAmp() / 1.05);
Serial.println(ACS.getmVperAmp());
}
delay(1000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,57 @@
//
// FILE: ACS712_20_DC_external_ADC.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to measure mA DC with external ADC
// URL: https://github.com/RobTillaart/ACS712
// use with Arduino Serial Plotter
#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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.setADC(testADC, 10, 1023);
// ACS.autoMidPoint();
// Serial.println(ACS.getMidPoint());
}
void loop()
{
int mA = ACS.mA_DC();
Serial.println(mA);
delay(1000);
}
// wrapper needed for external analogRead()
// as casting behavior is undefined between different function signatures.
uint16_t testADC(uint8_t p)
{
// simulation
return 600 + p;
// replace with an external ADC call.
// return ADS.readADC(p);
// return analogRead(p + 1); // use another internal ADC
}
// -- END OF FILE --

View File

@@ -0,0 +1,55 @@
//
// FILE: ACS712_20_determine_form_factor.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo to determine form factor
// 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);
int i = 0;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
ACS.autoMidPoint();
}
void loop()
{
Serial.println("\n\nAC \tP2P \tCREST");
for (int i = 0; i < 10; i++)
{
float mA_1 = ACS.mA_AC_sampling();
float mA_2 = ACS.mA_peak2peak();
float crest = 2.0 * mA_1 / mA_2;
Serial.print(mA_1);
Serial.print("\t");
Serial.print(mA_2);
Serial.print("\t");
Serial.println(crest, 4);
delay(100);
}
delay(100);
}
// -- END OF FILE --

View File

@@ -0,0 +1,47 @@
//
// FILE: ACS712_20_mV_noise_level.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);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
}
void loop()
{
ACS.autoMidPoint();
Serial.println();
Serial.print(" MidPoint: ");
Serial.println(ACS.getMidPoint());
Serial.print("Config Noise mV: ");
Serial.println(ACS.getNoisemV());
Serial.print("Detect Noise mV: ");
Serial.println(ACS.mVNoiseLevel(), 1);
delay(2000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,90 @@
//
// FILE: ACS712_20_AC.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo AC measurement with point to point
// URL: https://github.com/RobTillaart/ACS712
///////////////////////////////////////////////////////////////////
//
// DISPLAY
//
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define BL_OFF 0
#define BL_ON 1
#define DISPLAY_ADDR 0x3F // check
LiquidCrystal_I2C lcd(DISPLAY_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
uint32_t lastDisplay = 0;
#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, 185);
// ESP 32 example (might requires resistors to step down the logic voltage)
// ACS712 ACS(25, 3.3, 4095, 185);
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
lcd.begin(16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(BL_ON);
lcd.clear();
ACS.autoMidPoint();
}
void loop()
{
lcd.clear();
float f1 = ACS.mA_AC();
float f2 = ACS.mA_AC_sampling();
lcd.setCursor(0, 0);
lcd.print("mA: ");
lcd.print(f1, 1);
lcd.setCursor(10, 0);
lcd.print(f2, 1);
lcd.setCursor(0, 1);
lcd.print("FF: ");
lcd.print(f1 / f2);
delay(1000);
}
// -- END OF FILE --

View File

@@ -0,0 +1,54 @@
//
// FILE: ACS712_autoMidPointDC.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo detect DC midpoint.
// 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;
uint16_t midPoint = 0;
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
Serial.print("ACS712_LIB_VERSION: ");
Serial.println(ACS712_LIB_VERSION);
delay(10);
// might be different 1 cycle or 100.
start = micros();
midPoint = ACS.autoMidPointDC(1);
stop = micros();
Serial.println("ACS.autoMidPointDC()");
Serial.print("us:\t");
Serial.println(stop - start);
Serial.print("MP 1:\t");
Serial.println(midPoint);
midPoint = ACS.autoMidPointDC(100);
Serial.print("MP 100:\t");
Serial.println(midPoint);
}
void loop()
{
}
// -- END OF FILE --

View File

@@ -0,0 +1,49 @@
//
// FILE: ACS712_detectFrequency.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo detect frequency + timing indication.
// 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);
}
void loop()
{
// e.g. detect 50 or 60 Hz sinus signal
// blocks on bad signals.
start = micros();
float frequency = ACS.detectFrequency(45);
stop = micros();
Serial.print(stop - start);
Serial.print("\t");
Serial.println(frequency, 1);
delay(100);
}
// -- END OF FILE --

View File

@@ -0,0 +1,55 @@
// FILE: RMS_by_sampling.ino
// AUTHOR: Rob Tillaart
// DATE: 2022-08-15
// PURPOSE: demo RMS by sampling for AC current
// URL: https://github.com/RobTillaart/ACS712
//
// stand alone sketch to be used with a ACS712
#include "Arduino.h"
// #include "printHelpers.h"
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(__FILE__);
}
void loop()
{
// 50 Hz, Arduino UNO, 20A ACS
float current = mA_AC_sampling(50, 5000.0 / 1023, 100, A0);
Serial.println(current);
delay(1000);
}
float mA_AC_sampling(float frequency, float mVperStep, float mVperAmpere, uint8_t analogPin)
{
uint16_t midpoint = 511;
uint32_t period = round(1000000UL / frequency);
uint16_t samples = 0;
float sumSquared = 0;
uint32_t start = micros();
while (micros() - start < period)
{
samples++;
float current = analogRead(analogPin) - midpoint;
sumSquared += (current * current);
}
// Serial.print(scieng(sumSquared, 1, 3));
// Serial.print("\t");
// Serial.print(samples);
// Serial.print("\t");
float AmperePerStep = mVperStep / mVperAmpere;
float RMS = sqrt(sumSquared / samples) * AmperePerStep;
return RMS * 1000.0; // mA
}
// -- END OF FILE --

View File

@@ -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 --