How to make an LED blink with Raspberry Pi?

by Dec 28, 2018Raspberry Pi projects

Getting started with Raspberry Pi

This article deals with the very basic circuit from our electronics. From our childhood electronics textbooks till engineering books, the basic circuit which contains LED will be demonstrated. Maybe in our childhood we learned using a battery and a simple resistor and now we are going to learn using Raspberry Pi. The main objective of this article is to produce an output from the Raspberry Pi using the LED flashing on and off.

What is an LED?

LED ( Light Emitting Diode) is a light source when a suitable voltage applied across the leads, the LED releases energy in the form of light that is photons. You must make sure that the voltage applied across the LED should not be too high, or else the LED will be damaged and also the legs are connected across the circuit properly.

Things you need

  • Breadboard
  • Light Emitting Diode
  • Resistor > 68 ohms
  • Raspberry Pi
  • Male to female wires

Circuit Diagram

LED circuit design

Explanation

The Raspberry Pi has both positive and negative terminal like a battery. The negative terminal will be connected to the GPIO pin 6 which is a ground pin. We will make one of the bi-directional pin as an output, when the output is high it will act as a positive terminal of the battery. This battery will have a voltage of 3.3 V. When the output is low, the positive terminal is at 0 volts so no current flows.

Note: Use a resistor which is higher that 68 ohms because the voltage across the LED should not be high as it damages the LED.

  • Insert an LED into a breadboard.
  • Connect the resistor which is more than 68 ohms across the longer end of the LED.
  • Connect the GPIO pin 11 to the shorter end of the LED.
  • Connect the GPIO pin 6 (ground) to the other end of the resistor.

Python code and explanation

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode (GPIO.BOARD)
GPIO.setup (11,GPIO.OUT)
while True:
GPIO.output(11,True)
time.sleep(2)
GPIO.output(11,False)
time.sleep(2)

You can use any programming language for the coding part like Python, wiring Pi, C, node.js, etc. So, I am using Python here. Here is the full code for the LED flashing ON/OFF. You can save that with extension .py

import RPi.GPIO as GPIO
from time import sleep

The two lines above from the code explains, importing the libraries you will need RPi.GPIO to control the GPIO pins. Importing time to control how long the pin will be ON or OFF.

GPIO.setmode (GPIO.BOARD)
GPIO.setup (11,GPIO.OUT)

GPIO.BOARD indicates the numbering scheme you are using. The line (11,GPIO.OUT) means you are using GPIO pin 11 as output pin.

while True:
GPIO.output(11,True)
time.sleep(2)
GPIO.output(11,False)
time.sleep(2)

while is used to continue as long as the program runs.
GPIO.output(11,True) sets the output to high and remain that way for 2 seconds ( sleep(2)). The same is set to low and remain that way for 2 seconds. If you need time-lapse for 5 seconds, you can change that in time.sleep(5).

You can save this by any name with .py and run at Python IDLE in your Raspbian OS orelse you can run this at the command line:

sudo python ledblink.py

That’s it! You had done your first project with your Raspberry Pi. If you have any questions or suggestions feel free to contact me.

Creating a multiplication Skill in Alexa using python

Written By Monisha Macharla

Hi, I'm Monisha. I am a tech blogger and a hobbyist. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning!

RELATED POSTS

Magic Wand using Raspberry Pi and OpenCV

Magic Wand using Raspberry Pi and OpenCV

What if I could do things with a swing of a wand? Practically impossible but with technology maybe not. In this tutorial, we will use a raspberry pi and OpenCV to try and recreate something similar to a wand. It will perform specific functions when it recognizes...

IBM Watson IoT Platform with Raspberry Pi

IBM Watson IoT Platform with Raspberry Pi

IBM Watson is one of the leading cloud computing platforms there is. It has almost all the features one could expect from a modern-day cloud platform, from machine learning support to IoT, even till edge computing. In this tutorial, we will use raspberry pi and an...

Home Automation With Telegram and Raspberry Pi

Home Automation With Telegram and Raspberry Pi

In this Article we will see how we can use Telegram for Home Automation. Telegram is an extremely popular multi-platform messaging service. Just like any other messaging platform, it is used to send messages and exchange photos, videos, stickers, audio, and files of...

Home Automation System with Raspberry Pi and Flask

Home Automation systems are becoming increasingly popular for the level of convenience they offer. Imagine sitting on your couch and turning on/off lights or fans in the room without having to get up. You could also control blinds or even door locks. This project is a...

Smart Alarm Clock with Raspberry Pi

Smart Alarm Clock with Raspberry Pi

Smart Alarms are very common today yet the good ones expensive. What if you wanted to make a smart alarm clock on your own using as little hardware and cost as possible? You may ask isn't the RPI costly, yes it is but, the only purpose of using an RPI is easy to net...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....