How to Blink Led using Raspberry Pi

Maria Elijah
Geek Culture
Published in
5 min readJun 4, 2021

This article will guide through on how to blink LED(Light-emitting diodes) using Raspberry Pi and how to use the GPIO(General-purpose input/output) pins on it. At this point, If you haven’t installed the Raspbian OS, read my previous article on the installation of Raspbian OS here.

Before we dive into the practical aspect, let’s brush up on one special component of the Raspberry Pi — GPIO

  • GPIO known as General-purpose input/output pins is a powerful feature of the Raspberry Pi. These pins can be used to provide signals to components and also read signals from a component. for instance, Pin 1,2 supplies power(3.3V and 5v respectively). The function of the GPIO pin is customizable and can be controlled using several programming languages and tools. For further reading on GPIO pins, check here.
GPIO Pins

GPIO pins can be damaged easily so be careful when using and use 3.3 V logic.

Let’s start right on!

Hardware needed:

  1. LED
  2. 220 Ohms Resistor
  3. Jumper wires
  4. Breadboard
  5. Raspberry Pi 3 Model B+
  6. Ethernet cable
  7. Router
  8. a 5V 2A micro USB power adapter.

Software needed:

  1. PuTTy

First, enable ssh connection

i. Slot the MicroSD card into Card Reader and connect to the PC.

ii. Add the “ssh” file to the MicroSD card Root. Create ssh file name (without any extension) onto the boot partition of the MicroSD card

iii. safely remove the MicroSD Card. Now SSH is enabled.

iv. slot in the MicroSD Card to the appropriate slot on the Raspberry Pi.

Up next, Download and install Putty on the PC

PuTTy

PuTTY is an SSH and Telnet client, free and open-source software for the Windows platform. It is used to remotely access another computer.

Getting the Raspberry Pi’s IP address

We need the IP address to connect to the Raspberry Pi.

  • Connect one end of the ethernet cable to the Raspberry Pi and the other end to the Router.
  • Connect the PC to the Router
  • Go to the Router’s DHCP table, copy the IP address of the Raspberry Pi

Setting up Components

Connect the LEDs, resistors, Jumper wires using Breadboard and the GPIO pins on the Raspberry Pi.

At this point, the Raspberry Pi should be off as it is safer when connecting components.

Step 1: Connect the LEDs (red and yellow respectively) to the breadboard.

LED image from energy-saving lighting

The first thing I noticed about LED was the edges. It has a curved edge and a flat edge. the curved side of the LED is positive while the flat side is negative. Bet we’ve got a sharper eye than I did back then 😀and notice that a LED has a long and short leg which are positive and negative respectively.

Step 2: Choose the negative one and using the resistor, connect it up to GND (pin 39).

Step 3: Connect the other end to GPIO (pin 20 and 19) respectively. Feel free to use the picture as a reference.

An image depicting the connection

We can use different GPIO pins for our connection but make sure to follow all the rules.

Up next, power on the Raspberry Pi.

Note: if the LED comes on automatically, it means that there is something wrong with the connection.

Setting up the Raspberry Pi

  • Launch PuTTy, paste the Raspberry Pi’s IP address that we copied earlier on. The default port is 22
  • Enter the Username and Password which are ‘pi and raspberry’ respectively by default.
  • Upgrade and Update the Pi. It’s a good practice to always update and upgrade the pi each time we use it. Enter “sudo apt-get update” and “sudo apt-get upgrade”

Controlling the LEDs using Python code

  • Enter “sudo apt-get install python3” to install python.
  • Install gpiozero library.
  • Create a file ‘gzero.py’.
  • Write the following codes.

LEDBoard is used when we have more than one LED. A collection of LEDs can be accessed using LEDBoard. If it’s a LED, we’ll import “LED” instead of “LEDBoard”.

Save the file and go back to the console.

To run the program, just write “python3 file-name.py”.

NOTE: use the correct number of GPIO pin you’re connected to.

As long as the code keeps running, the LED turns on/off. Enter Ctrl + C to stop the python program

Conclusion

Make sure to connect the components correctly and use the correct number of the GPIO pin in your code.

Refresher time. Remember when we needed the IP address of the PI, we used a Router. This way isn’t pretty cool. Each time we need to connect to the Raspberry Pi, we have to set up a Router to get the IP address. How about using our mobile hotspot to connect? In my next article, we will learn how to connect to the Raspberry Pi using Mobile Hotspot.

--

--

Maria Elijah
Geek Culture

Flutter developer, IT support specialist and technical writer. I make DIY projects. I love learning as “learning is Life”