Why is C++ commonly used for Arduino robotics?

Hey everyone,

I'm a grade 11 student learning about Arduino and C++. I was wondering why Arduino mainly uses languages like C++ instead of Python. is it mostly because C++ is faster, or are there other advantages when controlling sensors and motors in robotics?

Thanks!

The big unsolved problem with Python is 'memory fragmentation' which means that you cannot guarantee that a Python program will run forever. In a teaching environment this isn't seen as a problem, but in the real world embedded systems are supposed to run forever without needing to be periodically restarted. Adafruit has been pushing Python for years under their CircuitPython brand name but even they acknowledge the problem.

Python is an interpreted language, while C and C++ is a compiled language resulting in direct machine code for the microprocessor. A huge increase in execution speed. Moreover, Python suffers from release version changes that make today's program may not work the same on the next version of the interpreter.

C and C++ have been around for over 50 years and were the first high level languages with compilers for microprocessors. Python is relatively new, is interpreted and slow. Only very limited versions of it (e.g. microPython) run on microprocessors without operating systems.

Speed, size and how close you can get to the metal.

10 years ago I posted button and led Uno R3 examples that ran void loop() at over 67000 Hz on average while telling you how fast. The examples were about button bounce as well as non-blocking code. How does uPython compare?

The original Arduino was implemented using an ATmega8 microcontroller, with 8kbytes of program memory and 1kbyte of RAM. That's completely incapable of running a Python environment (in any meaningful sense.)
Many of the currently popular Arduino boards are still based on chips that are incapable of running Python. (and "Uno R3" or "Nano" has 32k program memory and 2k of RAM.)

Adafruit is very big on "CircuitPython" and there is also "microPython", typically requiring a 32bit CPU running at 48MHz, at least 32kbytes of RAM and 128k of flash, and frequently augmented with additional external flash (2MBytes or more) to hold the source of the usual set of Python libraries... (Of course, some of those powerful 32bit boards are similar or cheaper than "real" Arduino boards. (Like an RPi Pico.))

(Also, Python wasn't nearly as popular ~20y ago when Arduino first appeared, and neither MicroPython nor CircuitPython existed.)

I hopes it's clear that to run an interpreted language requires executing the interpreter on the target as well as the user program that is interpreted.

Running a compiled program just requires loading and executing the compiled binary.

another issue with languages like Python is they need a garbage collector to manage the memory.

this garbage collector runs every now and then and when it runs it slows down the running programs

this doesn't do well with, say, interrupts and precise timing

The most notorious example - Blink - compiles into binary, which takes 924 bytes of FLASH(~ROM) for code and 9 bytes of RAM for data to run.

On classic Arduino UNO/Arduino Nano (atmega328P, 32kB FLASH, 2kB RAM) it uses like 2% of code memory and nearly none RAM.

It compiles on PC, where the main executable of the compiler - avr-gcc - takes 1.955.456 bytes on disc (the compiled size of the compiler) ... that is 60x more, than the FLASH size of UNO/Nano, but it is ok, as it compiles on PC and only the result of compilation - 924 bytes long - is needed for Arduino to run.

On the other hand any interpreter, like a Python - needs its full code to be present on the target (Arduino) at runtime, just to run/interpret the code. How good the interpreter can be, to fit into 32 kB and to be able run your code? (Way more limited, than you are used to use on PC.) And how much program space is left for your own program?

While it is possible to run some really small interpreters on Arduino UNO, it is not practical for learning programming, and it is not practical for nearly any purpose, for which are Arduino used.

While I agree with the answers given, they speak more to the limitations of the Arduino Uno than of Python.

Having all those problems with Python, I wonder why Arduino has come with Python for the MPU of their newly introduced UNO Q?

Python is a lovely language, if you have the memory and cycles to burn. It incorporates a lot of "advanced" algorithms (say, extended precision math, fractions, "dictionaries" based on hashing and lists based on dynamic arrays, etc) directly into the language, which is pretty neat (IMO.)

A lot of schools are teaching python as the first programming language for students.

And of course, the Raspberry Pi, which is nominally a direct competitor of the Arduino Q, emphasizes the use of Python.

I’ll hark back to the 1980s,

PASCAL Was widely used in teaching environment tor many similar reasons.

PYTHON has dropped into the same position for the 21st century.

Great teaching languages, but limited use in production software.

C++ was first released in 1985; its latest version was released in 2024.

Python was first released in 1989; its latest version was released in 2008.

Different programming languages are used for different purposes.

Both are multi-paradigm languages (object-oriented programming).

Half fair, half outdated.

Pascal is the fairer half of the claim. Niklaus Wirth designed it explicitly to teach structured programming, and that's mostly what it's remembered for today. But it wasn't always confined to classrooms — Turbo Pascal and later Delphi (Object Pascal) powered real commercial software from the 1980s through the 2000s, including early Skype and a lot of Windows business applications. So the "limited use in production" framing is accurate now, but it undersells Pascal's actual production history.

For Python I would disagree. That description fit Python's reputation around the early 2000s, when it was seen mainly as a scripting or glue language. It doesn't fit today. Python now runs core backend services at companies like Instagram, Spotify, and Dropbox, and it's the backbone of essentially the entire machine learning and AI industry, PyTorch, TensorFlow, and the broader LLM ecosystem are all built on it, in production, not just research. It's also deeply embedded in finance, scientific computing, and infrastructure tooling.

So your point might show your age :innocent::grinning_face: it’s more an historical snapshot than a general rule: it captures how people talked about these languages twenty years ago, but Python's teaching-friendliness clearly hasn't stopped it from becoming a default production language across huge parts of the industry.

To the original question, most has been said: Python simply can't compete with C and C++ for embedded and Arduino work. Python's interpreter and garbage collector need memory and processing overhead that many microcontrollers don't have to spare, a chip with 2KB of RAM can't fit a Python runtime at all, and even where it fits (like MicroPython on beefier boards), the interpreter's unpredictable pause times for garbage collection make it unsuitable for hard real-time tasks like precisely timed motor control or signal processing, where C and C++ let you know exactly how many clock cycles an operation will take.

It reminds me about Java. Embedded Java had its own shot at this world in the 1990s and 2000s , that's literally what Java was originally designed for, before it pivoted to the web, but it never took hold for similar reasons. The JVM itself needed more memory and flash storage than cheap microcontrollers offered, its garbage collector introduced the same unpredictable pauses that hurt Python, and stripped-down variants like Java ME never matched the tiny footprint and direct hardware access that C offered natively. By the time embedded systems got powerful enough to comfortably run a JVM, C and C++ already owned the ecosystem, tooling, and decades of trusted, battle-tested code, leaving Java only niche footholds rather than real displacement.

It should be noted that many python libraries depend on C/C++ implementations. Way faster, and more predictable with memory usage (on modern machines, large multi dimensional arrays (like used in AI) may also completely fill RAM, especially if you make copies of copies.

Absolutely correct, but as far as I can tell, the topic concerns Arduino, and therefore C/C++ versus Python.

There's no doubt about Python's usefulness, flexibility, and widespread use, but when it comes to small controller boards with limited resources (mostly without any underlying operating system, memory manager, or garbage collector), optimization is crucial. Furthermore, C and C++ compilers are available for many platforms.
Hence, C and C++ are the most direct and effective solutions. At the moment, there's no need to switch to others yet.

Not sure about your point there ?

Agreed, You took my thoughts a step further !
As for age, I admit nothing ! :hugs:

the Arduino Q has 2 gB of memory and runs at 2.0 gHz sufficient to run python at speed