-
1PCB Design
The PCB design is really just a motherboard to link the various breakout boards I used to the ESP-32 wroom dev board I used.
The PCB is powered by by the electrical hook-up and comes on everytime the hook-up is connected and powered up. For this I simply used a small 230V - 9V step-down transformer, a bridge rectifier and and LM7805 linear regulator with a larger smoothing capacitor and couple of smaller capacitors working as low pass filters. This is pretty much a simple "classic" power supply design, not sophisticated at all but it works.
The backbone of this project is the PZEM-004T module, known for simplifying energy monitoring tasks. While measuring RMS Voltage and Current poses relatively fewer challenges, accurately determining power consumption necessitates Power Factor measurement. The Power Factor varies based on the load's composition, whether primarily inductive, capacitive, or resistive. A purely resistive load maintains a power factor of 1, but the inclusion of capacitors or inductors causes voltage and current to shift out of phase, resulting in a power factor less than 1. A consumer is charged for "Real Power" which is calculated as below:
Real Power (P) = Volts (V) X Current (Amps) x PF
Despite the complexity of Power Factor measurement, it can be achieved with a microcontroller using zero-cross detectors on current and voltage, assessing the time difference. Opting to streamline my project, I employed the PZEM-004T module, efficiently handling these complexities.
The PZEM-004T module relies on a specialized integrated circuit (IC), alongside a current transformer and shunt resistor, to precisely gauge current flow and voltage levels in the connected circuit. Operating within the standard AC voltage range of 80-260V and capable of measuring currents up to 100A, it accommodates a wide array of electrical systems, making it a versatile choice for various applications.
Because the PCB has mains power coming into it I have made sure there are certain safety measures implemented. These include ensuring there is an air gap slotted into the PCB between the live and neutral incoming supply terminals. I have also removed the ground plane away from the zone on the PCB that has the mains supply. I am by no means a professional PCB designer so I urge you to seek professional advice before building a PCB that involves high voltages.
I've included a Zip file in the files section of this write-up with all the required files in it if you would like to have a PCB made up.
-
2The Code
As I mentioned in the project description, the point of this project was because I wanted the finished device to use. I wasn't planning to to anything technically ambitious so I just put some code together using the Arduino IDE and shamelessly took advantage of the library's available for the individual breakout boards and modules I used.
The code is for an ESP32-based energy monitor with display and Bluetooth Classic capabilities. Here's a break down of how the code functions.
Libraries and Definitions - The code begins with including necessary libraries for various functionalities such as communicating with the PZEM004Tv30 energy monitor, handling real-time clock (RTC), Bluetooth communication, OLED display, and SD card. Additionally, it defines some constants like the reset pin and screen dimensions.
Global Variables - The following Global Variables are then set
- SerialBT: BluetoothSerial object for Bluetooth communication. display: Adafruit_SSD1306 object for OLED display.
- chipSelect: Pin for SD card chip select.
- myFile: File object for writing data to the SD card.
- rtc: RTC_DS1307 object for real-time clock functionalities.
- pzem: PZEM004Tv30 object for interacting with the energy monitor.
Setup () Function
setup(): This function is executed once at the start of the program. It initializes serial communication, Bluetooth, display, RTC, SD card, and attaches an interrupt to the reset pin.Interrupt Function
There's an interrupt function resetEnergy() attached to the reset pin (pin 33). It resets energy values in the PZEM004Tv30 sensor when triggered.Main Loop
The main loop starts by getting voltage, current, power, energy, frequency, and power factor from the energy monitor. Getting the data from the PZEM-004T is very easy using the following code.
// Read the data from the sensor float voltage = pzem.voltage(); float current = pzem.current(); float power = pzem.power(); float energy = pzem.energy(); float frequency = pzem.frequency(); float pf = pzem.pf();
Next we check whether there is valid data from the PZEM-004T using the inan() (Is Not A Number) function. If the data is not valid a message is displayed on the Serial Monitor, this is really for de-bugging.
Provided the data is valid it calls the the following three functions:
- logData() - logs data to the SD card with a timestamp taken from the real time clock.
- displayData() - Displays voltage, current, power, and energy on the OLED screen.
- BTData() - Sends voltage, current, power, and energy over Bluetooth.
The voltage, current, power and energy data from the PZEM-004T is passed to the above three functions.
When the program returns to the main loop it prints the data to the serial console, the data also includes the Power Factor which I have not included in the OLED display function and SD card logging function as I don't believe it is required.
To summarise, the code initializes various components, continuously reads energy data, logs it to an SD card, displays it on an OLED screen, and sends it over Bluetooth classic.
I know the code needs work but a copy of the code is available in the files section of this write-up.
-
3Cell Phone App
I built a cell phone app to complement this project as I feel that we are entering an age when everyone expects their electronics to seamlessly connect to their phone. A cell phone app offers convenient access to real-time electricity data with a user-friendly interface. The ease of access should empower users to manage energy efficiently.
To continue with the pragmatic "just get it built" philosophy of this project I used a tool called MIT App Inventor to build the mobile App. MIT App Inventor is a user-friendly, visual programming environment that enables individuals to create mobile applications for Android devices with little or no app coding experience. It offers a drag-and-drop interface for assembling app components, simplifying the app development process. Here's a simplified method describing the steps I used for building the app.
- Create an Account: Visit MIT App Inventor website https://appinventor.mit.edu/ and sign in or create a new account.
- Start a New Project: Click on "Start new project," give your project a name, and choose a blank template.
- Design the User Interface (UI):
- Drag and drop components from the palette onto the designer canvas (phone screen).
- Add buttons, text boxes, labels, and other UI elements as needed.
- Set Properties: Customize the appearance and behaviour of UI components by adjusting their properties in the Component Tree or Properties pane. This could be the setting and sizing of text fonts.
- Add Functionality with Blocks:
- Click on the "Blocks" button to switch to the Blocks Editor.
- Use event handlers like "When Button Clicked" to add functionality.
- Drag and connect blocks to define the behaviour of the app, such as "if / then / else statements". For example the esp32 in this project broadcasts the data over bluetooth. The data is sent as one long string of characters. In the Arduino code have put the separator "|" between the values of voltage, current, power and energy consumed. The bluetooth app receives the string of data and then converts it into a list by iterating through the list and identifying each item on the list by splitting the long string of data every time it comes across a "|".
- Install the App on you phone:
- Click on the "Build" button to generate the APK file and install it on your device.
- Download the app
- Transfer the app to your phone, I used the SD card.
- Open up the folder on your phone where the App was saved, click on the app and install it.
Once you have installed the app on your phone you will need to click on the bluetooth icon and a list of available devices will show up. Click on the ESP32 device and you should be good to go and your real-time data will appear.
I have included some images of design and blocks of my App together with a short video showing the design and installation process.
-
4Final Assembly and Electrical Safety
I have drawn a simple electrical schematic showing how all the components are connected.
The system works in-line with a standard 16A mobile home electrical hook-up. The supply comes into the DIN rail enclosure via a 20mm IP68 (waterproof) cable gland. The Live, Neutral and Earth are connected to separate distribution terminals. From the Live distribution terminals live connections are made to the PCB, the PZEM-004T and the load connection. The live supply is passed through a CT (Current Transformer) which connects to the PZEM-004T to measure the current. From the Neutral distribution terminals neutral connections are made to the PCB, the PZEM-004T. The load neutral connects directly to the neutral distributions terminals. The load earth connection connects directly to the neutral distribution terminals. As with the incoming supply cable, the outgoing load cable passes through an IP68 20mm nylon gland.
The enclosure was chosen to provide an ingress protection rating of IP68 so that it can withstand the weather conditions if left outside. An IP68 rating signifies a device's high level of protection against dust and water. The "6" denotes complete dust resistance, ensuring no ingress of dust. The "8" signifies water resistance up to a specified depth and duration, typically beyond 1 meter and for more than 30 minutes. Devices with this rating can withstand submersion in water, making them suitable for various environments, including underwater activities and harsh weather conditions.
For safety reasons I decided to fit an RCD (Residual Current Device). An RCD is a safety device designed to protect against electrical shocks by quickly disconnecting the power supply when it detects a leakage of electric current. It works by continuously monitoring the balance of incoming and outgoing electrical currents. If an imbalance, indicating a leak or fault, is detected, the RCD immediately cuts off the electricity supply, preventing potential harm to users or damage to your RV. Although RCD's are a great safety device they still do not protect against stupidity, ignorance or shoddy workmanship. This is why I've put warning stickers on the enclosure to remind persons that this device is working at mains voltage and to disconnect it before opening up the enclosure.
Warning stickers are placed on the enclosure to make sure persons are aware the enclosure contains mains voltage and to disconnect the system before removing any screws.
The video below shows how the project was assembled inside the enclosure.
-
5Getting Some Data
The data from the Energy Monitor is Stored simply as a Text File. Once you have the text file you can change it into whatever format you like and turn it into graphs or whatever easy to read format you want.
-
6Final Comments
The project achieved what I wanted it to do but I would, like all projects, like to improve it. Here's what I would do:
- Sort out the OLED display so it doesn't flicker. Easy to do but it works so I left it out for now.
- Add Wi-Fi connectivity.
- Add security to the Bluetooth connection .
- Spend more time on the electrical and PCB design. I would probably work on making a more sophisticated power supply first.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.