Solar Power Diverter

by Chemiker in Circuits > Arduino

611 Views, 0 Favorites, 0 Comments

Solar Power Diverter

example_2.png
example_1.png
thumbnail_Screenshot 2023-12-31 at 13.36.19.jpg
image.png

If you live in a country with a big difference between the electricity buy and sell rate, then your solar setup will benefit from either battery storage or a solar diverter. The latter device "diverts" power from the solar panels to a dump load, usually a hot water cylinder (HWC). It works by rapidly switching the HWC element with a microprocessor controlled TRIAC. This works because the smart meter has “play”. A good analogy might be to imagine the steering wheel of your car which also has a bit of play, in that tiny movements left and right don’t change the direction of the car. In a smart meter, the play is usually 1/1000 of a kWh, or 3600 Joules. Any electricity movement in that 3600 Joule window will not be registered as electricity passing between your house and the grid by your smart meter. 

This project is an adaptation of Robin Emley’s open source project -> https://www.mk2pvrouter.co.uk/ where I’ve added an optional ESP32 (Devkit V4) to handle energy monitoring and internet connectivity. The arduino nano diverter, and esp32 work independently, so the system also works without the ESP32. Everything's plug and play, so you can simply upload the code and get it going. I recommend reading Emley's’ above-mentioned website, and https://openenergymonitor.org/ which both have detailed theory and background regarding the operation, ie. digital filtering, integer math etc.

Whereas Emley’s project uses a naked Atmel 320P chip with an add-on display, I’ve opted to use the inexpensive Arduino Nano which has a lot of components already integrated. I've replaced the display with two LEDs, one lights when the TRIAC's off, and the other when it's on. This very simple setup shows you exactly what's going on with the TRIAC and the flashing frequency is diagnostic for problem-solving. You may be wondering what's the point of the ESP32? Who cares about monitoring? The problem is that having a solar diverter guarantees that the HWC can only switch on if the current clamp detects power leaving your house, and therefore on dark cloudy days your HWC will simply go cold because the TRIAC can't switch on.   You can fix that by adding a manual boost button, but manual intervention is undesirable. What you really want is some additional functionality to sum up the daily HWC on-time and automatically calculate the required boost to be delivered at night. Maybe even boosting at a specific time when electricity is less expensive. You might even want to change the boost duration depending on whether it’s winter or summer. That's exactly what the ESP32 does here. As a bonus, you can even remotely monitor and control the device through the Blynk app (screenshot shown above).

This unit has been tried and tested by me for several years. It's saved me piles of money. It works great and requires almost no maintenance (1 TRIAC replaced in 3 years). I'd like to share it with the community.

Supplies

div2_triac.jpg
IMG_2318_jack.jpg

Obtain the parts listed in list.csv. Difficult to find parts might include the transformer (VTX-120-4206-406) which I got from ebay, and the 3.5 mm stereo plug (used for plugging in the current transformer). I found the stereo plug from Tayda Electronics (see photo). I managed to get 5 inexpensive TRIACS on ebay. I'm not sure if they were fakes, but one TRIAC lasted for about three years, constantly switching a 2kW load about quarter of a million times a day! Download the solar_project.zip file from here and upload to JLCPCB. (If you live in New Zealand, then send me a message and I can post you a board.)

Downloads

Assembly

old_apple_power_supply.jpg
power_diverter_V12_schem.png
image.png

Solder in all the components. You'll want to check the voltages. Solder a 240 V line to the transformer, making sure to place some electrical tape to the back so you don't electrocute yourself. Measure the voltages at the test points on the board and make sure that you've got 5 V, and 2.5 V. Next check the voltage divider that's used to send signals from the Nano to the ESP32. Write a quick Arduino sketch to switch digital pin 8 HIGH on the Nano. Plug the Nano into the board and make sure that digital pin 32 on the ESP32 is above 2.80 V (threshold for on) and below 3.30 V (max voltage for the ESP32). If all that checks out then your board is ready to go. You may be wondering why the transformer isn't also used to power the ESP32. The reason is because the ESP32 can draw high instantaneous currents which can mess up the mains voltage measurements. Therefore, you'll need to find an old iPhone usb power plug or similar to power the esp32.

Test Voltage and Current Sampling

Using the Arduino IDE, install this slightly modified version of Robin Emley's Raw Samples code to the Nano. Leave out the ESP32 out for now. Carefully remove the outer sheath from an old power cord of a heater or toaster, etc. so that you can clamp the YHDC SCT-013 current transformer (CT) to the phase or neutral lines. Ideally, you want a reasonable load 1kW or more so that you can at least see the current waveform move off the baseline. What you're looking for here is to see the voltage and current waveforms - that's all. Notice that depending on which orientation you clamp the current transformer to the wire affects whether the current is in phase, or out of phase with the voltage. That doesn't matter here, but later when you clamp the CT around the main line near your switchboard it needs to be in phase with voltage for the device to work correctly (you just do that later by trial and error).

Install Firmware

Using the Arduino IDE, install the firmware for both the Nano ( here), and for the ESP32 ( here). Don't forget to modify the #defines in the esp32 code to add your own WIFI and Blynk credentials. Look carefully at the include directives as you may need to install some libraries to get the code working. You'll of course need to sign up for a free Blynk account and configure each of the virtual pins from the Blynk website. Since making this project, Blynk has halved the number of datastreams per template from 10 to 5, so you'll have to pick and choose which datastreams are important to you. Below is a list of the Virtual Pins and their corresponding names that are incorporated into the above c++ code.


Virtual Pin, Name, data_type, min_value, max_value


V1, Hour Trigger, integer, 0 23

V2, Minute Trigger, integer, 0, 59

V3, Summer Required On Time, Double, 0, 10

V4, Winter Required On Time, Double, 0, 10

V5, Uptime, String,

V6, Boost Active, bool, 0, 1

V8 Last Successful Autocharge, string

V10, Triac Cycles Today, integer, 0, 1e8

V11 Calculated Boost, double, 0, 12


Make the TRIAC Controller

blahmoc3041.png

Using some good stiff perfboard to mount the TRIAC and MOC3041. Follow the circuit diagram for the MOC3041 and make sure you use thick copper wire to carry the 10 Amp 240 V hotline switched load. You don't really need the snubber, but if you decide to include it, then make sure you use a decent capacitor like a 1600-650V WIMA FKP 1.

Assemble the Device Into a Housing

diverter_mounted.jpg
div1.jpg
switch.jpg

Mount everything to an earthed metal faceplate. Ideally you'll end up with faceplate requiring only five attachments from the other side, ie. the 3.5 mm plug from the CT, phase, neutral and earth lines, and lastly the neutral line side of the HWC (see above photo). The TRIAC gets quite hot, so make sure you have a good heat sink attached directly to the TRIAC. An old CPU cooling block without the fan works well. I used a tiny bit of thermal paste too. Add a 3-position double pole double throw (DPDT) 15A switch. Make sure it's got 6 legs so that you can bypass the TRIAC to the HWC, and switch off the diverter at the same time. The whole device needs to be mounted right next to your switchboard because the CT needs to be clamped around a main line feeding your house. Make sure that you have either or both a dedicated HWC switch on the fusebox, or the HWC fuse in series with, and placed before your live phase line that feeds into your diverter box. You want to be able to switch the power off to work safely on it. I should probably say something about getting an electrician to do this. You don't want to void your house insurance or burn your house down. After you install everything and switch it on and fingers crossed, you'll see rapid alternating flashing of the LEDs (assuming 1; it's a sunny day, 2; the HWC thermostat hasn't switched off the element already, and 3; the CT is oriented correctly). That's it. Super simple! Enjoy paying less to the electricity company.

Home Assistant Integration

Home Assistant is a much more versatile alternative to Blynk, albeit with a steeper learning curve. This project can be easily incorporated into Home Assistant using ESPHome. All that’s required is to flash a custom module into ESPHome. One of the many benefits of Home Assistant is advanced scheduling through Node Red. I've written an ESPHome module which exposes a manual override switch for the hot water cylinder, and a sensor which records, for each minute of the day, the number of seconds that the hot water cylinder was on for, (it's essentially a pulse width accumulator with units of seconds of TRIAC-on-time per minute). Here are the steps to getting it working: (STEP1) Get Home Assistant up and running with ESPHome installed. Get an esp32 microcontroller and add it as a new device in ESPHome builder. Make sure it's all working, and you can do over-the-air updates. (STEP 2) In ESPHome, edit the yaml config file of your working example. Add the following directives and do a clean build:

external_components:
- source:
type: git
url: https://github.com/patrick-kiwi/esphome
ref: pulse-width-accumulate
components: [ pulse_width_accumulate ]

sensor:
- platform: pulse_width_accumulate
pin: GPIO32
name: "Hot Water Cylinder On-Time"

switch:
- platform: gpio
pin: GPIO33
name: "Hot Water Cylinder Switch"

Make sure that the ESP32 framework type is esp-idf and not arduino. It should look like this:

esp32:
board: esp32dev
framework:
type: esp-idf

Correct indentation is essential in yaml, and for some reason it's not showing correctly above. Download the yaml config file below to see the correct indentation of the above code. After compiling and uploading your amended code, you'll now have two new entities in Home Assistant, a hot water cylinder switch, and a sensor which records the seconds-per-minute TRIAC on-time. You'll need to obtain your new "entity ID"s, so in Home Assistant go to the main right-hand panel settings->devices&services->ESPHome and after a few more clicks find the cogwheel settings for each individual entity. The entity IDs can be copied from inside that settings menu. (Step 3) Download the Node Red Flow (here) and install it. You'll need to put your own hot-water-cylinder-on-time "entity ID" into the Get-history node, by double-clicking on it. That flow gives you the total hourly on-time in the last 24 hours. You now have the basic setup for making your own more advanced customised flows in Node Red.

One other thing that I would do is make a custom sensor in Home Assistant to keep track of daily hot water cylinder on-time. To do that you need to edit the yaml global config file and add an "integration sensor" (see below - the indentation is messed up, but I've linked a correctly formatted file in the link above).

# Integration Sensor measures total hot water cylinder on-time
- platform: integration
source: sensor.esphome_web_59b8c4l_test_hwc_on_time
unique_id: "sensor.integral_of_per_minute"
unit_time: min
method: left
name: "Test Sensor"

Under Source, you need to paste the entity ID of your - platform: pulse_width_accumulate sensor. Integrating one time with respect to another time seems a bit weird, but this gives you the precise time, in seconds, that the hot water cylinder has been on for. Once you've got those two sensors working, it would be nice if the on-time was in hours and had the correct units. Paste the template sensor into the config file. Under the value_template field, replace sensor.XXXX with the entity ID of your own Integration sensor.

# Template Sensor to display Test Sensor values in hours
- platform: template
sensors:
test_sensor_hours:
unique_id: "sensor.integrated_convert_to_h"
friendly_name: "Test Sensor (Hours)"
unit_of_measurement: "hours"
value_template: "{{ states('sensor.XXXX') | float / 3600 | round(2) }}"

I would probably add a pulse count sensor too for use in NodeRed. That should be enough to get you started.