Page 66 - Computing Book 8
P. 66
Programming Robots and Single-Board Computers Class 8
First Python Program with Raspberry Pi:
One of the classic electronic analogy to “Hello World” is to make an LED blink. It is one of the
very basic tutorials to begin with. To get started, you first of all need
to build the circuit on the breadboard; a board for electronic
prototyping.
Things we need to complete this project are:
• 1 Breadboard
• 1 LED
• 2 Jumper Wires
• 1 Resistor: 220Ω/1KΩ.
The circuitry of LED blink is pretty simple, you just have to
connect the electronic component with raspberry pi properly
as shown in the picture.
Connect resistor through jumper wire (represented in black
colour) with PIN 6.
Connect the positive leg of the LED with PIN 12 (GPIO 18).
After completing the circuitry, it’s time to move on to Raspberry Pi. Turn on the Raspberry Pi and
follow the steps below:
On the desktop, go the start menu and click on python IDLE and enter the following code:
Code: (only enter this) Function of the code (no need to enter this)
import RPi.GPIO as IO → # calling header file for GPIO’s of PI.
import time → # calling for time library to use sleep command for delays in program
IO.setmode (IO.BCM) → # programming the GPIO by BCM
IO.setup(18,IO.OUT) → # initialize digital pin as an output.
IO.output(18,1) → # turn the LED on (making the voltage level HIGH)
time.sleep(1) → # sleep for a second
IO.output(18,0) → # turn the LED off (making all the output pins LOW)
time.sleep(1) → # sleep for a second
IO.output(18,1)
time.sleep(1)
IO.output(18,0)
time.sleep(1)
IO.output(18,1)
time.sleep(1)
IO.output(18,0)
time.sleep(1)
BCM stands for Broadcom SOC channel. These pin numbers follow the lower-level numbering
system defined by the Raspberry Pi’s Broadcom System on Chip (SOC) brain.
The above program will turn the LED on and off thrice with a one second delay.
Resistor: The resistor is a passive device that controls or resists the flow of
current to your device, for example if we don’t use the resistor in the above
experiment, the LED may allow too much current to flow which can damage
both the LED and the Pi.
The City School /Academics/Computing Curriculum/Class 8/2020-2021 Page 65 of 75

