Raspberry Pi GPIO Emulator

This Raspberry Pi emulator simulates some of the functions used in the RPi.GPIO library (using python). The intention of this library is educational.

GPIO_Emulator

Installation

The easiest way is to download the zip file and extract the files in the same working environment of your script. To use the emulator just type the following at the beginning of your script.

from EmulatorGUI import GPIO

Simulation

This library simulates the following functions which are used in the RPi.GPIO library.

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

Test Example


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()

Download

Click here to download

31 thoughts on “Raspberry Pi GPIO Emulator

  1. This is extremely helpful and works with beautiful ease! No more than 30 seconds to get set up, clear indication of the state of the GPIOs (laid out to reflect the physical pins) and simple click-control of the input pins. Really nicely done, thank you!

    Liked by 1 person

  2. Wonderful! but I am a beginner. still I could not run the emulator. Are there any requirements are needed. I tried to run it windows as well as in raspberry itself. My python version is 2.7. looking for your assist.

    Like

    • It is implemented but it does nothing so it’s not going to give you any exceptions. Normally GPIO.Cleanup is used at the end of the script so for the emulator you simply restart your script and cleanup is done automatically.

      Like

      • But if I wish to run tests which repeatedly set up the pins and then check that the outputs are changing as expected I get exceptions as the pins are already assigned. I have altered the code to allow me to do this, but I am trying to see if there is a way of closing the window from the code. Is there an easy way to do this? Thanks!

        Like

  3. It is highly recommended to setup your pins at the start of the script and keep them set the same through the whole script. Why do you want your script to close the simulator window?

    Like

  4. Hi,

    Nice code indeed, very useful, I’m using it to debug a multi on/off sensor setup. I wonder if you could make buttons stick to “pressed” or “released” just with 1 click, instead of having to keep it down (which only lets you make 1 change at a time).

    Good work 🙂

    Like

  5. Thanks. I found it to be pretty handy for getting started writing the code on my laptop instead of having to do it on the Rpi. I need to make event-driven code now, instead of polling, and I realized that you’ve only emulated a small subset of the GPIO functions. Do you have any intentions of adding GPIO.add_event_detect() and similar?

    Like

  6. Hi,
    I work with Windows 7 and Python 3.1.2 .
    When I try the file “tester2.py”, I have :
    Traceback (most recent call last):
    File “C:\Users\François\Desktop\GPIOEmulator\tester2.py”, line 1, in
    File “C:\Users\François\Desktop\GPIOEmulator\EmulatorGUI.py”, line 4, in
    from TypeChecker import typeassert
    File “C:\Users\François\Desktop\GPIOEmulator\TypeChecker.py”, line 1, in
    from inspect import signature
    ImportError: cannot import name signature

    Have you a solution ?
    Thank you
    François

    Like

  7. Pingback: Emulating Raspberry Pi – Lamp of Socrates

Leave a comment