Raspberry Pi - LED

This tutorial instructs you how to use Raspberry Pi to control an LED. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

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
LED pinout

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!
How LED works

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

The wiring diagram between Raspberry Pi and LED

This image is created using Fritzing. Click to enlarge image

Video Tutorial