How To Start Raspberry Pi Programming

How To Start Raspberry Pi Programming

For people new to this platform, I show how to start Raspberry Pi programming with Python. I go through the guide step by step to help the new programmer execute their first Python program. Also, I include links to free software download below.

This article is more about the process than the software used. So if you are already familiar with code editors and FTP clients, you can use whatever software you prefer. However, the FTP client needs to support SFTP – check out the last article if you need to know more. Also, the code editor should support Python if you prefer to use features like code highlighting, auto-completion and function list and so on.

Pi Wars 2018 Update

So now I am preparing to start writing Python code. Also, my application to join the Pi Wars event has been submitted – good luck to me and the team. Furthermore, I’ve been looking at the various line following sensors. But after looking at the challenges, there is no longer a line following course challenge.

However, the straight-line speed test challenge has a white line going down the middle of the course. So the image sensor I was going to use for line following may also so work for this challenge.

I’ll be writing a brief about each challenge in future articles so stay tuned to catch those.

Raspberry Pi Programming

Hardware And Software Set-up

The set-up for this guide is a headless Raspberry Pi that is being programmed over Wi-Fi from a PC or laptop. The software installed on the PC, we will assume Notepad++ and FileZilla FTP client. Check earlier articles in this series to set up a headless Raspberry Pi.

Mac and Linux

If you are on a PC platform other than Windows you might consider using Brackets text editor. This editor supports plugins to enhance Python programming experience. Also, Brackets has plugins to enable SFTP file upload ability. Therefore reducing the need for a separate SFTP client.

Find links to featured software that is free to download below.

Raspberry Pi Programming Text Editor

Notepad++ Python Compatible Code Editor
Notepad++ Python Compatible Code Editor.

Using a code editor, which is essentially a powerful text editor, will make it a lot easier for Raspberry Pi programming. For instance using an editor installed on a platform we are familiar with will allow us to focus more on coding. Thus with practice over time we can naturally begin to look at other ways to write code.

Backup and File Structure
Raspberry Pi Programming File Structure Example
Raspberry Pi Programming File Structure Example

In this workflow of code editing, we will be writing code in the text editor, while also saving the code file on the PC. When we come to test the code we have written, we use the FTP client to transfer the saved code file to the Raspberry Pi.

File Backup

Thus in the workflow, we have file backup built in. So the backup file is saved on the PC and the working copy is sent to the Raspberry Pi. Had we run a text editor in the terminal window to edit our code file, we would have had to remember to backup the files after each session.

File Structure

When Raspberry Pi programming you need to be aware that there are three versions of Python installed on the Raspbian Stretch install. So executing your code with python will use the older version of Python. Thus executing code with python3 or python3.5 will use the newer versions of Python.

Raspberry Pi Programming File Structures
Raspberry Pi Programming File Structures.

The Python code that you write might not be cross-version compatible so it is a good idea to name the root folder of your Raspberry Pi Programming projects by the Python version. For example, if you are using python3 to run your code the root folder should be called python3.

The illustration above shows matching file structure between PC and Raspberry Pi. For example, python3 version is used in this case. So when I want to run code in any of the projects it contains, I know I need to use python3.

Raspberry Pi Programming Exercise

So now we are going to write a line of code and save it to our pyhon3/helloworld folder. Then we will transfer the folder with its contents to the Raspberry Pi using FTP client. Then we run the code. So let us get started.

Raspberry Pi Programming Folder Tree
Raspberry Pi Programming Folder Tree

Firstly create folders like the above illustration where the Documents folder is an existing folder of your choice on the PC. The file helloworld.py will be created later.

But note that file and folder names will be case sensitive on the Raspberry Pi, thus helloworld.py will be different to HelloWorld.py. Also for readability and consistency, best to avoid using spaces in names.

Hello World Test File
Hello World Test File

Secondly, start a new document and enter the following code.

print("Hello World")

Then save the file to the python3/helloworld/ folder on the PC.

Next, use the FTP client to copy the python3 folder and its contents to the Raspberry Pi home directory. Details about using the FTP client is in the last article.

Run Hello World In Terminal
Run Hello World In Terminal

Use an SSH client like PuTTY to open a terminal window and log in to your Pi account. Then change directory using the following.

cd python3/helloworld

The above directory or folder must exist for the above command to work.

Lastly, run the next command to execute the helloword.py program.

python3 helloworld.py

So the output of the program will be Hello World.

This is just the start. Follow this Pi Wars 2018 Series to get useful code snippets to run on your Raspberry Pi.

Links to featured

  • Pi Wars – Link.
  • Pi Wars USA – Link.
  • FileZilla – Link.
  • Notepad++ – Link.
  • Brackets – Link.

Related Articles

Pi Wars 2018 Raspberry Pi Headless Install

Raspberry Pi Headless Install Without A Display Guide – Link.

SFTP Client Connection To Raspberry Pi

SFTP Client connection to Raspberry Pi – Link.

Buying Featured Items

The purchase price is going to vary greatly depending on how quickly you want the items. Therefore shop around checking out Amazon, Ebay, Adafruit and local electronic stores.

UK Searches:

UK Amazon:

  • Raspberry Pi – Search
  • MicroSD Card – Search
  • Raspberry Pi Compatible Wi-Fi Dongle – Search

US Searches:

US Amazon:

  • Raspberry Pi – Search
  • MicroSD Card – Search
  • Raspberry Pi Compatible Wi-Fi Dongle – Search

On Closing

I hope you find this article useful – How To Start Raspberry Pi Programming, please like and share.