Raspberry pi programming in python

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.

Читайте также:  Get path environment variable in java

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:

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

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:

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

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

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:

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:

Type the following script to display the “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:

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:

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:

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.

Источник

How to Write and Run a Python Program on the Raspberry Pi

Circuit Basics

In this post, I’ll give you a quick overview of what a Python program is, what Python programs can be used for, and how to write and run a simple Python program on the Raspberry Pi.

PCBWay Ad

What is a Python Program?

Python is a very useful programming language that has an easy to read syntax, and allows programmers to use fewer lines of code than would be possible in languages such as assembly, C, or Java.

The Python programming language actually started as a scripting language for Linux. Python programs are similar to shell scripts in that the files contain a series of commands that the computer executes from top to bottom.

Compare a “hello world” program written in C to the same program written in Python:

Hello World Program in Python vs C Programming

Unlike C programs, Python programs don’t need to be compiled before running them. However, you will need to install the Python interpreter on your computer to run them. The Python interpreter is a program that reads Python files and executes the code.

It is possible to run Python programs without the Python interpreter installed though. Programs like Py2exe or Pyinstaller will package your Python code into stand-alone executable programs.

BONUS: Download the Raspberry Pi programming cheat sheet – a one page PDF guide with instructions on how to create and execute C programs, Python programs, and Shell scripts.

What Can a Python Program Do?

Like shell scripts, Python can automate tasks like batch renaming and moving large amounts of files. It can be used just like a command line with IDLE, Python’s REPL (read, eval, print, loop) function. However, there are more useful things you can do with Python. For example, you can use Python to program things like:

  • Web applications
  • Desktop applications and utilities
  • Special GUIs
  • Small databases
  • 2D games

Python also has a large collection of libraries, which speeds up the development process. There are libraries for everything you can think of – game programming, rendering graphics, GUI interfaces, web frameworks, and scientific computing.

Many (but not all) of the things you can do in C can be done in Python. Python is generally slower at computations than C, but its ease of use makes Python an ideal language for prototyping programs and designing applications that aren’t computationally intensive.

How to Write and Run a Program in Python

We’ll only cover the basics of writing and executing a Python program here, but a great tutorial covering everything a programmer needs to know about Python is the book Learning Python 5th Ed. (O’Reilly) by Mark Lutz.

Installing and Updating Python

Python 2 and Python 3 come pre-installed on Raspbian operating systems, but to install Python on another Linux OS or to update it, simply run one of these commands at the command prompt:

sudo apt-get install python3

Installs or updates Python 3.

sudo apt-get install python

Installs or updates Python 2.

Opening the Python REPL

To access the Python REPL (where you can enter Python commands just like the command line) enter python or python3 depending on which version you want to use:

Python and python3 REPL on the Raspberry Pi

Enter Ctrl-D to exit the REPL.

Writing a Python Program

To demonstrate creating and executing a Python program, we’ll make a simple “hello world” program. To begin, open the Nano text editor and create a new file named hello-world.py by entering this at the command prompt:

Enter this code into Nano, then press Ctrl-X and Y to exit and save the file:

#!/usr/bin/python print "Hello, World!";

All Python program files will need to be saved with a “.py” extension. You can write the program in any text editor such as Notepad or Notepad++, just be sure to save the file with a “.py” extension.

Running a Python Program

To run the program without making it executable, navigate to the location where you saved your file, and enter this at the command prompt:

Make a Python File Executable

Making a Python program executable allows you to run the program without entering python before the file name. You can make a file executable by entering this at the command prompt:

Now to run the program, all you need to enter is:

Here are some additional resources that will help you make the most out of programming in Python:

Hopefully you found this post useful. If you have any questions, feel free to leave a comment below. If you know anyone else that would enjoy this article, please share it! You can also get updates when ever we post a new article by subscribing!

Источник

Оцените статью