34

So lately I've been playing around with my Arduino and I was wondering if there was some way I could program the Arduino in C++. I've been programming it using the C++/Processing language in Vim and using a makefile to compile and upload to the Arduino.

But my goal is to be able to use classes and all the great C++ features (or at least sum) to program it. Eventually I would even love to program it in raw C and I'm just having troubles finding out how to do either. It would be great if someone could point me in the right direction or help me out.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michaelslec
  • 905
  • 1
  • 9
  • 20
  • The programming language of thr Arduino **is** C or C++. You can use classes whenever you want. –  Aug 30 '13 at 07:46

2 Answers2

63

Here is my experience: I'm building a robotic smart toy for autistic children using Arduino, sensors, motors, led and bluetooth. I wrote my own libraries to do exactly what I needed using C++. But I found out that the Arduino IDE Compiler is an older version that does not support the new C++11 features.

So I had to find myself a way to compile C++11 code and upload it to my Arduino. It turns out to be "pretty" basic: I needed a Makefile, the avr-gcc 4.8 toolchain and voilà! The makefile job is done by Sudar (https://github.com/sudar/Arduino-Makefile) and it works great. I had to customise it a bit to make it work for my project though.

Here is some documentation I've written for my project. You should take a look, it might be useful for you. https://github.com/WeAreLeka/moti/blob/master/INSTALL.md

Hope it helps! Cheers :)

EDIT 08/16/2014:

Because I got a lot a requests similar to this one from friends and other devs, I decided to set up some kind of framework to get up and running with your Arduino projects quickly and easily.

This is the Bare Arduino Project

Hope it could be of any help! If you find bugs or stuff that I could make better, feel free to fill and issue. :)

Tardis
  • 465
  • 2
  • 10
ladislas
  • 3,037
  • 19
  • 27
  • @Michaelslec hi there! it's been a while, and I'm happy to say that I followed your advice and am now learning Vim :) Still using some ST for speed sometimes but I force myself into Vim because it feels so awesome! – ladislas Jan 14 '14 at 11:38
  • I'm so happy for you! I actually switched to to Sublime Text 3 for quite some time but after a while I couldn't handle without vim. If you want, I would be happy to lend you my .vimrc file. I have some pretty cool shortcuts setup in it. Also, if you're in to C++ programming outside of Arduino I made some makefiles. Just pm if you would like to see them! Vim rocks! – Michaelslec Jan 17 '14 at 04:53
  • @Michaelslec I updated my answer with something you might like :) tell me what you think about it! – ladislas Aug 16 '14 at 19:23
  • thanks for your work. but I have a hard time understanding how this works: what if C++ code invokes some system calls (say fflush()) but the Ardunio system is not a full OS, doesn't even have MMU perhaps, so how are these sys calls going to be compiled into binary? – teddy teddy Nov 29 '15 at 23:31
  • @teddyteddy if the function you're calling is no available in avr-libc, the code won't compile. Arduino is not running any OS but you could run or build a RTOS like FreeRTOS or ChibiOS/RT for larger projects. – ladislas Nov 30 '15 at 07:13
  • @ladislas are you able to / how dififcult is it to use existing arduino-specific libraries, like the LED libraries etc? – Startec Jun 17 '17 at 05:25
  • @Startec what do you mean? all the libraries provided by arduino like pwm and serial are available. arduino installed by yourself must be put in the project. – ladislas Jun 19 '17 at 13:28
  • @ladislas I mean community libraries for things like the different arduino add-ons. Things like ethernet or firmata. Is there any change that is needed to use these from C++? – Startec Jun 19 '17 at 19:03
  • @Startec you can use them with C++ as well, you don't need to do anything. You just include them and use them as you would normally do in an Arduino Sketch. – ladislas Jun 20 '17 at 08:42
  • 1
    Thanks for the amazing work @ladislas. This was just what I was looking for! – tenacity Nov 07 '21 at 22:48
5

The language supported by the Arduino IDE is basically C++ with some additional features implemented by the Arduino programmers. Also, in a sketch you just code the setup and loop routines (there are a few others that you will eventually get to as you become a more advanced programmer).

In a sketch you can define classes in a library and include that library using the Arduino IDE. The Arduino IDE implements an Atmel compiler that creates code for the Arduino's processor (there are several models). You can work outside of the Arduino IDE (sounds like you are) but you still need to have a compiler that targets the correct Atmel processor.

Finally, C++ classes can become large, so at some point your source will outgrow what the processor can store. So, the Arduino classes are sparse and to the point!

To start with, I would use the Arduino IDE and write sketches (which are mostly C++ anyway). And as the occasion permits you can code your own libraries in C and/or C++.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21