Tag Archives: Analog Voltages

Beaglebone Black LESSON 6: Control PWM Signals on Output Pins from Python

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.

Beaglebone Black Pinout
Default Pin Configuration for the Beaglebone Black Rev. C.

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.

 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:

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.

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

Raspberry Pi LESSON 27: Analog Voltages Using GPIO PWM in Python

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.

Raspberry Pi Circuit
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:


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:


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:


Similarly, if you wanted full brightness, you would want a 100% duty cycle, which you could get with the command:


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:


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:


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: