raspberry pi

Raspberry Pi first Program and Basic commands, Python Programming

Description:

 

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.


allpcb circuit

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

  1. How to create a folder
  2. How to display files and folders
  3. How to create a file inside a specific folder
  4. How to write a basic hello world program in python
  5. How to run a program
  6. How to edit a program and so on.

Without any further delay let’s get started!!!

Amazon Links:

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

DISCLAIMER:

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.

Raspberry Pi first Program

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.

Raspberry Pi first Program

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.

Raspberry Pi first Program

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”.

Raspberry Pi first Program

If you have entered the login name and password correctly then you will be logged in.

Raspberry Pi first Program

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.

Raspberry Pi first Program

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.

Raspberry Pi first Program

After you press the enter key on your keyboard, everything on the screen disappears.

Raspberry Pi first Program

How to change the directory in Raspberry?

Now let’s again display the folders and files using the ls command.

Raspberry Pi first Program


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.

Raspberry Pi first Program

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.

Raspberry Pi first Program

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.

Raspberry Pi first Program

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.

Raspberry Pi first Program

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.

Raspberry Pi first Program

The editor will open, this is where you can write your program. Let’s write a very basic hello world program.

Raspberry Pi first 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.

Raspberry Pi first Program

to run this program simply type python first.py and press enter.

Raspberry Pi first Program

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?

Write

nano first.py

and enter.

Raspberry Pi first Program

as you can see file first.py is unwritable. Press control x to exit.

Now write

sudo nano first.py

and enter.

Raspberry Pi first Program

Press y.

Raspberry Pi first Program


Now write anything you want to print.

Raspberry Pi first Program

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.

Raspberry Pi first 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.

Raspberry Pi first Program

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:

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button