Table of Contents
Raspberry Pi first Program and Basic commands- This tutorial is a beginners level tutorial which explains how to write your first python program for Raspberry Pi and what are the most frequently used commands that every beginner should know. This Tutorial will help you quickly understand the python programming environment and moreover the commands I am going to share with you are almost used in every Raspberry Pi project. Although there are many Raspberry Pi commands, but for now you should only practice on the commands that I am going to share with you.
But before you can write your first program in python, you will need to set up your Raspberry Pi correctly so that you can access your raspberry. This is my second tutorial, which will only focus on how to use the most basic Raspberry Pi commands and then how to write your first python program for Raspberry Pi.
In my previous tutorial, I explained how you can set up your SSH network using Putty software so that you can wirelessly access your raspberry pi, using your laptop keyboard and mouse. I recommend you should watch this tutorial; the link is given in the related projects section. In this tutorial, we will cover
- How to create a folder
- How to display files and folders
- How to create a file inside a specific folder
- How to write a basic hello world program in python
- How to run a program
- How to edit a program and so on.
Without any further delay let’s get started!!!
Other Tools and Components:
Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Before you write a program, First of all Power up your raspberry pi, there is no need for a keyboard and mouse, as we will be using our laptop’s keyboard and mouse. If you are not using the SSH Network then you will need to connect the Keyboard and Mouse and you will also need an LCD. But as I have already set up SSH network, so I will use my laptop’s keyboard and Mouse.
As you can see currently no input and output devices are connected with the GPIO pins, as in this tutorial we are not using any sensors. The only thing we need to do is to power up the Raspberry Pi.
Our hardware is ready now let’s open the putty software, enter the IP address of your raspberry pi, if you don’t know about the IP address, watch my previous tutorial. Then click open.
After you click on the Open button you will be asked to enter your login name and password. Simply enter the login name as “pi” and the password is “raspberry”.
If you have entered the login name and password correctly then you will be logged in.
As you can see we are logged in.
Following are the most commonly used commands in Raspberry Pi.
Raspberry pi display files and folders:
First of all let’s start with the ls command which is one of the most frequently used commands, this command is used to display the folders and files. It’s use is very simply. Simply Write ls and press enter.
As you can see the list of all the folders and files.
Raspberry Pi clear screen command:
Another frequently used command is the clear command, which is used to clear the screen. Now to clear the screen you simply write clear and press enter on your keyboard.
After you press the enter key on your keyboard, everything on the screen disappears.
How to change the directory in Raspberry?
Now let’s again display the folders and files using the ls command.
Now this time let’s say we want to open the test folder. we simply write cd test and press enter. Using the ls command we can check what is inside this folder.
As you can see this folder has two files foo.txt and temp.py. to exit this folder we simply write cd and press enter.
How to make a Director in Raspberry Pi?
Another most frequently used command is the mkdir command. which is used for creating a folder. write mkdir followed by the folder name. Before we use this command first let’s clear the screen using the clear command. Now let’s say we want to create another folder with the name youtube. We simply write mkdir youtube and press enter. This command will create a folder with the name youtube. Write the ls command to check if the folder is created or not.
As you can see its created. now to open this folder simply write cd youtube and press enter. now if I use the ls command, you can see it has no files and folders.
Youtube is now our current working folder.
How to Write and Run a Python Program on the Raspberry Pi ?
Let’s create our first python program file with extension .py. simply write sudo nano first.py and press enter as you can see in the picture above. You can select any name but make sure to add the extension .py.
The editor will open, this is where you can write your program. Let’s write a very basic hello world program.
Unlike Arduino programming we don’t need any semi colons at the end. Press control o on your keyboard to save this file and simply press enter as we have already created a file, now press the control + x to exit the editor.
Now if we write the ls, you can see the youtube folder has one file first.py.
to run this program simply type python first.py and press enter.
You can see the hello world message. now let’s say you want to edit this program.
How to Edit the Raspberry Pi Python Program?
as you can see file first.py is unwritable. Press control x to exit.
sudo nano first.py
Now write anything you want to print.
Press control o to save the file and then at the end press control x to exit the editor. write python first.py to run this program.
How to delete a file in Raspberry Pi?
Now let’s say we have two files and we want to delete one file, for this we have a command rm simple write sudo rm then the file name and press enter.
As you can see the file is deleted. So that’s all for now. These were the most basic commands used in Raspberry Pi. You should practice on these commands. For the best understanding and step by step instructions you can also watch a video tutorial given below.
Watch Video Tutorial: