Skip to content

Instantly share code, notes, and snippets.

@adrien-f
Last active May 22, 2022 13:33
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save adrien-f/b5762a975839317118c7 to your computer and use it in GitHub Desktop.
Save adrien-f/b5762a975839317118c7 to your computer and use it in GitHub Desktop.
CLion and Arduino via Platform.io

CLion and Arduino via Platform.IO

Hi guys !

Here's my quick guide to set up CLion to work with the Arduino (and others) platforms. Once you get the gist (pun) of it, you should be fine !

Get Platform.IO

Follow the instructions here

Create a new project

# Create new folder YourProject
mkdir YourProject
platformio init -b uno -d YourProject

This will populate the folder YourProject with a src and lib folder.

Get the arduino libs

You should have somewhere on your drive the Arduino core library, on Windows I've found it in C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino. Copy its contents to the lib folder of your project along with the CMakeLists.txt file

Write your sourcecode

If you start having more than one file in your src folder, you'll need to add them to the src CMake file.

Read the Manual

CMake is a bit tricky to get at first, but it's a really powerful tool, and cross platform. With this setup you'll be able to develop on Windows and as well on Linux and OSX.

CLion

Open CLion and select "Import Project from Sources" and pick your newly created project. Once in the IDE, create a new Run configuration:

You will need to find the platformio executable and set it.

Enjoy !

cmake_minimum_required(VERSION 3.2)
project(YourProject)
add_subdirectory(src)
add_subdirectory(lib)
# Put this CMakeLists.txt in the lib folder (don't forget to rename it)
# Feel free to add others .h and .c to the list
add_library(arduino_core
Arduino.h
Print.cpp
HardwareSerial.h
wiring.c
wiring_shift.c
WInterrupts.c
wiring_analog.c
wiring_digital.c
WMath.cpp
wiring_pulse.c)
#
# Project Configuration File
#
# A detailed documentation with the EXAMPLES is located here:
# http://docs.platformio.org/en/latest/projectconf.html
#
# A sign `#` at the beginning of the line indicates a comment
# Comment lines are ignored.
# Simple and base environment
# [env:mybaseenv]
# platform = %INSTALLED_PLATFORM_NAME_HERE%
# framework =
# board =
#
# Automatic targets - enable auto-uploading
# targets = upload
[env:autogen_uno]
platform = atmelavr
framework = arduino
board = uno
targets = upload
upload_port = COM3 # Change your port here
# Put this CMakeLists.txt in the src folder (don't forget to rename it)
include_directories(../libs)
link_directories(../libs)
link_libraries(arduino_core)
add_executable(YourProject main.cpp)
// Put this file in src/main.cpp
#include "Arduino.h"
void setup() {
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
@adrien-f
Copy link
Author

Feel free to ask questions or comment !

@jaumard
Copy link

jaumard commented May 27, 2015

It's possible to have a basic test project that's follow this config ? Cause I try to follow the step (on Mac) and it have compile errors

@simarsenault
Copy link

I followed your setup and it works fine if I manually compile with "platformio run" (with working dir = where platforimio.ini is in my project). However, I can't make it work with the Run configuration (my setup is exactly like your screenshot). When I hit the run button (the green arrow), it lauches CMake instead of platformio: "C:\Users\Arduino.CLion12\system\cygwin_cmake\bin\cmake.exe --build C:\Users\Arduino.CLion12\system\cmake........"

Any help, anyone?

@antonagestam
Copy link

Hey @adrien-f, thanks for sharing this! It's working pretty good for me except that CLion can't find the Arduino.h even though it's in the lib directory. I've also tried marking that directory as "library root" but that doesn't fix it either. Do you have any ideas?

@whalemare
Copy link

This sample is not worked for me. I create my own, just clone this repo for check it out
https://github.com/whalemare/blink-clion

@marthinwurer
Copy link

So, I was having problems with CLion not finding the Arduino.h and failing to build anything. What I needed to do was to launch CLion from the command line (to get the right PATH), and then go to settings->build, execution, deployment->cmake and make a new profile for the board name that I had. This let the exported cmakelists from pio find everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment