Raspberry Pi: Python LED Blink

In this post we will look at what it takes to blink an LED on the Raspberry Pi using the GPIO pins and Visual Studio. We will use Visual Studio to show the process of software development. We will discuss the electrical components needed to attach an LED and resistor to the Raspberry Pi. We will also dive into the code required to turn the LED on and off.

Start connecting to your Raspberry Pi using a keyboard/mouse/computer screen or through VNC from your desktop computer. From the top left of the screen select the Visual Studio IDE.

Visual Studio restarts where we left off when we last worked in the environment.

From here we click on File -> New File

Note that we now have an option to start a Python file since we have developed at least one Python file in the IDE.

We are now ready to start development. Note that the file is Untitled. We can save it as a file with the “.py” extension of just start typing code.

If we start typing the IDE will suggest what we might want to finish. In this instance we want to include the gpiozero module so that we can talk to the GPIO pins from our program.

You do need to know a little about the module and interfaces/routines that are available in the module. In this example, we are pulling in the gpiozero module and talking to an LED method.

The import command says pull in the LED method from the gpiozero module and make it available to use. We want to pull in the sleep command as well so that we can loop and delay between turning on and off the LED. When we call the LED() method the IDE tells us what parameters are needed to call the method.

In this instance we are using pin 17 as an output to drive the LED. We need to assign the LED to a variable. We select “led” as the variable and call then turn on (with the led.on() call) or off (with the led.off() call). The sleep function allows us to sleep for a number of seconds. The while True statement says anything that is indented below that command will be executed in a loop.

Now that we have the code ready to run, we can save the file with File -> Save File

We call the file gpio_led.py and can now debug the code as desired using the IDE. Before we can debug we need to attach the external LED and resistor to pin 17 and ground.

We connect a wire from GPIO pin 17 to the long side of the LED. The short side of the LED is attached to a resistor. The other side of the resistor is attached to ground on the Raspberry Pi.

When selecting a resistor it is important to note that different color LEDs need different size resistors. Given that the resistor and LED are in series it creates a voltage division between the two elements. The GPIO pin will output 5 volts across both elements.

Putting a small resistor in series with the LED will cause the LED to be brighter. A larger resistor will take more voltage from the LED can cause it to not be as bright.

You need to make sure that the resistor is not too small because it will burn out the LED. If the resistor is too large the LED will never turn on. To calculate the resistor size, we need to know the voltage drop across the LED (which we can get from the chart above) and subtract that number from 5 volts.

In this example we have a 1.8 volt drop from the LED and a 3.2 volt drop needed across the resistor. If we assume 20 milliAmps of current we can either calculate the resistor needed

or look up in a table what minimum resistor value is needed.

In this example we would use a 150 Ohm resistor to get a 2 volt drop across the LED. Note that this would work for most LEDs but not the blue or white LEDs since it would need a 2 volt Vf drop. These LEDs would need a smaller resistor since there would be less voltage across the resistor given that the blue or white LEDs create a 3 volt drop rather than a 2 volt drop. Calculating the resistor size is a bit complex but starting with an LED in the 200 to 330 Ohm range and going up to 1K to 2K are good starting points to keep the LED from burning out. If you use too small of a resistor you risk driving too much voltage across the LED and melting it.

It is also important to note that the LED has two leads, one long and on short.

The long lead connects to the GPIO pin in our example and the short lead connects to the resistor. The other side of the resistor connects to ground. The gpiozero module assumes that you are driving the GPIO pin high to turn on the LED. This assumes that you are driving the anode (long side) and not the cathode (short side). If you put 5 volts on the cathode and ground the anode nothing will happen. The LED will stop current from flowing and will not light up. If you put voltage on the anode and ground the cathode the LED will turn on. By calling the led.on() routine, 5 volts is driven through GPIO pin 17 to go across the LED and resistor.

Running the code from the IDE will start driving pin 17 high and low with a second delay between turning on and turning off.

Note that there is no output in the console since we don’t print anything to the console. To stop the program click on the red outlined box at the top of the code.

Note that the console shows that we are ready to execute more code and is no longer running our Python code. If we want to run the code with a breakpoint we can set a breakpoint by clicking to the left of the line number.

When we try to run again we can only run with Debugger since we have a breakpoint. When we run the code stops at the breakpoint.

At this point we should have the LED shining brightly. Clicking on the Step Over button will execute the led.off() method.

This should turn off the LED and get ready to sleep again. Clicking on the Continue button will run the program until we hit the breakpoint again.

Once we understand how the program works to this point we might want to clear the breakpoint. To do this, right click on the breakpoint and either clear, disable, or edit the breakpoint.

If we had variables defined in the code we could look at the values stored in the code as it runs. Setting a breakpoint at a critical point can help with unknown values or logic associated with code. This is one of the critical improvements that an IDE brings over just developing at the command line. Unfortunately, the IDE does not help with timing issues in code because the IDE introduces a delay into the code as it steps through everything. The code runs much faster without a debugger. The IDE collects data as it debugs so that you can look at variables and state inside your code.

In summary, we can use Visual Studio to create, develop, and debug code on the Raspberry Pi. Using Python we can pull in the gpiozero module and control things like an LED attached to the GPIO pins. We can use the time modules to create delays so that we can see the lights blink on and off. We can change the delay with this code and in future posts we will look at changing the brightness of the LED using different parts of the gpiozero code.