Raspberry Pi 2 - Simple LED Blinking Program Using Python

You can read my previous article:

Here are the steps for flashing/blinking the LED on breadboard using python.

Download Source code: LedBlinker_Python.rar

Required List of components

  1. 1 - Raspberry Pi device with 5 V power supply.
  2. 1 - Bread Board
  3. 1 - Male to Female jumper cable
  4. 1 - LED

1. Firstly, connect the GPIO pin to the bread board as shown in figure.

 Connect the GPIO pin to the bread board
                                       Figure 1: Connect the GPIO pin to the bread board

2. Here we use the PIN Number: “ 4 “ (As per number 4(pin rank 7) , third from the top – left ). For getting the output from the Raspberry PI connect eh jumper wire from Raspberry PI GPIO pin “4” to bread board as shown in image.

Connect eh jumper wire from Raspberry PI GPIO pin
                           Figure 2: Connect eh jumper wire from Raspberry PI GPIO pin

3. Now take LED and put it on bread board as show in image.

Connect the plus terminal of LED to the Jumper cable (green cable in image) coming from the GPIO Pin “4” as shown in image.

 Connect the Plus terminal of LED to the Jumper cable
                     Figure 3: Connect the Plus terminal of LED to the Jumper cable

4. Now take another jumper cable (black cable in image), connect it the GPIO pin “Ground” (As per number 3 (Pin rank 6), third from the top – right ). And other terminal of this wire to minus terminal of the LED as shown in figure.

Black cable in image
                                                   Figure 4: Black cable in image

5. Here for detecting the plus/minus terminal of LED, bigger (toll) terminal of LED is plus and smaller (short) terminal of LED is minus. Or you can define it by cutting, the side part of LED is Minus terminal.

Detecting the plus-minus terminal of LED
                                      Figure 5: Detecting the plus-minus terminal of LED

6. Now write the python program for blinking LED every 1 second.

  1. ------------------  
  2. import RPi.GPIO as GPIO  
  3. import time  
  4. #assign numbering for the GPIO using BCM  
  5. GPIO.setmode(GPIO.BCM)  
  6. #assingn number for the GPIO using Board  
  7. #GPIO.setmode(GPIO.BOARD)  
  8.   
  9. cnt = 0  
  10. MAIL_CHECK_FREQ = 1 # change LED status every 1 seconds  
  11. RED_LED = 4  
  12. GPIO.setup(RED_LED, GPIO.OUT)  
  13. while True:  
  14. ifcnt == 0 :  
  15. GPIO.output(RED_LED, False)  
  16. cnt = 1  
  17. else:  
  18. GPIO.output(RED_LED, True)  
  19. cnt = 0  
  20.   
  21. time.sleep(MAIL_CHECK_FREQ)  
  22. GPIO.cleanup()  
7. Now run the program and you can see the following output:

Program Show the Output
   Figure 6: Program Show the Output

Download Source code: LedBlinker_Python.rar

Now you enjoy with your Raspberry PI and if you like my tutorial or have any query comment on this.

Up Next
    Ebook Download
    View all
    Learn
    View all