Raspberry Pi LESSON 29: Configuring GPIO Pins as Inputs

We are now ready to learn how to “read” values from the Raspberry Pi GPIO pins. In order to demonstrate this, we will show a simple example using buttons. If you ordered the Raspberry Pi kit we recommend, you already have everything you need, or you can pick your kit up HERE. To start with, you need to put together a simple circuit that connects two push buttons to your Raspberry Pi. Connect according to this schematic.

Raspberry Pi Buttons
Simple Circuit Connecting Two Push Buttons to the Raspberry Pi

Note that one leg of each button is connected to the ground rail on the breadboard, that is connected to the Pi ground at physical pin 6. Then we connect the left leg of the left button to physical pin 16, and the left leg of the right button to physical pin 12.

In order to read the state of these buttons, that is, whether they are being pressed or not, we need to write a python program. To begin with we must import GPIO library and specify that we want to

 Now we are ready to set the pin modes on the pins we are using. We are using pins 12 and 16. We will set up variables so that we can reference the pins by descriptive variables.

Note in our GPIO.setup commands, we are not just defining the pins as inputs, we are also activating pullup resistors with

With this command, the raspberry pi places a pullup resistor between the designated pin and the 3.3 V rail. This means that if we simply read the pin, we will read a “1”, “True”, or “High”, since the pin will see the rail through the pullup resistor. If we connect the pin to ground by pressing a button or switch, the pin will then read a “0”, “False” or “Low” because it will be a straight connection to ground, and as current flows through the pullup resistor, the 3.3 Volts will drop across the pullup resistor. Hence, the pin sees 0 volts.

The result is that with the pullup resistor activated, the pin will always report a “1” until something connects the pin to ground, and then it will read a “0”. This configuration should work for most things, but if you are getting unpredictable results which can result from electrical noise, then try using external pullup resistors.

 Now we are ready to read the values from the pins.

Notice that we read from the pin using the GPIO.input command. Also note that for reliable results you need to usually put a small delay in your code. This will help debounce the button, and will also give more stable results.

OK, so our final code is as follows:

This code will sit and monitor the buttons, and when one is pressed it will report that that button has been pressed.

Raspberry Pi LESSON 28: Controlling a Servo on Raspberry Pi with Python

In this lesson we will show you how to precisely control a Servo using the Raspberry Pi. First, for the small servo I am using, I have verified that it is safe to drive from the 5 volt pin (physical pin 2) on the Raspberry Pi. It is possible to damage your Raspberry Pi by drawing too much current out of a pin. So, if you are not sure about the current requirements of your Servo, it is best to power it from a 5 Volt source other than a Raspberry Pi pin. You can still control it from the Raspberry Pi if you use a common ground, but just get the power (red wire) from an external source. For my small servo, I can safely power it from Raspberry Pi physical pin 2.

The second point is that to control the servo, you have to use Pulse Width Modulation. So, I STRONGLY recommend that you go through LESSON 27 if you have not already. Lesson 27 shows you how to use PWM on the GPIO pins on the Raspberry Pi. If you are up to speed on PWM, this lesson will go a lot easier.

So, with that out of the way, we are ready to hook up our servo. For my servo, the ground wire is Black, the Red wire is 5 volt, and the yellow wire is the control line. If you are using a different servo, you will need to read the instructions to see what the color code is for your three wires, but it is likely similar to mine. The sketch below shows how you should hook the servo up. Notice I have the 5V hooked to the Pi physical pin 2, the servo ground hooked to the Pi physical pin 9, and the servo control line hooked to the Pi physical pin 11.

Raspberry Pi Servo
Servo Connected to Raspberry Pi GPIO Pins

Now with the Servo hooked up, we are ready to try and control it. For this example, we will work in the Python shell. To enter the python shell, open a Terminal window, and at the command prompt type:

$sudo python

It is important to include sudo, as the Raspberry Pi only allows access to the GPIO pins to the superuser. Hence, you need to enter the python shell as a superuser. When you type the command above, you should be moved into the Python shell, and should see the python shell prompt of >>>.

We are now ready to control the servo. We must first import the RPi library. These first steps should be familiar if you did LESSON 27.

>>>import RPi.GPIO as GPIO

Now we need to tell the Pi what pin numbering scheme we want to use. I like to use the physical pin numbers (See LESSON 25 for a diagram). So, we need to issue the command:

>>>GPIO.setmode(GPIO.BOARD)

Now we need to tell the Pi that physical pin 11 will be an output pin:

>>>GPIO.setup(11,GPIO.OUT)

The servos position is controlled by the pulsewidth of a 50 Hz PWM signal. Hence, we need to turn the PWM sequence on at 50 Hz. Note that for a 50 Hz signal, the Period of the signal is 1/50=.02 seconds, or 20 milliseconds. Keep this Period in mind as we will come back to it later. We start by creating a PWM object on Pin 11 with a 50 Hz signal with the command:

>>>pwm=GPIO.PWM(11,50)

We can now start the pwm sequence by giving a command to specify the DutyCycle of the signal. Before we do this, we need to talk a little bit about how servos work. A typical servo wants to see a frequency of 50 Hz on the control line. The position it moves to depends on the pulse width of the signal. Most servos behave roughly as such, but you will need to tweak these numbers for your particular servo. Typically, the servo will go to the full left position when it sees a pulse width of 1 millisecond, it will go the middle position when it sees a pulse width of 1.5 millisecond, and it will go to the full right position when it sees a pulse width of 2 millisecond. Note however, that on the Raspberry Pi we do not specify a pulse width, but we specify a DutyCycle. So, we can use the following relationship:

DutyCycle =PulseWidth/Period

Remember that Period = 1/frequency, so:

DutyCycle = PulseWidth/(1/frequency) = PulseWidth * frequency

The PulseWidth that will give us a full left position is 1 milllisecond. We now calculate the applied DutyCycle to give us the desired position:

DutyCycle = PulseWidth*frequency=.001 *50 = .05 = 5%

So, for a 50 Hz signal, if we set the DutyCycle to 5, then we should see the servo move to the full left position. Similarly, if we set DutyCycle to 7.5, we should get the middle position, and if we set it to 10 we should be in the full right position. You can get all the intermediate positions by linearly scaling between 5 and 10. Note that these values will vary between brands, and between individual servos, so play around with your servo to get it calibrated. We are now ready to apply a command to position the servo. If we want the servo in the full left position, we should set the DutyCycle to 5%. We do that with the command:

>>>pwm.start(5)

This will start the PWM signal, and will set it at 5%. Remember, we already specified the 50 Hz signal when we created the pwm object in our earlier commands. Now if we want to change the position, we can change the DutyCycle. For example, if we want to go to the middle position, we want a DutyCycle of 7.5, which we can get with the command:

>>>pwm.ChangeDutyCycle(7.5)

Now if we want the full right position, we want a duty cycle of 10, which we would get with the command:

>>>pwm.ChangeDutyCycle(10)

Remember, it is not DutyCycle that actually controls servo position, it is PulseWidth. We are creating DutyCycles to give us the desired PulseWidth.

Now, play around with your particular servo and then find the specific DutyCycles that lead to full left and full right positions. For my servo, I find that full left is at DutyCycle=2, and full right is at DutyCycle=12. With these values, I can create a linear equation that will give me any angle I want between 0 and 180. This will make the Raspberry Pi behave much more like the simple and intuitive operation of the Arduino.

To do the linear equation I need two points. Well, I know that for a desired angle of 0, I should apply a DutyCycle of 2. This would be the point (0,2). Now I also know that for a desired angle of 180, I should apply a DutyCycle of 12. This would be the point (180,12). We now have two points and can calculate the equation of the line. (Remember, play with your servo . . . your numbers might be slightly different than mine, but the methodology below will work if you use your two points)

Remember slope of a line will be:

m=(y2-y1)/(x2-x1)=(12-2)/180-0)=10/180 = 1/18

We can now get the equation of the line using the point slope formula.

y-y1=m(x-x1)

y-2=1/18*(x-0)

y = 1/18*x + 2

Putting in our actual variables, we get

DutyCycle = 1/18* (DesiredAngle) + 2

Now to change to that position, we simply use the command:

pwm.ChangeDutyCycle(DutyCycle)

I hope this makes sense. Watch the video as I step you through it carefully. If the writeup above does not make sense, hopefully the video will clear things up.

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:

>>>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()

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.

Raspberry Pi 2 Pinout
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.

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

and

$ 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:

>>>GPIO.setmode(GPIO.BOARD)

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.

>>>GPIO.setup(11,GPIO.OUT)

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

>>>GPIO.output(11,True)

Now to turn the LED off, we can do:

>>>GPIO.output(11,False)

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:

>>>GPIO.cleanup()

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.

Raspberry Pi 2 Pinout
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.