Blinking LED With Raspberry Pi Using Python In this tutorial, we will be creating a simple program to blink LED with Raspberry Pi. We will using the Python programming language to control the LED.
The Raspberry Pi is a credit card-sized computer that is powerful enough to be used for many tasks. It is a low-cost alternative to other more expensive computers and can be used for projects such as a media centre, a web server, or even a desktop PC. In this essay, I will explain how to make a LED blink using Raspberry Pi and Python.
Required Material
Building the Circuit Diagram of LED With Raspberry Pi
Every LED has two sides – one negative and one positive. First, you need to connect the LED to the Raspberry Pi. The positive lead of the LED should be connected to GPIO pin 23. The negative lead of the LED should be connected to a ground pin. Choose the negative one and using the resistor, connect it up to GND.
Setting Up the Raspberry
First, you’ll need to boot up the Raspberry Pi. Then you need to log in and open a terminal window. Now, you need to update the package version of Raspberry Pi using the below command.  How to Setup a Raspberry Pi for the First Time | Getting Started with Raspberry Pi
1 |
sudo apt-get update |
1 |
sudo apt-get install idle3 |
You have to install the RPi.GPIO package. This package is necessary to access the GPIO pins of Raspberry Pi. You can install this package in raspberry pi 4 using the below command.
1 |
sudo apt-get install rpi.gpio |
Code – Blinking LED With Raspberry Pi Using Python
Now, you can write Python code for blinking LED in a terminal window.
We will be using GPIO 23 for this tutorial. Once the LED is connected, you will need to open up a Python file. You can do this by opening the IDLE application that comes pre-installed on the Raspberry Pi.
Once IDLE is open, you will need to create a new file by clicking on File > New File. Now that you have a new file open, you will need to type in the following code
Source Code
Save the program named led.py and type “python led.py“ to run the program in a terminal window. Now, you will see that the two LEDs start blinking at one-second intervals. Here, you can see an image of a blinking LED.
1 2 3 4 5 6 7 8 9 10 11 |
#blinking with gpiozero library from gpiozero import LED from time import sleep led = LED(23) while True: led.on() print('LED ON') sleep(1) led.off() print('LED OFF') sleep(0.5) |
Python Programming
- Open Terminal
- Launch IDLE IDE by typing
- After the IDLE launches, open a new window by FILE>OPEN or Ctrl+N
- Type the code below in the window
- Save the code by FILE>SAVE or Ctrl+S
- To run your code RUN>RUN or Ctrl+F5
The following script will turn the LED on for one second, then off for one second:
Blink Code using RPi.GPIO Library
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import RPi.GPIO as GPIO import time ledPin = 16 #led Pin def setup(): GPIO.setmode(GPIO.BOARD) GPIO.setup(ledPin, GPIO.OUT) GPIO.output(ledPin, GPIO.LOW) print ('using pin%d'%ledPin) def loop(): while True: GPIO.output(ledPin, GPIO.HIGH) print ('LED ON >>>') # print information on terminal time.sleep(1) # Wait for 1 second GPIO.output(ledPin, GPIO.LOW) print ('LED OFF <<<') time.sleep(0.5) # Wait for 0.5 second def destroy(): GPIO.cleanup() # Release all GPIO if __name__ == '__main__': print ('Program is starting ... \n') setup() try: loop() except KeyboardInterrupt: # Press ctrl-c to end the program. destroy() |
Source Code For Breathing LED With Raspberry Pi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import RPi.GPIO as GPIO import time LedPin = 16 # LED Pin def setup(): global p GPIO.setmode(GPIO.BOARD) GPIO.setup(LedPin, GPIO.OUT) GPIO.output(LedPin, GPIO.LOW) p = GPIO.PWM(LedPin, 500) # PWM Frequence to 500Hz p.start(0) # initial Duty Cycle to 0 def loop(): while True: for dc in range(0, 101, 1): p.ChangeDutyCycle(dc) time.sleep(0.01) time.sleep(1) for dc in range(100, -1, -1): p.ChangeDutyCycle(dc) time.sleep(0.01) time.sleep(1) def destroy(): p.stop() GPIO.cleanup() if __name__ == '__main__': print ('Program is starting ... ') setup() try: loop() except KeyboardInterrupt: # Press ctrl-c to exit the program. destroy() |