"I want to connect my *thing* to the internet"
First question:- Why?
For me, Lucy Rogers, it's mainly for fun. I want my toy dinosaur to nod anytime someone tweets #wakedino. But also sometimes for a purpose - I want to be able to turn some lights on in my home from somewhere else. There are as many reasons as there are ideas and people with them.
I went to Lancaster University to study mechatronics - I wanted to play with robots and with "Spitting Image" type puppets and make cool "Great Egg Race" contraptions.
However, I soon declared "Electronics is black magic" as I got rather low marks in my electronics modules and decided to focus on mechanical engineering instead.
My career path was different than the one I had imagined at 18, and I have managed to ignore the gizzards of all electronic devices for the past decade or two. Ohm's Law (Voltage = Current x Resistance) has stuck in my brain though, and comes out when I am stuck for small talk at parties. (Hint: don't get stuck with me at a party).
But now I have come full circle, and again want to play with robots and with "Spitting Image" type puppets and make cool "Great Egg Race" contraptions. I want to play with "The Internet of Things". I have formed the company Makertorium Ltd. to make gadgets and gizmos and legitimise my playing.
So the next question is:- How?
The Raspberry Pi is a great starting point for connecting "things" to the internet. It is small in size, low cost, has built-in network connectivity, and has an open-source operating system (Linux) which lends itself to hacking. The really useful bit, and something many other small computers do not have, is the "General Purpose Input Output" (GPIO) port. The GPIO port allows the Pi to control a potentially huge variety of "things" by anyone with some basic knowledge of electronics and programming.
So you can just connect the "thing" to a Raspberry Pi and you are connected?
Sadly, no, it's not that simple.
James Macfarlane stopped me frying my Pi when he caught me about to connect a motor directly to the GPIO pins. I asked him to explain why: -
"The GPIO port only works with logic level signals. This means it can only supply 3.3v and a few mA of current from each pin. This is enough to power an LED but to control anything more powerful (like motors) we will need some additional circuitry to boost the current and/or voltage to a higher level.
One way of doing this is an open collector driver. This is an electronic circuit. Using the circuit, the GPIO pin from the Pi only needs to supply a small current to turn on a transistor via its base terminal. The transistor can then switch a much bigger current via its collector terminal. This big current flows back to a separate power supply, not via the Raspberry Pi, so there should be no risk of damaging the Pi (unless you make a mistake!)
The great thing about an open collector driver is we can choose any voltage and current we want for the load so long as its within the rating of the transistor. "
In a previous blog I described how I connected my standard lamp to the internet, so I can tweet it on and off. To do this I needed to control a 12V relay - so I used an Open Collector Driver.
While doing some of the BlackgangPi projects, Andy Stanford-Clark connected a toy dinosaur (http://www.maplin.co.uk/p/connex-action-dinosaur-kit-a28jt) he got for Christmas to a Raspberry Pi. He used an Open Collector Driver plus resistors to drop down the 3.3V from the Pi to 1.5V, but had some teething problems with the start up current of the motors.
Teething problems and setbacks can really dishearten the newbie electronics hacker (aka me).
I bought myself a toy Dino and simplified Andy's circuit back to the Open Collector Driver to use the Pi to switch it on and off. The Dino is still powered by the 1.5V battery.
So Open Collector Drivers seem to be the answer!
Where can I get one?
After realising the Open Collector Driver is so useful for connecting *things* to the internet, I wanted loads of them!
And wouldn't it be good if I could just buy them instead of having to make them from scratch?
Searching around, it seems one Open Collector Driver is not an option on the market. I can buy chips with many of them in, but the chips then still need connecting, and look rater complicated. And spider-like.
Making one involved cutting up veroboard and adding bits of wire etc. Although it's fun the first time, I did not really fancy making a lot by hand.
After realising that "I" would have to be the "they" in "they should make one" I started to investigate how to make a Printed Circuit Board (PCB). With a PCB, I'd then only have to solder the components on.
How do you make a PCB?
I decided to use Cadsoft's Eagle PCB Design Software.
Advantages: Freeware (free and non-commercial use only!)
James Macfarlane uses it and offered to help
Disadvantages: Not particularly intuitive (although there are tutorials).
Freeware version limited to 10x8 cm and two layers.
Costs if you want to use it commercially.
Loading and setting up the software was relatively pain-free. After that, the first step was to open a new project (File, New, Project) and name it NewProject. Then open the NewProject and make a new schematic.
Finding and adding the right parts was the most difficult thing for me.
Here are the parts I used, and which Eagle library they were in and what they were called - and their Farnell Part Numbers:
Here's the circuit:
The diode (1N4001) is required to protect the transistor. If the Load has any kind of inductive coil in it, e.g. it is a relay or motor, when the transistor is turned off there will be a back-EMF produced by the collapsing magnetic field. This can be large enough to damage the transistor. The diode limits this back-EMF and therefore protects the transistor when the transistor is turned off. (For more details on this consult a good electronics book, such as The Art of Electronics.) (Thanks to Mike @TheRealMike for suggesting I comment on this).
Once the schematic was made, I made a board (in Eagle) and re-arranged the bits to get them onto the smallest board I could, without crossing over any tracks. The price of getting the PCB manufactured is dependent on the board size - the smaller, the cheaper. Shaving millimeters off can save pounds.
I only used the bottom side (also known as solder side) for the tracks, which are shown in blue. The PCB manufacturer I used makes two layer boards, so I could have used both sides. I seemed to get lost in this process for many enjoyable hours, wondering if I move this component, can I make the board a little bit smaller etc.
Getting the Makertorium logo on took very many frustrating efforts.
The next step was to make the gerber files. There are numerous instructions on the web for this.
Having heard many Makers praise the PCB prototyping service of Ragworm, I gave them a go. It was a very simple process - I zipped and uploaded the gerber files, said how big the board was and paid. The price for two 20mm x 20mm boards was £4.96 - which probably works out cheaper than the veroboard!
About a fortnight later, my PCB's arrived. This was something I thought only *big companies* could do - not me, from the comfort of my sofa. I admit I did run around in an excited manner.
They were even packaged with love.
Soon it was soldered up and connected to my toy Dino ...
The black wire to the Pi (the board with the raspberry logo) is the power for the Pi. Red and blue jumper cables connect the Pi GPIO pins (Pin 7 and Pin 40 - Ground). A red and black wire goes from the AA battery in its case to the Open Collector Board. Another pair of red and black wires go from the Open Collector board to the Dino. The Pi is connected to the Wifi via an Edimax USB wifi dongle.
As mentioned in a previous blog Andy is helping me learn to use Node-Red - the visual tool for programming and wiring up the Internet of Things.
About 10 months ago, I would have had a brain freeze if asked to do any coding - but I am now quite happy to write a "twitter search and trigger a pin" flow - this took less than five minutes.
Now my Dino nods up and down whenever "#wakedino" is mentioned anywhere on twitter! I also put in a stop command after a five second delay.
After playing with the board for a while, and gleefully tweeting about it, some improvements were suggested (Thanks Chris Robbins (@Grallator) ! )
This version has no fixing holes - it's always useful to secure it to stop wires wobbling out etc.
The current of the Load is limited by the transistor to a maximum of 800mA. So although I could theoretically add something that needed a 24V power supply, if the current it draws is too high it would frazzle the transistor. Calculating the current on the load requires the resistance to be known - sometimes hard to find out - and doing some maths (V=IR) - which is somewhat off putting when you just want to plug and play. However, if I use a Darlington pair (two transistors that act as a single transistor but with a much higher current gain) instead of the single transistor, the current limit should not be a problem.
So - watch this space for Version 2 of the Open Collector Driver - or Thingatron, as it has now been named.
With special thanks to James Macfarlane, Andy Stanford-Clark, Stella and Connor at Ragworm and Chris Robbins (@Grallator)