Raspberry Pi GPIO Emulator

When developing Raspberry Pi GPIO projects on a laptop or desktop, it is useful to replace RPi.GPIO with a small emulator. This lets you test import paths, control flow, and output logic before running the code on the actual Pi.

Method 1: Use a simple local stub

Create a file named testRPiGPIO.py in your project directory, then import it instead of RPi.GPIO while testing:

import testRPiGPIO as GPIO

A minimal stub can look like this:

#!/usr/bin/python

BOARD = "board"
BCM = "bcm"
OUT = "out"
IN = "in"
HIGH = 1
LOW = 0


def setwarnings(mode):
    print(mode)


def output(pin, value):
    print(pin, ":", value)


def setmode(mode):
    print(mode)


def setup(pin, value):
    print(pin, ":", value)


def cleanup():
    print("clean-up")

# End

This method is enough for very simple scripts that only call setmode(), setwarnings(), setup(), output(), and cleanup(). If your program reads input pins, uses pull-up or pull-down constants, or depends on PWM or event detection, extend the stub or use a fuller emulator.

Method 2: Use a GPIO emulator package

Download Pi GPIO Emulator and extract it into your project folder. Then import it with:

from EmulatorGUI import GPIO

If you want to install a simulator with pip, use:

pip install GPIOSimulator

Then import it in your project with:

from RPiSim.GPIO import GPIO

Because package names and import paths can change over time, verify the installed package locally with pip show GPIOSimulator and check the package documentation if the import fails.

Supported methods include:

  • GPIO.setmode()
  • GPIO.setwarnings()
  • GPIO.setup()
  • GPIO.input()
  • GPIO.output()

Example

The following example uses the emulator import. On a real Raspberry Pi, replace it with import RPi.GPIO as GPIO.

from EmulatorGUI import GPIO
# import RPi.GPIO as GPIO
import time
import traceback


def Main():
    try:
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)

        GPIO.setup(4, GPIO.OUT)
        GPIO.setup(17, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(18, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(21, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(15, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        GPIO.setup(26, GPIO.IN)

        while True:
            if GPIO.input(23) == False:
                GPIO.output(4, GPIO.HIGH)
                GPIO.output(17, GPIO.HIGH)
                time.sleep(1)

            if GPIO.input(15) == True:
                GPIO.output(18, GPIO.HIGH)
                GPIO.output(21, GPIO.HIGH)
                time.sleep(1)

            if GPIO.input(24) == True:
                GPIO.output(18, GPIO.LOW)
                GPIO.output(21, GPIO.LOW)
                time.sleep(1)

            if GPIO.input(26) == True:
                GPIO.output(4, GPIO.LOW)
                GPIO.output(17, GPIO.LOW)
                time.sleep(1)

    except Exception as ex:
        traceback.print_exc()
    finally:
        GPIO.cleanup()  # This ensures a clean exit.


Main()

For real hardware testing, switch back to the official GPIO library on the Raspberry Pi:

import RPi.GPIO as GPIO

Run the emulator version first to check the control logic, then run the hardware version on the Pi to verify pin numbering, wiring, pull-up or pull-down behavior, and voltage output.

Leave a Reply