Coding for Arduino UNO (Measuring current using acs712 20A current sensor)
int mVperAmp = 100; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
void setup(){
pinMode(A0, INPUT);
Serial.begin(9600);
delay(10);
Serial.println(F("Init...."));
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; // sq root
AmpsRMS = (VRMS * 1000)/mVperAmp;
float Wattage = (220*AmpsRMS)-20; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.
Serial.print(AmpsRMS);
Serial.println(" Amps RMS ");
Serial.print(Wattage);
Serial.println(" Watt ");
Serial.println("");
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the maximum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 5)/1024.0;
return result;
}
Coding for nodemcu (Controlling relay)
#define BLYNK_PRINT Serial
#include<ESP8266WiFi.h>
#include<BlynkSimpleEsp8266.h>
char auth[] = "IoeQOU61afoVkwUrFHoQKZ4hHmWSYMz6";
char ssid[] = "SIANTAN@unifi";
char pass[] = "JRT25623";
BLYNK_WRITE(V0)
{
if (param.asInt()) {
digitalWrite(16, HIGH);
} else {
digitalWrite(16, LOW);
}
}
BLYNK_WRITE(V1) {
if (param.asInt()) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
}
BLYNK_WRITE(V2) {
if (param.asInt()) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
}
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
delay(10);
Serial.println(F("Init...."));
Blynk.begin(auth,ssid,pass);{
}
pinMode(16,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}
void loop()
{
Blynk.run();
}
Combine both coding
#define BLYNK_PRINT Serial
#include<ESP8266WiFi.h>
#include<BlynkSimpleEsp8266.h>
char auth[]="IoeQOU61afoVkwUrFHoQKZ4hHmWSYMz6";
char ssid[]="SIANTAN@unifi";
char pass[]="JRT25623";
const int sensorIn = A0;
int mVperAmp = 100; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
BLYNK_WRITE(V0)
{
if (param.asInt()) {
digitalWrite(16, LOW);
} else {
digitalWrite(16, HIGH);
}
}
BLYNK_WRITE(V1){
if (param.asInt()) {
digitalWrite(5, LOW);
} else {
digitalWrite(5, HIGH);
}
}
BLYNK_WRITE(V2){
if (param.asInt()) {
digitalWrite(4, LOW);
} else {
digitalWrite(4, HIGH);
}
}
void setup()
{
{
pinMode(A0, INPUT);
Serial.begin(9600);
delay(10);
Serial.println(F("Init...."));
}
Serial.begin(9600);
Blynk.begin(auth,ssid,pass);
pinMode(16,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; // sq root
AmpsRMS = (VRMS * 1000)/mVperAmp;
float Wattage = (220*AmpsRMS)-20; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.
Serial.print(AmpsRMS);
Serial.println(" Amps RMS ");
Serial.print(Wattage);
Serial.println(" Watt ");
Serial.println("");
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the maximum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 5)/1024.0;
return result;
Blynk.run();
}
After combined both codings, I can get the reading of measured current from the current sensor(acs712 20A) but can not control the relay throught blynk. Anyone can help to notice my mistake on combining both of the code.Thank you