If you code in Python, the RPi.GPIO Python library (included with Raspbian) lets you configure, read, and write to GPIO pins.
sudo apt-get install RPi.GPIO
If the library’s already installed, you’ll get a “…is already the newest version” message.
Otherwise, the package will download and install.
We’ve created a Python script that shows how to read and write with the RPi.GPIO library.
If you’re using a browser, click here for the script.
To download the script to the current directory using a terminal, enter
To print the working directory, enter
To view the script, enter
The comments in the script point out where it:
- Invokes the Python interpreter
- Includes the RPi.GPIO library and sets GPIO as a local name in the script
- Sets the pin-numbering scheme for the script
GPIO.BOARDoption means that that you’re referring to the pins by their physical pin number. To refer to the pins by their GPIO pin number, use the
- Configures physical pin number 13 as input.
Note: Physical pin 13 is mapped to rack position 10.
- Reads the input point mapped to pin 13 and stores it in a variable.
- Prints the point’s value to the terminal.
- Configures physical pin number 15 as output.
Note: Physical pin 15 is mapped to rack position 11.
- Turns On the output point mapped to pin 15.
- Turns Off the output point mapped to pin 15.
To run the script:
- If necessary, modify the pin numbers in the script to correspond to I/O modules on your rack.
- Make the script executable by entering
chmod 755 PythonScript.py
- Run the script by entering
The output looks something like this: