Tuesday, May 26, 2015

Control an LED to be blinking ON and OFF using Python

1. Connect an LED and a resistor to the Raspberry Pi as instructions.

2. Connect the power to GPIO4 pin of Raspberry Pi instead of 3.3V. (Connect the longer leg / pin of the LED (called the anode) to the GPIO4 pin.)

3. Type the commands below:

nano blink.py

This edits the blink.py file in the nano editor. Type the code below:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.OUT)

while True:
    GPIO.output(4,1)
    time.sleep(1)
    GPIO.output(4,0)
    time.sleep(1)




Execute the Python file:

sudo python blink.py

And the LED is blinking!!



No comments:

Post a Comment