Raspberry Pi Digital Output: Turn an LED On and Off

Photo of author

By Jackson Taylor

Home » Electronics Topics » Raspberry Pi » Raspberry Pi Digital Output: Turn an LED On and Off

As an Amazon Associate we earn from qualifying purchases.

A digital output is the simplest thing a Raspberry Pi can do with its GPIO header: set a pin to high (3.3 V) or low (0 V). Connect an LED to that pin and you can switch it on and off from a Python script. This guide shows the wiring and the code to do it on physical pin 40, and explains what each line does.

  • The examples use the Raspberry Pi physical pin numbering (the position of the pin on the 40-pin header), not the BCM/GPIO numbers.
  • Physical pin 40 is GPIO 21 in BCM numbering on boards with the 40-pin header.

What you need

Wiring the LED to pin 40

Never connect an LED straight to a GPIO pin. Without a resistor, too much current can flow and damage the LED or the Pi. Wire it like this:

  1. Connect physical pin 40 to one end of the resistor.
  2. Connect the other end of the resistor to the longer leg (anode) of the LED.
  3. Connect the shorter leg (cathode) of the LED to a ground pin. Physical pin 39 is a ground pin right next to pin 40, which makes the wiring easy.

Power the Pi off while you connect the wires, and check the pin positions twice before powering it back on. Counting the header from the wrong end is a common mistake.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

Turn on the LED on pin 40

This script sets pin 40 as an output and writes a high value to it, which lights the LED.

import RPi.GPIO as gpio         # imports the RPi.GPIO library
gpio.setmode(gpio.BOARD)         # use the physical pin numbers on the board
gpio.setwarnings(False)          # disable warnings
gpio.setup(40,gpio.OUT)          # set pin 40 as an output
gpio.output(40,1)                # writes a digital 1 (high) to it

Turn off the LED on pin 40

The only change is the value written to the pin. Writing 0 sets the pin low, and the LED goes off.

import RPi.GPIO as gpio         # imports the RPi.GPIO library
gpio.setmode(gpio.BOARD)         # use the physical pin numbers on the board
gpio.setwarnings(False)          # disable warnings
gpio.setup(40,gpio.OUT)          # set pin 40 as an output
gpio.output(40,0)                # writes a digital 0 (low) to it

Save each script as a .py file and run it with python3 filename.py from the terminal.

See also
Building Automation System using TEMPERATURE SENSOR AND GAS SENSOR SIMULATION IN PROTEUS

How the code works

Importing the library

The first line imports RPi.GPIO and gives it the short name gpio, so you can write gpio.output() instead of the full module name. The original comment called this a “header file”, but in Python it is simply a module import.

Choosing the numbering mode

gpio.setmode(gpio.BOARD) tells the library that the numbers you use match the physical pin positions on the header. The alternative is gpio.BCM, which uses the chip’s GPIO numbers. In that mode the same pin would be called 21. Pick one mode per script and stick with it.

Disabling warnings

gpio.setwarnings(False) hides the message RPi.GPIO prints when a pin was already configured by an earlier run. It is handy while testing, but it can also hide real mistakes, so remove it if you are debugging pin conflicts.

Setting the direction and writing a value

gpio.setup(40, gpio.OUT) makes the pin an output. gpio.output(40, 1) drives it high, and gpio.output(40, 0) drives it low. You can also write gpio.HIGH and gpio.LOW, which are easier to read.

One script for on and off: a blinking LED

Instead of keeping two files, you can put both states in one script. This example blinks the LED five times and then releases the pin.

import time
import RPi.GPIO as gpio

gpio.setmode(gpio.BOARD)
gpio.setup(40, gpio.OUT)

try:
    for i in range(5):
        gpio.output(40, gpio.HIGH)   # LED on
        time.sleep(1)
        gpio.output(40, gpio.LOW)    # LED off
        time.sleep(1)
finally:
    gpio.cleanup()                   # release the pins

The try/finally block makes sure gpio.cleanup() runs even if you stop the script with Ctrl+C. Cleanup resets the pins you used to their default input state, which is safer than leaving them driven.

Troubleshooting

  • The LED does not light: check that the LED is the right way around. The longer leg must face the resistor and pin 40.
  • It is always on or always off: make sure you are using the same numbering mode as your pin number. Pin 40 in BOARD mode is not pin 40 in BCM mode.
  • The LED is very dim: the resistor value may be too high. Try a lower value, but do not go without one.
  • Permission or import errors: confirm you are running Python 3 on Raspberry Pi OS and that RPi.GPIO is installed.
  • It does not work on a Raspberry Pi 5: the original RPi.GPIO library does not support that board. Use a compatible replacement such as rpi-lgpio, or the gpiozero library.
See also
DIGITAL OUTPUT - ARM M4 TIVA C SERIES TM4C123G - ENERGIA

Where to go next

Once an LED works, the same pattern drives relays, buzzers and transistor circuits, as long as you respect the Pi’s current limits. For more ideas, see these Raspberry Pi Python projects. If you work on the Pi without a monitor, learn how to connect your Raspberry Pi to a laptop and run these scripts over SSH.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

If you are comparing platforms, the same idea exists on other boards. See how it looks for digital output on an MSP430, digital output on an 8051, or PIC16F877A digital output.

FAQ

Which pin is physical pin 40 on the Raspberry Pi?

It is the last pin on the outer row of the 40-pin header, and it is GPIO 21 in BCM numbering. The pin next to it, pin 39, is a ground pin.

Do I need a resistor with an LED on a GPIO pin?

Yes. The resistor limits the current through the LED and protects both the LED and the Pi. A value in the hundreds of ohms is typical for a standard LED.

What is the difference between BOARD and BCM mode?

BOARD uses the physical position of each pin on the header. BCM uses the numbers assigned to the GPIO channels on the Broadcom chip. They are different numbering schemes for the same pins, so use only one in a script.

Does the LED stay on after the script ends?

It can. If you do not call gpio.cleanup(), the pin is often left in the state you set. Call cleanup at the end of your script if you want the pins reset.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works