I have a background in programming embedded systems (TI MSP430, Atmel ATxmega). How is programming an Arduino different than those? What knowledge about C can I take in to programming the Arduino?
-
Removed the C++ tag, since it's pure C.– XeoMay 9, 2011 at 1:51
-
3Sorry, I was confused because here in this example arduino.cc/en/Hacking/LibraryTutorial they do use C++– NathanMay 9, 2011 at 2:10
-
1I don't understand why the C++ tag has been removed. Arduino libraries are usually C++ classes. (Doh! two years old comments :-P )– Marcello RomaniMay 13, 2013 at 18:21
-
Still C++ and now almost 3 years old... 'er. :-P– Able MacDec 7, 2015 at 2:39
5 Answers
While I don't know about the ATXMega, the 8-bit AVR chips like the ATmega328 used on the newer Arduinos use the AVR-GCC compiler. This allows for compiling C and even C++ to an AVR chip. One level above the AVR-GCC is the AVR Libc, a C library that makes programming for the AVR a higher level task (no longer have to refer to registers directly, and so on).
The Arduino IDE uses AVR-GCC and AVR libc library in the backend. In addition, the Arduino IDE makes other libraries available, like a nice Serial interface.
Finally, the Arduino comes with a bootloader burned on the AVR chip. The bootloader simply makes it possible to program the AVR using a serial connection (from USB) instead of an In-Sytem Programmer or Development Board.
Enough backstory, to answer your question: The Arduino can be programmed in C and even C++. The libraries available are written in C and everything will compiled using AVR-GCC. The Arduino IDE isn't even required.
Edit
There seems to be a decent amount of interest in this topic. I wrote a blog post to try and give more in-depth details on the AVR, Arduino, and AVR-GCC.
-
1
-
-
@oldmud0 thanks, I must have deleted the GitHub repo at some point. I removed the reference to the Eclipse project from the answer. Sep 6, 2016 at 18:22
You can take your existing C knowledge when using Arduino.
The purpose was to allow artists/non-programmers to get started easily with hardware programming and tinkering, so the 'Arduino language' is just a wrapper to simplify development.
It should be a lot easier for you, as a C programmer to use Arduino. The documentation isn't lengthy at all, the wiki is nice and the people on the forum are enthusiastic and helpful.
Arduino is C, except that this is inserted into every program:
void main() {
setup();
for(;;) {
loop();
}
}
-
5
-
2There are some other exceptions as well when using the Arduino IDE. For example, there are problems using structures in function calls or returns unless you define the structure in a separate .h file. As it says in playground.arduino.cc/Code/Struct: The usual arduino workaround/hack is to have all functions that requires custom datatstructures to be placed in an additional .h file. Just create a new tab in the IDE and give it a name.h then #include "name.h" Feb 22, 2013 at 20:24
-
1Odd. As stupid an example as it might be, this: pastebin.com/aF3QLVce compiles under Arduino IDE v1.0.5. I'm downloading 1.5.6-r2 to give it a try... Out of curiosity, could you pastebin the code you tried? (BTW, I'm on Ubuntu 12.04 x64) May 12, 2014 at 7:13
-
1
-
1@PaulJurczak IIRC the Arduino IDE messes with the first lines of the .ino source file. This sometimes causes valid C++ constructs to become syntactically wrong. One solution to this is to put the lines giving you problems in a separate .h file, then including it (with double quotes) in the sketch. I've tried this with your example, and it works. (Also look at ViennaMike's comment above.) May 29, 2014 at 8:40
You can pretty much take all your knowledge with C and embedded systems and you will be more than OK. It's not hard to use at all. Bookmark the Arduino Reference page and you'll be writing stuff in no time.
Arduino is C-like and extremely easy to pick up. They have abstracted away from doing things like reading and writing into peripheral registers for doing basic tasks.
One look at some example code and the Arduino reference and you'll be up and running in no time if you've actually done real C on any other platform.