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×220 ohm resistor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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
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.


  • 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


  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!