Skip to content
Art M. Gallagher edited this page Jun 29, 2021 · 20 revisions

See also:

components

As well as the usual Raspberry Pi, this is the additional equipment we used for electronics experiments. Don't worry if you don't understand what any of these mean or do, we'll explain these as we go along.

Starter kit

  • Vilros RasberryPi Cobbler breakout
  • Vilros Breadboard

The first two were part of a Vilros starter kit, which also included some common electronic components, like resistors, LEDs and button switches.

By butchering old circuits from broken devices, and chopping or de-soldering them, we also got some

  • Capacitors, an assortment of low power components with various ratings

To make ad-hoc connections easier

  • crocodile clips, with shielded cables

sensors

  • Sintron Ultimate 37 in 1 Sensor modules (for Arduino)

basics

electronic components

Components are the individual little pieces used in electronic devices. Different kinds have different shapes or colours but they all have silver metal leads (sometimes called legs or pins)

electronic circuits

A circuit is a combination of components put together to do a particular thing.

Electricity flows when it has a way to go around. When simple electrical components are connected to each other 'all the way round in a circle' they form a simple 'circuit'.

When you look at pictures of circuits, you might see photos of green boards with gold lines and lots of little coloured pieces attached - these are actual circuits. Or you may see black and white diagrams with lots of pictorial symbols in an interconnected framework - this is a circuit diagram which makes it clear what is connected to what.

soldering

To create permanent circuits, use a hot soldering iron to melt solder and fix components in place

https://www.youtube.com/watch?v=H3-TfdZVBCc

Breadboard

We don't want to solder permanent circuits, we just want to put together experiments, so we use a breadboard

Have a look at the video at: https://www.youtube.com/watch?v=oiqNaSPTI7w

To make connections easily from the breadboard to the Pi we use the Vilros Cobbler breakout module https://www.vilros.com/raspberry-pi/rasp-pi-acc/raspberry-pi-cobbler.html

see also https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/breadboard/

GPIO

GPIO stands for General Purpose Input Output, and is the parallel rows of pins on one long side of the Pi.

It gives the connections (sometimes called an Interface) that the Raspberry Pi uses to connect to external electronics, to read signals (input) or send them (output).

For pictures and explanations see https://www.raspberrypi.org/documentation/usage/gpio/

The pins are numbered from 1, starting at the end where the ribbon cable has a red stripe, and alternating sides: 1 & 2 are at one end, then 3 & 4, 5 & 6, etc. A 'pinout' is a list of the pin numbers, saying what each of them does - see http://pi.gadgetoid.com/pinout

Light Emitting Diode (LED)

These are often used as indicator lights in electronic circuits, as they are cheap and use very little power. They must be connected the right way round as the "Diode" part of the name means 'only lets electricity pass one way'.

Sensors

The "Sintron Ultimate 37 in 1 Sensor Modules Kit" is sold as being for the Arduino, another popular experimental computer board. However, they are simply electronic components, so they may of course be used with the Pi, or any other electronic experiments you would like to make. To find out more see http://www.sintron-hk.com/Ultimate-37-in-1-Sensor-Modules-Kit-for-Arduino-MCU-Education-User-P3112448.aspx or to look at the list of sensors try http://www.amazon.co.uk/Sintron-Raspberry-Education-Documents-Available/dp/B00DU2IQ10#productDetails and for documentation and code (albeit arduino C programmes rather than Pi Python) download from https://www.dropbox.com/s/oitqcijo9za7j9w/37%20in%201%20sensor%20pdf%20files.zip

preparing your Pi

software installs

Python GPIO library

make sure this library is installed, from the command

sudo apt-get update sudo apt-get -y install python-rpi.gpio

if you use Python 3 then install...

sudo apt-get -y install python3-rpi.gpio

configuration

When you first install RPi.GPIO it uses something called /dev/mem, which is the name for a system memory device. Because this is an important part of your system, only the 'root' user can normally access it, using sudo.

So write your code, save it, then run it using

sudo python myprog.py

NEED TO CHECK if using one of the install methods from the project page avoids needing root - see https://pythonhosted.org/RPIO/#installation

alternatively see https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=26934

You could use a gpio group which has the relevant rights - see https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=7370

pigpio uses a daemon (program which is always running) to avoid needing to http://abyz.co.uk/rpi/pigpio/python.html

Troubleshooting

If you have any issues with control software, such as the Python GPIO libraries, after basic debugging, you can try direct control from the bash command line in Terminal:

' turn BCM7 / CE1 / pin 26 ON then OFF
PIN=7
echo "$PIN" > /sys/class/gpio/export
echo "out" | sudo tee /sys/class/gpio/gpio$PIN/direction
echo "1"   | sudo tee /sys/class/gpio/gpio$PIN/value
sleep 1
echo "0"   | sudo tee /sys/class/gpio/gpio$PIN/value
# credit http://raspberrypi-aa.github.io/session2/bash.html

projects

getting started

simple LED

There is a simple to follow steps to light an LED at http://www.thirdeyevis.com/pi-page-2.php

This does not use a breadboard, but is simple to understand. If you still feel unsure, have a look at the pictures in the next project.

Remember:

  • the long, knobbled leg goes to the + Signal and the short, flat leg goes to the - Ground
  • put a resistor between the LED and the rest of the circuit
    • between 50 Ohm and 350 Ohm should be fine
    • it can be attached via either leg, to either + or -
    • without the resistor the LED may draw too much current and burn itself out

button and LED

On the other hand you might want to see something being done on a breadboard, so you could check out this simple button and led, also bi-colour led [http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/turing-machine/two.html] - beware the typo getting the LED legs the wrong way round - Long is + and short is -

understanding input

Input means facts or information that we PUT IN to a computer. Or if you imagine yourself being 'inside' the computer, that are "read in". The I in GPIO stands for Input, so clearly reading information from the real world is something that the Pi was made for.

Digital or Analogue

Some people explain the difference between Digital and Analogue in terms of clocks. An analogue clock is one where the hands sweep around and rarely point at a precise number, but we know how to read "how far between" they are, and we know by how close the hands are when it's about to change minutes or hours. A digital clock very clearly and distinctly tells you a precise number of hours and minutes, but you are unlikely to know when the digits are "about to change", you just have to catch them as they do.

As far as the Pi's inputs are concerned digital essential means it is sensing something or it isn't ... there is a voltage (electrical current) flowing into the input, or there isn't ... True or False in the language of logic. This is why a button is so simple, it is pressed or it is not. Yes or No is the answer, which computers know as 1 or 0 in their language called "binary".

Now imagine you want to read "hot" or "cold", or more precisely "heat" or "lack of heat". When it is burning, it knows it's hot. When it is freezing it knows it's not. Great! but what about when it's warmish? What actually happens on the GPIO pins is it reads False when there is not much electrical current flowing, True when there is lots, and when it's in between it might read either True or False. So it's not reliable for reading "in-between".

The next circuit offers an interesting way to read between the 0 and the 1, the true and the false, by creating a circuit which turns strength into time - it sounds magic but it's really simple. Imagine a tap: it is turned off, or it is turned on. So how can we measure how much it is turned on? Simple ... and you can do this experiment with your eyes closed! Hold a cup under the tap and count how many seconds until you feel wet all over your hand, because the cup is overflowing. If it's a gush, it may only be two seconds, medium you make need five or ten seconds, a dribble you might need to count up to a minute or more. And if you fall asleep whilst counting, then when you finally wake up again you will know, if your hand is still dry, that the tap is turned firmly off. There, you have built a reasonably accurate flow meter "with your bare hands".

light detector

The simplest component for detecting light is a Photo-resistor or Light Dependent Resistor (LDR). In the dark it does not let much electricity through (high resistance), but the more it is exposed to light, the more electricity it allows through (lower resistance)

We will time how long it takes to charge a Capacitor, the longer it takes the more resistance their was through our photoresistor, therefore the darker it was.

The idea behind this circuit comes from http://raspberrypi.stackexchange.com/a/5169, which uses what is known as an RC Charging Circuit (Resistor - Capacitor Charging). Later, if you want to calculate the timings to expect from different values check out the rather technical low-down on RC Charging at http://www.electronics-tutorials.ws/rc/rc_1.html

To recognise the components we will use, see the photos in http://www.ninjagecko.co.uk/how-to-measure-the-amount-of-light-in-a-room-using-a-light-dependent-resistor/

For code ideas see:

with a laser

see http://blog.christianperone.com/?p=2383

not used

Gordon Drogon's projects are visually simple, and use a breadboard, but do not use the breakout and the coding is in C - https://projects.drogon.net/raspberry-pi/gpio-examples/tux-crossing/3-more-leds-and-a-button/

doing more

more buttons and LEDs

This project shows a circuit with more buttons and LEDs, and uses Python. It is not as clear and step-by-step, but gives useful ideas http://openmicros.org/index.php/articles/94-ciseco-product-documentation/raspberry-pi/217-getting-started-with-raspberry-pi-gpio-and-python

hacking more buttons

http://www.codingclub.co.uk/pi_controller.php

sensing temperature

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing

more precise input

Analog to Digital

The following experiments use a ADC called the MCP3008, from Microchip Technologies. https://www.adafruit.com/products/856 Fortunately it is common so plenty of people have posted examples of using them, but it is also cheap, and you can pick them up from many electronic suppliers.

ADC links

If you don't want the fun of making it yourself you can buy a £20 board

more powerful control

using a Relay

The power from the GPIO pins is not that great, so if you want to control something more powerful you can use a relay. The Sintron Component Kit included a relay (labelled Sintron_SRly)

A relay is a switch which controls high powered equipment. Whether it's mains-voltage lighting, fans or screens, or just needs more current from bigger battery packs like motors, the puny signal from the GPIO pins cannot give the power they need. What it can do however is make the magnetic coils inside the relay pull the metal switch across to make contact, turning the switch on and off.

Different Relay circuits may use different markings, but they generally have slim connecting pins for the input side and fat screw-terminals for the output side - obvious if you think about how much power each side is designed for. See a photo and a control circuit diagram by way of example.

  • Inputs

    • GND / G / Ground / Negative / -
      • connect to Ground (for the control circuit and the signal)
    • VCC / V / Voltage / Positive / +
      • connect to +5V to power the control circuit
    • IN / S / Signal / Input / S
      • connect to the GPIO pin to switch the relay as desired
  • Outputs

    • COM / Common
      • This is always one side of the switch - pick ONE of the next two to connect the other side
    • NO / Normally Open
      • the switch is open (turned off) until the Signal is activated - in other words "I'll tell you when to turn the device ON"
    • NC / Normally Closed
      • the switch is closed (turned on) until the Signal is activated - in other words "You can stay running, but I'll tell you when to stop"

Building it yourself

How to power a fan from the Raspberry Pi GPIO: If you have only mild power requirements then you can build a fan controller using a simple transistor, like the 2n2222 in this diagram with a pull-down resistor. The transistor protects the GPIO pin from the high current draw when the fan starts up, and there is also a diode to protect the Pi from back-current caused by the inductive windings in the fan when it stops.

If you want to know how to vary the speed of the fan using Pulse Width Modulation (PWM) then see Advanced Motor Control for robotics.

For more moderate power requirements you can use a Field-Effect Transistor (FET or a MOSFET) as in this post. See also this elinux page which has these and other circuits.

If you want even higher power, then you can use this last circult to drive a relay, or simply use a relay board as in the previous section.

to check out

some various bits of GPIO Python code https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=98140

  • [http://www.bristolwatch.com/] ** lots of different projects for different platforms - need to work through and see which are simple enough
  • [https://learn.adafruit.com/category/learn-raspberry-pi] ** this reseller has plenty of clear and informative how-to pages to learn about putting the components they sell to good use
Clone this wiki locally