Tag Archives: LED

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.

Arduino LESSON 1: Simple Introduction to the Arduino

I think most people would be amazed at how easy it is to program a microcontroller these days. In this video, I show you step-by-step how to write your first microcontroller program.  It is for the Arduiono microcontroller. I chose the arduino because you can buy one for around $20,  so you can get started for next to nothing.

RESOURCES: On all these lessons I will include resources on where you can purchase the items mentioned in the lecture.

Arduino Microcontroller: You can get a good deal on the arduino on Amazon. Arduinos are open source and many are cheap chinese knockoffs, so you want to make sure you get an “official” version, which you can at the link above.

Arduino Beginner’s Kit: While the bare arduino will get you started, I really suggest getting the Beginner’s Kit. The projects I will feature in this tutorial set will use the components in this kit, and it is probably cheaper to go ahead and get the kit than to buy the individual components as you go along. The kit is under $100 on Amazon.