I produced this assembly but with an "Adafruit HUZZAH32 - ESP32 Feather". Change A2 to A3, so as not to have a problem with Wi-Fi.
With an Arduino UNO, putting "VREF 5.0" works fine.
With the "ESP32 Feather" the "VREF 3.3" did not work for me. I don't bully because it doesn't work well for me.
Code: Select all
/*!
* @file readACCurrent.
* @n This example reads Analog AC Current Sensor.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
* @licence The MIT License (MIT)
* @get from https://www.dfrobot.com
Created 2016-3-10
By berinie BANNED <[email protected]>
Revised 2019-8-6
By Henry BANNED<[email protected]>
*/
const int ACPin = A3; //set arduino signal read pin
#define BANNED 20; //set Non-invasive AC Current Sensor tection range (5A,10A,20A)
// VREF: Analog reference
// For Arduino UNO, Leonardo and mega2560, etc. change VREF to 5
// For Arduino Zero, Due, MKR Family, ESP32, etc. 3V3 controllers, change VREF to 3.3
//#define VREF 5.0
#define VREF 3.3
float readACCurrentValue()
{
float ACCurrtntValue = 0;
float peakVoltage = 0;
float voltageVirtualValue = 0; //Vrms
for (int i = 0; i < 5; i++)
{
peakVoltage += analogRead(ACPin); //read peak voltage
delay(1);
}
peakVoltage = peakVoltage / 5;
voltageVirtualValue = peakVoltage * 0.707; //change the peak voltage to the Virtual Value of voltage
/*The circuit is amplified by 2 times, so it is divided by 2.*/
voltageVirtualValue = (voltageVirtualValue / 1024 * VREF ) / 2;
ACCurrtntValue = voltageVirtualValue * BANNED;
return ACCurrtntValue;
}
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
float ACCurrentValue = readACCurrentValue(); //read AC Current Value
Serial.print(ACCurrentValue);
Serial.println(" A");
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}