May 15, 2019 • 33 • 4.2
Now that you have installed the Linux Raspberry Pi OS system on your Raspberry Pi, you can start creating your first Python programs. The Raspberry Pi has GPIO input/outputs. They allow communication with external components. For example, we can connect a temperature sensor, then retrieve the data it sends through these GPIOs.
To start, we'll write a very simple program that blinks an LED. This basic program uses the Raspberry Pi GPIOs without requiring prior knowledge.
To blink an LED using a Raspberry Pi, you'll need:
Once everything is gathered, let’s do the wiring.
To connect the LED to the Raspberry Pi, we'll need a resistor. LEDs operate at a voltage and current specified by the manufacturer. Here's how to choose the right resistor.
To determine the optimal resistor value, simply apply loop and Ohm’s laws as shown in the diagram.
For an LED operating at 2V, 20mA, we need:
R = (3.3-2)/0.02
= 65Ω
If the exact calculated value doesn't exist, pick the closest standard value.
A breadboard lets you easily connect components together without soldering. The board has two areas:
A diagram makes it easier to understand. Each colored line represents a set of interconnected holes.
Once everything is wired, boot your Raspberry Pi and connect via keyboard/mouse or SSH to write your first Python program on Raspberry Pi.
Let’s start by creating a file. As Raspberry Pi OS is a Linux distribution, the commands are standard Linux commands you may already know Simply use:
sudo nano filename.extension
For example, we can name the program as follows:
sudo nano led.py
This command creates the led.py file in the home directory (/home/pi by default) and opens it in a text editor.
For the first program, I commented line by line :)
Once we finish editing the file, we need to save and exit the editor. Press Ctrl+X, then y (for yes), and finally press Enter. We return to the command prompt having closed the editor.
To run the program we created, just enter:
sudo python led.py
Therefore, the general command to run a Python program is:
sudo python filename.py
This second beginner tutorial is complete. We learned how to create and run a Python program on the Raspberry Pi to blink an LED.
Connect to Raspberry Pi via SSH
Choose and use a camera on Raspberry Pi
Raspberry Pi 5 (8 GB version)
90.70 €
Raspberry Pi camera 5MP 1080p
12.95 €




4.2/5 | 53 votes