In this lesson we will explore how to use the PWM commands we learned in the last lesson to control the brightness of LEDs in a circuit. (If you do not have a Beaglebone Black yet, you can pick one up HERE.) In order to proceed with this project, you should hook the following circuit up to your Beaglebone Black.
This Circuit is Used to Demonstrate a Dimable LED Project
Note that we are using pins “P9_14” and “P9_22” which are good working PWM pins. Note the current limiting resistors are 330 ohm. Always connect the long leg of the LED towards the control voltage.
The video steps you through the code to control the LED brightness.
This lesson shows a simple example of how to blink two LEDs from the GPIO pins on the Beaglebone Black. To get going, you will need to hook up the following circuit. (If you have not ordered your Beaglebone Black, you can get one HERE.)
Circuit for Blinking LEDs from Beaglebone Black
Note that the Top LED is connected to Pin “P9_12” and the bottom LED is connected to Pin “P9_11”. We are using 330 ohm current limiting resistors.
The video lesson takes you through several examples of how to blink the LED. Watch the video, and do the examples. Then play around on your own and see what you can make the LEDs do.
In this lesson we are ready to bring together a lot of what we learned in earlier lessons. We will create dimable LEDs which will respond to two buttons. If one is pressed the LED will gradually grow dimmer. If the other is pressed, the LED will gradually grow brighter. This will require us to use our skills in using GPIO inputs, pullup resistors, GPIO outputs, and PWM.
For convenience we will use the same circuit we used in LESSON 30, shown below. Also, if you want to follow along with these lessons, you can buy the gear you need HERE.
This Circuit Controls two LED from Push Buttons Using the Raspberry Pi
The objective of this circuit is that we want the LEDs to grow brighter each time the right button is pushed, and we want them to grow dimmer each time to left button is pushed.
The video above steps through and explains this code.
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fromtimeimportsleep# Library will let us put in delays
importRPi.GPIO asGPIO# Import the RPi Library for GPIO pin control
GPIO.setmode(GPIO.BOARD)# We want to use the physical pin number scheme
button1=16# Give intuitive names to our pins
button2=12
LED1=22
LED2=18
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Button 1 is an input, and activate pullup resisrot
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Button 2 is an input, and activate pullup resistor
GPIO.setup(LED1,GPIO.OUT)# LED1 will be an output pin
GPIO.setup(LED2,GPIO.OUT)# LED2 will be an output pin
pwm1=GPIO.PWM(LED1,1000)# We need to activate PWM on LED1 so we can dim, use 1000 Hz
pwm2=GPIO.PWM(LED2,1000)# We need to activate PWM on LED2 so we can dim, use 1000 Hz
pwm1.start(0)# Start PWM at 0% duty cycle (off)
pwm2.start(0)# Start PWM at 0% duty cycle (off)
bright=1# Set initial brightness to 1%
while(1):# Loop Forever
ifGPIO.input(button1)==0:#If left button is pressed
print"Button 1 was Pressed"# Notify User
bright=bright/2.# Set brightness to half
pwm1.ChangeDutyCycle(bright)# Apply new brightness
pwm2.ChangeDutyCycle(bright)# Apply new brightness
sleep(.25)# Briefly Pause
print"New Brightness is: ",bright# Notify User of Brightness
ifGPIO.input(button2)==0:# If button 2 is pressed
print"Button 2 was Pressed"# Notify User
bright=bright*2# Double Brightness
ifbright>100:# Keep Brightness at or below 100%
bright=100
print"You are at Full Bright"
pwm1.ChangeDutyCycle(bright)# Apply new brightness
pwm2.ChangeDutyCycle(bright)# Apply new brightness
sleep(.25)# Pause
print"New Brightness is: ",bright#Notify User of Brightness
In this lesson we will show how you can control LED’s from push buttons. In order to get started, you will want to expand the circuit we built in LESSON 29 to include two LEDs. The schematic below shows how you will want to hook things up (Also, remember you can see the Raspberry Pi pinout in LESSON 25). Also, as we have mentioned before, if you want to follow along with us in these lessons you can get a kit that has all the gear you need HERE.
This Circuit Controls two LED from Push Buttons Using the Raspberry Pi
In the video lesson, we take you through the code step-by-step. We use the techniques learned in LESSON 29 to detect if a button has been pushed. We introduce two new variables, BS1 and BS2, so indicate the state of the LED’s. A BS1=False means the LED1 is off. A BS1=True means the LED is on. This concept allows us to determine whether we should turn the LED on or off when the button is pushed. Basically, we want to put it in the opposite state when a button is pushed. The code is below. The video shows how it works.
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fromtimeimportsleep# Import sleep Library
importRPi.GPIO asGPIO# Import GPIO Library
GPIO.setmode(GPIO.BOARD)# Use Physical Pin Numbering Scheme
button1=16# Button 1 is connected to physical pin 16
button2=12# Button 2 is connected to physical pin 12
LED1=22# LED 1 is connected to physical pin 22
LED2=18# LED 2 is connected to physical pin 18
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Make button1 an input, Activate Pull UP Resistor
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)# Make button 2 an input, Activate Pull Up Resistor
GPIO.setup(LED1,GPIO.OUT,)# Make LED 1 an Output
GPIO.setup(LED2,GPIO.OUT)# Make LED 2 an Output
BS1=False# Set Flag BS1 to indicate LED is initially off
BS2=False# Set Flag BS2 to indicate LED is initially off
while(1):# Create an infinite Loop
ifGPIO.input(button1)==0:# Look for button 1 press
print"Button 1 Was Pressed:"
ifBS1==False:# If the LED is off
GPIO.output(LED1,True)# turn it on
BS1=True# Set Flag to show LED1 is now On
sleep(.5)# Delay
else:# If the LED is on
GPIO.output(LED1,False)# Turn LED off
BS1=False# Set Flag to show LED1 is now Off
sleep(.5)
ifGPIO.input(button2)==0:#Repeat above for LED 2 and button 2
If you remember our Arduino Lessons, you will recall that we could write analog voltages to the output pins with the ~ beside them. The truth is, though, we were not really writing analog voltages, we were just simulating analog voltages using pulse width modulation (PWM). The arduino was able to put out 5 volts. Hence, if you want to simulate a 2.5 volt signal, you could turn the pin on and off every quickly, timing things such that the pin was on half the time and off half the time. Similarly, if you wanted to simulate a 1 volt analog out, you would time things so that the 5 volt signal was on 20% of the time. For many applications, such as controlling LED brightness, this approach works very well. Arduino made it easy and transparent to the user to generate these analog-like output voltages using the analogWrite command.
This capability is also available on the Raspberry Pi GPIO pins. However, the implementation requires you to think in terms of a signal with a frequency and a duty cycle. Consider a signal with a frequency of 100 Hz. This signal would have a Period of 10 milliseconds. In other words. the signal repeats itself every 10 milliseconds. If the signal had a duty cycle of 100%, it would be “High” 100% of the time, and “Low” 0% of the time. If it had a duty cycle of 50% it would be high 50% of the time (.5X10 milliseconds= 5 milliseconds) and low 50% of the time (.5X10 milliseconds = 5 milliseconds). So, it would be high 5 milliseconds, and low 5 milliseconds for a total period of 10 milliseconds, which as we expect, if a frequency of 100 Hz. (Note that the Period of a signal = 1/frequency, and frequency = 1/Period)
Note on the Raspberry Pi, the output voltage is 3.3 volts as opposed to the 5 volt output on the Arduino. Hence, the Raspberry Pi can only simulate analog voltages between 0 and 3.3 volts. For this example, we will be playing with the following circuit again. Note we are using physical pin 9 as the ground and physical pin 11 as the power pin. See Lesson 25 below for a diagram of pin numbers on the Raspberry Pi.
This Circuit Will Blink a Red LED
OK, enough background, lets start playing with some code. On examples like this, I think it is easiest to operate from the Python Shell, as this allows us to observe the effects of our commands one at a time. To enter the Python Shell, type sudo python at the linux command line in a terminal window. The sudo is important as it allows you to enter the python shell as a superuser. Access to the GPIO pins requires superuser privileges. Also, remember that to exit the python shell and return to the Linux command prompt you enter Ctrl-d. So, type in sudo python to go to the python shell. You should see the >>> prompt indicating you are not in the python shell. The first thing you need to do is import the RPi library:
>>> import RPi.GPIO as GPIO
Now tell the Raspberry Pi which pin number scheme you want to use (See Lesson 25). I prefer to use the physical pin numbering system as I find it easier to remember. To use the physical pen numbering system, you would enter this command:
>>> GPIO.setmode(GPIO.BOARD)
Note, if you prefer the BCM system, replace BOARD with BCM in the command above.
Now we need to tell the Pi that physical pin 11 will be an output. We can do that will the command:
>>> GPIO.setup(11,GPIO.OUT)
At this point we could write the pin high or low, but our objective here is to use PWM, so we need to do a few more things. First, we need to create a PWM object. I will call my object my_pwm. We will need to pass the parameters of the physical pin we want to use, and the frequency. I like to use 100 Hz, which gives us a period of 10 msec. The command we need for this is:
>>>my_pwm=GPIO.PWM(11,100)
Remember capitalization needs to be EXACT! Now to start the pwm we need to decide what DutyCyle we want. Remember, the DutyCycle is the percentage of the period that the signal will be high. If we wanted to approximate a 1.6 volt signal, we would note that 1.6 is about half of the 3.3 coming out of the Pi, so we would want a 50% duty cycle. The command for this would be:
>>>my_pwm.start(50)
When you type this command you should see the LED come on, if you have connected things correctly. It should be at about half brightness.
Now if you would like to change the brightness, just change the Duty Cycle. For example, if you wanted the LED very dim, you might set a 1% duty cycle. You could do this with the command:
>>>my_pwm.ChangeDutyCycle(1)
Similarly, if you wanted full brightness, you would want a 100% duty cycle, which you could get with the command:
>>>my_pwm.ChangeDutyCycle(100)
Again, please remember that the capitalization has to be exact. Now you can get any brightness you want by changing the duty cycle to anything between 0 and 100, inclusive.
Note you can also change the frequency of the PWM signal. Lets say you wanted a frequency of 1000 Hz. We could do this with the command:
>>>my_pwm.ChangeFrequency(1000)
Note that by doing this there is no perceptible change in LED brightness because you have not changed the relative on and off time of the signal. You are just going faster, but not impacting the fractional time the signal is on and off, hence the LED brightness does not change.
While in these examples we have done things from the control line, you can write python programs that will run the commands for you. For example, write a program that asks the user how bright he wants the LED, between 0 and 100, and then set it to that brightness by adjusting the Duty Cycle, as we did in the example above. Play around with different pins and different frequencies and values. Become familiar with these commands.
Now, finally, if you want to turn pwm off, you would use the command:
>>>my_pwm.stop()
Also, remember that you should always clean up after yourself, so at the bottom of your program, or before you exit the shell, always release your pins and clean up using the command:
>>>GPIO.cleanup()
Making The World a Better Place One High Tech Project at a Time. Enjoy!
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok