Short: What do you need to use the pins on your Pi (RPi.GPIO).
Besides your Raspberry you're going to use an LED and a button. I keep that tutorial short and will only show the very basics. Everything else will be shown in different tutorials.
|1||Raspberry Pi||B 2|
The wiring can be done according to the following shematic and the table.
|GND (Pin 6)||Cathode|
|3V3 (Pin 1)||Pin 1|
|Pin 18||Pin 2|
The button does not be connected to the 3.3V pin. That's due to the logic within the following code: I use Input-Pullup. Once you have set up everything propperly, you can head on.
As I mentioned I use RPi.GPIO to talk to the pins. Get it and get the python-dev-tools too.
sudo apt-get install update sudo apt-get install python-dev python-rpi.gpio
RPi-GPIO comes pre-installed with Jessie, but an update is never wrong.
That time not on GitHub, but here:
import RPi.GPIO as gpio ledPin = 17 buttonPin = 18 def init(): gpio.setmode(gpio.BCM) gpio.setup(ledPin, gpio.OUT) gpio.setup(buttonPin, gpio.IN, pull_up_down = gpio.PUD_UP) gpio.add_event_detect(buttonPin, gpio.RISING, callback = buttonRising, bouncetime = 300) gpio.add_event_detect(buttonPin, gpio.FALLING, callback = buttonFalling, bouncetime = 300) gpio.output(ledPin, gpio.LOW) def buttonRising(channel): gpio.output(ledPin, gpio.HIGH) def buttonFalling(channel): gpio.output(ledPin, gpio.LOW) if __name__ == "__main__": init() while (true): pass
Create a file on your Pi named simpleGPIO.py and copy the content from above into it. Start the script:
sudo python simpleGPIO.py
Yes, you have to run it as super user. The reason is that the file where the pins are managed needs some rights to be edited.
The script starts at the bottom. It calls the init()-function. Here I set the category to BCM (alternate BOARD exists)
- BCM: The pins are named after the Broadcom SOC channel. This means the pin number in the code is the same as the number after the keyword GPIO.
- BOARD: The pins are named after the numbers inside the circle.
Attention: The numbers accourding to BCM have changed after Model B.
After that I set both pins depending on in- or output. I add an event to the button pin. That way I can call a specific function every time an rising or an falling edge is recognized.
And that's it. Super quick. Questions? Hit me!