Working with LEDs & Buttons

Introduction to Raspberry Pi 4 Physical Computing with Raspberry Pi 4
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.18
List Price:  €95.97
You save:  €28.79
£55.85
List Price:  £79.79
You save:  £23.94
CA$98.21
List Price:  CA$140.31
You save:  CA$42.09
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,270.71
List Price:  R1,815.38
You save:  R544.66
Лв131.35
List Price:  Лв187.66
You save:  Лв56.30
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪260.01
List Price:  ₪371.46
You save:  ₪111.45
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$68,444.99
List Price:  CLP$97,782.75
You save:  CLP$29,337.76
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,773.54
List Price:  ISK kr13,962.80
You save:  ISK kr4,189.25
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,251.41
List Price:  ARS$100,363.46
You save:  ARS$30,112.05
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
Already have an account? Log In

Transcript

Video, working with LEDs and buttons. In this video, we will learn about pulse width modulation and implement it on LEDs. Later, we will learn to work with multiple LEDs and individually control their brightness. Finally, we will learn to work with buttons. You have now learned to switch an LED on and off using Python. What if I told you that we could do much more than turning the LED on enough CWM also known as pulse width modulation can be used to control the brightness of an LED pw M is a method of controlling the average power delivered to the device by essentially switching the power on and off at a very high frequency.

Due to the human eyes inability to see very fast switching. We perceive this as less power. With this concept we can control The brightness of LEDs speed of motors and so much more in a PWM signal, one period is the total sum of on time and off time. The effective power delivered depends on the duty cycle or power cycle, which is the fraction of the total period when the system is powered on. So, if the duty cycle is 50% it means that the LED will be on with after brightness GPIO zero comes with a class for implementing p w m in an LED called p w m LED. Let's see it in action openly led underscore p w m dot p y code from the repository and run the script.

The watch how the LED reacts. You can see that the brightness various from none to have to full brightness on each passing second in the script First we imported the P w m LED and sleep class from the GPIO zero and time modules respectively. Then we created an instance of this class by assigning it to a variable, we have entered the GPIO number that is 17 as the argument for the class, then we created an infinite while loop inside the while loop, we use the dot operator to set the value of the variable inside the instance of the bw m led class called value. We can provide any values from zero to one to this, where zero means zero percent duty cycle for the P WM signal, which means of while one means hundred percent duty cycle, which means full brightness or on in this example, but setting the value to 0.5 PW m signal with 50% duty cycle is created actually making the LED glow in half brightness, the sleep of one instruction gives a delay of one second between each p w m changes.

Now, I will give you an activity tinker with this code and make the LED gradually pulse like shown here. You will need to fine tune the delay and the number of p w m changes to achieve this effect. Now, let's say you want to control multiple LEDs at the same time. Then GPIO zero comes with a great inbuilt class called led board even has inbuilt methods to control multiple led brightness using p w m. We will wire up one more led to the setup. Here we are connecting with the positive leg of the green LED to GPIO three through a current limiting resistor. This led we'll be sharing the same ground pin as the red LED Now open the LED board dot p by script and run the code you can see that the following sequence of events occur both LED lights up, then both LED lights are off.

Later the red LED is on by green one is off. Now the green one is on and the red LED is off and the cycle continues. In the code, you can see that we have imported the sleep and led bought classes from the GPIO zero and time modules respectively. Then, we assigned the class led board on the variable led us with the LED GPA iOS 17 and three as the arguments of the class. Now inside the infinite loop, we have used the own method and of method to switch on and off all the ladies. Next comes the interesting part.

Here we have set value to a tuple where each element in the tuple are mapped to the LD in the same order as assigned earlier. In this case, as the first element is one, the first led that is, the LED connected to GPIO 17 will be on and the other one will be off as the second element is zero. To control brightness of multiple LEDs using p w m, we need to modify this code like the following. First, we need to enable the P w m option in the class by passing an extra argument called p w m equals true while creating the object. Now, you're free to put any values from zero to one for each LED as shown here. Here's how it translates to the LED output.

Next, let's learn about buttons. There are many types of buttons, aka switches available. We will With tactile push buttons, as these are the most commonly available and breadboard friendly buttons, connecting the Raspberry Pi's GPIO to a momentary tactile push button switch is a fairly simple circuit. We connect one side of the button to an input pin on the Raspberry Pi. In this case, we use GPIO to on the other side of the switch, we connect to ground through a resistor. The resistor is used as a current limiting resistor to protect our input pin by limiting the amount of current that can flow.

Now open the button led dot p by script from the repository and run the code. Now push the button and you can see the LED light up. The code is self explanatory. The only thing that stands out is the is underscore pressed method. The following figure shows the most commonly Use the button methods and their functions. Please check out the resources session to know more about them.

I will now give you an activity to create a reaction time game using two buttons and enter lady. Basically the LED should randomly light up and whoever presses the button faster wins. It's a two player game. Thus, use to push buttons. I will give you a hint. You will need to use the inbuilt module called random and its method to create random slip times for the LED summary.

In this video we have covered the following p w m in LEDs, controlling multiple LCDs working with buttons. In the next video, we will learn to interface and work with light sensors and motion sensors

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.