Raspberry Pi first Program and Basic commands, Python Programming

(Last Updated On: August 5, 2019)

Raspberry Pi first Program

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.


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 setup 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!!!

The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:

Raspberry pi 3 full package: https://amzn.to/2PA0UWF
Power bank: https://amzn.to/2DQD2rJ
Arduino Uno: https://amzn.to/2tVz1vu
Mega 2560: https://amzn.to/2ze1kdu
GSM sim900A: https://amzn.to/2LLeFzy
Neo 6m GPS module: https://amzn.to/32XCYiX
Mq-2 gas/smoke sensor: https://amzn.to/313Sx6O
Nodemcu esp8266 wifi module: Best price https://amzn.to/2NAvmiB
LM7805 Voltage Regulator: https://amzn.to/2IZEl5x
Potentiometer: https://amzn.to/2JSLbfg
330 ohm resistors pack: https://amzn.to/2NtlAeh
5v 2A adaptor: https://amzn.to/2GsNfcw
2n2222 npn transistors: https://amzn.to/2EmXsIb
10k Resistor: https://amzn.to/2KTd6OW
lm7805 Voltage Regulator: https://amzn.to/2IZEl5x
330-ohm resistors pack: https://amzn.to/2NtlAeh
LDR pack: https://amzn.to/2m2khGR
female DC power jack socket: https://amzn.to/2KEo1gt
470 uf capacitors: https://amzn.to/2xDCOzf
5×7 cm vero board: https://amzn.to/2OHGbvn
female headers: https://amzn.to/2zqmtiJ
connection wires: https://amzn.to/2DpBuW7
Super Starter kit for Beginners: https://amzn.to/2KJvmKG
Jumper Wires: https://amzn.to/2KMoVXs
Bread Board: https://amzn.to/2MS4q8X
12v Adaptor: https://amzn.to/2Ntr6h1
PCB plate: https://amzn.to/2IUwpCt
Variable Supply: https://amzn.to/2MT4Qfj
Digital Multimeter: https://amzn.to/2Nvft9i
Vero Board / strip board: https://amzn.to/2MTf9jD
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2zfoNuJ
Solder wire: https://amzn.to/2ufUMWf
Wire Stripper: https://amzn.to/2KOqxfU
wire cutter: https://amzn.to/2ucIq14
PCB small portable drill machine: https://amzn.to/2Nu62XF


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:

 

Recommended For You

About the Author: 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...

Leave a Reply