The ’80s saw the rise of single board computers, with prime examples including the ZX Spectrum, Commodore 64, and Amiga computers. But the introduction of the Raspberry Pi has resulted in a huge surge of interest in single board computers. In this article, we will look at making programs on the Pi and an engineer’s first impression of the Raspberry Pi.
Do Not Fear the Pi
Despite being an avid computer fan and hugely passionate about microcontrollers, I was very skeptical of the Pi when it first came out. The computer, based around an ARM processor, is commonly used to run Linux, has video outputs and USB inputs, and includes a GPIO header. When observing the Pi, I would constantly ask questions, including:
- “How do you program the Pi itself?”
- “Why would I even need a Pi if it's just a cheap computer?”
- “Linux is a horrid OS for beginners, so how will this benefit anyone?”
- “There are almost no hardware projects based around it, so how can it be a hardware controller?”
- “What advantage does a Pi have over a microcontroller such as the PIC18 range?”
While many may disagree, these questions were valid when the Pi first came out. The first Pi computer did not have networking and used a composite video output, which made it incompatible with most modern computer displays unless a video converter was used. On top of that, if a program was written with the Raspberry Pi being used as a hardware controller (similar to a PIC), how easy would it be to do? Could this code be made to operate on boot? How difficult would it be to learn? Does the Raspberry Pi have dedicated peripherals, including serial ports, I2C, SPI, timers, ADCs, etc.?
Because of these questions, I hid in my small cave composed of stacked PIC chips and never bought a Raspberry Pi. However, the Raspberry Pi has undergone many changes, with one of the latest major changes being onboard Wi-Fi. The inclusion of onboard Wi-Fi was critical in my decision to stop worrying and love the Pi, as it easily allows the Pi to enter the IoT realm, which, as you know (unless you have been living under a rock), is the next big thing in the electronics industry. Internet access is also important with Linux as software packages can easily be added with the sudo apt-get command, and languages such as Python can have libraries easily added with PIP. So how do you program a Pi? What can you expect and what language should you start off with?
Python loves Pi
The Raspberry Pi usually comes with a copy of Raspbian which, if you don’t have it already, is an absolute must for beginners. The OS, which can be partly tricky to install if you don’t have a pre-programmed SD card, provides you with a near complete environment for programming and office use. The listed programming IDEs include:
- BlueJ Java IDE
- Geany Programmers Editor
- Greenfoot Java IDE
- Python 2 and 3
- Scratch 1 and 2
- Sense HAT Emulator
- Thonny Python IDE
The most common languages you can use on the Raspberry Pi include C, C++, Java, and Python. The Geany IDE provides a simple command-line free method for creating C programs, while BlueJ and Greenfoot are useful for Java development. Python programs can be written using the Python 2 IDE, Python 3 IDE (recommended over Python 2), and Thonny. If you are new to Python, Thonny may be a better alternative to the standard Python IDE, as it is designed for beginners with an easy debugging system, where Python code can be executed step by step and variables seen.
Which language should you use? No idea! Having said that, and after lots of study and considering my needs (IoT, voice recognition, GUI apps, games and graphical routines), I decided that Python is the way forward on the Raspberry Pi.
Why Python? First, Python is a very easy language to get straight into (especially if you already have programming experience), as there is very little messing around. For example, Java programs require main functions with lots of setting up before they even execute, and C programs require make files and are notoriously difficult to use with modern features such as GUIs. If you open the Python IDE, create a new file, enter “Print(“Hello”)” and run that, it will print “Hello”. No computer language in existence is simpler than Python when it comes to running basic programs, yet advanced programs can be designed, which has the other major languages on their toes. Of course the major issue with Python is its slow speed when compared to other languages, but for most projects, Python will be suitable.
Your First Python Program
When learning any computer language, there are three ways you can go. The first way involves hours of example code and tutorials, the second way is doing the same but not enjoying it, and the third way is to think of a program that you would find useful and then attempt to make it. So my first Python program, believe it or not, was a PC-based oscilloscope that involved streaming data from a serial port and then graphically displaying the data using PyGame. It should be stated that I am an experienced programmer, so I knew what to look for when making the program, but that should not dissuade you from trying something a little more complex.
The golden rule in any language is GIYF: Google is your friend. You will not believe the number of tutorials, example code, and other source code for every library that Python has to offer. If you get stuck and cannot figure it out, you can Google error codes and ask others online. Chances are you will solve your problem in 10 minutes.
Now, down to business! You will need to start by loading the Python 3 IDE, which can be done by clicking the Raspberry Pi Logo start menu (top left). Click programming, then Python 3. A window will pop up, which is the main Python interface that can be used to enter code directly or be used as the console output for Python programs. From the main console window you need to click File > New File and in the window that pops up click “Save As” and save the file. The name of the file does not matter, but for now just call it “MyFirstProgram”. Once saved, enter the following code and then save the file.
A = 10 B = int(input(“Enter a number: ”)) print(A + B)
Save the file with the code copied in and then run the program by either pressing F5 or going to Run > Run Module. You should be taken to the main Python console and be prompted with the question “Enter a number:”. Then when you enter a number, the console should print the number you entered plus 10. But what happened here and why did it happen?
Python executes code line by line and unlike other languages which look for a main function, Python executes code from the very first line in the file. So in our case, the first thing that Python did was created a variable called A (a number that we can assign numbers to), and assign it the value of 10.
A = 10
The second instruction that Python executed was creating the variable named B and then assigning it the value of what the user enters. User input can be obtained in Python by using the input() function, and if we put some text inside the input function, the Python program will print that before getting the user’s input. So in our case, we wanted to print the string “Enter a number:”, so to do this we put this string into the function. But input returns a string (a written sentence) instead of a number (integer), so to convert the user input into a number, we stick int next to the input function.
int( input(“Enter a number: ”) )
The last instruction takes the variables A and B and then adds the two together. The result is then printed to the console window, and this marks the end of the program!
print(A + B)
This program, albeit small, uses a number of programming paradigms, including functions, variables, and conversions, but these are all very easy to understand. While this article will not go into more depth, you can already see the simplicity involved with Python and how you will be getting programs going in no time.
The Raspberry Pi is a fantastic single board computer with plenty of CPU power for most user applications, GPIO for hardware projects such as robots, and a user-friendly Linux distribution, which means that even those who are completely new to programming should have no problem getting into it!