Raspberry Pi ProjectsTutorials

Blinking LED With Raspberry Pi Using Python

Blinking LED With Raspberry Pi Using PythonIn this tutorial, we will be creating a simple program to blink LED with Raspberry Pi. We will be 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. One of these tasks is controlling a LED. In this essay, I will explain how to make a LED blink using Raspberry Pi and Python.

Required Material

  1. Raspberry Pi 4 Model B
  2. Raspberry Pi Expansion Gpio
  3. Light-Emitting Diode (LED)
  4. Jumper Wire
  5. Breadboard

Building the Circuit Diagram of LED With Raspberry Pi

Building-the-Circuit-Diagram -of-LED-With-Raspberry-Pi 4

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

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.

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

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.

Python Programming

Led Blinking using Raspberry Pi – Python

  • 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:

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Ad
Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker