Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
1.1.1 Blinking LED
Introduction
In this project, we will learn how to make a blinking LED by programming. Through your settings, your LED can produce a series of interesting phenomena. Now, go for it.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
Schematic Diagram
In this experiment, connect a 220Ω resistor to the anode (the long pin of the LED), then the resistor to 3.3 V, and connect the cathode (the short pin) of the LED to GPIO17 of Raspberry Pi. Therefore, to turn on an LED, we need to make GPIO17 low (0V) level. We can get this phenomenon by programming.
Note
Pin11 refers to the 11th pin of the Raspberry Pi from left to right, and its corresponding wiringPi and BCM pin numbers are shown in the following table.
In the C language related content, we make GPIO0 equivalent to 0 in the wiringPi. Among the Python language related content, BCM 17 is 17 in the BCM column of the following table. At the same time, they are the same as the 11th pin on the Raspberry Pi, Pin 11.
T-Board Name |
physical |
wiringPi |
BCM |
GPIO17 |
Pin 11 |
0 |
17 |
Experimental Procedures
Step 1: Build the circuit.
Step 2: Go to the folder of the code and run it.
If you use a screen, you’re recommended to take the following steps.
Find 1.1.1_BlinkingLed.py and double click it to open. Now you’re in the file.
Click Run ->Run Module in the window and the following contents will appear.
To stop it from running, just click the X button on the top right to close it and then you’ll back to the code. If you modify the code, before clicking Run Module (F5) you need to save it first. Then you can see the results.
If you log into the Raspberry Pi remotely, type in the command:
cd ~/raphael-kit/python
Note
Change directory to the path of the code in this experiment via cd
.
Step 3: Run the code
sudo python3 1.1.1_BlinkingLed.py
Note
Here sudo - superuser do, and python means to run the file by Python.
After the code runs, you will see the LED flashing.
Step 4: If you want to edit the code file 1.1.1_BlinkingLed.py,
press Ctrl + C
to stop running the code. Then type the following
command to open 1.1.1_BlinkingLed.py:
nano 1.1.1_BlinkingLed.py
Note
nano is a text editor tool. The command is used to open the code file 1.1.1_BlinkingLed.py by this tool.
Press Ctrl+X
to exit. If you have modified the code, there will be a
prompt asking whether to save the changes or not. Type in Y
(save)
or N
(don’t save).
Then press Enter
to exit. Type in nano 1.1.1_BlinkingLed.py again to
see the effect after the change.
Code
The following is the program code:
Note
You can Modify/Reset/Copy/Run/Stop the code below. But before that, you need to go to source code path like raphael-kit/python
. After modifying the code, you can run it directly to see the effect.
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
LedPin = 17
def setup():
# Set the GPIO modes to BCM Numbering
GPIO.setmode(GPIO.BCM)
# Set LedPin's mode to output,and initial level to High(3.3v)
GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
# Define a main function for main process
def main():
while True:
print ('...LED ON')
# Turn on LED
GPIO.output(LedPin, GPIO.LOW)
time.sleep(0.5)
print ('LED OFF...')
# Turn off LED
GPIO.output(LedPin, GPIO.HIGH)
time.sleep(0.5)
# Define a destroy function for clean up everything after the script finished
def destroy():
# Turn off LED
GPIO.output(LedPin, GPIO.HIGH)
# Release resource
GPIO.cleanup()
# If run this script directly, do:
if __name__ == '__main__':
setup()
try:
main()
# When 'Ctrl+C' is pressed, the program destroy() will be executed.
except KeyboardInterrupt:
destroy()
Code Explanation
#!/usr/bin/env python3
When the system detects this, it will search the installation path of
python in the env setting, then call the corresponding interpreter to
complete the operation. It’s to prevent the user not installing the
python onto the /usr/bin
default path.
import RPi.GPIO as GPIO
In this way, import the RPi.GPIO library, then define a variable, GPIO to replace RPI.GPIO in the following code.
import time
Import time package, for time delay function in the following program.
LedPin = 17
LED connects to the GPIO17 of the T-shape extension board, namely, BCM 17.
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
Set LedPin’s mode to output, and initial level to High (3.3v).
There are two ways of numbering the IO pins on a Raspberry Pi within RPi.GPIO: BOARD numbers and BCM numbers. In our projects, what we use is BCM numbers. You need to set up every channel you are using as an input or an output.
GPIO.output(LedPin, GPIO.LOW)
Set GPIO17(BCM17) as 0V (low level). Since the cathode of LED is connected to GPIO17, thus the LED will light up.
time.sleep(0.5)
Delay for 0.5 second. Here, the statement is delay function in C language, the unit is second.
def destroy():
GPIO.cleanup()
Define a destroy function for clean up everything after the script finished.
if __name__ == '__main__':
setup()
try:
main()
# When 'Ctrl+C' is pressed, the program destroy() will be executed.
except KeyboardInterrupt:
destroy()
This is the general running structure of the code. When the program
starts to run, it initializes the pin by running the setup(), and then
runs the code in the main() function to set the pin to high and low
levels. When Ctrl+C
is pressed, the program,
destroy() will be executed.