What programming language should you use with the Raspberry Pi?
If you are new to the Raspberry Pi, you might find yourself at the crossroads of choosing a programming language to learn.
There are so many languages out there and what you decide to learn will have a profound effect on what you can do. So, let’s take a look at the languages you can use with the Raspberry Pi.
Pre-loaded languages on the Raspberry Pi
Perhaps the biggest hint you can take from Raspberry Pi is the languages that come pre-loaded onto the Raspberry Pi OS.
On every installation, you will have Python, Scratch and C or C++ pre-installed.
So let’s take a look at these three languages first.
Python: great general purpose language for Raspberry Pi
- Easy to learn
- A lot of libraries for components
- Lots of great tutorials to get you up to speed
- Can be used on the Raspberry Pi microcomputers and Pico series
- Not suitable for website development (front end, specifically)
- Slower than C or C++
If you are starting from zero, then Python is probably the quickest and most efficient way for you to get started.
For one, Python is one of the easier languages to pick up.
Another reason is that there are a lot of libraries written in Python for various sensors and components. As a result, a good amount of scripts for tutorials and projects will be written in Python.
Finally, learning Python will help you interact with all boards in Raspberry Pi’s lineup. You can create projects for the Raspberry Pi 4 as well as the Raspberry Pi Pico.
The makers of the Raspberry Pi also make it easy for you to build projects in Python. For one, all Raspberry Pi microcomputers (e.g. Pi 4) come with Thonny which is a Python IDE and all Raspberry Pi Picos will read MicroPython by default.
Thonny makes Python easy on the Raspberry Pi
Raspberry Pi OS also comes pre-loaded with Thonny, which makes it really easy to run Python scripts.
Thonny makes programming in Python easy because you can easily start and stop scripts by pressing the “RUN” or “STOP” button in the IDE.
Whereas, if you use another editor, you will probably have to type the script into the Terminal in order to run it.
MicroPython for Raspberry Pi Pico
Python is particularly useful to learn if you want to use the Raspberry Pi Pico.
Unlike the Raspberry Pi microcomputers, the Raspberry Pi Pico will only take three languages by default: C or C++ and MicroPython.
This means that if you have learned Python, you will be able to transition into writing MicroPython, since the syntax is the same.
The main difference between Python and MicroPython is the latter having a smaller standard library.
In addition to MicroPython, CircuitPython is another Python derivative that is used in the Raspberry Pi Pico. CircuitPython is created by Adafruit.
Is Python a good language for Raspberry Pi users?
You will definitely need to know Python if you want to build projects for the Raspberry Pi.
I found a script online but it was in Python and that sent me down the path of learning Python.
If you want the most efficient, quickest and easiest way to build Raspberry Pi projects, then learning Python is the way to go.
Where to learn Python for Raspberry Pi programming
Learning path — these resources compile links where you can learn specific parts of Python. They are general learning paths and not specific to the Raspberry Pi.
Harvard OpenCourseWare, CS50’s Introduction to Programming with Python
Corey Schafer’s Python beginner tutorial playlist
Raspberry Pi-specific courses — These are courses meant for beginners
Raspberry Pi Foundation — this course will give you a general overview of programming in Python and it dives into game programming and interaction with sensors and components.
Real Python, Build Physical Projects With Python on the Raspberry Pi — this is a great guide for people who have a sensor and component kit and want to dive into code that interacts with the GPIO. Unlike general Python tutorials, these will teach you how to wire up simple sensors and how to interact with them with a Raspberry Pi 4.
Scratch for Raspberry Pi
One of my friends was showing me to his LED traffic light that he did with his Raspberry Pi 4.
He used Scratch.
Why did a senior software guy use Scratch?
Scratch describes itself as “the world’s largest coding community for children and a coding language with a simple visual interface that allows young people to create digital stories, games, and animations.”
My friend was definitely not a child.
When I asked him why, he gave me an answer that was as useful as a Git commit message, “well its simple to program”.
To be fair, when I visited the Raspberry Pi Store in Cambridge, UK, they also had a Pi running Scratch.
So, it must be good for something!
Is Scratch a good programming language for Raspberry Pi users?
I suppose it’s good for scratching the surface of programming.
The whole premise of Scratch is to make programming easy for kids and teens. Instead of writing abstract code, Scratch allows you to create programs by joining blocks together.
In order to blink an LED with Scratch, you’d assemble these blocks:
Now, compare this to a Python script to blink the LED,
import RPi.GPIO as GPIO from time import sleep GPIO.setmode(GPIO.BOARD) GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) while True: GPIO.output(8, GPIO.HIGH) sleep(1) GPIO.output(8, GPIO.LOW) sleep(1)
Perhaps it’s the same reason why some people prefer to use a GUI instead of a command line. Instead of changing the value of a variable with several backspaces, arrow keys and other key presses, you would instead just click and pick.
For simple projects, learning and teaching, Scratch is a great choice.
Downsides to Scratch for Raspberry Pi
If you missed the pun in the previous section, here it is again: Scratch only scratches the surface.
Inevitably, once your project becomes bigger, you will want to use a more mainstream language like Python or C or C++ because you will be able to access the wealth of code, libraries and scripts available online.
If you’re curious how Scratch works, you can quickly get a feeling of how its “building blocks” method works on its online environment.
- Raspberry Pi has a few Scratch courses, including ones that involve hardware building.
- Aaron Maurer has a YouTube tutorial to show you how to use Scratch 3 and blink an LED.
C or C++ for Raspberry Pi
The ultimate programming languages that give you ultimate control are also one of the hardest to learn and master.
What is the difference between C and C++?
C++ is in a way an extended version of C, which includes a lot of features like native designed-in object oriented programming support. The ++ is a pun on the way operators can be incremented in C and C++ itself. For example if you have a variable cats, and want to add one to it, you can simply write cats++;
C++ especially is very dynamic and constantly gets updates. It is a complex language to learn. For beginners, C might be a more appropriate language to choose.
What is the main difference to other languages used for the Pi?
These languages are compiled – that is you write code, and then use a special application called a compiler to produce machine byte code which the processor of your target architecture understands natively. In other words, if you want to run an application on both the Pico W and the Raspberry Pi, and your desktop computer (x86 based), you will need to run a specific compiler for each of them.
Another example of a compiled language is Rust, which has some big advantages. You see, C and C++ need you to take care of certain things like garbage collection – if you don’t do that, your application might crash. Or the whole computer might crash if you write device drivers. With Rust there are additional safe guards for the developer.
Another popular compiled language developed by Google is called Go. You might notice references to them from time to time.
My suggestion for you to get started if you have never coded before are interpreted languages. They do not need a compilation step, but are read in by an application which then executes the right command. A good language to get started with is Python (or MicroPython).
However, if you ever want to create mind-blowing projects like playing Doom on a Raspberry Pi Pico then you’ll need to know C, or C++.
C or C++ are generally one of the three languages that’s most widely used on the Raspberry Pi, the other being Python. Therefore, a good portion of this section will talk about the benefits and cons of the two.
Let’s start by comparing the three:
|C and C++||MicroPython|
|Memory management||Manual||Automatic (garbage collection)|
|Usage by community||Wide usage, especially used in advanced projects where minute control is necessary||Widely used by beginners to advanced users.|
|Libraries for components||Generally available||Generally available|
Should a beginner learn C or C++?
There’s nothing stopping you from going through a trial by fire.
Because what doesn’t kill you makes you stronger.
Most projects can be achieved with Python as well as C or C++. The availability of libraries for the three languages are also widespread and you’re unlikely to get bottlenecked by the lack of a library.
How tenacious are you?
C or C++ can be a bit less fun for the beginner programmer because there’s a lot of aspects that you would have to think about which you wouldn’t in Python.
For example, in C and C++, you’ll have to manage your memory. You’ll also need to think about what kind of data type to assign a variable. It can get tedious if you are not aware of the implications of the pros and cons of choosing a type.
For example, if you want to assign an integer in C and C++, you will have to decide if you want:
- unsigned char
- signed char
- unsigned int
- unsigned short
- unsigned long
The choice of one of these will have implications on your program’s speed and memory usage.
And while Python is less efficient, you will only need to do this in order to declare an integer:
integer = 999999999999999
But no pain, no gain, right?
Once you’re familiar with how C or C++ work, you’ll start reaping the benefits. As the Raspberry Pi isn’t a very powerful computer by today’s standards, being able to write efficient code will help you get the best performance out of its limited hardware.
Setting up C and C++ takes more time
When the Raspberry Pi Pico boards came out, I had to write about setting up the Pico for Python and C and C++.
Python was super easy. Download Thonny. Plug in Pico. Flash MicroPython. Done.
C and C++ were much harder and more tedious. It’s evident — Raspberry Pi publishes a PDF just on the topic of C/C++ set up. Let’s just say you had to interact with the command line in order to set it up.
You’ll always C the light
C is about 40 years old and still used today.
What does that say about it?
It’s one of the most dynamic languages you can learn which can be used to create software and manipulate hardware.
So, if you are keen on learning a language that is extremely useful and powerful, C could be the best first language to learn.
Psst… C and C++ can be used on other boards
I’m writing this in 2022 where Raspberry Pi stock is a big issue.
One of the biggest things people are asking is, “What alternative boards are there? I can’t build projects with a non-existent Raspberry Pi.”
There are many alternative boards out there, and you can check them out here.
If you learned C or C++, you will be able to dive straight into using an Arduino or ESP32 or ESP8266 board, whose programs are almost exclusively written in a C or C++.
Those boards might not take Python, so if the reason why you’re into the Raspberry Pi is to create projects with microcontrollers, you might find C or C++ to be a more transferrable language.
Learning C or C++ for Raspberry Pi
MagPi publishes a magazine for beginners for free.
MagPi also publishes a list of places to learn C (not specific to Raspberry Pi)
Raspberry Pi publishes a PDF that teaches you how to use C/C++ with the Raspberry Pi Pico
Here be dragons
In the section above, we’ve talked about the programming languages you should consider learning.
The following section talks about potential languages that can also be used with the Raspberry Pi, but the use cases and community aren’t as large as when using Python, C, C++ and Scratch.
No, not really.
It still has its uses.
Look who’s laughing now?
HTML + CSS
- You plan to do something web-based
- You plan to serve a web page using the Pico W
This one site has helped me a lot: Interneting is Hard
It’s a bit out of date. For example, it doesn’t cover CSS Grid or aspect-ratio.
But Interneting is Hard has an effective teaching method and can teach you enough to build a web page for the Pico W.
You can supplement any lacking knowledge with Google, W3Schools or Mozilla Developer Network.
Recommendations if you want to…
Get results fast: Python or Scratch
Teach kids how to program: Scratch
Get ultimate control of your hardware: C or C++
Build hardware projects: Python or C or C++, don’t bother with anything else
Become godlike (with a ungodly investment of time): C or (especially) C++
Please don’t refer to “C/C++” as 1 language. That just confuses beginners. They are TWO very different languages with different learning curves and different abilities.
Thank you for the feedback – I have amended the article and expanded a bit upon the difference between the languages.