DIY-IOT: How to construct a device, that turns on/off other devices via the Internet – Part 3/4: The hardware

IMPORTANT: READ THIS, before deciding to build something like this device. This device is connected directly to the mains power, which is potentially lethal to mess around with! I am not kidding you – the best scenario when subjected to electrical shock is that it hurts like h*ll, worst case scenario is that you DIE. You should not mess with mains power without a minimum of knowledge.

I have been shopping ingredients since last article, so now we’re going to bake a cake. The recipe is simple:

  1. Grab a pot and add the ingredients
  2. Add solder
  3. Add heat, and stir
  4. Add a few drops of blood, a minor burn, and a liberal amount of obscene words (here you really must allow the imagination to flourish!)
  5. Stir again
  6. Realize that it is not working as intended
  7. Stare angrily at the setup for a couple of hours (you might want to take a small detour to the last part of step#4), give up for now and go to bed.
  8. Fix the totally obvious error that you should have discovered after 5 nanoseconds of pondering.
  9. Try again
  10. If it still doesn’t work, go to step#7, and remember last part of step#4: consider increasing level of obscenity
  11. If it works, raise your hands, and step carefully away from the table

Just kidding! The hardware for our gizmo is quite simple. The only electronics to be designed and constructed are the driver circuits for the two relays. I decide to position a couple of LEDs above the power outlets to indicate if they’re on or off. The driver circuits for the relays will drive these LEDs.

Why not connect the relays directly to the GPIO ports of the Raspberry Pi? The reason is, that a microprocessor is only able to supply a tiny amount of current – in the case of the Raspberry Pi, the GPIOs are able to source approximately 15-16 mA, and a typical relay require significantly higher current, in particular initially when the relay is just activated, and the coil is energized.

A relay consist of a coil wound around a ferrite core, that create an electro magnet once a current is passed through the coil. The magnet attract a switch that closes the mains circuit.

Relay2

Source: Wikipedia

The relay I picked from the drawer require 6 V/60 mA, so there is definitely the need to boost the output from the Raspberry Pi GPIO pins. First of all the voltage level on the GPIO is 3.3V, and secondly we’re unable to source that amount of current. A small test reveal that the relay is perfectly able to operate on 5V instead of 6V, which is fortunate, since 5 V is readily available on the Raspberry Pi.

Luckily a driver circuit is really simple to design, using a standard NPB Bipolar Junction Transistor. A BC547 is perfectly suitable, since it can handle 0.5W without any cooling, and the relay will at the most draw 0.36W. We’ll use the transistor as a switch, and therefore we deliberately drive the transistor into saturation.

In short a transistor is a component with three terminals: Collector, Base and Emitter.

2016-01-22 10.17.02

The clever thing about a transistor is, that a small current through base-emitter I_b can control a larger current through collector-emitter, I_c. The relation between I_b and I_c is called the gain, and for a BC547 it’s around 100. Therefore, if we need I_c to be 60 mA, we must pass at least 0.6 mA through I_b, and to make sure, let’s say 1.5 mA.

The base resistor R_b determine the current flowing through the base in accordance with Ohms law, U=R \cdot I.

Output from the GPIO pin is 3.3V and the voltage drop across the base-emitter of a BC547 is aroubnd 900 mV, and hence the voltage across R_b will be 3.3-0.9=2.4V.

Thus we calculate the value of R_b as R_b = \frac{2.4V}{1.5mA} = 1.6 k\Omega. To allow for component tolerances, I select R_b = 1 k\Omega.

Let’s have a look at the schematics of the circuit, which will be connected to the Raspberry Pi. As you can see, the schematic consist of two identical circuits, one for each relay to be controlled.

driver_schematic_1

The base resistor, R2 is already described, but there are a few more things to explain, namely LED1/R1 and D1.

LED1 is the light emitting diode used to indicate if the power outlet is on, and R1 limits the current flowing through LED1 to approximately 18 mA.

The purpose of D1 is to protect the transistor from the reverse polarity transient that occur from the relay. Electrically speaking a relay is a coil, and one of the basic properties of a coil is, that a magnetic field is created when a current is flowing through the coil. Once the current is cut off the magnetic field start breaking down causing the voltage across the coil to dramatically rise. The voltage will be in reverse polarity, and can be more than 100 times the original voltage. This phenomenon is utilized in the ignition coil in a car to create the spark across the terminals of the sparkplugs. In our case the spark could be inside the transistor, which is something to avoid! The diode is reverse biased to the power supply, and is therefore inert in the circuit durming normal operation. Once a reverse polarity voltage occur across the diode it conducts, and allow the EMF from the coil to dissipate quietly. In this configuration the diode is typically called a flyback, clamp or suppressor diode.

Now the hardware is described. If we were to make a commercial product the next step would be to design the layout of the PCB, but since this is a one-off project with very simple hardware I just implement the circuits on a piece of veroboard.

On my shopping spree I stumbled over a box for only $4 – a bargain! – and next step is to shoehorn all the stuff into this box, that admittedly is a teenyweeny bit too small. But $4! I had to have it! I take special precaution to isolate and fixate all wires and connections relating to the mains power in order to minimize the risk of electrical shock, short circuits and fire.

First thing is to decide where to put the electrical outlets on the front plate. I choose a location near the top of the box to maximize the available room inside the box for the electronic parts:

bore_huller

Now we can start fiddling with all the parts to see how to best fit them inside the box. Primary, sizeable, components are:
– The Raspberry Pi itself
– The PCB with relay drivers
– The two relays
– A power supply for the Raspberry Pi

Let try to make an overview of the combined system to plan how to connect all the subsystems:

system_diagram

The power supply for the Raspberry Pi is a standard USB power supply usede for e.g. smartphones and tablets. The one I found in the drawer is fairly small, and it is possible to solder the mains wires directly to the PSU (saves space).

After a fair amount of fiddling I were nearing the conclusion, that I had bought a too small box, but then I got stubborn: Come hell or high water, it MUST fit! The problem was that the Raspberry Pi was too long to fit crosswise, but only because the micro USB plug from the power supply was too long. A solution could be to try and find a connector with a 90 angle, but it was late and buying stuff is not in the spirit of a true DIY’er. So I grabbed a pair of pliers, and cut off the damn connector, then soldered the wire directly to the Raspberry Pi. There are a number of suitable places to do this – I chose to solder the wires across C6 (the large capacitor next to the USB connector.

indhold_af_kasse

Now all the bits and pieces are mounted and connected. As said earlier, the most important thing is to take special care to ensure that no wires or connections carrying mains power are exposed to the touch.

Notice: I have mounted an Ethernet connector on the side of the box. This is necessary to setup the wifi, unless you want to take the box apart before installation.

This concludes the part on creating the hardware. I hope you enjoyed reading. I survived the process without getting electrocuted and only with minor burns and a slightly sore throat from all the swearing.

But does it work? Does the box look okay from outside, and what idiot forgot that he was holding the front part of the box, when drilling holes for the Raspberry Pi mount?

Read the next episode, where the software is written and the completed device demonstrated.

1 thought on “DIY-IOT: How to construct a device, that turns on/off other devices via the Internet – Part 3/4: The hardware”

Leave a Reply

Your email address will not be published. Required fields are marked *