Lesson 3 – Multiple Blinking LED Lights
Booting up the Raspberry Pi
- This section assumes that you have read through “Lesson 0 – Setup” and have a working Raspberry Pi you can connect to remotely using SSH or VNC.
- If that’s not the case please head back to “Lesson 0 – Setup” and work through the instructions provided.
- Let’s now get started and boot up the Raspberry Pi.
- Grab a good quality USB cable and a USB power adaptor (2.5A).
- Plug one end of the USB cable into the plug and the other microUSB end into the Raspberry Pi.
- This should now power up the Raspberry Pi.
- Once the Raspberry Pi has booted up, please ensure that it is able to connect it to the network so that you can access it over VNC.
- If you are using a local monitor connected to the Raspberry Pi, you are all sorted.
- Else get connected to the Raspberry Pi using VNC.
01 / What are the GPIO pins on the Raspberry Pi – (From http://www.raspberrypi.org) A powerful feature of the Raspberry Pi is the row of GPIO (general-purpose input/output) pins along the top edge of the board. A 40-pin GPIO header is found on all current Raspberry Pi boards (unpopulated on Pi Zero and Pi Zero W). Prior to the Pi 1 Model B+ (2014), boards comprised a shorter 26-pin header. Any of the GPIO pins can be designated (in software) as an input or output pin and used for a wide range of purposes.
The numbering of the GPIO pins is not in numerical order; GPIO pins 0 and 1 are present on the board (physical pins 27 and 28) but are reserved for advanced use (see below).
- Voltages – Two 5V pins and two 3V3 pins are present on the board, as well as a number of ground pins (0V), which are unconfigurable. The remaining pins are all general purpose 3V3 pins, meaning outputs are set to 3V3 and inputs are 3V3-tolerant.
- Outputs – A GPIO pin designated as an output pin can be set to high (3V3) or low (0V).
- Inputs – A GPIO pin designated as an input pin can be read as high (3V3) or low (0V). This is made easier with the use of internal pull-up or pull-down resistors. Pins GPIO2 and GPIO3 have fixed pull-up resistors, but for other pins this can be configured in software.
As well as simple input and output devices, the GPIO pins can be used with a variety of alternative functions, some are available on all pins, others on specific pins.
- PWM (pulse-width modulation)
- Software PWM available on all pins
- Hardware PWM available on GPIO12, GPIO13, GPIO18, GPIO19
- SPI0: MOSI (GPIO10); MISO (GPIO9); SCLK (GPIO11); CE0 (GPIO8), CE1 (GPIO7)
- SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK (GPIO21); CE0 (GPIO18); CE1 (GPIO17); CE2 (GPIO16)
- Data: (GPIO2); Clock (GPIO3)
- EEPROM Data: (GPIO0); EEPROM Clock (GPIO1)
- TX (GPIO14); RX (GPIO15)
Checkout an interactive pinout diagram for the Raspberry Pi at – https://pinout.xyz/
Let’s put together the circuit
- Our next tutorial is called, “Multiple blinking LED lights”.
- We will start off this tutorial by first putting together the circuit using a breadboard.
- We will wire up 8 LED’s, connect it up to the Raspberry Pi through 8 x 220 Ohm resistors, power it up and write some python code.
We use a resistor to limit the current flowing through the LED. In this lesson, we will learn how to program Raspberry Pi to make 8 LED’s blink and use code to trigger off the LEDs (on/off) in different patterns. Now, let’s get started. Components required for this tutorial include –
- 1 Raspberry Pi
- 1 Breadboard
- 1 Raspberry Pi Cobbler / T-Extension Board
- 1 40-Pin Cable to connect the Raspberry Pi Cobbler / T-Extension Board to the Raspberry Pi
- 8 LEDs
- 8 Resistors (220Ω)
- Dupont jumper wires
Lets wire up your circuit based on the fritzing diagram below.
PLEASE NOTE – Please make sure you have disconnected your breadboard from the Raspberry Pi before commencing build of the circuit. Once you have put the circuit together, get someone around you to review the circuit and confirm that the connections are proper before you proceed and power up the breadboard.
In this experiment, we have connected 8 x 220Ω resistors to the anode (the longer leg of the LED is the Anode and the shorter leg of the LED is the Cathode) of each of the 8 x LED’s, followed by the other end of each of the resistors to our 3.3 V power source on the breadboard (using the Raspberry Pi Cobbler / T-Extension Board) and finally connected the connect the cathode (the shorter leg) of the each of the LED to pins (B17, B18, B27, B22, B23, B24, B25, and B4 respectively) on the Raspberry Pi. The fritzing diagram below provides a view of all of the connections that need to be made for this tutorial.
To summarize our connections look like the following –
- Setup the Raspberry Pi Cobbler / T-Extension Board on the Breadboard.
- See the fritzing diagram below to understand what a suitable setup might look like.
- Note that one pair of the Ground/3.3V connects to the upper two (horizontally connected) rows of the breadboard.
- The second pair of the Ground/5V connects to the lower two (horizontally connected) rows of the breadboard.
- Wire up each of the 220Ω resistors to the anode (the longer leg of the LED is the Anode and the shorter leg of the LED is the Cathode) of the LED
- Wire up the other end of the resistors to our 3.3 V power source on the breadboard (using the Raspberry Pi Cobbler / T-Extension Board)
- Wire up the cathode (the shorter leg) of each of the LED to to pins (B17, B18, B27, B22, B23, B24, B25, and B4 respectively) on the Raspberry Pi.
How Does It Work
We can see from the schematic diagram below that each of the Anodes of the LED connects to a 220 Ohm current-limiting resistor and then to 3.3V power source (on the Raspberry Pi using the Raspberry Pi Cobbler / T-Extension Board). Therefore, to turn on an LED, we need to program the pins (B17, B18, B27, B22, B23, B24, B25, and B4 respectively) on the Raspberry Pi to be low (0 Volt). This can be achieved using a few different programming languages but in our case we will be using Python.
By varying the timing between turning the pins (B17, B18, B27, B22, B23, B24, B25, and B4 respectively) high or low you can introduce different types of lighting patterns. Why not try some interesting lighting patterns out. Build on the example given and try creating different lighting patterns.
Here’s what the real world project will look like once you have wired up all the relevant components i.e. Raspberry Pi 3B+, Raspberry Pi Cobbler / T-Extension Board, breadboard, 8 x 220 Ohm resistor and 8 x LEDs.
Let’s Write Some Python Code
Open up the Thonny editor on your Raspberry Pi and let’s start putting together some code……
#!/usr/bin/env python import RPi.GPIO as GPIO import time # Set 8 Pins for 8 LEDs. LedPins = [17, 18, 27, 22, 23, 24, 25, 4] # Define a function to print message at the beginning def print_message(): print ("========================================") print ("| 8 LEDs |") print ("| ------------------------------ |") print ("| LED0 connect to GPIO0 |") print ("| LED1 connect to GPIO1 |") print ("| LED2 connect to GPIO2 |") print ("| LED3 connect to GPIO3 |") print ("| LED4 connect to GPIO4 |") print ("| LED5 connect to GPIO5 |") print ("| LED6 connect to GPIO6 |") print ("| LED7 connect to GPIO7 |") print ("| |") print ("| Flow LED effect |") print ("| |") print ("| SunFounder|") print ("========================================\n") print 'Program is running...' print 'Please press Ctrl+C to end the program...' raw_input ("Press Enter to begin\n") # Define a setup function for some setup def setup(): # Set the GPIO modes to BCM Numbering GPIO.setmode(GPIO.BCM) # Set all LedPin's mode to output, # and initial level to High(3.3v) GPIO.setup(LedPins, GPIO.OUT, initial=GPIO.HIGH) # Define a main function for main process def main(): # Print messages print_message() leds = ['-', '-', '-', '-', '-', '-', '-', '-'] while True: # Turn LED on from left to right print "From left to right." for pin in LedPins: #print pin GPIO.output(pin, GPIO.LOW) leds[LedPins.index(pin)] = 0 # Show which led is on print leds time.sleep(0.1) GPIO.output(pin, GPIO.HIGH) leds[LedPins.index(pin)] = '-' # Show the led is off # Turn LED off from right to left print "From right to left." for pin in reversed(LedPins): #print pin GPIO.output(pin, GPIO.LOW) leds[LedPins.index(pin)] = 0 # Show which led is on print leds time.sleep(0.1) GPIO.output(pin, GPIO.HIGH) leds[LedPins.index(pin)] = '-' # Show the led is off # Define a destroy function for clean up everything after # the script finished def destroy(): # Turn off all LEDs GPIO.output(LedPins, GPIO.HIGH) # Release resource GPIO.cleanup() # If run this script directly, do: if __name__ == '__main__': setup() try: main() # When 'Ctrl+C' is pressed, the child program # destroy() will be executed. except KeyboardInterrupt: destroy()
See a PDF copy of the tutorial here – <Link>
About the Raspberry Pi
The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.
The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.
You can read more about the Raspberry Pi here – RaspberryPi.org.
- This development track is based on the Rasbperry Pi and the SunFounder Super Starter Kit v3.0 for the Raspberry Pi.
- You will need access to both the Raspberry Pi 3 B and the electronics components part of the SunFounder Super Starter Kit v3.0 for the Raspberry Pi kit to be able to work on these tutorials.
- If you haven’t purchased the Raspberry Pi 3 B yet please head over to our store and purchase one now. You can pick up the SunFounder Super Starter Kit v3.0 for the Raspberry Pi from SunFounder’s website.
- Depending on where you live you might also be able to pick up the Raspberry Pi and SunFounder Super Starter Kit v3.0 for the Raspberry Pi at your local electronics hobby store.
- You can read more about the Raspberry Pi here – RaspberryPi.org.