Tag Archives: Raspberry Pi

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.

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:


Raspberry Pi LInux LESSON 26: Controlling GPIO Pins in Python

In this lesson we will actually begin to control the GPIO pins from the Raspberry Pi. We will start by looking at how to write a pin high or low. We will be doing this in the Python programming language. A really important thing to remember is that the default “Pi” user does not have access to the pins, so for these examples to work, you must run the programs with “sudo”. The sudo command executes as super user, and will give the program access to the GPIO pins.

To begin with, lets build a simple circuit. If you purchased the kit we showed in lesson 1, you should have all the components you need to follow along with these examples. If you have not purchased a kit yet, you can get one on amazon.com HERE.  In this first lesson we will just be looking at blinking an LED. So, you can now go ahead and hook up the following circuit. For reference, we show below the pinout of the Raspberry Pi.

This figure shows the Raspberry Pi GPIO pinout

In this example we will be using physical pin 9 as a ground, and physical pin 11 as the control pin. You can now go ahead and hook up the following circuit. Please remember the direction you plug the LED in matters . . . the long leg needs to connect to pin 11. The resistor used should be about 330 ohms.

This Circuit Will Blink a Red LED

In order to become familiar with the commands, I like to start in the Python shell. Basically, we give the commands to python one line at a time and watch what happens. Then later, we can write and run programs.

Note that recent versions of the Raspberry Pi distribution include the RPi library, but if you have an older distribution, update your system using these commands:

$ sudo apt-get update


$ sudo apt-get upgrade

If you have not done this is a while, it can take some time to download and install the updates.

We are now ready to begin to work with the GPIO pins. To enter the python shell, open a terminal window on the Raspberry Pi, and you will want to type:

$ sudo python

Be sure and use the sudo command above, as that will give you administrative access to the GPIO pins. Now, you should get the python command shell prompt, that looks like this:


At this point, any command you type will be executed by the python interpreter. You can basically execute a python program one line at a time. Note, to exit the python shell type Ctrl-d.

OK, so lets see if we can control the LED!

First, we need to import the RPi library. Note this is case sensitive, so be careful to do capitalization exactly:

>>> import RPi.GPIO as GPIO

Now, we need to initialize the GPIO to use either the BOARD or the BCM pin numbering schemes. In the diagram at the top of this lesson, the BOARD numbering convention is shown in the center two columns. If you want to use the BCM numbering scheme, you would use the numbers indicated in the outer two columns. In these examples, I want to use the physical pin numbers, as that is easier to me to keep track of things. Hence, I will want to use the BOARD scheme. I can do that with this command:


As you can imagine, if you want to use BCM, BOARD should be replaced with BCM in the command above.

If you remember in our Arduino lessons, we had to do pinmode commands to tell the arduino whether pins are inputs or outputs. We do an analogous thing in Raspberry Pi. We need to tell the Pi whether we will be using a pin as an input or output. In the wiring diagram above, you can see that we want to power the LED from physical pin 11, so we need to set that as an output.


We are now ready to turn the LED on. We can do this by setting pin 11 to True:


Now to turn the LED off, we can do:


You can now play around with different GPIO pins, and turn the LED on and off as you like. Before leaving the Python shell, be sure to clean up the GPIO. You do this by giving the cleanup command:


This will ensure you do not get error messages if you try to work with the GPIO pins again. It is a good practice to always cleanup after you are done.

Raspberry Pi Linux LESSON 25: Raspberry Pi 2 Pinout

In our earlier lessons we have taken you from installing the operating system, all the way through creating and running your first python program. At this point, you know how to generally operate the Raspberry Pi platform. Now we are ready to start building projects, and getting the Pi to perform for us. The first thing we will need to understand is which pins do what. The pi has many pins, so the diagram below shows what each pin can do.

This figure shows the Raspberry Pi GPIO pinout

In order to understand pin number, make sure to have your pi oriented as shown in the figure.  Now look at the center two columns on the chart. These show you the physical pin number. The outer two columns of the chart show you the bcm  numbering. Which numbering system you use depends on how you configure things in the software. We will cover this in the next lesson, but for now know there are two different numbering schemes. For the examples in this series of lessons we will use the bcm numbering scheme, so we will be using the number references in the outer two columns.

Also notice that some of the pins are multi-purpose. For example physical pins 3 and 5 can be GPIO pins, or they can be configured for I2C. Similarly, 8 and 10 can be general purpose GPIO pins, or can be Tx and Rx.  Note the GPIO pins are analogous to your digital input/output pins on Arduino (the ones without the ~ by them).

In general when setting up a project I try and select GPIO pins that are not multi-function. In this way if I ever expand the project and want to add Tx/Rx or I2C capability, those pins are still free.

Raspberry Pi Linux LESSON 23: Changing File and Folder Permissions

In this lesson we look at changing file and folder permissions. In the earlier lessons we learned that we can view the file and folder permissions by navigating to the folder of interest, and then giving the ls -l command. In order to change the permissions, we will use the chmod command. This video explains how to use the chmod command to control who has what permissions.