Raspberry Pi - LED Module

A Raspberry Pi usually runs without a screen, so it is hard to tell what your Python script is doing. One bright LED on the desk can tell you a lot: the script has started, the network is down, or the CPU is getting hot. This tutorial instructs you how to use Raspberry Pi to control a 10mm LED module, which is an LED that comes with its resistor and driver already on a small board. In detail, we will learn:

Raspberry Pi LED Module

We suggest:

Hardware Preparation

1×Raspberry Pi 5
1×LED Module
1×Jumper Wires
1×Optionally, Breadboard
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

Or you can buy the following kits:

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.
Additionally, some of these links are for products from our own brand, DIYables .

Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.

Overview of LED Module

The DIYables LED module is a 10mm diffused round LED soldered on a small 20mm x 20mm black PCB. You can think of it as an LED with its resistor and driver already on board. A 330Ω resistor limits the LED current, and a small transistor switches the LED. The LED takes its power from the VCC pin, not from the Raspberry Pi GPIO pin. The IN pin only tells the transistor to turn on or off, and a 1KΩ resistor on that line keeps the GPIO current very small. This matters on a Raspberry Pi: its GPIO pins work at 3.3V and can only give a little current, but the LED still gets full brightness from VCC. The module comes in a pack of 5 colors: red, yellow, blue, green and white. It works from 3.3V to 5V DC.

LED module pinout
image source: diyables.io

The module has a 3-pin 2.54mm right-angle male header, labeled in this order:

  • GND: This pin must be connected to ground (0V) of the Raspberry Pi
  • VCC: This pin must be connected to a supply of 3.3V or 5V. On the Raspberry Pi, you can use 3.3V (pin 1) or 5V (pin 2)
  • IN: This pin must be connected to a GPIO pin of the Raspberry Pi. It controls the LED

How the IN pin controls the LED:

IN pin Python code LED state
HIGH (3.3V) GPIO.output(18, GPIO.HIGH) ON
LOW (0V) GPIO.output(18, GPIO.LOW) OFF
PWM signal pwm.ChangeDutyCycle(0 to 100) ON, brightness follows the duty cycle

The module is active HIGH. A higher PWM duty cycle keeps the LED on for a longer part of each period, so the LED looks brighter.

※ NOTE THAT:

The arrangement of pins on a module can differ between manufacturers. It is essential to always use the labels printed on the module when working with it. Be sure to take a close look!

Wiring Diagram

The wiring diagram between Raspberry Pi and LED module

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:

Raspberry Pi Screw Terminal Block Shield
  • LED module GND pin to Raspberry Pi GND (pin 14, or any other GND pin)
  • LED module VCC pin to Raspberry Pi 3.3V (pin 1) or 5V (pin 2)
  • LED module IN pin to Raspberry Pi GPIO18 (pin 12)

Is 5V on VCC safe for the Raspberry Pi? Yes. The 5V only feeds the LED through the transistor on the module. The IN pin is only an input that the GPIO pin drives, so no 5V comes back into GPIO18. Choose 5V if you want the LED a bit brighter, or 3.3V if you like to keep everything on the 3.3V rail.

The module pins plug straight into female jumper wires, so a breadboard is not required. Use one only if you want to place several modules side by side.

How To Program for Raspberry Pi to Fade LED Module

GPIO18 can output a PWM signal. With RPi.GPIO, you create it with GPIO.PWM(18, 1000) and set the brightness with pwm.ChangeDutyCycle(), from 0 (off) to 100 (full). The script below raises the duty cycle from 0 to 100 in steps of 5, then lowers it back to 0. Each step lasts 40 milliseconds, so one full fade in and fade out takes about 1.7 seconds.

Detailed Instructions

  • Keep the same wiring. The IN pin stays on GPIO18 (pin 12).
  • Create a Python script file led_module_fade.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-led-module """ import RPi.GPIO as GPIO import time # Use BCM numbering (GPIO numbers, not physical pin numbers) GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) LED_MODULE_PIN = 18 # GPIO18 (pin 12) connected to the IN pin of the LED module STEP = 5 # change the brightness by 5% on each step STEP_DELAY = 0.04 # wait 40 milliseconds between steps GPIO.setup(LED_MODULE_PIN, GPIO.OUT) pwm = GPIO.PWM(LED_MODULE_PIN, 1000) # 1000 Hz PWM signal on the IN pin pwm.start(0) # start with the LED off (0% duty cycle) try: while True: # fade in: 0% -> 100% print("Fading in...") for brightness in range(0, 101, STEP): pwm.ChangeDutyCycle(brightness) time.sleep(STEP_DELAY) print("Brightness: 100% (full)") # fade out: 100% -> 0% print("Fading out...") for brightness in range(100, -1, -STEP): pwm.ChangeDutyCycle(brightness) time.sleep(STEP_DELAY) print("Brightness: 0% (off)") except KeyboardInterrupt: print("Stopped by user") finally: # Stop the PWM and release the GPIO pin pwm.stop() GPIO.cleanup()
  • Save the file and run it with this command:
python3 led_module_fade.py
  • Watch the LED module. It slowly gets brighter, then slowly gets dimmer, like breathing.
  • Check the terminal. The script prints a line at the start and at the end of each fade:
PuTTY - Raspberry Pi
Fading in... Brightness: 100% (full) Fading out... Brightness: 0% (off) Fading in... Brightness: 100% (full) Fading out... Brightness: 0% (off)

Press Ctrl + C keys in the terminal to stop the script. It stops the PWM and cleans up the GPIO pin.

To make the fade slower, increase STEP_DELAY (for example 0.08). To make it smoother, decrease STEP (for example 2).

※ NOTE THAT:

RPi.GPIO makes PWM in software. When the Raspberry Pi is very busy, you may see a small flicker at low brightness. This is normal for a status light.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Additional Knowledge

LED Module vs Bare LED

Let's compare the LED module with a bare LED on the Raspberry Pi:

LED Module Bare LED
Resistor on the board (330Ω) you must add one (for example 220Ω)
Polarity pins are labeled GND, VCC, IN you must find the anode(+) and cathode(-)
Who powers the LED VCC, through the transistor the GPIO pin itself
Current from the GPIO pin very small the full LED current
Wires to connect 3 2, plus the resistor

Evidently, the LED module is easier and safer for a beginner, and it takes almost no current from the GPIO pin. A bare LED is cheaper and smaller, so it fits better when you build many lights on one board.

Project Ideas with the LED Module

  • Script running light: turn the LED on at the start of your Python script, so you know the headless Pi is working.
  • Network alarm: blink the red module fast when the Raspberry Pi cannot reach the internet.
  • CPU temperature warning: read the CPU temperature and turn on a yellow module when it goes too high.
  • Heartbeat light: fade a green module slowly to show the Pi is alive.
  • Traffic light model: use the red, yellow and green modules on three GPIO pins.

Video Tutorial

※ OUR MESSAGES

  • 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!