Monitor voltage, current, power, and energy with your Arduino UNO R4
What is this library?This library makes it easy to use the PZEM-004T V4.0 energy monitor with your Arduino. The PZEM-004T is a device that measures electrical parameters like voltage, current, power, and energy consumption. With this library, you can read all these values with just a few lines of code.
The library works with popular Arduino boards including Arduino UNO R3, UNO R4, Nano, and Mega. It handles all the complex communication with the sensor, so you can focus on your project.
What can you measure?The PZEM-004T sensor can measure:
- Voltage - (80-260V AC)
- Current - (0-100A)
- Power - (0-23kW)
- Energy consumption - (0-9999.99 kWh)
- Frequency - (45-65Hz)
- Power factor - (0.00-1.00)
Fast data reading: You can read all measurements at once with just one command. This is 6 times faster than reading each value separately.
Simple setup: Just a few wires to connect, and you're ready to go. The library takes care of all the technical details.
Clear examples: The library comes with four example programs that show you how to use different features.
Error handling: If something goes wrong, the library tells you what the problem is, making troubleshooting easier.
How do you wire it?Connecting the PZEM-004T to your Arduino is simple. You only need four wires:
For Arduino UNO R4:
For Arduino UNO R3 or Nano:
Here's a simple example that reads and displays all measurements. This works on Arduino UNO R4:
#include <PZEM004Tv40_R4.h>
// Create PZEM object
PZEM004Tv40_R4 pzem(&Serial1);
void setup() {
Serial.begin(115200);
pzem.begin
}
void loop() {
// Read all values at once
if (pzem.readAll()) {
Serial.print("Voltage: ");
Serial.print(pzem.getVoltage(), 1);
Serial.println(" V");
Serial.print("Current: ");
Serial.print(pzem.getCurrent(), 3);
Serial.println(" A");
Serial.print("Power: ");
Serial.print(pzem.getPower(), 1);
Serial.println(" W");
}
delay(1000);
}This code reads all measurements once per second and prints them to the serial monitor. The readAll() function gets all valuePZEM-004T in one go, which is much faster than reading each value separately.
Key featuresEfficient reading: The library can read all six measurements in about 200 milliseconds. If you read each value separately, it takes about 1200 milliseconds. That's 6 times slower!
Energy reset: You can reset the energy counter to zero with a simple command. This is useful when you want to measure energy consumption over a specific period, like a day or a week.
Multiple devices: You can connect multiple PZEM sensors to the same Arduino by giving each one a unique address. The library makes it easy to change the address of a sensor.
Error detection: If the sensor doesn't respond or sends corrupted data, the library tells you what went wrong. This helps you fix problems quickly.
Example programs includedThe library comes with four ready-to-use examples:
1. Basic Reading: Shows how to read and display all measurements.
2. Individual Parameters: Compares reading all values at once versus reading them one by one.
3. Reset Energy: Demonstrates how to automatically reset the energy counter at set intervals, with cost calculation.
4. Change Address: Shows how to change the sensor's address for multi-device setups.
Common problems and solutionsNo data received: Check your wiring, make sure the sensor has power, and verify you're using the correct address.
Timeout errors: Add a longer delay between readings. The sensor needs time to process each request.
Wrong readings: Make sure the current transformer (CT) clamp is installed correctly around the wire you want to measure.
Address change fails: Only connect one sensor when changing addresses. Multiple sensors with the same address will cause conflicts.
How to installUsing Arduino IDE:
- Open Arduino IDE
- Go to Sketch → Include Library → Manage Libraries
- Search for "PZEM004Tv40_R4"
- Click Install
Using PlatformIO:
Add this to your platformio.ini file:
lib_deps =
bharanidharanrangaraj/PZEM004Tv40_R4Manual Installation (alternative method):
- Download the library as a ZIP file from GitHub
- Open Arduino IDE
- Go to Sketch → Include Library → Add.ZIP Library
- Select the downloaded ZIP file
- Restart Arduino IDE
Here are some things you can build with this library:
- Home energy monitor to track electricity usage
- Solar panel power monitor
- Smart power strip with individual outlet monitoring
- Battery charge/discharge monitor
- Power quality analyzer
- Cost calculator for appliance usage
If you have questions or find bugs, visit the GitHub repository at PZEM004Tv40_R4. You can report issues or ask for help there.
The library is free and open source under the MIT License.












Comments