# Using RPi.GPIO with the inkyphat library

**URL:** https://forums.pimoroni.com/t/using-rpi-gpio-with-the-inkyphat-library/8013
**Category:** Support
**Created:** [June 25, 2018, 12:36pm UTC](https://forums.pimoroni.com/t/using-rpi-gpio-with-the-inkyphat-library/8013 "2018-06-25T12:36:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![citlee](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@citlee](https://forums.pimoroni.com/u/citlee)
#### Post date: [June 25, 2018, 12:36pm UTC](https://forums.pimoroni.com/t/using-rpi-gpio-with-the-inkyphat-library/8013/1 "2018-06-25T12:36:21Z")

</div>

Hi,

I am using an inkyphat display with the [inkyphat library](https://github.com/pimoroni/inky-phat) installed via `sudo apt-get install python3-inkyphat` on a fresh install of the latest Raspbian Lite (2018-04-18 Stretch) on a Pi Zero W. I am intending to attach buttons, and update the display depending on what was pressed. I’m trying to use RPi.GPIO with the inkyphat library, but I’m having no luck. I’ve read the docs and looked at all the examples, neither of which mention GPIO, and looking at the library source shows that it is also using RPi.GPIO. Unfortunately I don’t know enough Python to go any further - I don’t know if it’s a quirk of the libraries or, more likely, simply me not knowing something obvious. I couldn’t find anything helpful via Google either, which is unusual.

It appears as though the inkyphat library is setting the GPIO mode to BCM (11), so it gives me an error when I try to set it myself, which I have to do in order to use the pins. I pared the script down to the absolute basics:

```
pi@raspberrypi:~ $ cat test.py
#!/usr/bin/env python3
import inkyphat
import RPi.GPIO as GPIO
print (GPIO.getmode())
#GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

pi@raspberrypi:~ $ ./test.py
11
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 132, in _display_exit
    self._display_fini()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 230, in _v1_fini
    self._busy_wait()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 378, in _busy_wait
    while(GPIO.input(self.busy_pin) != wait_for):
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

```

… which is fair enough. However:

```
pi@raspberrypi:~ $ cat test.py
#!/usr/bin/env python3
import inkyphat
import RPi.GPIO as GPIO
print (GPIO.getmode())
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

pi@raspberrypi:~ $ ./test.py
11
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 132, in _display_exit
    self._display_fini()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 230, in _v1_fini
    self._busy_wait()
  File "/usr/lib/python3/dist-packages/inkyphat/inky212x104.py", line 378, in _busy_wait
    while(GPIO.input(self.busy_pin) != wait_for):
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

```

What’s the correct way to be able to use RPi.GPIO with inkyphat?

Thanks,  
Lee

---

<div class="post-metadata">

### Author: ![gadgetoid](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/gadgetoid/32/13_2.png) [@gadgetoid](https://forums.pimoroni.com/u/gadgetoid)
#### Post date: [June 27, 2018, 10:56am UTC](https://forums.pimoroni.com/t/using-rpi-gpio-with-the-inkyphat-library/8013/2 "2018-06-27T10:56:15Z")

</div>

Don’t call `GPIO.cleanup()` you’re triggering the error by trashing the GPIO while `inkyphat` is still using it.

What’s happening:

1. You import inkyphat, it sets up the GPIO (BCM mode to be precise)
2. You import RPi.GPIO, this does nothing, it’s already imported so you just get a reference to it
3. You `GPIO.setmode(GPIO.BCM)` which also does nothing because it’s already set
4. You `GPIO.cleanup()` which cleans up the global state of the RPi.GPIO library (including that within the Inky library)
5. The script exits, triggering the \_atexit handler (Note the `Error in atexit._run_exitfuncs:`)
6. The exit handler tries to use the GPIO, but you’ve run `GPIO.cleanup()` so it’s reset back to its default state and baulks

Arguably the inky library should call that itself, here:

```auto
    def _v1_fini(self):
        self._busy_wait()
        self._send_command(_VCOM_DATA_INTERVAL_SETTING, [0x00])
        self._send_command(_POWER_SETTING, [0x02, 0x00, 0x00, 0x00])
        self._send_command(_POWER_OFF)

```

---

<div class="post-metadata">

### Author: ![citlee](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@citlee](https://forums.pimoroni.com/u/citlee)
#### Post date: [June 29, 2018, 9:13am UTC](https://forums.pimoroni.com/t/using-rpi-gpio-with-the-inkyphat-library/8013/3 "2018-06-29T09:13:55Z")

</div>

Well, that was obvious, and never occurred to me. Thanks for sorting that out!
