Example Code for Raspberry Pi-Digital Port LED Blink

This article provides a step-by-step guide on how to make an LED blink using a Raspberry Pi by utilizing the GPIO ports, including the necessary hardware, software setup, wiring diagram, and Python sample code to control the LED blinking.

Hardware Preparation

Software Preparation

  • Install RPi.GPIO library:pip install RPi.GPIO

Wiring Diagram

DFR0566 IO Expansion HAT for Raspberry Pi 4B/3B+ Tutorial
  • Plug a LED on the Pin27 on the expansion board.

Sample Code

import RPi.GPIO as GPIO
import time
import atexit
blinkPin=27
atexit.register(GPIO.cleanup)
GPIO.setmode(GPIO.BCM)
GPIO.setup(blinkPin,GPIO.OUT)
while True:
    GPIO.output(blinkPin,GPIO.HIGH)
    time.sleep(1)
    GPIO.output(blinkPin,GPIO.LOW)
    time.sleep(1)

Result

The LED will begin to blink on and off.

Additional Information

IO expansion board offers 28 groups (D0-D27) of digital ports that are led out via Raspberry Pi ports GPIO0~GPIO27 (BCM codes). GPIO Learning Guide.

Was this article helpful?

TOP