A2 Electronics Practical
A2 Electronics Practical
pet
attached
rogramming Raspberry Pi a. To Control
Tcelback from switch connccted
to
LEIDs
GPIO pins LED+Sw)
attachedto the GPIO pins
Aitn: Programming Raspberry Pi to Control LEDs
Objective: of RaspberryPi.
some basics of the GPIO Pins
10 understanding LEDs attached to the GPIO pios.
Dlo writea python programto control
from switch connected to GPIO píns
c. Write a python programto get fecdback
Circuit/Block Diagram:
77
(PZ220
LEDI
Hode Bvt1
Ragberry M3
Gn4
RI
1k9
GIA
LEDI
Coen
r tt
Pin On Raspberry
---| 0.
LED1 GPIO14
GPIO15 10
LED2
GPIO18 12
LED3
GPIO23 16
LED4
GPIO24 18
LED5
GPIO25 22
LED6
GPIO08 24
LED7
| LED8 GPIO07 26
SWITCH1 GPIO20 38
SWITCH2 GPIO21 40
GND 3
GND
Procedure:
Note: The Raspbian operating system comes with Python already installed
1. Setting up the circuit:
a. Turn OFF the Raspberry Pi while buildingconnecting the circuit board/components
according to diagram.
b. Then turn the Raspberry Pi and check the Raspbian OS loaded properly or not. Ifnot
then check the circuit connection again,
2. Python Programming:
Python via IDLE
A. Start Raspberry Pi in desktop mode, open the applications menu in the top left of your
SCreen. and navioate to
Pytthon shel.
False)
output(LED1, GPIO.
ese:
True) (LED1,
RIO.output
new_input_state
False: == if
input(SWITCH1) new_input_state
GPIO. =
True: while
GPIO.OUT) setup(LED1, GPIO.
pull_up_down=GPI0. setup(SWITCH1,
IN, GPIO. GPIO.
PUD_UP)
14 LED1
=
SWITCH1
21 =
(False)
setwarnings GPIO.
(GPIO.BCM) setmode GPIO.
time Anport
GPIO RPi.GPIO
as Aport
SP_2_SWitch_LED.py
RASP_2_switch_LED.py
LEDCOUnter.py
Dcounter.py
LED interfacing with Raspberry PI
import RPi.GPIO as GPIO # import GPIO 1ibrary
import time # import time library
GPIO.setmode(GPIO.BCM) # use BCM pin numbers
GPIO. setwarnings(False)
LED1=14
LED2=15
LED3=18
LED4=23
LEDS=24
LED6=25
LED7=8
LED8=7
2nd
08/03/2022, 11:01
py
RASP_3_LED ringcounter . c l e a r
PI
LED interfacing with Raspberry
#
1ibrary
GPIO # import GPIO
import RPi.GPIO as library
import time # import time use BCM pin numbers
#
GPIO. setmode (GPIO. BCM)
GPIO. Setwarnings (False)
LED1=14
LED2=15
LED3=18
LED4=23
LED5=24
LED6=25
LED7=8
LED8=7
output
GPIO.setup(LED1, GPIO.OUT) # setup pin 14 as output
setup pin 15 as
GPIO. setup(LED2, GPIO. OUT) #
pin 18 as output
GPIO. Setup(LED3, GPIO.OUT) # setup output
GPIO.setup(LED4, GPI0.OUT) # setup pin 23 as output
24 as
GPIO.Setup(LED5, GPI0.OUT) # setup pin
GPIO.setup(LED6, GPI0.OUT) # setup pin 25 as output
8 as output
GPIO. Setup(LED7, GPIO.0UT) # setup pinpin 7 as output
GPIO. setup(LED8, GPIO.OUT) # setup LED2, LED1]
count=[LED8, LED7, LED6, LED5, LED4, LED3,
disp_=[0,0,0, 0,0,0, 0, 0]
disp_1=[0,0,0,0, 0, 0, 0, 1]
disp_2=[0,0,0,0,0,0, 1,0]
disp_3=[0,0,0,0,0,1, 0, 0]
disp_4=[0,0, 0, 0, 1,0, 0, 0]
disp_5=[o,0,0,1, 0, 0, 0, 0]
disp_6=[0,0, 1,0,0,0,0,0]
disp_7=[0,1,0, 0,0,0, 0, 0]
disp_8=[1,0,0, 0, 0, 0, 0, 0]
disp_0=[0,0,0,0,0,0, 0, 0]
while True:
GPIO. output (count, disp_0)
time.sleep(1) # time delay 1s
GPIO. output (count, disp_1)
time.sleep(1) # time delay 1s
GPIO.Output(count, disp_2)
time.sleep(1) # time delay 1s
GPIO. output (count,disp_3)
time.sleep(1) # time delay 1s
GPIO.output(count, disp_4)
time. sleep (1) # tine delay 1s
GPIO.output (count, disp_5)
time. sleep (1) # time delay 1s
O8/03/2022, 11:01 RASP_3_LED ringcounter.py
GPO.output(count,
time.sleep(1) disp_6)
# time delay 1s
GPIO:output (count, disp_7)
ime.sleep(1) # time delay 1s
GPIO.output(count, disp_8)
time.sleep(1) # time delay 1s
GPIO.cleanup() # clear all pins