How to learn Arduino / How to Learn C

Status
Not open for further replies.
Hi,

I am somebody who is a solid Javascript programmer and knows a bit about Python 3. I am absolutely fascinated by microcontrollers and really want to master them as well as I can. I have repeatedly heard Arduino described as a simple or beginners IDE and language. I am curious to hear what very experienced programmers (like you Paul) have to say about the benefits of learning actual C vs learning Arduino well. It seems like knowing C would come in handy for jobs and other situations but I do not know if knowing C automatically makes you better at Arduino.

There is a book, Beginning C for Arduino http://www.amazon.com/Beginning-Arduino-Programming-Technology-Action/dp/1430247762 and I am wondering if that is where I should start to learn Arduino.

Basically, from someone who knows more about Arduino and C than I do:

Does knowing one help the other?

Which (in your opinions) makes the most sense to learn first?


If I want to be Jedi-Caliber at Arduino, do I need to learn C eventually?
 
The Arduino uses mainly C++, not C

But a little research on the Arduino website and the Arduino forum would have told you that already ;)
 
Yes, you can use Teensy with pure C. That is Teensy 2 and Teensy++2. I am not sure that is the case with Teensy3.x. Others are more qualified to comment on that.
However, in that case you would likely not be using the Arduino IDE or many of its libraries as many of those are written in C++.

I am not trying to discourage you to learn C. But if you want to work with Teensy and Teensyduino then Learning C++ gets you to solutions quicker.
 
I think a beginner can delay understanding C++ and its use in the libraries, and just write plain C code.
Just duplicate the C++ class instances for Serial and serial and the like and not dig into how and why of that as a beginner.
Just begin with blinky LEDs, variables, loops, constants, simple arithmetic, booleans.

IMO, learn C as a base, then decide how far you need/want to dig into the obtuse world that is C++ for a beginner.
 
It all depends on your goals. If your fascination with microcontrollers is all about tweaking registers to make hardware do stuff and you want to learn how to use any microprocessor, then learn C and use Arduino as a source of examples with the idea of leaving it behind.

If your goal is to make projects by assembling components and it's all about the concept, then focus on learning to put existing components together. Arduino will keep you going for a long time.

If you'd like to make a career, then you'll needed both C and C++, but bear in mind, C++ was designed to do much more than C.

If you're interested in designing great systems, learn a real variety of programming languages including assembly language at the low end of the abstraction spectrum and functional programming languages at the high end. While you're at it learn algorithms so you know better than to reinvent the wheel all the time.
 
I think there are several different things here ; language syntax, typical programming style and APIs.

While Arduino uses C++, "learning Arduino" really means learning the Arduino API, libraries and hardware, which is really separate to the language. There is also the way the language is used. Small micros just don't have the resources to handle full-blown object oriented C++ (mostly). So micros tend to be programmed using "C++ light", or mostly "C-style" with some C++ features.

I looked through the book on Amazon preview, it looks like it would be a good start. Although it is described as "Beginning C for Arduino", user code in Arduino is compiled as C++. C is pretty much a subset of C++, so it amount to the same thing.
 
All I'm saying is that one must learn to crawl, walk, run and C to C++ and C++ obtuse like derived classes and templates is running.

I usually advise people to learn C on a big computer where crashy code is not so mysterious, with debuggers and traps.
 
C++ is awful but if one carefully studies best practices and pitfalls, it's less awful than C. In the old days, I would have recommended C as a stepping stone to C++. Now, there are so many good books that I think that if your goal is to be a professional programmer, go ahead and learn C++ first. Pick up C as an historical curiosity. C is still important, but it will never eclipse C++.

Regarding the obtuse features of C++, it would be better to learn these things in languages that weren't burdened with the design goals of backward compatibility and performance obsession; but outside of college, not many people are willing to spend the time learning languages like Scheme, Smalltalk and Haskell.

I completely agree about learning in an environment where it's easier to see what's going on. On the other hand, I'll encourage any approach to programming that gets the learner all fired up, even including Visual Basic. If a PC printing, "Hello, World!" is boring but an MCU blinking LED is exciting, who am I to discourage someone from learning "the hard way"?

Going back to the first post in this thread, I was thinking about the notion of "Jedi-caliber Arduino coding" and an image of Yoda playing miniature golf popped into my head.

YMMV :)
 
If you're just getting started with hardware, definitely start with Arduino.

The Arduino IDE is quite primitive, which will probably drive you crazy if you're used to a fully featured IDE. There's a "use external editor" feature in File > Preferences, which you'll probably turn on after 20 minutes, so you can edit the code in your favorite IDE and just use the Arduino window to compile and upload.

I wouldn't worry too much about the finer points of C vs C++. Compared to Javascript, the main thing you'll need to get used to is hard types. In Javascript, variables can be of any type and they're automatically converted as you use them. In C & C++, you have to declare the type of every variable. String are merely arrays of char, where the buffer is fixed length and you have to be careful not to store more than will fit. It will probably feel very rigid, because, well, it is. The rigidity is also extremely efficient, especially with integer types.

As with many subjects, there's 2 approaches to learning, which could be described as "walk before you can run" and "just dive in". One of these is much more fun than the other. If you're doing this just for you own personal goals, I'd recommend the fun way.

Either way, it's pretty easy to hit roadblocks as you go. Feel free to post your questions here, but do try to follow these guidelines. Questions with concise but complete code and all relevant details to reproduce the problem get much better answers, and save every's time, so please take the extra moments to ask well a formed question if you do get stuck.
 
I think you have asked a really good question.
I have been programming PIC microcontrollers in C since 2005. Programming in C is a lot more productive than using Assembly language which I used on microcontrollers before.

The Arduino libraries provides a much easier learning curve since the Arduino libraries provide a layer of abstraction between the programmer and the hardware. I'd be somewhat concerned with issues related to debugging with Arduino. Debugging requires one to understand the system they are using. With that in mind it is probably quite important to have the resource of forums like this one to provide some help when a person is getting stuck. I think Paul is giving good advice in post #11 regarding what to do if/when you hit a roadblock.

I'm still on the fence as to if Arduino is a good thing to teach/learn so I'd suggest getting your feet wet with Arduino starting with the online documentation. Beyond that I don't know. I think it depends on how much time you want to invest. To write your own C++ libraries for the Arduino you really just need C and enough C++ to create your own simple classes. C is a relatively small language and if you only have enough time for one language it seems like a good place to start (beyond just getting ones feet wet with Arduino). If you learn some C you can also write simple programs for your PC. My perspective is that I will never do enough programming to fully jump into learning C++. I prefer the C/python combination.
 
Didn't the PIC world of C have C++?

I use C/C++ and Python. The object oriented classes and inheritance, and so on is the same concept for both C++ and Python. You can write Python that's not object oriented, not use classes, just as you can avoid C++ and write C.

Best to learn C++ on a big computer - Windows, Linux, etc. Not Objective C or C#, those are, IMO, political languages.

You can toggle between Python and C++. I use Python 2 rather than Python 3, by preference, to keep it simpler.
 
I'm still on the fence as to if Arduino is a good thing to teach/learn so I'd suggest getting your feet wet with Arduino starting with the online documentation. Beyond that I don't know.

That's an important point. Arduino is not meant to be system for teaching, but it is used for teaching. And can be a problem. The abstraction provided by the arduino interface hides most of the hardware aspects, so arduino is rather a tool for learning how to program (with hardware attached) than a tool for learning how to program hardware. If you feel comfortable with arduino and try to write your own library for a specific peripheral, you'll have to learn something completely different.
 
Didn't the PIC world of C have C++?

I use C/C++ and Python. The object oriented classes and inheritance, and so on is the same concept for both C++ and Python. You can write Python that's not object oriented, not use classes, just as you can avoid C++ and write C.

At the risk of going off topic:
The smallest and oldest PIC devices really could only be programmed in assembly due to a small hardware stack and very little RAM. The PIC18FXXXX devices are the lowest end devices that Microchip stated were optimized for running C programs. Note that these devises have a 32 level hardware stack (the PIC16Fxxxx devices only have an 8 level hardware stack). The Microchip C18 ANSI C compiler does not include malloc in the standard library it comes with and it does not compile c++ code (there was no expectation that one would want to try to allocate memory from the heap). I will skip the 16 bit PICs because I am not familiar with them. The XC32 compiler is gcc and so it is good for both C and c++ and the 32 bit PIC's have more ram than the 8 bit pics.

With the low cost of 32 bit PICs i'm thinking there is little point in using the PIC18F devices anymore.

The teensy3.1 is quite nice with the tiny board including the micro USB connector, preloaded bootloader and lots of I/O pads. It is ready to go.

Best to learn C++ on a big computer - Windows, Linux, etc. Not Objective C or C#, those are, IMO, political languages.

Well I would just say I'd rather use a language that belongs to everyone and not a language that belongs to a corporation.

You can toggle between Python and C++. I use Python 2 rather than Python 3, by preference, to keep it simpler.

I guess. I want to program to make my computer more useful for myself and the people I work with. I don't really need c++ since I am not writing commercial software, and python programs go together faster.

I'm more likely to use python 2.7 because it works with IPython, scipy and matplotlib.
 
Last edited:
I remember my first computer included toggle switches and a small hex keyboard to program in assembly. For this then 12-yo, that was a bit of a stretch and the machine quickly gathered dust. The Arduino IDE, abstracts things enough to make the MCU accessible to a large group of people without requiring a lot of training. But you still have to learn how a computer executes code and for that reason alone, it's a useful exercise. Whether you learn C or C++ is a different question. I'd focus on the language that makes more sense to you (both have idiosyncrasies) and then for Jedi-like performance™ I'd consider using a 'real' IDE instead of Arduino to write the code in, i.e. Ecplise or its many alternatives.

The Arduino IDE is great for beginners. But for someone who is moving on to writing libraries and the like, other IDE's offer much better insight into what is going on, how to integrate multiple libraries, and so on. So rather than get used to the Arduino IDE, I'd counsel you to find a good IDE that you like and then just use the Arduino IDE to upload the payload to the MCU.
 
I used PICs in the day of UV erasing. They had no stack pointer, no frame pointer, silly-small code and data ARM page sizes, and so on.
I dismissed them decades ago. Funny too, how Microchip has elected to drag their lemming customers from PIC-8 to a phoenix-from-the-ashes revived MIPS chip.
Well, each to his own!

C++ and classes (in Python and others too) isn't about commercial vs. hobby. It's helping one get from small simple programs to a more complex project where you REALLY want to reuse others' code. Libraries. Bulding blocks that you need not design. It's about code reuse, avoiding reinvention.
 
Last edited:
I think the different perspectives represented here reflect different needs in different environments. In educational environments cost is always a primary consideration. Easy to use development tools that allow simulation and stepping through the code while displaying the contents of memory locations / contents of variables can be very educational.

Complexity is also an issue. A datasheet that is less than 400 pages is easier to deal with that one that is more than 1200 pages. The same goes for the computer language. The classic K&R is less than 300 pages. Engineering and Science students have a lot of things to cover. I have seen courses which were called C++ courses but did not fully cover C. Essentially C basics with printf() replaced by cout.
 
Last edited:
I too am a solid Javascript (and Java) programmer, who has been hacking around with Teensy's and Arduinos. It's like going back in time 30yrs and has been driving me nuts! The most useful book I found on C was written in 1989! For example, an array doesn't know how long it is, so you have to create the array, and then a 2nd variable to hold it's length. If you accidently run over the end of the array, it just silently corrupts memory, which may or may not crash your Teensy. Either way it's not good.

There is a weird preprocessing stage, that munges all the header files together. Exactly what to put in headers, and where to put the #includes (can you put #includes in other headers?) seems to be a black art to me. Just try various things until the linker stop complaining!

Superficially C/C++ is similar to Javascript & Java i.e. they all have use curly brackets but that's about it. Although Arduino sketches are compiled as C++, they aren't really the whole C++ stack. You don't have malloc() and new() for example, I think most of the libraries that use C++ create a singleton object for themselves, and you use that. You don't really create objects like you do in Java.

It's entirely different to Javascript. There are two scopes: function and global. You can't nest functions, so that's it. Don't even think about closures.

All the "good practice" I know has gone out the window, because it's so much more difficult in C. You'll probably just end up making everything global to avoid using pointers!

The Arduino "IDE" really really sucks, I've been using this -http://www.visualmicro.com/ - with Atmel Studio. They are both free downloads, and VASTLY better than the Arduino IDE.

Personally for these reasons I really don't think that the Arduino family is good for beginners. It's perfect for very basic sketches making a light flash on and off, but as soon as you want to get more advanced the Arduino IDE is no use at all, and C is an archaic language where you'll spend more time jumping through the hoops of the language, than actually achieving you aims. I'm really hoping that projects like this - http://micropython.org/ - get more traction, to drag microcontrollers into the 21st Century. Or that someone ports Go (http://golang.org/) to microcontrollers.

Sorry for the rant, been getting very very frustrated with using C (especially trying to use pointers & arrays), when I could do what I want in 10min in Java!
 
What goes around comes around

Some interesting perspectives there.

I too am a solid Javascript (and Java) programmer, who has been hacking around with Teensy's and Arduinos. It's like going back in time 30yrs and has been driving me nuts!

I was messing around with hardware and programming 6502s 30 years ago, so it's great to find this type of stuff is topical and cool again! The hardware is a lot cheaper, and the software more powerful (C++ vs BASIC) but otherwise pretty similar. For people like me, it's like being a pig in mud :)

Personally for these reasons I really don't think that the Arduino family is good for beginners. It's perfect for very basic sketches making a light flash on and off, but as soon as you want to get more advanced the Arduino IDE is no use at all, and C is an archaic language where you'll spend more time jumping through the hoops of the language, than actually achieving you aims. I'm really hoping that projects like this - http://micropython.org/ - get more traction, to drag microcontrollers into the 21st Century. Or that someone ports Go (http://golang.org/) to microcontrollers.

Two observations there : MicroPython has a port for Teensy 3.1 already, but otherwise should be easy to port to other ARM processors, probably would run ok on a Due.

MicroPython is written in ANSI C :) It seems we still need people with solid C skills to provide powerful tools for people to program without needing any C skills.

Personally I'm not keen on Python, interpreted language, implicitly typed variables, mandatory indenting - it's like going back 30 years!

Java was originally intended to run in embedded devices, but when they realised it required far too much resources for that, it was targeted as a C++ replacement for PCs. Now that even Linux capable ARM chips are a few dollars, it is realistic to run these sort of resource heavy languages on low-end devices. Although it still seems mad to have Java running in a washing machine!
 
Some interesting perspectives there.
I was messing around with hardware and programming 6502s 30 years ago, so it's great to find this type of stuff is topical and cool again! The hardware is a lot cheaper, and the software more powerful (C++ vs BASIC) but otherwise pretty similar. For people like me, it's like being a pig in mud :)
Unfortunately, I feel more like a fish out of water :)

Two observations there : MicroPython has a port for Teensy 3.1 already, but otherwise should be easy to port to other ARM processors, probably would run ok on a Due.

I didn't realise that, it's good news, I'll try it out. Hopefully it will get an ecosystem and community behind it as well, especially library support. There's very little I could do with a Teensy if I didn't use all the libraries people have written to do the heavy/low level lifting. If a large proportion of those start getting ported to python it will be great.

MicroPython is written in ANSI C :) It seems we still need people with solid C skills to provide powerful tools for people to program without needing any C skills.

C does seem to be the only low level language in use, and the less of it I have to do the better!

Personally I'm not keen on Python, interpreted language, implicitly typed variables, mandatory indenting - it's like going back 30 years!

Java was originally intended to run in embedded devices, but when they realised it required far too much resources for that, it was targeted as a C++ replacement for PCs. Now that even Linux capable ARM chips are a few dollars, it is realistic to run these sort of resource heavy languages on low-end devices. Although it still seems mad to have Java running in a washing machine!


Personally on a technical level I would choose Java over Python as well, but there is more of an open source ethos around python. I think Java is seen as too "enterprise-y" and being owned by Oracle with the recent lawsuit against Google over it doesn't exactly encourage it's adoption by the open source community.

I don't understand why microcontrollers are still have so little CPU & ram. I can get a raspberry pi with 512Mb ram for the same price as teensy! I know some people like the challenge of developing in such a constrained environment, but I would rather have enough h/w to do what I want, without having to worry about saving every single byte. Optimised code is also generally less readable, and so harder for other people to follow.

Are there any products around £25-30 that are embedded like a teensy/arduino (i.e. don't run a full blown OS like a r-pi does), have a decent amount of RAM (lets make it easy by saying at least 10Mb, it's not much to ask!), and can be programmed in a higher level language like Python or Java?
 
The "fish out of water" feeling is no much fun, but we all go through it. Keep adjusting your goals as needed to keep things fun. It's really not that bad to learn C if you already know Java. Automatic memory management in Java definitely spares you from some difficult bugs as compared to C, but once you learn good habits those mistakes are rare. After you've had some practice with C (or any unfamiliar technology), the ugly stuff fades into the background and you have more options.

To make a microcontroller bigger and faster generally means making it take more power and making it more complex. The dynamic power used by an IC currently is related to the number of signals toggling back and forth between 0 and 1 during a given period of time. The static power is related to the amount of circuitry that's energized. Many of the tricks for making processors faster involve adding logic. For example, if a processor can do a good job of predicting way a decision is likely to go (branch prediction), then it can get a head start on subsequent instructions (pipelining or speculative execution). Bigger memories mean more circuitry energized.

As the complexity ramps up so does the development time both for the manufacturer and the developer, especially if safety is a concern. All that complexity has to be analyzed and proven to satisfy the safety goals of the device.
 
There's a flip-side to the above discussion... take small Linux like RPi or Beaglebone. A majority of users can jump in on this with a lot less knowledge of how computer internals work. So there's the argument that the smaller the CPU and memory, the harder it is to learn and know and the tools on small micros are more primitive as a rule.

Many of us in Teensy, AVRfreaks and the like enjoy taking a small micro with a blank or near-blank canvas and being creative. Others like paint-by-numbers.
 
Steve, it's not fair to call the next level of abstraction up from your favorite one 'paint-by-numbers'.

Your 'nearly-blank canvas' isn't nearly blank unless you ignore all sorts of pretty shiny things that are already there to make life easier for you: processor startup sequence, libraries, compilers, linkers, loaders, editors, debuggers, oscilloscopes, protocol analyzers, and on an on. This is true all the way down. ASIC design isn't a blank canvas. Foundry standard cell libraries and ASIC design tool take care of so many things. No engineering or science works from a blank canvas. We all stand on the shoulders of giants, painting by numbers with the tools and techniques handed to us.
 
Status
Not open for further replies.
Back
Top