Example Code for Raspberry Pi – Digital LED Light Emitting Module

Last revision 2026/01/20

This article provides a beginner-friendly introduction to controlling a digital LED light-emitting module using Raspberry Pi's GPIO pins and Python programming. It covers the necessary hardware setup, wiring instructions, and includes sample code for toggling the LED on and off, aiming to establish a solid foundation for more advanced Raspberry Pi projects.

Introduction

Let’s start our Raspberry Pi journey from lighting up an LED!

In terms of hardware, you'll learn about Raspberry Pi LED and GPIO output, which is important for later projects. In this process, you'll also come into contact with Python programming and you may find that programming isn't as complicated as you think.

Now let's use Raspberry Pi to control a digital LED light emitting module in this basic project.

Learning Contents - LED Flicker

Guide:
In the first project, we will learn about the following contents:

  1. Internal circuit resolution of digital LED light emitting module
  2. Basic uses of Thonny Python IDE
  3. Basic Python code for operating GPIO.

Hardware Preparation

Wiring Diagram

  • Connect the necessary peripherals such as the screen, power, keyboard and mouse to your Raspberry Pi.

  • Install the IO expansion board on the Raspberry Pi. And connect the LED light-emitting module to the digital port 12 of the expansion board. Then start it up.

  • By analyzing the LED light-emitting module circuit, we can see that the signal pin is directly connected to the base of an NPN transistor, whose collector and emitter are respectively connected to the negative electrode of the LED and the negative electrode of the power supply, and the positive electrode of the LED is connected to a current-limiting resistor and then connects to VCC. That is to say, when the signal pin appears at a high level, the collector and emitter of the transistor are connected, then the circuit forms a loop, lightening up the LED. When the signal pin is in low level, the LED will be off.

  • Open Thonny Python IDE to copy the following program into it

Sample Code

import RPi.GPIO as GPIO   # Import the python module provided by the Raspberry Pi
import time      # Import time package to control flicker

LED=12     # Define the pin number to which the LED is connected

  GPIO.setmode(GPIO.BCM)      # Set GPIO mode, BCM mode is universally available to all Raspberry Pi
  GPIO.setup(LED,GPIO.OUT)     # Set GPIO12 to output mode
  while True:             # Execute the following commands in an infinite loop
    GPIO.output(LED,GPIO.HIGH)    # Set the LED signal pin high (i.e. turn on the LED)    
time.sleep(1)       # Delay one second
    GPIO.output(LED,GPIO.LOW)     # Set the LED signal pin low (i.e. turn off the LED)
    time.sleep(1)       # Delay one second

  • Click ‘Save’ to set the file name and the path you like to save

  • Click ‘Run’, then you can see the LED is flickering

Was this article helpful?

TOP