I tried it with an Arduino Nano v3. With a delay of 100 ms between serial plot and sensor read it showed a nice sine wave and I was able to set the potentiometer to get a wave that is not cut off.
I tried to replicate with ESP32, same program, only analogRead(33) instead of (A0). Kauderwelsch, values with spikes sometimes reacting on mains connections and disconnections but not correlating.
I read in the espressif forums to connect a 0.1 uF capacitor to eliminate noise on the ADC.
Maybe the ESP32 is not really good in that, I tried with ESP8266:
Readings with an ESP-201 (ESP8266 ESP-201 module - first impressions | smarpl.com) every 100ms:
and after I applied a voltage divider to lower the 3v3 from the module to 1v max for the ESP8266 to read I got a nice clear sine wave, red arrow where I unplugged:
I continued reading many posts here, in the espressif forum and reddit and I would not say I found directly: do not use the ESP32 for analog readings but very often I found something similar to “for the real readings I use ADS1x15 or Arduino”. And the ESP8266 seems to be quite useful for that, too.
I continued, build up a testsetup with the ESP32 and the ZMPT101B only but with the 0.1 µF ceramic capacitor
and with the sketch changed to 99ms delay between Serial.print and proper adaption to 3V3 and 12-bit
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
delay(600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(33);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (3.3 / 4095.0);
// print out the value you read:
Serial.println(voltage);
delay(99);
}
I out of a sudden got a nice sine wave alternating around ~1.68V. I had to play around a bit with the delay, between 97ms and 99ms it looks good, shorter or longer does not print a nice curve and above 100 it gets super squeezed.
So I went back to ESPHome and with this:
- platform: adc
pin: GPIO33
name: "Mains Voltage"
id: mains_voltage
unit_of_measurement: "V"
update_interval: 5s
attenuation: 11db
I get 1/2 Vcc of the ESP32.
[21:59:12][D][sensor:125]: 'Mains Voltage': Sending state 1.69400 V with 2 decimals of accuracy
Constantly between 1.677 - 1.695 V, not jumping up and down or other weird things as seen before. As soon as I plug it in max values between 0.6 and 2.7 V pop up, so it’s time to calculate the RMS over it - this feature request would now come in very handy: AC Voltage with ESP32 and ZMPT101B · Issue #1389 · esphome/feature-requests · GitHub
Did you switch back to the power monitor project already