Solar Heating Control

Keywords: RaspberryPI, Home Automation, Temperature Sensor, AD converter, Relay, Solar, Heating, Ventilation, Control with Web Interface,Data Logging

Abstract: To create a solar heating control this article describes how to connect temperature sensor via SPI to a RaspberryPI and how to use GPIO-pins to control relays. In the second part there are detailed control algorithms for a combined solar and combustion heating with web interface to visualize the data logging and as control pannel.

Raspberry Pi Logo

1Set up the Raspberry Pi
1.1Set up the Web Server
1.2Access the Server via Internet
2Electrical Connection of Sensores and Actuators
2.1AD Converters connected via SPI
2.2Relay on GPIO via Opto-Coupler
2.3PWM Control of Ventilation via i2c
2.4OneWire Temperature Sensor
2.5Ultra-sonic Distrance Measurement
2.6Serial RS232 connection
2.7Stepper Motor Control
2.8Get Weather Forecast
3Heating Control
4Ventilation Control (Paul Lüftung, Thermos 200)
5 z-wave

no warranty for any of the provided information


When my heating control broke down I decided to create my own to

As heating setup I have one central hot water storage tank (550 l Water including 180 l tap water), which provides heat for warm water consumption and heating of my house.
There are two possibilities to heat up this tank which have to be controlled:

  1. Solar heating
    One solar collector (6.93 m2, (TK-Solar) with pump and sensors for hot loading temperature and return flow temperature .
  2. Wood pellet burner
    The Calimax "Twist 80/20" oven has its own control for lighting the fire and control the circulation pump.
    To activate it there are contacts which have to be closed without potentitial by my control. To do this a second relay is needed.
So far I have 4 sensors (Ttank, Tcoll, Tload, Tret) and 2 actuators (Asolar, Aoven) to handle... So let"s set up the RaspberryPI, design the electronis, implement the communication and design the control.

1 Set up the RaspberryPi

You have to buy the RaspberryPI, a 5 V power supply and a (16 Gb) SD card - in total 46 EUR in my case.

First you have to format the SD card. My first attempt was to use gparted under openSUSE 12.x and I figured out that there is a problem with this version and than I used SDFormatter and it worked well.

Afterwards download the to the SD card, unzip it being root and remove the zip-file.

To connect the RaspberryPI have a look into the quick-start-guide-v2_1.pdf - just for inital start it is helpful to have a USB keyboard and mouse and a monitor with HDMI connection to hand.

Choose Raspbian as operating system to be installed and have a cup of coffee...

After installation, to have the bash shell by default do
chsh -s /bin/bash

And since I like the GNU emacs for programming:
sudo apt-get update
sudo apt-get install emacs

And to set the time zone:
sudo cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime

And meld is a good tool to compare two file or directory versions
sudo apt-get install meld

Update the system

If the Raspberry Pi is connected to the internet, you should apply an update regularly:
sudo apt-get update
sudo apt-get dist-upgrade

1.1 Set up the Web Server

To set up the web server, a good introduction I found is:

Find out the IP address of the device by
and the IP address of the router by

If you are working on some computer and would like to detect other available machines, try nmap

To use ssh to log in from some remote machine edit
nano /ets/hosts
(on all your computers) and add computer names for the fixed IP numbers:   raspberry   miraculix

From now on you can login to your RaspberryPI from any other computer in your network by
ssh -X pi@raspberry
(If you should work on a Windows PC you might have to use Putty.)

If your raspberry will be connected to the internet, better change the ssh port from 22 to anything else by
nano /etc/ssh/sshd_config
and restart the service
/etc/init.d/ssh restart
First check that this channel is not used by any other service, check
more /etc/services
Before you close your actual shell, use some other and try to log in!
ssh pi@raspberry -p 1234

The essential sequence to set up the web server is:
A more comprehensive renaming of user pi is there.

cd /etc/network
sudo nano interfaces
#replace the line "iface eth0 inet dhcp" with:

iface eth0 inet static
nano /etc/resolv.conf #check: nameserver
sudo reboot
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install php5-mysql
#for data logging

To test the server start a browser (dillo) on your RaspberryPI and enter http://localhost/.
To modify this start page edit /var/www/index.html or replace this by your index.php.

1.2 Access the Server via Internet

To make your RaspberryPI visible from the internet you have to open ports in your router (Speedport W723V) and direct them to the RaspberryPI. To have the web server available open port 80 and for the ssh connection port 22.
To test this you have first to figure out your (actual) IP - there are different tools in the internet like
From any external IP you may type your IP like into the URL of the browser or test
ssh -X pi@

My first Heating Control PCB on a strip board

Qty Title Price
1 MCP 3208-CI/P, 12-bit A/D Converter mit SPI, 8-Kanal / DIL-16 2.48 €
2 FIN 36.11, 5V Finder-Subminiaturrelais, 1x UM, 250V 10A, 5V 1.85 €
1 UP 832EP, Lochrasterplatine, Epoxyd, 160x100mm 3.39 €
1 WSL 26G, Wannenstecker, 26-polig, gerade 0.13 €
2 Transistor BC337-40 0.14 €
2 Diode 1N4148 0.06 €
3 Widerstand Metall 1W 1.2 kOhm 0.48 €
4 Widerstand Metall 1W 2.2 kOhm 0.64 €
2 Pfostenverbinder RM 2.54 26 Pol 1.00 €
1 IC-Fassung 16 Pol 0.20 €
1 Leiterplattenklemme 10 Pol 1.65 €
total 12.02 €

My RaspberryPi break out board. Additionally I added connectors for 5 V and Ground which are connected via two pins each to the RaspberryPi. This way they could be used as alternative voltage supply to the whole system and for control measurements I have these potentials easily accessible. The board is already prepaired to have a second AD converter to handle more signals.

For a more advaned approach, have a look to Electrical Connection of Sensores and Actuators.