0% found this document useful (0 votes)
12 views13 pages

Raspberry Pi LED Control Guide

The document provides instructions for programming a Raspberry Pi to control an LED and a button using the gpiozero library and RPi.GPIO. It explains how to light an LED, control its brightness, and respond to button presses. Additionally, it includes an example of using a seven-segment display with GPIO pins and offers a modification to display numbers in reverse order.

Uploaded by

ayushbadwaik.425
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)
12 views13 pages

Raspberry Pi LED Control Guide

The document provides instructions for programming a Raspberry Pi to control an LED and a button using the gpiozero library and RPi.GPIO. It explains how to light an LED, control its brightness, and respond to button presses. Additionally, it includes an example of using a seven-segment display with GPIO pins and offers a modification to display numbers in reverse order.

Uploaded by

ayushbadwaik.425
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

Raspberry PI Programming

Lighting an LED

LEDs are delicate little things. If you put too much


current through them they will pop (sometimes
quite spectacularly). To limit the current going
through the LED, you should always use a resistor
in series with it.
Try connecting the long leg of an LED to the Pi’s
3V3 and the short leg to a GND pin. The resistor
can be anything over about 50Ω.

The LED should light up. It will always be on, because it’s
connected to a 3V3 pin, which is itself always on.
Lighting an LED

The LED should now turn off, but now it’s on a GPIO pin, and
can therefore be controlled by code.
Lighting an LED

In [1]: from gpiozero import LED


In [2]: led = LED (17)
• To make the LED switch on, type the following and press Enter:
In [3]: [Link]()
• To make it switch off you can type:
In [4]: [Link](
Lighting an LED

from gpiozero import LED


from time import sleep

led = LED(17)

while True:
[Link]()
sleep(1)
[Link]()
sleep(1)
Using buttons to get input
Now you’re able to control an output component (an LED), let’s
connect and control an input component: a button.
•Connect a button to another GND pin and GPIO pin 2, like this:

from gpiozero import Button


button = Button(2)

button.wait_for_press()
print('You pushed me')
Using buttons to get input

from gpiozero import LED, Button


from time import sleep

led = LED(17)
button = Button(2)

button.wait_for_press()
[Link]()
sleep(3)
[Link]()
Example 1: Control LED with Raspberry Pi GPIO
•Turn On and OFF LED w/ Gpiozero

from gpiozero import LED

from time import sleep

red = LED(17)

while True:
[Link]() #turn led on

sleep(1) #delay for 1 second

[Link]() #turn led off

sleep(1):
Example 1: Control LED with Raspberry Pi GPIO
•Blinking LED w/ [Link]

import [Link] as GPIO


•Turning LED on and off with [Link]
from time import sleep
import [Link] as GPIO
[Link](False)
from time import sleep

[Link](False)
[Link]([Link])

[Link](17, [Link])
[Link]([Link])

[Link](17, [Link])
while True:
[Link](17, True)
[Link](17, True)
sleep(2)
sleep(1)
[Link](17, False)
[Link](17, False)

sleep(1)
•LED with variable brightness w/ Gpiozero

from gpiozero import PWMLED

from time import sleep

led = PWMLED(17)

while True:

[Link] = 0 # off

sleep(1)

[Link] = 0.2 # 20% brightness

sleep(1)

[Link] = 0.4 # 40% brightness

sleep(1)

[Link] = 0.6 # 60% brightness

sleep(1)

[Link] = 1 # full brightness

sleep(1)
•LED with variable brightness w/ [Link]

import [Link] as GPIO


[Link](dc) # where 0.0 <=
from time import sleep
dc <= 100.0
[Link](False)
sleep(0.2)
[Link]([Link])

[Link](17, [Link])
for i in range(0, 10):
p = [Link](17, 100) # 100 is
dc -= 10
frequency
[Link](dc) # where
dc = 0
0.0 <= dc <= 100.0
[Link](dc) # where dc is the
sleep(0.2)
duty cycle (0.0 <= dc <= 100.0)
[Link]()
for x in range(3):
[Link]()
for i in range(0, 10):

dc += 10
Analyze the following program carefully and answer the question below:

import [Link] as GPIO


import time
segments = [2,3,4,17,27,22,10] # Example pins
nums = [
[1,1,1,1,1,1,0], # 0
[0,1,1,0,0,0,0], # 1
[1,1,0,1,1,0,1], # 2 i. Explain the sequence of numbers displayed on the seven-
[1,1,1,1,0,0,1] # 3 segment display when the above program is executed.
]
[Link]([Link]) ii. ii. Modify the output sequence of the program so that the
for seg in segments: seven-segment display shows numbers in reverse order (3
[Link](seg, [Link]) → 2 → 1 → 0). Write only the modified loop section.
[Link](seg, 0)
for i in range(4):
for j in range(7):
[Link](segments[j], nums[i][j])
[Link](1)
[Link]()

You might also like