I already did a couple of tutorials using the Arduino IDE, but I’m used using PhpStorm and missed the interface and key bindings. So I decided to upgrade to the JetBrains All Products Pack (since I also want to learn Go) and use CLion. It’s not nearly as easy to set up, but I think it will be worth it in the long-run.

Note: I have an Elegoo UNO R3, which is the same as the Arduino Uno Rev3. If you have a different board you may need to use different settings (mainly Board Type). I’m also using Windows – the steps for any other OS will differ slightly.

Installation

Install Arduino IDE

Even though we’re not going to use it, it seems to be necessary to install Arduino IDE so the SDK is available. (I would guess it’s possible to install just the SDK, but I haven’t figured out how yet.)

Install MinGW

Download MinGW and install it in the default location – C:\MinGW\.

After the main installer finishes, it will open the MinGW Installation Manager program. Use that to install mingw32-base and mingw32-gcc-g++ (click each one and Mark for Installation, then Installation > Apply Changes).

Install CLion

It can be installed manually, but I recommend using Jetbrains Toolbox – it makes it easier to install new versions.

Install CLion plugins

Launch CLion. On the “Welcome” dialog, go to Configure > Plugins > Marketplace (or from a project, File > Settings > Plugins > Marketplace) and install:

Then restart CLion to complete their installation.

Creating a new project

Connect the Arduino

Make sure the Arduino is connected before launching CLion so the COM port is detected.

Create a project

On the “Welcome” dialog, select New Project. Enter the following:

  • Project type: Arduino Sketch
  • Location: Choose where to create the project
  • Board type: Arduino/Genuino Uno
  • Programmer: leave blank
  • Port: For me there was only one listed (COM5); if there are none, ensure the Arduino is connected and restart CLion

Press Create.

Fix syntax error in CMakeLists.txt

Straight away I got this error:

CMake Error at CMakeLists.txt:5 (set):
Syntax error in cmake code at
  D:/Dropbox/Arduino/Test Project/CMakeLists.txt:5
when parsing string
  ${<@PROJECT_NAME@>}_BOARD
syntax error, unexpected cal_SYMBOL, expecting } (4)

This seems to be a known bug. To fix it I replaced all 10 instances of <@PROJECT_NAME@> with just PROJECT_NAME.

When prompted by CLion, I recommend enabling Auto-Reload.

Configure the project

The next error was:

CMake Error at cmake/Platform/Arduino.cmake:2130 (message):
  ALL_SRCS not set: must define SRCS or SKETCH for target Test_Project

Add this to CMakeLists.txt (adjust the path as needed):

link_directories(c:\\Program Files \(x86\)\\Arduino\\libraries)

And uncomment this line:

# set(${PROJECT_NAME}_SKETCH Test_Project.ino)

Then create the file named above (e.g. Test_Project.ino) in the root of the project. To do that, right-click the root folder, then New > File and enter the filename. (Note: selecting New > Arduino Sketch doesn’t seem to do anything.)

Test it

Write a test program

Paste in this content, which just flashes the built-in LED (so no wiring required):

#include <USBAPI.h>

void setup() {
    Serial.begin(9600);
    Serial.println("Running setup");
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    Serial.println("Running loop");
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
    digitalWrite(LED_BUILTIN, LOW);
    delay(100);
}

Upload it

In the top-right corner, select “upload” in the dropdown, then click the Build icon (or press Ctrl-F9).

Note: If the Build icon is still greyed out, press File > Reload CMake Project. Don’t use “Run” or “Debug” because it will attempt to run the executable on the local PC rather than the Arduino, which of course won’t work.

The onboard LED (labelled “L”) should start blinking rapidly.

Adjust the delay() values to try making it blink at different speeds. Press Ctrl-F9 to upload it each time. It will execute automatically after uploading.

Check the Serial Monitor

At the bottom of the screen, click the Arduino Support tab. Click the “Edit Settings” icon and select the same port as earlier, then click OK. Then click the “Connect” button.

What you should see is “Running setup” once, then “Running loop” repeated over and over.

If everything is working, now you’re ready to start writing your own programs and building hardware circuits. I’m using the Elegoo UNO Project Super Starter Kit, which comes with a bunch of components and detailed tutorials. (There’s also one called The Most Complete Ultimate Starter Kit which has more hardware, and presumably more tutorials.)

Other notes

Upgrading CLion

After upgrading CLion, I got this error when building:

The system cannot find the path specified.
mingw32-make.exe: *** [cmake_check_build_system] Error 1
Makefile:951: recipe for target 'cmake_check_build_system' failed

I had to edit cmake-build-debug/Makefile and update various paths with the new build number. I’ve not yet worked out how to avoid that (e.g. use relative paths).

References