Digital output, it is very wonderful features of a Raspberry Pi(RPi), RPi has some GPIO pins(General Purpose Input Output) which is helps to give digital data to digital output devices like led, Relay, Seven segment, led cube, led matrix etc.
In Digital world mainly two things are their which is ON & OFF; in uP language we can say that is 1 & 0 or 5v & Gnd. In case of RPi GPIO pin generate 5v or high signal to turn on led and gnd or low signal to turn off led.
So lets start how its work !!!
Digital Operation in RPi
- Check any Python package installed in RPi
- Open terminal paste below code
- If python and python3 is Not installed copy the below code and paste in terminal.
sudo apt-get update sudo apt-get upgrade sudo apt-get install python sudo apt-get install pyhton3
Install GPIO library for python
sudo apt-get install python-rpi.gpio python3-rpi.gpio
Led is connected to GPIO2 which is pin3 of RPi as we know RPi generate 5v but led workings on 3v for that purpose we need one Resistor ie. 220E which is connected series with led and led cathode is connected to Gnd of RPi(pin6)
import RPi.GPIO as GPIO ## Import GPIO library import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) ## Use board pin numbering GPIO.setup(3, GPIO.OUT) ## Setup Pin 3 to OUTPUT while True: #Infinite Loop GPIO.output(3, True) ## Turn on pin 3 print ("led on") time.sleep(1) GPIO.output(3,False) ## Turn off pin 3 print ("led off") time.sleep(1)
- Save above code in .py
- E.g.: led_blink.py
- Run code in terminal
- If use Thonny IDE