concept led in category raspberry pi

appears as: LEDs, n LED, LEDs, LED, LED
Hello Raspberry Pi!: Python programming for kids and other beginners

This is an excerpt from Manning's book Hello Raspberry Pi!: Python programming for kids and other beginners.

In this chapter, you’ll set up your Pi to control small light bulbs called light-emitting diodes (LEDs). You’ll make the LEDs blink using Python. To do this, you’ll need to learn a bit about how to build electrical circuits on breadboards. If you’ve never heard of a breadboard, don’t worry! It’s a small board with lots of holes in it to make it easier to build electrical circuits. You’ll also be using short wires (called jumper wires) to connect certain holes. You’ll even learn how to add resistors that keep your LEDs from burning out. See figure 6.1 for a list of parts and what they look like; gather the parts, and let’s get started!

It’s time to connect the red LED. LEDs only let electricity flow through them one way, so it’s important to put them in the right way. LEDs have two wires or legs. The longer leg is called the anode and connects to the positive side of the circuit (see figure 6.12). The shorter leg, called the cathode, connects to the negative or ground side of the circuit.

Figure 6.12. LEDs have two legs (wires) coming out of them. The longer leg is called the anode and connects to the positive side of the circuit. The shorter one is called the cathode and connects to the negative side of a circuit.

Finally, you create an infinite while loop and turn the LED on (set GPIO.HIGH) and off (set GPIO.LOW). You also add a delay using the sleep method found in Python’s time library. Notice how the sleep function takes a parameter that is the number of seconds to sleep or pause. In this case, you use 1 second:

while True:
    GPIO.output(LED_pin_red, GPIO.HIGH)
    print("On")
    time.sleep(1)
    GPIO.output(LED_pin_red, GPIO.LOW)
    print("Off")
    time.sleep(1)

The print commands display messages to the screen. Although they aren’t necessary to blink the LED, they can help debug your program. If you do use them, the screen could quickly fill with messages. Set a longer delay time to prevent this. If you see the messages on the screen but your LED isn’t lighting up, then you probably have an error in your circuit and not in your program. Check your wiring, try turning around the LED, or try a different LED in case that one is defective.