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.

1 Answer 1

8

In the terminal type:

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

If you are using Python 3.x run these commands.

sudo apt update
sudo apt upgrade
sudo apt install python3-pip python3-dev
pip3 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.

4
  • 1
    why sudo apt-get upgrade? seems like that is completely unnecessary
    – knocte
    Aug 27, 2016 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, 2016 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, 2019 at 9:56
  • RPi.GPIO Python package provides a class to control the GPIO on a Raspberry Pi.
    – karel
    Nov 5, 2019 at 10:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .