0% found this document useful (0 votes)
45 views6 pages

A2 Electronics Practical

The document provides instructions for programming a Raspberry Pi to control LEDs using GPIO pins and a switch. It includes a circuit diagram, GPIO pin connections, and sample Python code for setting up and controlling the LEDs based on switch inputs. The guide emphasizes the importance of correctly setting up the circuit and using the Raspbian operating system with Python pre-installed.

Uploaded by

gauravnarkhede36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

A2 Electronics Practical

The document provides instructions for programming a Raspberry Pi to control LEDs using GPIO pins and a switch. It includes a circuit diagram, GPIO pin connections, and sample Python code for setting up and controlling the LEDs based on switch inputs. The guide emphasizes the importance of correctly setting up the circuit and using the Raspbian operating system with Python pre-installed.

Uploaded by

gauravnarkhede36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

tothe GPIO píns &To

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

a. Basi c LED Comnon Catlode


Cannetin
b Basic LeD ComDoD Anode Con
P_2
{2)
GPIO Pin Connection: * t
R P

r tt

Pin On Raspberry
---| 0.

| Pin On Kit |GPIO


HARDWARE

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

IO.setup(LED1, GPIO.OUT) # pin


setup 14 as output
GPIO. setup(LED2, GPIO.OUT) # pin
setup 15 as output
GPIO. Setup(LED3, GPIO.0UT) # pin
setup 18 as output
GPIO.setup(LED4, GPIO0.OUT) # pin
setup 23 as output
GPIO. setup(LEDS, GPIO. OUT) # setup pin 24 as output
GPIO. setup(LED6, GPIO.0UT) # setup pin 25 as output
GPIO. setup(LED7, GPIO. OUT) # setup pin 8 as output
7 as output
GPIO. setup(LED8, GPIO.OUT) # setup pin LED2, LED1]
count=[LED8, LED7, LED6, LEDS, LED4, LED3,
disp_ON=[0,0,0,e,0,0, 0, 0]
disp_OFF=[1, 1,1, 1, 1, 1, 1, 1]
while True:
time. sleep(1) # time delay 1s
GPIO.output (count,disp_OFF)
time.sleep(1) # time delay 1s
IO.output (count, disp_ON)
high
HGPIO.output (LED1, True) # set LED1 low
##GPIO.output (LED2, False) # set LED2
##time.sleep (1) # time delay 1s
##
##GPIO.output(LED1, False) # set LED1 low
high
##GPIO.output(LED2, True) # set LED2
##time.sleep(1)
## high
##GPIO.output (LED1, True) # set LED1
LED2 low
##GPIO.output(LED2, False) # set
##time.sleep(1)

GPIO.cleanup() # clear all pins


(co

RASP_3_LED ringcounter.py 2nd


d a a ( I )

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

You might also like