In this tutorial I am going to show you how to turn on and off an LED using a Raspberry Pi (Model B in this case), with a simple for loop in Python language.
Parts Required
In addition to your Raspberry Pi running Raspbian, you will need:
- A Breadboard
- An LED
- A 330 ohm resistor
- Two Male-Female jumper wires
The Raspberry Pi’s GPIO Pins
The left image below shows the pin layout for a Raspberry Pi Models A and B (Rev 2). The right image shows the pin layout for a Raspberry Pi Models A+, B+, Raspberry Pi 2 and 3. The new 40 pin Raspberry Pi’s shares exactly the same layout of pins for the top 13 rows of GPIO pins.
Building the Circuit and Schematic
The circuit consists of a power supply (the Raspberry Pi), an LED and a resistor to limit the current that can flow through the circuit. You will be using one of the ground (GND) pins and a digital output provided by GPIO 17 (pin 11). When you put the digital output at high state, that means it outputs 3.3 volts, and the LED will light. Now, take a look at the circuit schematic below:
Code
Step 1 – Turn on your Raspberry Pi and open the terminal window.
Step 2 – Create a new text file “LED.py”.
Step 3 – Type in the following code to turn on and off the LED one time:
Once you have typed all the code and checked it, save and exit the text editor with “Ctrl + x” then “y” then “enter”.
Step 4 – Run the code by type the following command:
Now you will see the LED turn on for a second and then turn off.
If you want to turn on and off the LED multiple times you can use the next code with a for loop.
Normally I do not learn post on blogs, but I wish to say that this write-up very compelled me to try and do so! Your writing taste has been surprised me. Thanks, very great article.