Developing Arduino sketches with JetBrains CLion: A minimal example.

The official Arduino Desktop IDE is fantastic at what it was made for. After downloading, opening your first sketch (say, blink.ino) and flashing this to your connected Arduino hardware takes all of 3 seconds.

However, once your sketches become a little more complex, a more sophisticated IDE with code navigation, documentation and context-sensitive completion can be a great help.

Currently, one of the better solutions is the Arduino extension for Visual Studio Code. You can be up and running quite quickly, and after adding the necessary include directories to your config, the built-in IntelliSense C++ helps immensely with code completion, navigation and inline documentation.

Overview of this barebones solution

However, this post is about getting CLion working with your Arduino projects, without using any additional software besides the Arduino IDE and CLion.

It’s slightly less straight-forward than with Visual Studio Code, but could be worth it, as CLion is arguably a better C++ and general programming IDE than Visual Studio Code.

An example of CLion editing the converted blink sketch with the m0 toolchain file installed. The documentation for digitalWrite is shown inline. At the left the file’s structure, and below the serial monitor plugin for CLion that can be used to see what the arduino is sending us.

The core of the solution is to create a cmake toolchain file for the Arduino, based on compilation parameters extracted from a verbose run of the Arduino Desktop IDE 1.8.5 in command-line mode.

Furthermore, it is important that all sketch code is moved out into .cpp files, and the necessary includes (importantly Arduino.h) and function prototypes are added. The main .ino (sketch) file has to be maintained with the same name as the containing directory, but it can be empty, which is what I usually do.

There are CLion arduino plugins which you could try (I could not get any of them working completely), but with this minimal example, you get to know exactly what is going on behind the scenes.

Up and running CLion, cmake and the blink example

To get you started, I have converted the minimal stock Arduino blink sketch to a CLion-compatible project, including toolchain files for the AVR-based Uno and for the ARM-based M0 boards, and made it available on github as arduino-clion-minimal. You can easily modify your existing projects by just copying and modifying the CMakeLists.txt and the relevant toolchain file.

To try out CLionized blink, clone this repo, then open it with CLion.

Importantly, go to Preferences | build, execution, deployment | CMake and then add -DCMAKE_TOOLCHAIN_FILE=arduino-uno-toolchain.cmake or -DCMAKE_TOOLCHAIN_FILE=arduino-m0plus-toolchain.cmake (my current favourite Arduino hardware!) depending on your hardware platform.

After opening, you should now have three build (Ctrl-F9 – the button to the left of the target selection, NOT run) targets at the top right of the CLion UI: arduino-clion-minimal for quick compile-only checks, verify for full arduino building, and upload for full arduino building and uploading:

Before you upload, make sure that you’ve selected the correct board type and port with the Arduino desktop IDE.

Conclusions

With this simple setup, you should have access to all of CLion’s programming facilities during the development of your Arduino sketches.

Furthermore, Dmitry Cherkas’s Serial Port Monitor plugin can be used as Arduino serial port monitor for a more fully integrated experience.

Let me know in the comments how it went!

Which jumper to set on the ITEAD XBee shield v1.1 for use with a 3.3V Arduino

I had to use the ITEAD Studio XBee shield v1.1 with an Arduino m0 (SAMD21) board, which is a 3.3V board, whereas the most common Arduinos are 5V.

At the time of this writing, the shield’s website was not very clear on how exactly to set the jumpers (zone 5: “When operated in 3.3V, install the jumper” — which one?!), and the rest of the internet also did not seem to know.

Fortunately their support did eventually get around to my requests, and the setup seems to work, so now I can put this information online to help future travellers.

Keeping the “xbee shield v1.1” text the right side up, only the left jumper in zone 5 should be set. Here’s a photo of the shield mounted on an Arduino M0 clone:

For use with a 3.3V Arduino, only the left jumper in block 5 should be installed.

Here is the illustration that was sent by itead tech support:

Let me know in the comments what your experiences were with this shield!

The RobotDyn Joystick shield has the XBee TX / RX lines switched to D0 and D1 or completely disconnected

RobotDyn offers a well-manufactured Joystick and XBee shield for the Arduino Uno which I am currently using for some IEEE 802.15.4-related experiments.

However, as it is not mentioned in any official documentation, I want to document here that the XBee TX / RX lines are connected to the Arduino D1 and D0 lines respectively and can only be disconnected via the “USB sketch update / Wireless” hardware switch at the top left:

The D0 and D1 lines are of course also connected to the Arduino’s main serial interface and connection to the host computer. This is why the switch has to be on “USB sketch update” when you program the board.

Unfortunately, this also means that it won’t be possible to send debug output from the Arduino to the host machine’s serial monitor when the XBee is active, i.e. the switch is in “wireless” mode. This is especially problematic when writing and debugging new programmes that use the XBee radio module.

With the SparkFun XBee Shield, one can switch the mounted XBee to use either pins 0 and 1, or pins 2 and 3 (see the section named “UART/SoftwareSerial Switch”), neatly solving this problem. It would have been great if the RobotDyn unit had done something similar, but keeping cost under control probably played a role.