Create and run a Python program on Raspberry Pi


May 15, 2019 • 0 • 0

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.

Create and run a Python program on Raspberry Pi - Raspberry Lab

Prerequisites

To blink an LED using a Raspberry Pi, you'll need:

Once everything is gathered, let’s do the wiring.

Wiring an LED to the Raspberry Pi

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.

If voltage and current are not provided by the manufacturer, a 100Ω resistor is generally sufficient.

Information

Which resistor to choose?

To determine the optimal resistor value, simply apply loop and Ohm’s laws as shown in the diagram.

Calcul resistance necessaire pour LED Raspberry Francais

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.

Schéma du branchemant d'une LED au Raspberry 3
Information

How a breadboard works

A breadboard lets you easily connect components together without soldering. The board has two areas:

  • Side rails: Connect VCC and GND here. They’re available along the whole line. Here, they’re not used as we only power an LED.
  • Center area: Components are connected here. Each hole in a row is linked to the others, but rows are separated by a gap in the middle. Usually, row numbers are shown every 5 rows.

A diagram makes it easier to understand. Each colored line represents a set of interconnected holes.

Fonctionnement plaque d'essai / Breadboard - Raspberry Lab

Create the Python .py file

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.

Writing the Python program

For the first program, I commented line by line :)

Save the program

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.

Run the Python program

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.

Read next

Article image

Connect to Raspberry Pi via SSH


Article image

Choose and use a camera on Raspberry Pi