Raspberry Pi

How to program the Raspberry Pi using the Python

A programming language tells the computer what to do by executing the code written with the help of characters and strings for a specific output. Although there are many programming languages that can be used on Raspberry Pi to program it like C and Java, in this guide, we are going to use Python programming language to program the Raspberry Pi.

What is a Python programming language

Python is a high-level programming language that is used for web development on the server-side to create different web applications, moreover, it is also being used to interact with databases in order to modify the data of the database. The syntax of Python is easy to write as well as to understand but the indentation is very important to consider in the Python programming language. Python language IDE (Integrated Development Environment) Thonny is supported by all the operating systems which include Windows, macOS, and Linux distributions including the Raspberry Pi.

What is a Raspberry Pi

A single-board computer used in different projects like robotics and automation projects is known as a Raspberry Pi. The Raspberry Pi supports many operating systems, but the recommended one is the “Raspberry Pi operating system,” launched by the Raspberry Pi foundation. The Raspberry Pi operating system comes with many Python IDEs like the MU editor and Thonny. We can use Python to program the Raspberry Pi for different tasks.

What are the basics of the Python Programming language

The syntax of every programming language is different from other programming languages and the main objective is to learn the syntax of the specific programming language. The Python programming language is simple and in this section, we will explain some basic commands of the Python programming language.

Printing a string: We can print the strings by storing them in the variables as well as we can also print the strings directly by using the print command. Both are discussed. We will first print the “Hello! Welcome to LinuxHint” by storing it in the “Welcome” variable, and next, we will print the exact string directly using the print command in Thonny Editor:

Welcome = “Hello! Welcome to LinuxHint”
print (Welcome)

The output is:

Now we will print the string without storing it in the variable:

print (“Hello! Welcome to LinuxHint”)

The output of the above command will be:

Using loops in Python: Like other programming languages, we can use the for loop and while loop, the syntax of the for loop in Python Programming language is:

operating_system=[“Linux”,” RaspberryPi”,” Windows”]
for x in operating_system:
    print(x)

The output of the above for loop in the Python is:

The output of the above for loop in the Python is:

i=1
while(i<5):
     print(i)
     i=i+1

The output of the above Python while loop is:

Using conditional statements in Python: Another important structure of programming is the conditional statements in which the output of the program is decided on some conditions. These conditional statements contain the switch statements and if-else statements. The most basic and convenient one is if-else statements which are going to be explained with the help of the next Python script. Copy and paste the mentioned below python code in any IDE, we are using the Thonny IDE:

x = 5
if x > 0:
    print("Number is greater than zero")
else:
     print("Number is less than zero")

Explanation of code: In the above Python code, we have stored five numbers in the variable “x” and then with the help of an if-else statement we print the output on the basis of the value stored in x. If the value of x is equal and greater than 0, then the if statement body will be executed. If the value of “x” is less than 0 then the else body will be executed.

Click on the “Run” icon on the toolbar of Thonny to execute the above Python code:

How to program Raspberry Pi using the terminal

We can also use the terminal to make, compile and run the Python programs, for this, we will first make a file using any text editor and name it using the extension “.py”, in our case, we are going to create a file with the “LinuxHint.py” using the command:

$ nano LinuxHint.py

Type the following script to display the “Welcome to LinuxHint”:

print (“Welcome to LinuxHint”)

Save the file using the shortcut key of the nano editor, CTRL+S then exit the editor and run the script of the file by using the command:

$ python LinuxHint.py

Note: Make sure the Python packages have been installed on the Raspberry Pi, if they are not installed, then you can use the apt package manager to install the Python package from the repository of the Raspberry Pi operating system.

How to use the Python gpiozero library to program LED with Raspberry Pi 4

The gpiozero library contains a number of functions that are used to manage the GPIO pins for different applications. For understanding, we will use the gpiozero library to control the LED connected to the GPIO pin of the LED. We require the components which are a breadboard on which the hardware is configured, some male to female jumper wires for making connections, and the Raspberry Pi 4 along with a 220 ohms resistor and a LED. For the connections of the LED with the Raspberry Pi 4 and the hardware configuration will look like this:

Proceeding towards the Python code to control the led, we will first create a text file using the nano editor and will name it LedProject.py:

$ nano LedProject.py

Copy and paste the mentioned below Python code in the newly created text file with the name of LedProject and then exit the nano editor by saving the file:

from gpiozero import LED #imports the LED functions from gpiozero library
from time import sleep   #imports the sleep function from time library
led = LED(17)            #declared gpio pin 17 for LED and store it in led
while True:              
#initiated the while loop until the conditions are true
          led.on()       #turn on led
          sleep(2)       #generate a delay of 2 seconds
          led.off()      #turn off led
          sleep(2)       #generate a delay of 2 seconds

In the Python code used to control the LED, we simply used the functions of the LED by importing them from the gpiozero library and also importing the sleep functions from the time library. Then simply in the infinite loop, we have turned on and turned off the LED connected at GPIO pin 17 with a delay of 2 seconds.
We will run the following command to execute the Python code written in the LedProject.py:

$ sudo python LedProject.py

The working of the LedProject.py Python code in the hardware is shown below:

Conclusion

The Raspberry Pi can be programmed using different programming languages, but it is convenient to use the Python programming language. The Raspberry Pi operating system contains many IDEs for the Python programming language. In this write-up, we have learned different basic commands of Python programming.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.