Starting Electronics needs your help! Please make a donation to help cover our hosting and other costs. Click the donate button to send a donation of any amount.
Created on: 28 April 2014
An LED connected to one of the Raspberry PI's GPIO pins can be flashed on and off using a program written in the Python programming language.
In this article, an LED on the Raspberry PI serial port and breakout board is used, but any LED connected to a GPIO pin on the Raspberry PI will work.
This video shows the LED being flashed by the Raspberry PI:
Can't see the video? View on YouTube →
The Python source code for flashing the LED is listed below:
#!/usr/bin/python # library for time delay import time; # library for controlling pins (GPIO pins) import RPi.GPIO as GPIO; # set GPIO pin 7 as output GPIO.setmode(GPIO.BCM); GPIO.setup(7, GPIO.OUT); # loop until user presses Ctrl + C while True: GPIO.output(7, True); time.sleep(1); GPIO.output(7, False); time.sleep(1);
Copy and paste the above source code to a text file and save it to the Raspberry PI, e.g. as flash.py.
To run the code from the command prompt on the Raspberry PI, enter the following:
sudo python flash.py
You must be in the same directory as the flash.py file.
Press Ctrl + C to exit the program.
As an Amazon Associate I earn from qualifying purchases: