Nov 302012
 
The whole case with Pi inside.

I was looking for a Rev 2 Pi, as I wanted one to see if the revision checker function of the Python Gertboard programs worked properly.

Farnell was quoting up to 3 weeks lead time, RS are out of stock. Frown Nobody else seemed to have it in stock, but over at ModMyPi, they said they would have some in within a few days. Sold to the man in a hurry Razz It turned out that it was with me within 48 hours, which was excellent.

If you buy a Raspberry Pi from ModMyPi you have to buy a case as well (it comes out at around £40 delivered – with a case). But the cases looked interesting and you can mix and match the colours for top and bottom section. So I bought one with a black base and a white top. I know – boring! They offer some lovely bright colours, but I wanted to stick a RasPi.TV label on the top – so white seemed like a good choice to go with the RasPi.TV logo. And black seemed practical for the base.

The whole case with RasPi inside.

Looking at the case, I have to say I think it’s really well thought through and cleverly designed. I like it. Yes

The base makes use of the Rev 2 board’s holes so you can attach the RasPi board to the base with two small screw bolts (provided).

Underneath the base – optional rubber feet are provided too

The top or ‘lid’ has holes so you can see the LEDS. There are also holes for all the ports, and ventilation holes top and bottom (I’ve covered the top ones with my label ROTFL)

Holes for LEDs to shine through

The ‘lid’ also has a small perforated section over the GPIO ports where you can punch out a slot for easy ‘lid on’ GPIO access.

Closer look at optional GPIO “punch-out” slot

I haven’t punched mine out because I prefer option 2, which is simply to remove the lid, which you do easily by gently squeezing the “springy thing” on the side. If you don’t squeeze it, the lid is hard to remove – so it won’t come off by accident.

“Springy thing” you press to remove the lid

It’s a beautiful piece of design work. A simple thing – a box, that does its job very, very well.

Most of the time I leave the lid off. The base is invaluable to me for protection against shorting the underside of the board – for example, by putting the Pi down on coins, or keys, or other little bits of wire that seem to follow me around since I got interested in electronics. Grin

If you’re in the market for a case, I can heartily recommend this one as robust, practical and versatile. And the price is right at £5.99. You can view the ModMyPi case selection here.

Nov 232012
 
Breadboard contents

Don’t try this unless you know what you are doing. Mains electricity can kill you.

Update

Since publishing this blog post, I’ve had some feedback about the way I’ve done this and it needs improvements and a redesign to make it safer. Please don’t copy this. I’ve removed some photos so I’m not setting a bad example.

Back to the original post

Ever since getting my Gertboard in late September, I’ve been thinking about using a Raspberry Pi to switch “proper things”. By “proper things” I mean real, useful, BIG things using mains electricity. Eek!

The Gertboard has a relay controller, but to actually switch anything larger than a few millivolts you need to attach relay(s).

What’s a relay?

A relay is a device which allows a low power circuit to switch a higher power circuit. In the most popular relays, the switch is controlled by an electromagnet switch (solenoid). When there’s current flowing through the low power circuit, an electromagnetic solenoid switch flips from one position to another, switching on/off the higher power circuit (depending on how it’s set up). It makes an unmistakeably characteristic “click” sound. relay-click Click the play button to hear the relay click sound.

5V relay

Learning from the Gertboard

So I bought some relays to experiment with. The first thing I noticed was that the feet of the relays don’t line up very nicely with a standard breadboard. So they had to be bent a bit to make connections. I can see people breaking these with frequent plugging in and out of breadboards. Frown

I did some preliminary tests with the Python version of the Gertboard ocol (open collector) program I wrote, using it to switch leds on and off. I love leds, but they’re a bit tame aren’t they? Wink I want to switch some “proper things”. That means connecting up to mains electricity. Eek!

The other thing the Gertboard taught me was how to use a Darlington Array. Without going into great detail (because I don’t know it ROTFL) a Darlington array is a collection of Darlington Pairs. A Darlington Pair is a pair of transistors used for amplifying a very low switching current up to a useful level.

The Darlington Array on the Gertboard has 8 Darlington Pairs on it, but 2 are not connected (which is why it can run 6 relays, not 8). I got hold of some cheap Darlington Array chips and decided to do some experiments directly interfacing to the Pi. But first there was some hardware work to do.

Mains electricity + breadboard = bad idea

There’s no way you’d get me (or anyone sensible) plugging mains-connected wires into a breadboard. Too hazardous. Eek! No. What was needed was proper screw terminals and the relay soldered down onto a proper board. A PCB (printed circuit board) with holes in the right place for relays and screw terminals for the mains wires would be a good solution, but I made do with stripboard for the time being.

So I decided to solder up a 5 Volt relay, Darlington Array and screw terminals onto a piece of stripboard. I still had to bend the legs on the relay a bit, but, once soldered down, they’re there for good. You’ve already seen the top side of the board.

Test positive

A quick test with a mains powered lamp, with a hacked plug, provided proof of concept. A click, not a bang!
YAY – it works just like it should. Yes

Then, much piddling about in a live Python environment ensued; enjoying the simple satisfaction of being able to switch a lamp on and off at the touch of a keyboard.

This opens up all sorts of possibilities

Then my mind started wandering…
If you can use your Pi to switch on a lamp…
…and your Pi is connected to the internet…
…and you have a smartphone…
…you can log into your Pi and switch it on/off with that Cool

The possibilities for remote and/or automated control of virtually anything just became a reality. The possibilities are limited only by our imagination (and maybe our budget Wink).

OK, but I need a demo for the next Milton Keynes Jam

So my daydream about remotely controlling the universe by connecting relays to my RasPi met with the harsh reality that the MK Jammers would be expecting me to come up with something…

  • new
  • avant-garde
  • eye-catching
  • and hopefully just plain fun.

Last time we had flag waving, but, although that’s extremely cool, that is soooooooo September. The world’s moved on. We need something more. Wink

  • We need a RasPi-switched, mains-powered fan, run by a Python script and temperature sensor. But that’s not enough…
  • We also need a RasPi-switched, mains-powered lamp, running from the same script, with a light sensor to tell it when it’s dark enough to switch on. But that’s still not enough…
  • We need a beeper to alert us when the fan is going to spin up (I’ve got no guard on it – hate those things). More…
  • We need different beeps for fan and lamp. More…
  • We need an indicator led, pulsed with the beeper. More…

Ain’t you getting a bit greedy? Wink

OK, let’s have some custom sounds as well (even if it’s only recordings of me talking). Just so we can have it super silly, fun, eye-catching, attention-grabbing, noisy, interactive, memorable…

And it’s all got to be safely packaged. So here’s what we ended up with…

Nov 132012
 
Labelled version

Ever since getting the LCD working and temperature and light level logging using COSM and the Pi Cobbler, I’ve wanted to compact all of this onto a single add-on board.

I’ve mentioned this in forum posts and other blog articles, but a part of me thought it wouldn’t happen. It took me a few weeks to bring it all together, but it’s finally done and it works Smile

The circuit

I don’t have a full circuit diagram. I may try to put one together later. Smile It’s a bit messy

The back of the board. Here be dragons.

but I’ll try to describe the important bits.

RasPi.TV Weather Station

Two analog temperature sensors (TMP-36) are connected to 3V3 and GND and the middle pins connected to channels 1 & 2 of the analog to digital converter. (Connected to the two 3 pin headers at the top of the photo.)

A voltage divider (right centre near the chips) made up of two resistors (20k and 10k) splits 3V3 voltage into 1.1V and 2.2V. The 1.1V feeds into a dual op-amp (small chip), set up as a voltage follower. One op-amp gives a steady 1.1V to the reference voltage pin (Vref) of the Analog to digital converter (large chip). The other op-amp feeds 1.1V to two light sensors (LDRs), which in turn are grounded with 20k pull-down resistors (top centre). The non-grounded ends of these resistors are connected to channels 3 & 4 of the ADC.

A 16 pin header socket supports and connects the LCD.

The rest of it is just wiring up the various necessary connections to the Pi, which I’ll outline in the next section.

The connections to RasPi

Four GPIO ports handle the Analog to Digital Converter, which can connect up to 8 channels. I’m only using 4 at the moment, but even with 8 channels connected, it would still only use 4 GPIO ports.
Six GPIO ports control the LCD (used in 4 bit mode – to use in 8 bit mode would need 10 ports)
Two ports; SDA and SCL handle the i2c barometric pressure sensor

Breadboard and PCB calibration differences

Once it was all up and running, it was time to calibrate the temperature sensors. When I did the breadboard circuit, I had to add about 2 degrees to each sensor to get accurate readings. The first thing I noticed with the PCB version was that the readings were both about 2 degrees too high. So that correction factor was removed. Other than that, I had to add 0.1 to one of them so that they would both read the same side by side with my reference thermometer. The only thing I can think that would make a 2 degree correction factor necessary on the breadboard would be poor quality connections, or maybe the ribbon cable caused a small (~20 mV) voltage drop?

Having to calibrate it for each Pi

I set this up on one Raspberry Pi, but am intending to run it on another one. One thing I noticed straight away when moving it to the second Pi was that the temperature readings were different. A quick measure of the input voltage to Vref showed that this Pi was giving a slightly higher voltage (1124 mV compared with 1115 mV for the other Pi). When 1 mV is approximately 0.1 degrees, a difference of 9 mV is nearly a degree. This is noticeable. So it was necessary to tweak the program to use the exact Vref voltage for each Pi separately. It must be due to small differences in the 3V3 voltage regulator.

This shows that you can’t just switch from one Pi to another and assume everything will be exactly the same. Test and measure. Smile

Nov 072012
 
pressure_sensor

Works with Raspbian hf Aug/Sept 2012

Under Pressure

From previous blog posts, you’ll know I have a Raspberry Pi set up to read two temperature sensors and two light sensors (inside and outside) and log the data online at COSM

Setting up temperature sensors and COSM feed

But, as ever, “we want more than that!” So I thought it would be fun to add a barometric pressure sensor. Looking around, it seemed like the BMP085 was a good bet. Even better than that, Adafruit have written Python libraries for it and some setup instructions here.

Their instructions are great – perfectly tailored for their Occidentalis Linux distro. I use Raspbian hf, so I’m detailing instructions for that. They’re very similar, but a couple of subtle differences. So, first we’ll start with the software…

sudo apt-get update
sudo apt-get install python-smbus

y to confirm

This installed i2-tools as well, which we’ll use a little later. Smile

How to Enable i2c in Raspbian

Then it was necessary to make a tweak to enable i2c in raspbian. A quick google of how to enable i2c in raspbian revealed this page from S K Pang (I bought my Pi Cobbler from S K Pang). Here’s what I did…

sudo nano /etc/modprobe.d/raspi-blacklist.conf

…and comment out line 3 (the one with i2c in) with a # at the start of the line…
ctrl+o
<ENTER>
ctrl+x

commented out the i2c line (3)

Then…
sudo nano /etc/modules
you need to add…
i2c-dev on the last line

Add i2c-dev on last line

ctrl+o
<ENTER>
ctrl+x

Now we’re going to add the user pi to the group i2c.

sudo adduser pi i2c

adding pi to i2c group

Now we need to reboot to activate the new settings. Or if your sensor is not yet connected, you could shut down and connect it while the Pi is powered down. (Do disconnect the power after shutting down) Smile

sudo reboot to reboot, or sudo halt to shut down.

Hooking it all up

The board my sensor came on has eight pins. Only four of them are used here. Your board may be different, depending on where you source it from. Here’s a shot of mine…

  1. GND goes to Ground on the Pi
  2. 3.3 goes to 3V3 on the Pi
  3. SDA goes to SDA on the Pi (Rev 1 GPIO 0|Rev 2 GPIO 2)
  4. SCL goes to SCL on the Pi (Rev 1 GPIO 1|Rev 2 GPIO 3)

Login and try it out

When it comes back up, log in as pi and type
i2cdetect -y 0 (change 0 to 1 if you have a Rev. 2 Pi)

If your sensor or other i2c device is connected correctly, you’ll get an indication of its address.

My sensor is at 77

Adding pi to the i2c user group means we no longer need to use sudo for i2c access commands.

Now we need to install some software from the lovely guys at Adafruit. We use git for this. If you don’t have git installed, install that first with sudo apt-get install git. Then,
git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code
cd Adafruit_BMP085

Then you can test out your sensor with…
python Adafruit_BMP085_example.py

And if you did everything correctly, you should be rewarded with three readings; temperature, pressure and altitude…

It works Smile

I was elated. This worked straight away for me and the whole thing took about 15-20 minutes. Smile

Let’s go COSMic

I’m not going to fully detail this part, but essentially I took the parts I needed from the Adafruit_BMP085_example.py script and libraries…

This needs to be near the top of your python script
from Adafruit_BMP085 import BMP085
bmp = BMP085(0x77)
set up resolution mode of sensor

And these files need to be in same directory as your script
Adafruit_BMP085.py
Adafruit_I2C.py

And these function calls will get you your data
temp = bmp.readTemperature()
pressure = bmp.readPressure()
altitude = bmp.readAltitude()

Then I incorporated the bits I needed into my weather station Pi’s Python code, to log the new sensor’s data in my COSM feed…

PeterO from the Milton Keynes Raspberry Jam forum pointed out the trace could do with some low pass filtering, to make it a bit less noisy. So I tweaked my script to give a weighted average of the last five readings. It’s a little smoother. Smile Could probably be further improved. I may well tweak it some more.

If you install a BMP085, I hope yours works out as smoothly as mine did. Smile

Nov 052012
 

Download Python Gertboard Programs

ocol – Open Collector – Darlington Array Relay Driver

I’ve managed to get the open collector ocol program written in Python. The ocol program drives the Darlington Array on the Gertboard. This can be used to switch up to 6 relays, each on a separate GPIO port. I’ve managed to write versions for both wiringpi for python (ocol-wp.py) and RPi.GPIO (ocol-rg.py)

They are both included in the download link, which will always contain the most up to date versions. Here’s a comparison video of Gert’s ocol program running first, then my Python version.

buttons

And this weekend I seem to have been on a roll, so I got the buttons program written too. This one is only done with RPi.GPIO because I can’t find how to set the pullups to up using wiringpi for python.

buttons-rg.py – buttons program using RPi.GPIO is also in the zip file to download. This program enables you to test the three buttons, which you can use as inputs to the GPIO. You can see the port status changes on the screen immediately. (You can hook up led indicators too.)

Download Python Gertboard Programs

Nov 022012
 

Download Python Gertboard Programs

I’ve been having a go at producing Python 2.7 versions of some of the Gertboard test suite. It’s partly as a learning experience for me and one which I hope will help others too.

Which GPIO? Why not both?

There’s two different ways to handle GPIO (General Purpose Input Output) in Python at the moment…

  • Ben Croston’s RPi.GPIO
  • Gordon Henderson’s WiringPi (Python Wrapper by Gadgetoid)

The ultimate aim is to have versions of the Python programs using both systems. As a file naming convention, we’ll take the name from the original Gertboard program and add -rp.py for RPi.GPIO and -wp.py for WiringPi.

I had a bit of trouble getting WiringPi for Python up and running, so when I wrote the motor program, I used RPi.GPIO, which I’ve used before. It’s easy to use and is now included in the Raspbian image. So if you’ve got a recent image, you’ve already got RPi.GPIO installed.

When I’d got that working reasonably well, I thought I’d have a go at installing WiringPi for Python again. This time the installation worked (but not as easily as would be nice). So I decided to test out the use of WiringPi for Python with a slightly easier program. I’ve replicated the leds program as faithfully as I can.

The idea with both of these Python programs, was to make the user’s experience as identical as possible to what would happen if they ran the original Gertboard test program. I’ve tested both programs against the originals and they are as close to identical as I can make them.

Fair Comment

I’m sure people will have comments about programming style, etc. (people always do) and I’m also sure that there is plenty of room for improvement. I welcome that, as long as you’re nice and constructive. Smile

I’ve put lots of my own explanatory comments in the script to try to explain what each section does. This is with a view to other people learning too.

It’s very interesting to me that the Python leds program can be written in ~40 lines, whereas the leds.c C source code takes a lot more lines than that. I don’t know if that’s the way it is between C and Python, or if the C program was written to be friendlier for newcomers to understand, but it looks the same at the other end, from the point of view of the user.

Fully functional

I’ve used functions, which is something fairly new to me. I’m starting to see the power of them. When I first wrote the scripts, I used several consecutive loops, but then thought “these loops are basically the same. What about if I had one common function and used ‘function arguments’ to handle the basic differences in the loops?” It seemed to work out OK, but obviously it makes the code slightly harder to read and understand.

leds-wp.py

leds-wp.py is as close a copy as I can make it, uses wiringpi for python. To make use of it, you will need to have wiringpi for python installed. You can install it with…

sudo apt-get update

sudo apt-get install python-dev python-pip

sudo pip install wiringpi

If you don’t have WiringPi installed, or have trouble with it, use leds-rg.py, which is the RPi.GPIO version.

motor.py

motor-rg.py uses RPi.GPIO and software PWM, which is not quite as smooth as the hardware PWM used by the original, but it works. It works better in the August 2012 Raspbian than the September version. Possibly to do with the flexible overclocking messing around with the timings. I hope to do another version with hardware PWM once I work out how to do it.

Update 5 November 2012: I have now implemented the hardware PWM in motor-wp.py

You can download these files from here…
Download Python Gertboard Programs

The main download page with instructions is here…
http://raspi.tv/downloads

And if you want to discuss these programs or porting others in the Gertboard test suite into Python, then either leave a comment below or you might like to visit this thread on the Raspberry Pi forums.

Update 3 November 2012 ~15:00: Just added leds-rg.py – leds program using RPi.GPIO
Update 3 November 2012 22:02: Just added ocol-rg.py and ocol-wp.py – open collector darlington array relay triggering program for both GPIO methods.
Update 4 November 2012 15:30: Just added buttons-rg.py – buttons program with RPi.GPIO
Update 5 November 2012: I have now implemented the hardware PWM in motor-wp.py