Blinking a LED with Raspberry Pi

By | September 13, 2017
Hi, friends! I hope you guys are doing fine.

Today in this tutorial we’ll be teaching you with one of the easiest project, that you can start with a raspberry pi. Blinking a led with help of Python Code.

The code is quite simple to start with. You can see the code below with the explanation. This project is intended to give you the basic understanding of working with Pi. And, we are certain that you have just not bought or intended to buy a Pi only for the sake of blinking a LED. But this is one of the basic program which will build the foundation for you for bigger and complicated IOT projects. So let’s begin :

Things required:

  • Raspberry Pi 2/3 (any version will do just fine)
  • Ethernet cable for connecting to the laptop and raspberri Pi.
  • An LED
  • Few connecting wires

 Buckle up your seat belts… cause the things are going to get>>>>>”NuRdY”. 😉 

The layout of the pins is given in the pic below. In the code, you will have to write down the pins that you will be using for the LED. So must know which pins can be used and the pin number. The pins of Pi are called GPIO pins. And the number of the corresponding pins can be seen in the picture below.

Connections

1.) Connect the ground/Anode/negative pole of the LED to any one of the ground pins of the PI(look at the pic given to find out where  are the ground pins)

2.) Connect the positive end of the LED to the pin no. 3(GPIO 3) because that is what I have mentioned in the code, you can change it according to your need.

KUDOS!! The electronics part is done… Now let’s get started on the code!!!

Coding

1.) Open the Python 3 application from your raspberry pi operating system and select on new file

2.) Write the code as given in the pic

3.) Save the code along with the extension, make sure you don’t change the location otherwise while executing, the command will get bigger. Save it will .py extension because it’s a python program

4.) After saving, open a terminal, and write

Python filename.py

Replace filename with the name you have given to the file and as you will press enter, the led will start to blink.

Code Explanation

The code here is really simple. First, we have imported the two libraries for using GPIO and time function.

Then we have initialized the pin and set its mode to output. After that, we have sent a high signal first and used time command to hold the program for a while and then sent a low signal and again called the time function so that the blinking can actually take place.

Now, this program will work only once, hence, in the beginning, I have written a while loop with a 1 because that will always return a true value and make it an infinite loop, so that out LED just keeps on blinking. Please see that the whole program in Intended because of the while loop, so keep indentation in mind while coding in python.

So this is it. You are a Pi developer now!! KUDOS to that. We will be back soon with some other project and the level will rise slowly so that you can catch up with our pace and learn with your basic fundamentals being clear.