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
Explanation
The Raspberry Pi has both
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
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.