Raspberry_Pi_Education_Manual

Notes:

LED circuit

Now we know all about data and the Raspberry Pi’s input/output options, let’s get on with building something!

This is about the simplest circuit you can build to test the GPIO outputs of a Raspberry Pi. This circuit contains just two components: a 1k resistor and an LED (Light Emitting Diode). The resistor is used to limit the current that flows out of the Raspberry Pi and into the LED. If there is too much current, you could break something!

LED circuit experiment wiring diagram.

Tip...

Please be careful: If you use too much current then you could

easily break something!

Note that you need to connect the LED up the correct way round. The flat side of the LED denotes the negative side; the longer leg denotes the positive side of the LED. The Gertboard already has LEDs wired up exactly like this on some channels. The following Python program will let you switch the LED on and off. To complete this exercise you will need the Raspberry Pi GPIO modules. You can either install these from the Raspberry Pi SD card or download them from http://pypi.python.org/pypi/RPi.GPIO/

import RPi.GPIO as GPIO # set up pin 11 to output GPIO.setup(11, GPIO.OUT) state = False while 1: GPIO.output(11, state) command = input ( "Press return to switch the LED on/off or 'Q' to quit: " ) if command.strip().upper().startswith( 'Q' ): break state = not state

Note that this Python script must be run with superuser privileges (as root). You can do this by running your program from the command line and putting “sudo” in front of the command you are typing. For example: “sudo python led.py”.

Human-computer interfacing

131

Made with FlippingBook flipbook maker