Raspberry Pi - LED
This tutorial instructs you how to use Raspberry Pi to control an LED. In detail, we will learn:
- How to connect the LED to Raspberry Pi
- How to program Raspberry Pi to turn the LED on or off.
- How to program Raspberry Pi to blink the LED.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of LED
The LED Pinout
LED has two pins:
- The Cathode(-) pin: should be connected to the negative of power supply
- The Anode(+) pin: should be connected to the positive of power supply via a resistor
How It Works
The below table shows the LED state according to how the power connects to LED's pin
LED cathode(-) pin | LED anode(+) pin | Condition | LED state |
---|---|---|---|
GND | VCC | via a resistor | ON |
GND | PWM | via a resistor | ON, variable brightness |
GND | GND | any | OFF |
VCC | VCC | any | OFF |
VCC | GND | any | burned! cautious! |
As shown on the above table, by generating a PWM signal to the anode (+) of an LED, the brightness of the LED varies in accordance with the PWM value. This has been explained in detail in Arduino Nano fade LED tutorial.
※ NOTE THAT:
- For most of LED, a resistor is required to protect LED from the current. There are two options to place the resistor: between the anode(+) and VCC, or between the cathode(-) and GND. The value of the resistor depends on the specification of the LED.
- Some kinds of LEDs have a built-in resistor. In this case, the resistor is not required.
Raspberry Pi - LED
When a Raspberry Pi's pin is set up as a digital output, it is possible to program the pin's voltage to be either GND or VCC. Connect the Raspberry Pi's pin to the anode(+) pin of the LED using a resistor. This will enable us to control the state of the LED through programming.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:
How To Program Raspberry Pi to blink LED
The below provides instruction how to write Raspberry Pi code in Python to blink the LED.
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file led_blink.py and add the following code:
- Save the file and run the Python script by executing the following command in the terminal:
- Check out the LED state. You should see the LED blink with one-second intervals.
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
Code Explanation
Check out the line-by-line explanation contained in the comments of the source code!
※ NOTE THAT:
The code above utilizes time.sleep(). This function prevents Raspberry Pi from executing other tasks while the delay is occurring. If your project requires performing multiple tasks, avoid blocking Raspberry Pi by utilizing the non-blocking method for Raspberry Pi.