Basics of solar power management with arduino

Some thoughts about solar power

Using solar power is a new, booming industry. Solar power is free and available for eternity. The only problem with it is that currently the best solar panels use only around 13% of total incoming solar energy.

Even with the bad rate, more and more people decide to use solar panels because they are clean. The easiest setup of a solar panel as the panel itself and a converter that converts the incoming voltage to AC current.

Why there is a need for controlling the power

Using the simple setup is trivial and most of the people like to implement this one, instead of creating some sort of controlled setup where the power flow is controlled by a logic.

Solar panels are not cheap, especially if someone wants to also get the costly additional equipment that gives control over the energy flow. Also in some places the energy supplying company does not pay for the extra power generated by the solar panels.

There is a way, however, that can prevent extra energy flowing back into the system. The main question is: what to do with the extra power?

What to do with the extra power?

Store in batteries

Usually the energy generated by the solar panels is stored in batteries. During the day the batteries get charged and they supply the needed energy during the night. The problem with batteries is that they have a relatively short lifecycle and there is a lot of energy loss during the charge and discharge of the batteries.

Convert it to heat

A good use for the extra energy is converting it to heat. There are cases when a house is heated with wood. Wood costs a lot, so solar energy converted to heat reduces the need for heat from wood. This could be done by rerouting the energy through a resistor that heats up the water in the boiler.

Roll out the solars from it

In some setups the solar panels rotate during the day to always face the sun. When there is an energy surplus, they can be rotated out from the sun just enough to maintain a steady energy income.

The idea of converting energy to heat

The idea of converting the extra energy to heat is pretty simple. We use a device to check the flow of the current at the entry point from the energy supplier. If the current flows backwards, meaning back into the grid, we start to re-rout energy to a resistor that heats up the water.

A device checks the amount of power flowing backwards and manages the re-routed energy accordingly to maintain a steady control over the consumption. If the water reaches a given temperature, we re-rout the energy surplus to the batteries, charging them.

Technical approach to conversion

In this example we use a cheap approach to controlling our energy flow. We use the famous Arduino device for everything we need to do. With the first device, we measure the current and the voltage to decide how much we need to re-rout.

The second and middle device is just receiving data, converts it and sends out the converted data to different outputs.

The third device is only responsible to manage the energy consumption of the resistor.

Arduino as cheap and reliable instrument

Arduino is a beautiful project. It is a simple microcontroller with analog and digital in- and outputs. Since it is a simple microcontroller, there are few chances of error when using such a device. When controlling energy flows, it is probably the best choice.

To get an arduino or any device needed for such a project, just check out your favourite online store, like

Creating the structure

The structure of the project is also pretty simple. The measurement of the current or voltage can be done easily, for example with open energy monitor.

When we are able to measure the current flow and can decide which way the current flows, we just have to create a code that calculates the rate of re-routed energy. Calculating this value depends on a lot of things. In this example, we map the size of the resistor to a byte (0 – 255). Also, we roughly estimate the maximum output of the solars. Using these values we calculate our variable. For example in case of a 2k resistor:

int ourVal = round((255/2000)*Irms);

Of course, we add some conditions to prevent our value to go below 0 and above 255.

When we are done with the calculations, we simple create an encrypted string with all the data we want to send, and send it over Serial, or I2C communication. Using other than these devices will make the reading slow.

Also, using Serial is better for long-range communication with RS485 shields. It allows you to send data over 1200 meters.

In the middle, also an arduino is used. This arduino sends the control value to the third device over PWM. This is a sure method over short ranges and consumes very little processor steps. Also there is a serial LCD to monitor the current values and a LAN shield to build a small web server that displays the measurements over internet.

The third arduino uses the 50Hz zero interrupt and the control value to decide how much the resistor consumes. It uses the upgoing arc of the sinus wave over a triac to make the resistor consume as much as needed.

Writing the code

I will show you just examples, because this idea can be put into real life in many ways.

We use emonlib for monitoring:

#include "EmonLib.h"

For measurement we use:

float powerFactor     = emon1.powerFactor;      //extract Power Factor into Variable

float Irms            = emon1.Irms;      //extract Irms into Variable

float supplyVoltage   = emon1.Vrms;             //extract Vrms into Variable

float realPower       = emon1.realPower;        //extract Real Power into variable

We calculate the control value:

step = round(sqrt(realPower * realPower) / 10);

if (step < 1) leptek = 1;

if (step > 255) step = 255;


if ( realPower < calib )


if (controlVal < 255)


controlVal += step;




controlVal = 255;






if (controlVal > 0)


controlVal -= step;




controlVal = 0;





After we have the control value, we just send it through our favorite protocol.

On the second device we can use the following library to display our data on an LCD: serLCD

To have a web server, we can use the built-in library for the LAN shield:

#include <Ethernet.h>


To send the PWM value, we just have to use:

analogWrite(pwmPin, controlVal);

The third device uses an interrupt function to manage the consumption on the resistor. In the setup we attach the interrupt:

attachInterrupt(0, light, FALLING);

After this, we write the function:

void light() {


if (dim <= 1) {

//Turn TRIAC completely OFF if dim is 0

digitalWrite(AC_pin, LOW);



if (dim >= 255) { //Turn TRIAC completely ON if dim is 255

digitalWrite(AC_pin, HIGH);



if (dim > 0 && dim < 255) {

//Dimming part, if dim is not 0 and not 255

delayMicroseconds(34 * (255 - dim));

digitalWrite(AC_pin, HIGH);


digitalWrite(AC_pin, LOW);



We just need to use a global variable that is accessible for this function. We read this value on an analog port and we are done.

Final thoughts

Since I am a programmer, I cannot go into details about the electronics. The code itself is simple, the logic behind it, self-explanatory. The idea itself has the main focus, implementing it has many, many ways.