Blink - Controlling an LED
Let’s write our first program to toggle an LED connected with GPIO pin on our virtual target. First, we need to create a file named
blink.py. Right-click on the file explorer tab and select
New Filefrom the menu; a new prompt will open: name it
blink.pyand click OK. The file will be created. Alternatively, we can use the terminal.
Now open the empty blink.py file, paste the following code and save it.
import RPi.GPIO as GPIO # Importing the GPIO controller module
ledPin = 9
print("Setting Broadcom Mode")
# Pin Setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
print("Setting LED as an output")
print("Here we go! Press CTRL+C to exit")
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly
GPIO.cleanup() # cleanup all GPIO
Now the program is ready and we just need to run it. Go to the terminal and write:
The program will run in a loop and toggle the LED on Pin number 9.
To stop the program press Ctrl+C.
Congratulations! We now know how to control the GPIO pins.