TryingArduino

<disclaimer> This article is an emotional rant and may contain some strong language.</disclaimer>

Intro

Studying the available open source solutions for my 1000.1000 project, I found quite a few interesting frameworks for DIY sensors. One of the most complete and fast growing is the My Sensors project. The Framework, like many others, is based on Arduino, with both the traditional and more exotic hardware.

I did quite some embedded software development and other programming throughout my life, as some projects on my site show, but only from a classical approach. I have used multiple Arduino boards for low price and convenience and I have ported a few Arduino libraries to a regular environment, but my experience with the pure Arduino way of software was very limited. I was aware of the limitations, but not quite so…

Playing with examples

Getting the MySensors to work was fast, I setup a gateway, a dimmer node and a button node. Grabbed openHAB controller and made a rule to toggle the LED at the press of the button. Added a motion sensor and another rule in openHAB to turn the light on for some time when there is motion. Sweet. Great work mysensors.org!
dsc_8188

Ok this looks really easy and well off, let’s dig into how this works. Oh….s**t! Can’t navigate through the code using the Arduino Ide, well, ok, fine, I knew it was basic.

Let’s just open the source files of the MySensors library used in the example and look at them. S**t! The fu***ng Arduino IDE cannot even open the very .cpp and .h source files used in the libraries without some inconvenient hacks.

cannotopencpp

OK, there are other IDEs out there, surely Atmel studio has been bragging to me for a while that it supports Arduino. Fire it up, open the project, and voila! Can jump to function definitions….but wait! All the files used in the project are not easily visible and it is terribly difficult to figure it out the whole mysensors library. Better, but not good enough.
atmel_studio_arduino_project
Let’s convert the project to a regular Atmel Studio project.
atmel_studio_convered_arduino_project
Much better, all most of the source files required are pulled from the Arduino folders into a nice, complete project where one can see all the things happening in the background of the Arduino. And yeah, Arduino does a lot of things behind you back and uses some hardware blocks inside the microcontroller, but that is another story.

But it won’t compile. See, for the sake of simplifying things, Arduino code breaks some rules that normal C/C++ expects. While the Arduino processes the files in the background to make sure they become compatible with gcc, this does not happen here anymore since this project is now a normal C++ project. Untangling things would take a significant effort for such a complex library.

As far as I know, there is no easy way to get both things – the nice complete project with all sources and advanced IDE features and Arduino to compile it, but please correct me if this exists. This is valid for other IDE alternatives, like Eclipse and it is caused by the Arduino way of life.

How does it feel?

Arduino is supposed to be like some aid to swiming. Only that it turns the waters muddy so you cannot see what’s below and you cannot actually swim, you are forced to walk through water. It sorts of works if the water isn’t too deep and your purpose is to get across the river, not learn how to swim.

Don’t get me wrong, it has a very useful place in this world. The community around it had built one of the largest collections of open source embedded code and the far east manufacturers can sure get a board in your hands for cheap. Who cares if it uses a counterfeit or out of spec chip or both, you can get a lot of stuff done.

The alternative is difficult

Actually it isn’t. Let’s look at an example: try reading an analog voltage.
This is how the Arduino sketch looks like, independend to what Arduino hardware you use. Let’s try the newer Arduino Zero.
arduino_read_analog_pin
There is some setup, but nothing to do, it happens in the background, and then you just loop through reading the value. Looking at the ADC it feels like there is more to it, but changing settings will get you digging around the internet a lot.

Now let’s take a similar example using a the SAMD21 ARM that is found on the newer Arduino Zero but in Atmel Studio, which is at the moment one of my favourite microcontrollers. The datasheet shows that indeed, ADC does have few more tricks up its sleeve:
samd21_adc_diagram
Atmel studio has built in example project to read the ADC using the Atmel Software Framework (ASF). This is the program it produces (removed comments):
atmel_studio_read_analog

Here’s what you immediately learn by looking at the code: there’s a main() function where everything happens, which Arduino normally hides. system_init() takes care of some basic initialisation of the chip. Then you need to initialize the ADC. To get a value you need to tell it to start converting and wait for a result while the ADC is busy, because this takes time. A little bit more complex in appearance, but no different than what actually happens if you use an Arduino, remember the hardware is the same.

But here there is nothing hidden, nothing happens out of your sight or control, you have now discovered more is needed to make a microcontroller work. You can explore the whole code with the ease of a powerful IDE.
And here is where something magic happens, have a look at what is inside the ASF provided function that sets the default way to use the ADC adc_get_config_defaults(&config_adc):
atmel_studio_default_adc
It immediately becomes obvious what are the different settings one can play with. Even for a total beginner, jumping back and forward between this configuration and the ADC diagram above maked it pretty obvious how things can be changed to do a lot more if the user wants to. You don’t need to understand everything at once, a lot of things will be intuitive and some will need the datasheet. This is the way embedded programming normally works, you control the hardware, not use in whatever hidden and obscure way the Arduino wants you to. Atmel provides this nice hardware abstraction layer through ASF, but one can, of course, also use the most efficient way of editing the actual configuration registers following the datasheet.

Conclusion

The complexity is always there, but in Arduino it is hidden. This might be good, but it is also inaccessible and that is why it remains a mystery for a lot of users who never grow beyond it. It’s exactly the wall you hit when you want to dig in and see what really happens under the hood that stops you.

My advice: use Arduino for simple fast things, profit from the community written libraries and buy the cheap hardware, but do learn about the whole world beyond it.

I am not complaining about the loss of performance due to poor code implementation, and in the end not even about the bad IDE. It’s the hide things under the rug to make it look simpler and cleaner approach that upsets me.

So where does this leave me with my home automation project? Tough dilemma, I like my ARM with great IDE and partially written code, but a readymade platform covering 80% of my needs based on cheap hardware and terrible Arduino is also tempting.

Note: there are no links to Arduino sites because they split up some time ago and I don’t find either of them more relevant than the other or worth including both.

Bookmark the permalink.

2 Comments

  1. Hi !
    As you I moved from Arduino to ARM (STM32) for anything more than simple and fast projects.
    But you should try Platformio for Arduino developments, it has lot of improvement over default IDE

    • Not a big fan of freemium crippleware.
      For pro usage on the job, employer should pay for pro tools. For playing as a hobby, I prefer totally free alternatives, not ones where some key features are left out and cost way too much to justify not going to a different alternative.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.