Which programming language should you use for a Raspberry Pi?

Raspberry Pi Programming Languages Title Image

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

pi dht22
Python code written in Thonny

Pros:

  • 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

Cons:

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

If you were to use another language, for example, JavaScript (via NodeJS), you might find yourself stuck without a library for a common sensor.

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.

CircuitPython

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?

YES. Absolutely.

You will definitely need to know Python if you want to build projects for the Raspberry Pi.

I started my programming journey with JavaScript. Outside of the Raspberry Pi sphere, JavaScript is a very efficient language because you can create programs and websites.

A four-digit clock
A four-digit seven-segment display

However, one day, I had to create a program for a seven-segment display and there was no way to do it in JavaScript.

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.

Learney

Roadmap

Harvard OpenCourseWare, CS50’s Introduction to Programming with Python

YouTube videos

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’s a senior developer with a lot of knowledge in programming in Java and JavaScript, so I was expecting him to have written it in some programming language.

But no…

He used Scratch.

What?

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

scratch at raspberry pi store

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:

Scratch
Scratch sequence to blink LED. Photo from Aaron Maurer’s YouTube.

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.

Learn Scratch

If you’re curious how Scratch works, you can quickly get a feeling of how its “building blocks” method works on its online environment.

Scratch IDE

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
SpeedFasterSlower
Learning curveHarderEasier
Memory managementManualAutomatic (garbage collection)
Compiled? Interpreted?CompiledInterpreted
Usage by communityWide usage, especially used in advanced projects where minute control is necessaryWidely used by beginners to advanced users.
Libraries for componentsGenerally availableGenerally 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:

  • int
  • char
  • unsigned char
  • signed char
  • unsigned int
  • short
  • unsigned short
  • long
  • 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.

JavaScript (including NodeJS)

A JavaScript program that logs data from a DHT22 temp/humidity sensor on the Raspberry Pi (and serves a web page showing the data)

JavaScript is one of the most useful languages out there for the hobbyist programmer because you can create apps, websites (with HTML and CSS) and backends with them.

One of the biggest advances in the JavaScript sphere is called NodeJS. Previously, JavaScript was mostly browser based, but with NodeJS, you can run a backend and create apps for your Raspberry Pi and other computers.

Specifically to the Raspberry Pi, there are libraries that allow you to manipulate the GPIO pins of the Raspberry Pi. So, if you want to build a simple projects, you won’t have a problem with running a JavaScript program.

However, the biggest bottleneck in your project is the lack of a community where Raspberry Pi and JavaScript intersect.

This becomes evident when you visit someone’s Github and inevitably, their project is written in Python or C, C++. Very few programmers use JavaScript with the Raspberry Pi.

So, is JavaScript a write-off?

No, not really.

It still has its uses.

One use case where just learning JavaScript can help you accomplish your goals is if you want to use your Raspberry Pi as a web server. You can write the backend in ExpressJS (a backend framework) and the frontend in React/Angular/Vue (frontend frameworks), then you’d use PM2 (a process manager) to deliver the back and front ends. In this scenario, you can do it completely in JavaScript.

Raspberry Pi Pico W needs JavaScript

A while ago at a React meetup, someone teased me for writing vanilla JavaScript. He teased, “what year is it? The 2000s?”

If you have a Raspberry Pi Pico W, you’ll eventually need to know how to write vanilla JavaScript.

Look who’s laughing now?

While the web development world has moved on to using a frontend framework like React, Angular or Vue, serving websites on the Raspberry Pi Pico generally requires you to know how to write vanilla JavaScript. If you look at our tutorials in our Pico W mega article, you’ll see.

The slider on the top right controls the servo with the help of JavaScript

One of the biggest benefits of the Pico W is that it allows you to serve a web page, which you will probably use to control something. For example, you could have the Pico W serve a web page with a slider that controls a servo. In order to communicate the value of the slider to the Pico W, you will need to know JavaScript.

Learn JavaScript

I can only recommend the one web page that has proved the best resource for me to learn JavaScript:

https://javascript.info/

This resource is VERY good for Pico W users because it teaches you every aspect of vanilla JavaScript and also browser events. You will need to know the latter in order to effectively control the Pico W and the UX of the web page that you’ll serve.

HTML + CSS

HTML and CSS aren’t really programming languages but they are closely related to the JavaScript section. They are useful if:

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

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

Host a web server and build websites: HTML, CSS, JavaScript

Build an IoT project with the Pico W: Python, HTML, CSS, JavaScript

Become godlike (with a ungodly investment of time): C or (especially) C++

4 Comments

  1. Doug on January 29, 2023 at 5:07 pm

    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.

    • raspi berry on February 4, 2023 at 11:46 am

      Thank you for the feedback – I have amended the article and expanded a bit upon the difference between the languages.

    • NADEEM INAMDAR on July 4, 2024 at 9:48 am

      I disagree with the statement “They are TWO very different languages”. C is subset of C++. Of course C++ is much more difficult to learn than C.

  2. Pedro on April 9, 2023 at 3:20 pm

    Gracias por éste artículo

Leave a Comment