In earlier lessons we learned how to turn pins on and off with the Arduino digitalWrite command. This is great if you only want 0 or 5 volts. In this lesson we show how you can get the in between values by using the analogWrite command. In later lessons we will show more specifically how analogWrite works, but this present lesson gets you started, and shows you how to control brightness of an LED. In all these lessons we are using the Arduino Super Starter Kit, which you can pick up HERE.
In this lesson we will create a dimable LED. We will read an analog voltage from a potentiometer, and use that to set the brightness on an LED. In order to proceed with this lesson, you will need to connect the following circuit:
Note we are using P9_32 as the reference voltage on the voltage divider, we are using P9_34 as the reference ground, and we are using P9_33 as the analog sense pin. We also using P9_14 as the PWM output pin. Note the current limiting resistor in series with the LED is 330 Ohm.
The object of this circuit is to read the value of the potentiometer and then to use that to set the brightness on the LED. We know that the value we read from the potentiometer will be between 0 and 1. We know that what we can control on the PWM pin is the duty cycle of the 3.3 volt signal. We know that when the potentiometer reads 0, we want a 0% duty cycle on the PWM pin, which would have the LED off. This is our first point:
We also know that when we read 1 from the potentiometer, we want to apply a duty cycle of 100%, or have the LED be full bright. This is our second point:
If we created an equation for the line between these two points, we could calculate the duty cycle that should be applied based on the potentiometer reading. The problem with this is the way our eye perceives changes in brightness. We perceive exponential changes, so if we connected the two points with a linear relationship we would see lots of change at the low end of the scale, but as we continued to move the potentiometer, the brightness would appear to saturate. In order to have a nice smooth transition from full dim to full bright as the potentiometer is moved from left to right, we need to fit an exponential curve between the two points above. We want the LED to be off when the pot is full left, and full bright when the pot is fully to the right. We could use the following exponential equation:
Duty Cycle = C^(Analog Read) – B
This should do the trick, but we need to figure out what the constants C and B need to be. We do this by first plugging in the first point (0,0) from above:
0 = C^0 – B
Anything raised to 0 power is 1, so we have:
0= 1 – B
So B = 1. There, we have our first constant. We use this, and our second point to find C.
100 = C^1 – 1
Now we have everything we need to calculate the Duty Cycle from the value we read from the potentiometer. The final equation is:
Duty Cycle = 101^(Analog Read) – 1
Note that this relationship has the desired properties. When we read a 0 from the potentiometer, we apply a Duty Cycle of 0% to the PWM pin, and the LED is off. When we read a 1 from the potentiometer, we apply a Duty Cycle of 100% to the LED and it is full bright. The exponential shape of the curve between these two points ensures that we will perceive a smooth increase in brightness as we turn the potentiometer up. Math works! It would be very hard to do this by trial and error.
We are now ready to begin developing our code. The video lesson explains the code line-by-line, and we are using commands we learned in the last few lessons.
import Adafruit_BBIO.ADC as ADC
import Adafruit_BBIO.PWM as PWM
from time import sleep
The code works very well, and produces a very smooth transition from fully off to fully bright.
In Lesson 4 and Lesson 5 we showed how to do digital writes to the GPIO pins using Python. (If you have not picked up your Beaglebone Black Rev. C yet, you can get one HERE) With digital writes, we could generate an output of 3.3 volts or 0 volts. For many applications, we would like analog output, or the in between voltages. The Beaglebone Black, as with most microcontrollers, can not produce true analog output. However, for many applications, an analog output can be simulated by creating a fast on/off sequence where the analog value is simmulated by controlling the ratio of on time and off time. This technique is called Pulse Width Modulation, or more simply, PWM. Consider a 3.3 volt signal, which is turning on and off with a frequency of 50 Hz. A 50 Hz signal has a Period of: Period=1/frequency=1/50=.02 seconds, or 20 milliseconds. If during that 20 millisecond period, the signal was “High” for 10 milliseconds, and “Low” for 10 milliseconds, the signal would act like a 1.65 volt analog signal. The output voltage therefor could be considered the rail voltage (3.3 volts) multiplied by the duty cycle (percentage of time the signal is high.
For the Beaglebone Black, only certain pins can be used for PWM signals.
In the chart above, the purple pins are suitable for PWM output. You can see there are 7 pins which can produce PWM signals. In this lesson we show you how to control those pins.
In order to control PWM signals, we are going to use Python and the Adafruid_BBIO Library. Recent versions of Beaglebone Black Rev. C are shipped with the library already part of the operating system. If you are getting errors indicating that you do not have the library, update your operating system to the latest Debian image for the Beaglebone Black.
In order to use PWM in Python, you must load the Adafruit Library. If you have the recent versions of Debian Wheezy for the Beaglebone black, the library will already be on your system. If you do not do an update and upgrade on your operating system.
To begin with, you will need to load the library.
import Adafruit_BBIO.PWM as PWM
Next up, you will need to start the PWM on the pin you are using. We will use pin “P8_13”. Remember you must use one of the purple colored pins on the chart above. We start the PWM with the following command:
PWM.start("P8_13", 25, 1000)
This command puts a 1000 Hz signal (Period of 1 mSec) on pin P8_13, with a duty cycle of 25%. This should yield a simulated analog voltage of .84 volts.
We can change the duty cycle after this initial setup with the command:
This command would change the duty cycle to 90%, which would simulate a voltage of 3.3 * .9 = 2.97 volts.
You can also change the frequency of the signal using the command:
This would change the frequency to 100 Hz (Period of 10 mSec). Changing the frequency does not really affect the net result of PWM in most applications, although it does matter for many servo applications.
After you are done, you can stop the PWM with the command:
And always remember to clean up after yourself with:
Play around with the Python Program below. Connect a DVM to your Beaglebone Black, and measure the DC voltage at the output pin. The DVM should show your anticipated voltages.
import Adafruit_BBIO.PWM as PWM
PWM.start(myPWM, 0, 1000)
for i in range(0,5):
DC=input("What Duty Cycle Would You Like (0-100)? ")
Considering that the simulated analog voltage V=3.365 X Duty Cycle, how would modify the program above to ask the user for the Voltage he desires, and then calculate the duty cycle that would give that voltage. Your assignment is to modify the program above where the user inputs desired voltage, and DC is calculated. Use a DVM to check your results