Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's an example code for interfacing a ZMPT101B AC Voltage Sensor with an Arduino. This code will allow you to measure the AC voltage up to 250V.
- This code initializes the ZMPT101B sensor on analog pin A0 and takes readings of the AC voltage every second. It also includes a calibration function that you can use to calibrate the sensor to get accurate voltage readings.
- Please note that this code assumes that your Arduino board has a 5V reference voltage, and the ZMPT101B sensor has a sensitivity of 200 mV per V. Be sure to check your specific board and sensor specifications before using this code. Additionally, working with high voltage can be dangerous; take all necessary precautions and safety measures while working with AC voltage.
- */
- #include <Arduino.h>
- // Pin definitions
- const int sensorPin = A0; // Analog input pin connected to the ZMPT101B output
- const float VOLTAGE_REF = 5.0; // Reference voltage of the Arduino (5V for most boards, but check your specific board)
- const float SENSOR_SENSITIVITY = 200.0; // Sensitivity of the ZMPT101B module (200 mV per V)
- // Calibration variables
- const int calibrationSamples = 1000; // Number of samples for calibration
- const float calibrationVoltage = 110.0; // Voltage of your AC source for calibration (e.g., 110V or 220V)
- void setup() {
- Serial.begin(9600);
- pinMode(sensorPin, INPUT);
- // Calibrate the sensor
- float calibration = calibrate();
- Serial.print("Calibration value: ");
- Serial.println(calibration);
- }
- void loop() {
- float voltage = readVoltage();
- Serial.print("AC Voltage: ");
- Serial.print(voltage);
- Serial.println(" V");
- delay(1000);
- }
- float calibrate() {
- float sum = 0;
- for (int i = 0; i < calibrationSamples; i++) {
- sum += analogRead(sensorPin);
- delay(1);
- }
- float avg = sum / calibrationSamples;
- return (avg * VOLTAGE_REF) / (1023 * SENSOR_SENSITIVITY);
- }
- float readVoltage() {
- const int numSamples = 100;
- float maxVal = 0;
- for (int i = 0; i < numSamples; i++) {
- float sensorValue = (analogRead(sensorPin) * VOLTAGE_REF) / 1023;
- if (sensorValue > maxVal) {
- maxVal = sensorValue;
- }
- delay(1);
- }
- return maxVal / calibrate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement