Raspberry Pi Heater Controller

UPDATE (2013-02-15): I’ve discovered why the DS18B20 was giving out the wrong temperature! Silly mistake.. I had wired the Vdd to 5V instead of 3.3V. In the process I’ve blown GPIO pin 4 on one of my Raspberry Pi’s.. bummer but installed a new Pi and re-wired the Vdd on the DS18B20 to 3.3V, now it’s working fine.

Over the last few evenings I’ve been working on my Raspberry Pi heater controller… The hardware is now finished and I’ve started on the software.  The controller uses a DS18B20 to monitor the temperature and one of the GPIO pins to drive a relay which in turn drives a contactor (the heater is a 1.5kW air blower, that is why the contactor is required). Here is a quick overview of the setup so far.

Circuit diagram:

Heater control schematic (in KiCAD)

Heater control schematic (in KiCAD)

The parts in their box:

Heater control in it's box...!

Heater control in it’s box…!

The 1-wire input through a 3.5mm stereo jack and the relay drive transistor:

Relay drive transistor (under shrink wrap)

Relay drive transistor (under shrink wrap)

The recycled phone charger PSU:

The recycled phone charger switch-mode PSU

The recycled phone charger switch-mode PSU

The relay and contactor combo:

Relay and contactor piggy backed

Relay and contactor piggy backed

The control software is going to be web based and will allow me to set a temperature set point, override the control, and if I get to it, will also have some sort of timer to allow the heater be set to come on and go off at pre-set times.

There is a pile of info out there on 1-wire temperature sensors and connecting them to the RasPi.  One of the better sites I found was: www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/temperature/

Setting the GPIO pin is simple, this script gets run at boot up:

echo "17" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio17/direction
echo "0" > /sys/class/gpio/gpio17/value
chmod 777 /sys/class/gpio/gpio17/value

Now the lighttp web server can have access to the GPIO pin.  Here is a snippet of the PHP that does the business…

$s = $_REQUEST["s"];

if ( $s == 'on' ) {
        $cmd = 'echo "1" > /sys/class/gpio/gpio17/value';
        exec( $cmd );
        }

if ( $s == 'off' ) {
        $cmd = 'echo "0" > /sys/class/gpio/gpio17/value';
        exec( $cmd );
        }

I’m having an interesting problem with my 1-wire readings at the moment. For the first five minutes or so the reading are correct, but after a while the readings jump by 40 degrees! The 1-wire CRC is fine so I don’t think there is a problem with the bus, but I’ll have to take a look with the scope to see if I can get to the bottom of it… very strange!

Interfacing the Carambola (1-Wire and I2C)

Got my Carambola just after New Years day.. Thought it was about time to see what I could do with it!

Wanted to look at the i2c bus and 1-wire to see if I could get the heating control project ported from the BeagleBone over to the Carambola. The module came with a stock OpenWRT install from what I could make out.  Neither I2C, GPIO or 1-Wire worked so followed the Wiki over at 8devices.com, downloaded the source and got building.

I’m on my fifth or sixth recompile/build! But I guess it goes with embedded territory, every module has to be build and a new image installed. At the moment I can’t figure out how to get the repositories for opkt working to install packages on the live system. The system build tools work out of the box and are easy to get to grips with. The only thing that slows one down is finding the correct options in the “make menuconfig”. But once again the Wiki has good instructions. On my current build for this test I have i2c, gpio and 1-wire installed.  Here are the links for the three steps:

1) i2c can be found in the PCF8575 example.
2) GPIO can be found in the Simple GPIO control, but watch out the gpioctl program is no longer available so you have to do everything using the /sys filesystem!
3) 1-Wire stuff can be found in the 8devices forum. (I’ve included the patch, which works fine on the latest kernel, in the resources zip file at the bottom.)

KiCAD time (all the KiCAD files for this are in the resource ZIP file at the end of this post).

Carambola electrical test schematic

Carambola electrical test schematic

Soldering some 0.1inch headers on to the Carambola helps!

Carambola GPIO headers

Time to bread-board the circuit up:

Carambola electrical test setup on the breadboard

Carambola electrical test setup on the breadboard

The upper few rows of the breadboard are for the 1-Wire, the rest is the I2C MCP23017.

So lets see if the MCP23017 is on the i2c bus..

root@OpenWrt:~# i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- 24 -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

Yep, at 0×24, cool.. For this test I’m just going to make all of port A outputs and all of port B inputs (you can do much more with the registers inside the MCP23017, but lets keep it simple!). First thing to do is adjust the MCP’s registers to suit:

root@OpenWrt:~# i2cset -y 0 0x24 0x00 0x00
root@OpenWrt:~# i2cset -y 0 0x24 0x16 0xff

The first line sets port A as an output port, the second enables the internal pull-ups (100K) on port B, which is input by default.

Switching on any of the three LEDs is easy, just write the bit pattern to register 0×09 (GPIOA).. so for the first LED we’d need to do this:

root@OpenWrt:~# i2cset -y 0 0x24 0x09 0x01

For the second:

root@OpenWrt:~# i2cset -y 0 0x24 0x09 0x02

I’m sure you see where this is going…

To read the bit pattern on port B is easy.  We just need to read register 0×19 (GPIOB).

root@OpenWrt:~# i2cget -y 0 0x24 0x19
0xff    <- if switch is open
0xfe    <- if switch on pin GPB0 is closed

The 1-Wire is as easy as always under Linux, just look in the /sys/devices/w1_bus_master1 and you should see a folder for each of the 1-Wire DS18B20′s you have connected to the bus.

All the stuff you should need for this is here: Carambola_test (zip file).  In the zip file you’ll find the KiCAD schematic and library of the Carambola header, MCP23017 and the DS18B20.