DesignSpark Electrical Logo
  • Brought to you by
  • RS Components
  • Allied Electronics

February 1, 2017 11:46

Interfacing hardware with the Raspberry Pi

An exploration of GPIO pins, LED and switch control.

Last week I embarked on my journey with Raspberry Pi; I set it up and had it perform some simple commands. Knowing nothing about Linux and Python, initially I was daunted by even the simple task I was given, but I needn’t have been. It was simple, easy, and quite fun once I got the hang of it. My next task was to play around with the General Purpose Input/outputs, — better known as GPIO pins — using it to interface external hardware directly rather than via one of the USB ports. This is a much simpler way to The Pi is versatile and accommodating: it can be connected to anything from fundamental devices like LEDs and buzzers, to robots and smart mirrors.

GIPO and Pinout Predicament

First off, we need to know what the pins are and do. GPIO pins are the 2 rows of 20 pins. The pins have various functions, there are power pins, ground pins and pins that are ready to be set to an input or an output. The Pinout diagram showing each pins function can be found in the book provided in the Starter Kit, or easily looked up online on the Raspberry Pi website. The first time I saw a pin diagram it might has well have been Gallifreyan for all I could decipher, but much like Python, it turned out to be pleasingly straight forward once I considered it further.
The pins are physically numbered 1-40, but also have have GPIO numbers and names, those with names have set functions, e.g GND is a ground pin. The ones with numbers can be used as general purpose inputs and outputs. Some have both, pin GPIO Pins 14 and 15 for example have the names TXD and RXDT respectively but physically, they are pins 8 and 10. These can be used as GPIO pins, but also double up as the transmit and receive pins for the Pi’s serial port. This can be seen on the diagram below.


Worth noting here is that all GPIO pins run on a maximum voltage of 3.3V — any higher and you frying your Pi, not ideal. Whether being set as an input or an output, the GPIO pin will either be set to logical O or logical 1. Handily we can check them too. An input set to logical O with be below 1.7v and if set to logical 1 will give a reading of above 1.7v. Set as an output logical O the pin will give you 0v and a logical 1 will give that all important number, 3.3v. We can also call logical 0 False, and logic 1 True. This is worth noting as it comes up in the coding.

Interfacing- Build it and they will code.

The building

Ok, so now we understand the pins and logic behind them, we can get our LED blinking. To do this I used the GPIO pins and some jumper cables to wire up a breadboard fitted with an LED and resistor, and then used my new best friend Python to control the signal. I built my circuit first and coded second. The building is undemanding and the pins can only sink or supply a tiny current, so I used a resistor of 470Ω to protect them. I connected that across column C on my bread board, from row 1 to 6, and my LED across column E, the anode (long leg) in row 6 and the cathode in row 8. I then attached the jumper cables to the GPIOs. I used two male/female jumper cables, one red and one black to make things easy. Connecting the female end of my black cable to GND pin 5 and the male end to column A row 8. Then used same set up with the red cable, the female end to pin 18 and the male end to column A row 1. The coding was as a painless as the building, logical and elementary.

The coding.

First off we need to set up Python so that it can access the pins. We do this by importing the Pi’s GPIO library using the statement import RPi.GPIO as GPIO. The Pi also need to be able to use time, so we import that library in the same way. We need to let the Pi know if we want to use the physical board/header numbering or chip numbering scheme for the pins in our code. I chose to use the GPIO names and number rather than the physical numbers, and so gave the command GPIO.setmode(GPIO.BCM) BCM being an abbreviation for Broadcom, the manufacturer of the System-on-Chip used by the Pi. From there you’re set. It as easy as setting pin 18 as the output using GPIO.setup(18, GPIO.OUT) and that this is the pin connected to the LED using led_pin=18 and GPIO.setup(led_pin, GPIO.OUT).
Now the Pi knows what we’re connecting and where, we can start telling it what to do with that information by using the statement try: In the screenshot above you can see both the line of coding and an explanation of it. In Python, anything that follows a hashtag is not part of the coding, it is only a comment there to inform the reader of the code what is happening. As you can see from the explanations it’s rather easy to follow: turn on, delay, turn off, delay, and it will stay in that loop until you stop exit the program. The True and False in the coding are referring to the logic levels we discussed earlier. Like I said, painless, logical to follow and quite enjoyable.

Switching it up to look at the real-world applications

If your still with me, you can take it a little further. With a bit of modification, we can adapt it to a real-world application: the control of a switch. We can do this in pretty much the same way we made the LED blink. We get rid of the breadboard and move the red jumper cable to pin 23. We’re going to make the Pi tell us when a button/switch has been pressed, the connecting of the jumper cables acting as our switch. The logic and the commands remain the same throughout. Importing the pin library, importing the time library, letting the Pi know we’re using pin names, that instead of being an led on pin 18 it’s a switch on pin 23. The juicy bit comes from the line of code GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) which enables an internal pull-up resistor on pin 23 to keep the input on logic level 1(True) unless it is connected to ground, which will override this and change the logic level to O(False). We see this in the instruction that, while true: meaning that while set at logic 1, if the switch pin is registered a logic level False, which will happen when the jumper cables are connected, returning the power to GND. The Pi is instructed to return value ‘button pressed’ and then sleep for 0.2seconds. Now every time the jumper cable ends are connected ‘button pressed’ will pop up on the screen like the screen shot below.

Implementing the know how.

This can lead us to fascinating places when we think about all the application of this simple logic and what we can control with it. In the introduction, I mentioned robots and smart mirrors and in my previous post I mentioned my interest in Medical Engineering. I have pondered on the implementation of electronics within the NHS and come up with so many ideas where the use of cheap and simple technology could revolutionise procedure, save time, make documentation easier, free up nursing time and solve countless other problems. The ones I’m most interested in though are saving money and increasing patient health and dignity.
My proposal is the use of a moisture sensor in the beds of bed bound patients in the hope of reducing the development of pressure sores that cost the NHS billions each year, cost the patients their health and in some cases their dignity. In my next post, I’ll be offering a little bit more information about the NHS budget, current procedures, my production of a cheap and reliable moisture sensor and how it could be implemented to save money, increase patient health and make the whole hospital experience a little more bearable.



I Graduated from the University of Bradford with a degree in Chemistry and Forensic Science and currently I am studying towards a HND in Electronic and Electrical Engineering while interning at AB Open.



February 26, 2017 15:53

Wow ! You are going to have one great future in anything you want to do. Good luck and I hope you can put your training to great use! Stay well. ,