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