Ubuntu – GPIO on raspberry pi

raspberrypi

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.

Best Answer

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.

Related Question