Well, obviously Dave's heart is a Raspberry Pi. In my case it's the model B and not a model B+, because it still uses the nice large SD cards. Micro-SD cards have the disadvantage that you can't possible get them in and out of the Raspberry Pi when you build your Pi into a custom box. The rest of the electronics around Dave are for power supply and input and output.
The hardware I describe here shows you what I have built in to Dave.
You don't necessarily have to copy it as a whole.
You may opt for more or for less relay outputs.
You may omit the 433MHz transmitter and/or receiver.
Also the power supply can be made in at least a dozen other ways.
And do you really need (all) the temperature sensor inputs?
Last but not least, you may not need the separate AVR processor which I have built in to Dave. Or you would want to use a different one, one with more I/O pins perhaps. Or just one which just happens to lay about somewhere.
All these and probably more options are available to you. Perhaps you would want to implement an I2C interface, which I haven't implemented on Dave. It is, as they say, your party.
Dave lives in a box which I had lying around. It was an old sample and it prooved to be a nice fit for everything I wanted to implement around Dave. I admit, it's not the most beautiful box, but I can assure you that Dave is perfectly happy in it.
On top of the box I have placed a 4 output mains socket, one which is customary here in Europe. I have modified this socket so that every one of the 4 outputs can be powered individually. Three of these outputs are switched using one of 3 relays. The fourth output is powered permanently.
All the "dangerous" stuff is placed inside the top shell of the box.
Dave's own power comes from a standard 12V wall wart power supply, which is connected to the permanent powered output socket on top of the box.
I sacrifice one possible switchable output socket for this, but I have plenty more to choose from.
I haven't bothered to add a fuse to the mains circuitry. There isn't one in a normal power socket either, so why bother putting one in here. I expect the wall wart, used to power Dave, will have its own fuse anyway.
The relay board I used is a ready made one from Banggood.
There's no way I could have made this my self for this ridiculously low price.
This board is powered by 5V. And all you have to do to switch a relay is to pull its input low. You don't even have to worry about adding a back-EMF protection diode because the relays are driven through opto-couplers. So basically all you're RPi has to do is turn an LED on.
I have made 2 minor changes to this relay board. I have replaced the input connector, because I like these better than the original header ones. And I have bridged a thick common wire to the center contact of all the relays. That way I only need one power feed.
And here's a little peek inside the bottom halve of the box.
The blue bread board is a pad board, which means that there are no tracks on the bottom side, just pads around each hole.
I use roadrunner prototype wiring to connect everything together, except for the power lines, which have to be a bit thicker.
The Raspberry Pi is mounted upside down on top of a header connector on the base board, which not only supports the Raspberry Pi mechanically, it also handles all the internal connections. The entire board can drop down into the box with not a millimeter to spare. Once down it can slide to the back side, which makes all the connectors stick through the rear panel.
There was not enough room on the rear panel to fit all the connectors, so I've used a 4-way RCA connector block for the four auxiliary relay control outputs.
A strip of LEDs sticks through the front panel. One of those LEDs is simply the power indicator, while the other 7 show the status of all the individual relays.
The connector hanging loose is the one which goes to the relay board in the top halve of the box.
Finally you see an orange and yellow wire sticking out of the right side of the box. These are the transmit and receive antenna's respectively. There simply wasn't enough room inside the box for these 17cm long wires. And this way I'm absolutely sure they don't accidentally touch one of the dangerous parts in the top halve of the box.
Let's start with the most important part, the power supply.
Without it nothing works.
The 12V power comes in through X4, it is then fused and D9 protects the system from reverse polarities.
C1 is an extra filter cap, which is not absolutely necessary as the power converter also has a filter cap built in.
The 12V power supply is then fed into a DC-DC power converter which I've scored on Eebay. But it can just as well be obtained from Banggood, just like the other modules I've used in Dave. I use a 12V input here, because that is the maximum voltage for the 433MHz transmitter module, which will give me the maximum control range. If you don't want or need a 433MHz transmitter you may use just about any input voltage from 8V to 35V.
Just a word of advice. These DC-DC converters are ideal building blocks and cost next to nothing when ordered from China. They don't require any heat sinks and are therefore rather energy efficient. However the output voltage is adjustable. And since we need a 5V output we need to set this output before any of the 5V logic is installed or connected. Otherwise you risk frying these parts. So make sure you set the output to 5V before building it into your project.
Apparently special High Voltage DC-DC converters exist and are sold on the internet.
However they may be fakes and may actually die when you raise the input voltage over 35V, which is the maximum voltage for the standard version.
Julian Ilett shows you the proof in a video.
You shouldn't feed Dave more than 12V anyway, if you have the 433MHz transmitter module fitted like I have.
Probably the second most important part of the entire system is the Raspberry Pi itself.
On the diagram the entire Raspberry Pi is reduced to the single connector SV1.
Power is applied to pins 2 and 4, which is connected to the 5V output of the DC-DC converter.
This 5V is reduced to 3.3V on the RPi board, coming out of pin 1, which can supply up to 50mA.
The only on-board part requiring the 3.3V is the AVR chip, which needs significantly less than 50mA.
Furthermore the 1-wire temperature sensors will be connected to the 3.3V rail, but they draw next to nothing too, allowing you to connect dozens of those without any problems.
The rest of SV1 is dedicated to I/O lines. Some go to the relay switching part, some go to and come from the 433MHz transmitter and receiver, while others are connected to the AVR chip.
I have included an AVR micro controller, the Atmega 8. Well in my case it is the pin compatible 88, but you may use just about any AVR processor you like. Or none at all, if you don't need it.
This AVR processor will be used to control the 433MHz transmitter and receiver. Maybe the Raspberry Pi could do that himself too, but having a dedicated processor for this timing critical stuff is a lot easier. And since these things cost only €1 nowadays, I didn't bother trying to teach the Raspberry Pi to do these real-time tricks.
The SPI bus from the AVR is connected to the SPI bus of the Raspberry Pi. That way the Raspberry Pi can be used to program the AVR processor, without the need for plugging in a programmer or requiring a boot loader. The actual programming can be done with avrdude, a programmer which runs on Linux and supports dozens of programming tools.
Communication between the Raspberry Pi and the AVR can be done over the UART serial interface. You don't need any level converters here, because both devices operate from the same 3.3V power supply.
For the rest the relay control lines from the Raspberry Pi can also be controlled from the AVR. My intention is to let the AVR handle all the I/O driving, under control from the Raspberry Pi. That way the state of the outputs won't be affected by an occasional reboot of the Raspberry Pi. Also restoring to the last known state after power up can be done much easier by the AVR.
X1, X2 and X3 are 3.5mm jack plugs, which can be used to connect DS18B20 thermometers to the system. This part of the circuit is so simple that hardly more can be said about it. A 10Ω series resistor is used to power the thermometer sensors and a pull up resistor of 4k7 pulls the input line high while the input or output are not driven low.
Here are some of those modules again you can buy very cheap from the internet. I've bought mine from Banggood again. You can buy them separately or as in this case as a set.
The transmitter module takes from about 5V to 12V from the power supply.
The higher the voltage, the wider the control range.
You probably don't want to have the range too wide though, otherwise you might start controlling your neighbour's lights :-)
One disadvantage of these modules is that they don't come with a lot of information on how to operate them. But a quick test taught me that the input is high active. This means that we must use a pull down resistor to prevent the transmitter to start radiating when the processor isn't awake yet.
The receiver module runs on 5V and has an output signal which is either 0V or 3.9V. I think your AVR micro controller doesn't mind very much if this voltage is slightly higher than its power supply voltage. Nevertheless I've decided to include a small voltage divider to reduce the maximum voltage to 3.3V.
As antennas I have simply cut two wires at 17.3mm length and used these as transmit and receive antenna. The wires stick out of the side of the box, which I'm not bothered about. If you don't like this you might buy a set of Helical antennas from Banggood. I didn't know they had them, otherwise I might have used those myself. Too late now, I'm not going to change it anymore.
T1, T2 and T3 are the relay switchers.
Not really rocket science if you ask me.
Just a pull down resistor and a series resistor driving the base of the transistors.
The drive signal can come from the Raspberry Pi or from the AVR micro controller, whichever you like.
The output from the transistors go straight to the connector SV2, which connects to the relay board in the top half of the box. Finally there's one LED per output which shows the current output stage on the front panel of the box.
As transistors you can pick just about any universal NPN transistor here, I use the BC547 which is quite common in Europe. With the designated transistors you can switch up to 100mA per channel, which is more than enough for driving just 2 LEDs.
Please note that there are no back-EMF protection diodes placed between the transistor collector and the +5V rail. For normal relays you certainly want such a diode anti parallel to your relay coil. But since the relay board I'm using has opto-couplers on the input, you don't need these diodes here.
T4 to T7 are used as inverters for the 4 auxiliary relay drivers.
These inverters drive T8 to T11 respectively, which are used as high side drivers for an external relay.
Using high side drivers allows me to have a common ground on the auxiliary relay outputs, which is better than having one of the power rails as common output.
For the NPN transistors I've used the BC547 again, while the PNP transistors are BC558s, but again, just about any other universal NPN and PNP types will do just as well.
Again there are no back-EMF protection diodes here. If you want to drive a relay coil directly you'll definitely will need one. However it's best to put that as close to the relay coil as possible.
With the jumper on SV4 you can select a 5V or 12V output, depending on whether you connect pins 1 and 2, or 2 and 3 together, respectively.