News

10 Raspberry Pi upgrades part 1

Supercharge your Raspberry Pi with ten essential add-ons

Upgrade your Pi with these essential add-ons

The Raspberry Pi is a fantastic piece of British engineering. Just five years ago the concept of a credit card-sized computer capable of playing video at 1080p and offering a full desktop PC experience would have been considered witchcraft. Claiming that it was able to do all this and more at a retail price of a little over £25 would have seen you committed.

It’s clear that the Raspberry Pi has been a revolution for small form factor computing, but it’s not quite perfect – only so much technology can be packed into a tiny space, after all. For enthusiasts and makers there’s a growing list of extra-curricular add-ons that can help make their projects, gadgets and Intenet Of Things devices come to life. We’ve selected ten of the most essential and over the next eight pages we’ll show you where to get them and how to get started…

Upgrade your Pi with these essential add-ons
Upgrade your Pi with these essential add-ons

Read analog inputs with your Raspberry Pi

What you’ll need

MCP3008
Breadboard
Prototyping cables (male to female)

What is it?

If you want to add the ability to read analog sensors for your Raspberry Pi projects you’ll need a very particular piece of hardware – an analog to digital converter (ADC). The cheapest and simplest way to implement this with the Raspberry Pi is using an ADC chip like the MCP3008, which adds eight analog inputs at the cost of just four GPIO pins. With a small selection of prototyping parts and a sprinkling of Python code you could be reading temperature sensors or using an analog thumb-pad to create your own games controller in next to no time at all.

Why do it?

The Raspberry Pi’s GPIO pins are great – they allow you to interact with the physical world in really cool ways, simply by turning the voltage
to its various pins on and off. With its single pulse-width-modulation-capable (PWM) pin, the Raspberry Pi can even offer relatively fine-grain control to change the brightness of the LED light or alter the speed of a DC motor – but for fans of electronics-based projects, analog sensing is something that’s sorely missed and readily available on Arduino and similar microcontrollers. With it you can get very precise feedback from a potentiometer (a twisty knob like a volume control) or get accurate readings from something like an IR reflectance sensor to help your robot not bump into things. You could also use an ADC- enabled Raspberry Pi to check the ambient light to turn a night-light on automatically using a cheap-as-chips light dependent resistor (LDR), otherwise known as photocells, as we’ve demonstrated on our example breadboard.

How to use it

The MCP3008 ADC does take a little bit of setting up, but once it’s set up it’s very easy to start using. This tiny chip can be bought for as little as £2 from most good electronics stockists. Since it’s unlikely that you will want to hardwire it into your initial projects, we would recommend using a breadboard and male-to-female prototyping cables to put it all together as we’ve demonstrated in our Fritzing diagram. Although the diagram looks relatively complex, we’re actually only using four GPIO pins to control the chip logic and the 3.3V and Ground pins to co-ordinate power to the other four pins along the right side of the chip (the small semi-circle at one end of the chip denotes its ‘top’). The pins down the entire left side of the chip are for your analog inputs.

Fritzing diagram for analog sensors
Fritzing diagram for analog sensors

Installing necessary software

Step 01 Clear the blacklist

With a terminal window open, type:

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

Add a hash symbol (#) to the start of each line in the file and then press CTRL+X, then Y and press Enter to save and exit the file.

Step 02 Install Python dev tools and spidev

There are a couple of critical packages we need. At the terminal type sudo apt-get install python-dev python-pip. Once the packages are installed type sudo pip install spidev then finally sudo reboot to restart your Raspberry Pi.

Step 03 Read the sensors

To print the result from any analog sensor connected to the left side of the MCP3008, simply use the following code example – write it in Leafpad and save it as adc_test.py. In the script we are reading the first two pins to see the results from our two photocells. Run it with sudo python adc_ test.py to test it.

import spidev
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

spi = spidev.SpiDev()
spi.open(0,0)

def readadc(adcnum):
   if ((adcnum > 7) or (adcnum < 0)):
      return -1
   r = spi.xfer2([1,(8+adcnum)<<4,0])
   adcout = ((r[1]&3) << 8) + r[2]
   return adcout

while True:
   print “Photo Cell 1: “ + str(readadc(0))
   print “Photo Cell 2: “ + str(readadc(1)
   time.sleep(1)

Use an analog distance sensor

The distance sensor
The distance sensor

What is it

An IR reflectance sensor such as the Sharp GP2D120 IR Range Finder offers a great way to sense the world, using the infra-red light spectrum to ‘ping’ its surroundings in much the same way as a bat employs echo location to avoid obstacles in the dark. The closer an obstacle is to the sensor, the higher the reading you’d receive from its analog reading (between 0 and 1023).

Why do it?

These kinds of sensors are perfect for Raspberry Pi projects that require any kind of acknowledgement of its surroundings. For example, you could place a sensor near a bird perch and have it trigger the camera module to take a picture should a reading indicate activity in your target area. Alternatively, you could employ several sensors on an autonomous robot – if the left sensor gets a high reading, it’s trivial to then tell your robot to turn right.

How to use it

We’re going to use the robot example and the test code we produced for the MCP3008 to show how easy it is to read and decipher the readings from these sensors to make a robot make seemingly intelligent decisions:

import spidev
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

spi = spidev.SpiDev()

spi.open(0,0)

front = 0

trigger = 450

def readadc(adcnum):
   if ((adcnum > 7) or (adcnum < 0)):
      return -1
   r = spi.xfer2([1,(8+adcnum)<<4,0])
   adcout = ((r[1]&3) << 8) + r[2]
   return adcout

def read_sensor():
   result = readadc(front)
   return result

while True:
   reading1 = read_sensor()
      if reading1 > trigger:
         print “Sensor”, str(reading1), “triggered: Run!"
      else:
         print "Nothing to see here."
      time.sleep(0.5)

Take pictures with your Pi

What is it?

Released last year, the Raspberry Pi camera board is a small printed circuit board (PCB) which houses a tiny digital camera sensor of 1.3 megapixels. It's able to take photos, video and display a live preview of what the camera can see.

The Raspberry Pi camera is an excellent piece of kit
The Raspberry Pi camera is an excellent piece of kit

Why do it?

Because of the size and portability of the Pi, it's almost as convenient as a camera phone, albeit with more controls via the specialised libraries and software. Taking pictures and video could be done with a webcam, however that would have to take up a USB port. The camera board slots into a specific video-in connector that doesn't take up any of the conventional ports, a connector specifically designed with the then future camera in mind.

How to use it

Turn off your Raspberry Pi and make sure you disconnect the power. In between the HDMI port and the ethernet port there's a special connector. Pull up the tabs on both sides to open up the port and slot the ribbon into the opening with the silver connectors facing towards the HDMI. Gently push down the tabs to secure it and then connect the Raspberry Pi back up to power. To start using it, you will need to activate the camera module within the config settings.

Enable the camera module

Step 01 Update your Pi

You will need to make sure everything is up to date. Open the LXTerminal and update the firmware followed by the software:

$ sudo rpi-update
$ sudo apt-get update && sudo apt-get upgrade

Step 02 Enter configuration

Once the updates are complete you can update the configuration files. This is the same one you saw after installing Raspbian. Staying in the terminal, open this with:

$ raspi-config

Strong 03 Enable the camera module

Go to the Enable Camera option and change the setting to Enable. Then, go to Finish on the main interface and it will reboot your Pi with the camera working after login. The Pi camera is now accessible via the command line and its operation is simple. To take a picture, use:

$ raspistill -o [imagename].png

This will display a five second preview before saving an image. Videos can be shot with:

$ raspivid -o [videoname].h264

...which records for five seconds. There are options you can add to each command that let you control exposure, preview length, framerate and resolution. There's also the picamera Python module that enables you to control it via Python.

Add a power switch

What is it?

The USB Cable with Switch is simply a binary switch sitting between a male and female USB A port: the connector present on every USB cable. In our case, the A port connects to source of the power for the Pi, allowing it to interrupt the flow of electricity and effectively turn it off and on.

Why do it?

As many users of the Raspberry Pi already know, once the Pi is turned off you will need to disconnect and reconnect the power in order for it to start booting up again. This switch allows you to skip this task and do it in a more traditional manner.

How to use it?

The main component in this setup is the USB Cable with Switch from Pimoroni, however you'll also need a micro USB cable to connect to the female end and a mains adapter that allows you to directly plug a USB A port into it.

Control servos

What you'll need

Adafruit 16-channel PWM/Servo Driver board

Hobby servos from

Battery holder

What is it

Adafruit’s 16-channel Servo/PWM Driver board is an impressive piece of kit. With it you can control up to 16 hobby servos using just two pins on your Pi (via I2C). You can daisy- chain 62 of these boards together on a single Pi, so you are able to control a staggering 992 PWM servos or LED lights at the same time.

The adafruit servo can be used with robots
The adafruit servo can be used with robots

Why do it?

PWM (Pulse Width Modulation) is a challenge for the Pi as it only has a single compatible pin. Since the Pi isn’t a real-time device like an Arduino, software-driven PWM can be hit and miss. While it works well controlling LEDs, controlling a servo on a general- purpose computer like the Pi can lead to undesired results like jittering.

How to use it

If you want to be able to make anything that requires precise moving parts, a dedicated hardware solution to PWM is a must, and that’s exactly what Adafruit’s 16-channel Servo/PWM Driver board offers. Once all of the soldering is done, you need to set up your Pi to talk to the board with I2C. Check out our step-by- step for software usage below.

How it works

With the software installed and your driver board hooked up with a battery pack attached you can experiment with servos in your projects.

With the third-party Python library we install in the boxout below, it’s easy to program the servos. See bit.ly/1nNDaXC for more details.

Grab the Python Library

Step 01 Install dependencies

To set up I2C you need to make sure I2C communication isn’t blacklisted. Type sudo nano /etc/modprobe.d/raspi- blacklist.conf and comment (add an #) to the start of the line ‘blacklist i2c-bcm2708’.

Next you need to install SMBus and I2C tools. You can do this in the terminal with the following command:

sudo apt-get install python-smbus i2c-tools

Step 02 Grab the Python library

Since the Raspbian distro comes complete with git, it’s really easy to grab the official Adafruit Python library with the following command in the terminal:

git clone https://github.com/adafruit/ Adafruit-Raspberry-Pi-Python-Code.git

You can navigate into the folder with:

cd Adafruit-Raspberry-Pi-Python-Code/ Adafruit_PWM_Servo_Driver

Step 03 Install a third-party library

While the official library is good, it's incredible basic and doesn't make servo operation easy. Luckily, there’s a small third- party library that uses the important bits from the Adafruit library and builds on it. Grab it using git from the terminal window with:

git clone https://github.com/ labatrockwell/raspberrypi-experiments.git

Give your Pi ears

What is it?

The Adafruit Electret Microphone Amp is a clever piece of microphone design that uses a permanently charged material, called electret, forgoing the need for a polarising power source. Electret effectively has a built-in static electric charge that wont decay for hundreds of years – some pretty clever stuff. The Adafruit Electret Microphone, shown here, comes with a built-in amplifier and is rigged and ready to use bar soldering.

Give your Raspberry Pi ears
Give your Raspberry Pi ears

How to use it

Simply connect it to your 3.3 voltage, ground and an analog input provided by our first project in this feature. You simply plug output from the electret straight into a free ADC-enabled pin and you can measure the sound response from the microphone. If you want to get fancy, all you need to do is convert the values coming from the ADC to a PWM-compatible value (simply divide it by four) and you can use your voice to trigger the brightness of an LED light.

×