12

I am running ubuntu on a raspberry pi and I cannot figure out how to activate the GPIO pins. I am trying to activate a python script with a button press.

Here is my script:

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    if input_state == False:
    os.system('python backup.py')
        time.sleep(0.2)

GPIO.cleanup()

When I run this I get an error that says; no such import as import RPi.GPIO as GPIO

Any help would be appreciated.

8

In the terminal type:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip python-dev
pip install --user RPi.GPIO  

The raspberry-gpio-python examples are worth reading. In the Inputs example there is this code snippet:

while GPIO.input(channel) == GPIO.LOW:
    time.sleep(0.01)

It waits 10 ms to give CPU a chance to do other things.

| improve this answer | |
  • 1
    why sudo apt-get upgrade? seems like that is completely unnecessary – knocte Aug 27 '16 at 22:06
  • 1
    Because python-pip is a third-party package manager and therefore inherently less stable than using apt to install software from the default repositories, I would prefer to update all the other software before installing pip. – karel Aug 28 '16 at 0:32
  • does this add the /dev/gpio device as seen in the raspbian kernel or this this a python workaround? – FalcoGer Nov 5 '19 at 9:56
  • RPi.GPIO Python package provides a class to control the GPIO on a Raspberry Pi. – karel Nov 5 '19 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.