Example Code for Raspberry Pi-Digital Output

Last revision 2026/01/06

Control a LED connected to GPIO16 (BCM) of the Raspberry Pi via the expansion board's digital port.

Hardware Preparation

  • 1x DFR0604 IO Expansion HAT for Raspberry Pi, Purchase Link
  • 1x LED
  • 1x Raspberry Pi

Software Preparation

  1. Install the RPi.GPIO library. Input the command on Raspberry Terminal:
    sudo apt-get install rpi.gpio  
    
  2. Input the following command to check if the RPi.GPIO library is installed successfully:
    gpio.readall  
    
  3. If all pins are displayed on the terminal, the library installation succeeds.

Wiring Diagram

Connect a LED to the digital port corresponding to GPIO16 (BCM) of the Raspberry Pi (labeled as 16-25 on the expansion board).

Other Preparation Work

Create a file (e.g., using nano: sudo nano digitalTest.py) and add the sample code.

Sample Code

digitalTest.py

# -*- coding:utf-8 -*-
# digitalTest.py

import RPi.GPIO as GPIO
import time

blinkPin = 16

GPIO.setmode(GPIO.BCM)

GPIO.setup(blinkPin, GPIO.OUT)  # Set GPIO16 is OUTPUT

for i in range(0, 10):
  GPIO.output(blinkPin, 1)
  time.sleep(1)
  GPIO.output(blinkPin, 0)
  time.sleep(1)
GPIO.cleanup()

Result

The LED connected to GPIO16 will blink 10 times (on for 1 second, off for 1 second).

Additional Information

The board leads out 10 GPIO (BCM encode) ports of Raspberry Pi: GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, GPIO24, GPIO25. These ports can be used directly like the Raspberry Pi's native GPIO ports.

DFR0604 IO Expansion HAT for Raspberry Pi Zero/Zero W Digital

Was this article helpful?

TOP