Morse Code on an LED
This tutorial will guide you through safely connecting up an LED to your Raspberry Pi and being able to turn it on and off from Python. Then you will write a program to take input from the keyboard and send it out in Morse code from the LED.
If you are using one of our SD cards or images the necessary tools should already be included; if not you will need to download and install the Python RPi.GPIO library.
Plug your ribbon cable into your breakout board and the breakout board into your breadboard. Plug the other end of the ribbon cable into your Raspberry Pi, being careful to ensure that the red stripe corresponding to pin 1 is on the correct side (it should be at the edge of the board).
Wire up the LED through the 220Ω current-limiting resistor to a pin of your choice, observing the polarity of the LED. (The flat side of the LED should be connected to the ground pin, and the other side should be connected to the GPIO pin.)
If you wish to drive a much larger LED or another device which draws more power you will need to use a transistor so as not to burn out the Pi’s pin.
Now that our circuit is wired up correctly (you can connect to 3.3V instead of the GPIO pin to test it’s working), it’s time to get programming!
to get a Python console running as root – this is necessary as GPIO pin access isn’t available in userspace.
import RPi.GPIO as GPIO import time pinNum = 8 GPIO.setmode(GPIO.BCM) #numbering scheme that corresponds to breakout board and pin layout GPIO.setup(pinNum,GPIO.OUT) #replace pinNum with whatever pin you used, this sets up that pin as an output #set LED to flash forever while True: GPIO.output(pinNum,GPIO.HIGH) time.sleep(0.5) GPIO.output(pinNum,GPIO.LOW) time.sleep(0.5)
Your LED should now be flashing: success! You can now control your LED from Python. You can stop the flashing by pressing Ctrl-C.
Now let’s write a program to translate your input to LED Morse code.
For more detail: Morse Code on an LED