6

I'm new on Raspberry Pi and I would to know how can we simulate/mock the sensors (or GPIO) so I can test just my python code, mocking the Highs and Lows of the pins?

Thanks in advance!

André Schuster
  • 165
  • 6
  • 18
  • 1
    I would not directly access the GPIO. You can create a software layer for accessing GPIO and mock this during tests – hek2mgl Nov 11 '13 at 18:19
  • I thought about that too but if I wrote a code to calculate the distance of an object using an ultrasonic sensor, for example, would you add a layer just to read the pins (which them could be mocked)? I was thinking in something like: `when(GPIO.input(OUTPUT_PIN)).return(0)` – André Schuster Nov 11 '13 at 18:38
  • Hmm I'm not a python coder. If `GPIO` is already an object you might just mock this – hek2mgl Nov 11 '13 at 18:56

2 Answers2

2

I've made a RPi GPIO simulator to allow you to quickly test python scripts in a web browser.

Here's an example for flashing an output pin: https://create.withcode.uk/python/A5

Alternatively, there's a library that you could download to simulate the inputs / outputs offline:

https://roderickvella.wordpress.com/2016/06/28/raspberry-pi-gpio-emulator/

pddring
  • 181
  • 3
1

IF you just don't want to trust a software layer, you could try manually setting the pin high:

The GPIO pins on the Raspberry Pi work with a voltage of 3.3VDC.

Pins 1 & 17 on the P1-GPIO header outputs 3.3VDC.

You could CAREFULLY try to connect a jumper from Pin 1 or 17 to the GPIO input pin you want to test. Connect it to test high, disconnect to test for low.

MAKE SURE YOU DON"T USE THE 5VDC GPIO PINS, YOU COULD/WILL DAMAGE/KILL YOUR PI!

Deanie
  • 2,316
  • 2
  • 19
  • 35