The blinking LED is pretty much the "Hello World" of GPIO with microcontrollers. I've recently got hold of a Raspberry Pi Zero so I thought I'd give it a go and see how easy it is, and thanks to a couple of libraries written in Python, it's super easy. I also happen to have recently fallen in love with Python so it's a win-win.
So this bit is super easy. I found a website called pinout.xyz which can be used as a pinout guide for the Raspberry Pi's 40 pinouts. For my circuit I used BCM 17 (physical pin number 11) to connect my LED to.
So what do you need?
- a basic LED
- 330 Ohm resistor (the exact resistance requires some maths but for the purposes of this 330 Ohms will work okay)
- Some jumper cables (male/male or male/female)
- Raspberry Pi Zero
- 40 pin header connector (male or female, depending on your jumper wires - you'll need the opposite gender basically)
First thing to do is to solder that 40 pin header connector to the Raspberry Pi Zero. My soldering skills are woeful so don't worry, even an imbecile like me can manage it. I used a male header because I also bought the Adafruit Pi Cobbler to break it out on to a breadboard easily but this isn't required.
- Take your jumper wire and attach it to physical pin 11 (BCM 17) which is the 6th pin down on the inside.
- Plug the other end into the breadboard.
- Then take your LED, take the longer 'leg' and push that down on to the same row as the jumper wire you just plugged in to the breadboard.
- The shorter 'leg' can go in to the row below or above.
- Then, take your resistor and plug one end into the same row as the shorter 'leg' of the LED from the previous step and then the other end either straight to the ground column of the breadboard or to another row on the breadboard (which I did below).
- Then take your second jumper wire and plug that into either the ground column of the breadboard or into the same row as the other end of your resistor, depending on the method you chose in the previous step.
- The other end of the jumper wire needs to be plugged into one of the ground pins on the Raspberry Pi - physical pin 6 will do, which is the third pin from the top on the outside.
Something like the above. Yes I know I've used a 560 Ohm resistor. I couldn't find my box of resistors so made do with this one. It'll still work, just won't be as bright.
So there are two easy ways to do this in Python using libraries, both of which come pre-installed on the latest Raspian image. For people that might not have updated their packages recently or have an older version of Raspian, lets do a little check and install what we need.
First lets run Python
At the prompt type the following and check what version of RPi.GPIO we have
import RPi.GPIO as GPIO GPIO.VERSION
It should return you a version number. I think 0.6.3 (as of 15-4-2017) is the latest version. If you don't have that you can update in the usual way.
sudo apt-get update sudo apt-get upgrade
Secondly, if you don't have GPIO Zero you can install it with the following
sudo apt-get install python3-gpiozero python-gpiozero
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) GPIO.output(11, False) while True: GPIO.output(11, True) sleep(1) GPIO.output(11, False) sleep(1)
So what are we doing here?
- Line #1 - Importing the library
- Line #4 - Setting the mode (more on this later)
- Line #5 - Initialising a GPIO pin
Then, to turn the LED on and off, we set the value of the pin to be True or False, and thus we ceate our blinking LED. The while loop will continue indefintely and there should be a second pause between the blinks.
from gpiozero import LED from time import sleep led = LED(17) while True: led.on() sleep(1) led.off() sleep(1)
What we're doing here is more or less the same but we're making use of the GPIO Zero wrappers
- Line #1 - Importing LED from the libary
- Line #4 - Initialising a GPIO pin as an LED
Then we're doing the same as before except that we're using the
off() methods to turn the LED attached to the GPIO pin on and off.
But why are your pin numbers different?!
Good question, I'm glad you asked. If you notice on Line #4 of the first example, we set the mode as 'GPIO.BOARD'. Now with the RPi.GPIO library you have the option of using the board numbering or the broadcom numbering. Now, the GPIO Zero library uses the Broadcom numbering by default (although I believe you can override this) so that is why it is pin 17 on that example and pin 11 on the former. Now, what you might want to do is do GPIO.setmode(GPIO.BCM) and then you could subsitute 11 for 17 in the RPi.GPIO example.
Which shall I use?
Basically, I think GPIO Zero is providing a wrapper to the slightly lower level commands needed to manipulate the GPIO pins via the RPi.GPIO library. In fact I believe GPIO Zero uses RPi.GPIO and wraps things in some nice easy to use methods. For most basic things the speed of which you can write stuff using GPIO Zero will probably make it the best choice, but if you want to go slightly lower level then you may wish to use RPi.GPIO still.
So there you have it, two slightly different methods for controlling GPIO pins to blink an LED with Python on the Raspberry Pi and Raspberry Pi Zero. If you have any questions or spot any errors, please do let me know.