Getting Started With Arduino and C

Hey everyone,

I'm fairly new to Arduino, so I'm looking forward to getting started with it. My application of interest is using a Arduino to send the bits of a character serially (i.e. a PWM square wave with the ones and zeros of the charecter represented by pulses). Anyways, I'd like to do this with C (or another general language), since I'll be moving away from Arduino eventually. I know there is a huge reservoir of information floating around the web about coding in C with Arduino, but I'd like to ask the Arduino veterans, what they think the best way to go is? My experience is in MATLAB and C# (with a little assembly).

BTW: I have a UNO board.

Thanks

James

If you program the Arduino you are using C++, if that answers your question.

My application of interest is using a Arduino to send the bits of a character serially (i.e. a PWM square wave with the ones and zeros of the charecter represented by pulses)

Not sure what that means.

Thanks Nick. I realize the Arduino IDE uses C++, but I would like to use native C, so that I can easily switch to a basic AVR chip later on. I can't really explain the other part well yet, as I'm still looking into the methods of transferring data for my application. For now, I'm just looking for the best way to get started with using native C on Arduino. I've found a few sites, but there are so many; this may be faster. So far, I've downloaded AVRDude and GCC, but haven't gone through tutorials with them yet. If Arduino isn't a good starting point for AVR chips, I could try using something else, but I like the fact that Arduino has a bunch of built in peripherals and support.

Er, you can write in C if you want to. Why do you want to? The "basic AVR chip" is programmed with the output from g++. You can supply C or C++ files to it. Some of the Arduino libraries are in C, some in C++.

I'm not sure what you mean by "native C". Despite their files extensions the Arduino .pde or .ino files are effectively .cpp files.

The linker rather aggressively removes libraries you don't use, so you get pretty tight code out of the compiler/linker system.

Er, you can write in C if you want to. Why do you want to? The "basic AVR chip" is programmed with the output from g++. You can supply C or C++ files to it. Some of the Arduino libraries are in C, some in C++.

I'm not sure what you mean by "native C". Despite their files extensions the Arduino .pde or .ino files are effectively .cpp files.

The linker rather aggressively removes libraries you don't use, so you get pretty tight code out of the compiler/linker system.

Main reasons are to learn C, but also because I would like to port to a plain AVR chip without the Arduino (since they are much smaller and cheaper) in a few months once my code is tested and working. It's my impression that these controllers will not work with the "Arduino" language, and instead run plain C. For now, being able to plug the board into my PC and make use of the I/O pins so easily is awesome for getting started and debugging; that's what I got the UNO. Your info has been helpful though. I'll just go through some tutorials tomorrow, and hopefully get some lights blinking via C.

This seems like the best candidate for a 1st project so far: http://balau82.wordpress.com/2011/03/29/programming-arduino-uno-in-pure-c/

Thanks for the help. :)

Main reasons are to learn C

Admirable goal, but there are not a lot of companies hiring C programmers. C++, yes.

but also because I would like to port to a plain AVR chip without the Arduino (since they are much smaller and cheaper)

An ATMega328 is about 1/2" wide, 1/4" thick, and 1.5" long. With a bootloader, they are about $6 in quantities of 1. Buy more, and the price drops.

The ATTiny chips are not that much cheaper. A bit smaller, yes.

It's my impression that these controllers will not work with the "Arduino" language, and instead run plain C.

Wrong. None of them "run C". They all execute machine code that the linker creates from object files whose original language might have been C, C++, assembler, FORTRAN, COBOL, etc.

There seems to be some sort of myth spring up that there is a language that is Arduino specific or even a separate Arduino language. This is of course nonsense as most of us know but I wonder why such a stupid idea has taken hold?

Grumpy_Mike: There seems to be some sort of myth spring up that there is a language that is Arduino specific or even a separate Arduino language. This is of course nonsense as most of us know but I wonder why such a stupid idea has taken hold?

Maybe a lack of anything to the contrary. Going in, I knew what C/C++ was, so was able to figure things out. Maybe a short paragraph is needed somewhere in the installation notes or elsewhere that a beginner would trip across it early on.

The Language Reference page mixes standard C/C++ language features, etc. with Arduino-specific functions (digitalWrite, etc.) and there's just a small footnote. So I can understand how someone new to Arduino, especially if they didn't have much background in programming, could be confused.

For that matter, I'd bet that a lot of people who have written C/C++ for a while don't fully understand that printf() is not part of the language.

The Arduino development environment is intentionally designed to conceal the fact that you are writing a C++ program. Arduino creates new terminology for standard ideas -- like "sketch" -- and names the files ".pde" or ".ino" rather than ".c" or ".cpp". Arduino overlays non-standard approaches to several standard LibC things, arguably in the interest of usability and discoverability, but in the end, masking the relationship to standard build environments. The documentation attempts to explain the language from scratch, without simply stating outright that it's C/C++.

There are things I really love about Arduino -- the ease of build and re-flash of firmware is magic compared to other embedded systems I have messed with. But I personally find the "training wheels" attached to the "sketch" program framework a bit frustrating. I wish it was more obvious that you were writing in C/C++ and that that the AVR-GCC and AVR-LIBC references were treated as primary sources in all the documentation. But the way things are, I am not remotely surprised that non-programmer folks get a fair ways in, without twigging that they are working with something that is standards-based and has useful references outside of the direct Arduino community.

I know there is a huge reservoir of information floating around the web about coding in C with Arduino, but I'd like to ask the Arduino veterans, what they think the best way to go is?

Back to your original question. The best way to go is to buy a book. Preferably the one I wrote called 'Programming Arduino: Getting Started with Sketches'.

A search on Amazon will find you all sorts of good C programming books and if you know C#, you will not find it hard. But you will end up writing code where function names start with a capital - curse you Bill Gates!

Books are distilled knowledge! Save yourself some time.

Grumpy_Mike:
There seems to be some sort of myth spring up that there is a language that is Arduino specific or even a separate Arduino language. This is of course nonsense as most of us know but I wonder why such a stupid idea has taken hold?

There does seem to be some strange nonsense floating around. On another forum, which I have since left, the designer of a gadget used an Atmega328 chip to control various things. There was some discussion about whether it should be “Arduino compatible” or not, or whether they should “avoid the Arduino” in order to make it faster/smaller/whatever. I hate to break their bubble of silliness, but once you use an Atmega328 chip it is “Arduino compatible” whether you like it or not.

It is possible to use the Arduino IDE to enter a C++ or C program, compile and link it using g++ and end up with the machine code that the chip understands. You don’t have to use the IDE, but you don’t save a great deal by not using it. The resulting program can be loaded on the Atmega328 chip whether or not it is on an “Arduino board”. You can program with a bootloader, the SPI interface, or a high-voltage programmer.

For that matter, I’d bet that a lot of people who have written C/C++ for a while don’t fully understand that printf() is not part of the language.

You might want to re-check your copy of ISO/IEC 9899 (the international C language specification):

7.19.6.3 The printf function

Synopsis

#include <stdio.h>
int printf(const char * restrict format, …);

Description

The printf function is equivalent to fprintf with the argument stdout interposed before the arguments to printf.

Returns

The printf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred.

And I don’t think it’d be advisable to suggest to the experts over on comp.lang.c that there is such a thing as a C/C++ language… ]:slight_smile:

I think you are missing the point.

The printf() is not part of the C language, it is an extension that is in the stido library. Many people mix up what is an extension and what it part of the language. Once you #include <stdio.h> then printf() becomes part of the language because C is extensible but it is not part of the core language. Hence you can’t use it on an arduino unless you include the extension library.

Grumpy_Mike: There seems to be some sort of myth spring up that there is a language that is Arduino specific or even a separate Arduino language. This is of course nonsense as most of us know but I wonder why such a stupid idea has taken hold?

Hmmm.... Possibly because of the following text at http://arduino.cc/en/ ?

The microcontroller on the board is programmed using the Arduino programming language (based on Wiring)

Brad.

This is all part of the conspiracy to make the Arduino user-friendly.

  • Don't tell people they are programming in C++.
  • Don't end file names in .cpp (but rather .pde or .ino).
  • Make minor fiddly changes to the way the source files are preprocessed to save you from having to use function prototypes.
  • Hide warnings, even though those warnings may, well, warn you of something important you are doing wrong.

This is all well and good while you do the first couple of tutorial programs. "Hey everyone! I wrote a program and didn't have to learn C!".

Then it starts to come back to bite. We get questions like this about what the language "really" is. Where is it documented? Does it have classes like C++? Can I convert my Arduino program to C? And so on.

Indeed, the official Arduino documentation has not been a help in the war against misinformation. :wink:
But that is no reason to give up the ‘good fight’.

Home page content:

The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing). Arduino projects can be stand-alone or they can communicate with software running on a computer (e.g. Flash, Processing, MaxMSP).

[quote author=Brad Burleson link=topic=91893.msg691353#msg691353 date=1329278927]

The microcontroller on the board is programmed using the Arduino programming language (based on Wiring)

[/quote]

If you follow that link you quoted you find this:

The Arduino language is based on C/C++.

It's not a total secret.

[quote author=Nick Gammon link=topic=91893.msg691382#msg691382 date=1329281642] [quote author=Brad Burleson link=topic=91893.msg691353#msg691353 date=1329278927]

The microcontroller on the board is programmed using the Arduino programming language (based on Wiring)

[/quote]

If you follow that link you quoted you find this:

The Arduino language is based on C/C++.

It's not a total secret. [/quote]

Well java is also based on C++. It should say "The Arduino language is C++" No messing around with the "C/C++" since writing code that compiles in C but not in C++ doesn't work.

[quote author=Nick Gammon link=topic=91893.msg691382#msg691382 date=1329281642] [quote author=Brad Burleson link=topic=91893.msg691353#msg691353 date=1329278927]

The microcontroller on the board is programmed using the Arduino programming language (based on Wiring)

[/quote]

If you follow that link you quoted you find this:

The Arduino language is based on C/C++.

It's not a total secret. [/quote]

It's a lot like the way our politicians use their language, mostly to not say anything so as to not offend or in anyway be accountable for what they did said. ;)

I voted for Arduino before I voted against it.