Overview
This is to show how to set up your Arduino and CLion for arduino projects.
These instructions are for installation on Ubuntu 20.04. Other Operating Systems and Ubuntu versions may need changes or additional steps.
Install Tools
Everything in this section needs to be done only once.
- Install project devino-app-template
- run
./do_install full
to install all necessary packages - run
./do_check
to check all packages are installed and tool versions are acceptable
Other Tasks
- I put everything into a "projects" subdirectory off of $HOME.
mkdir -p ~/projects
- add your userid to the dialout group. Normally in Ubuntu you must have root access to access a serial port. This command gives your_userid access to the serial port devices in /dev/tty_USB*.
sudo adduser $USER dialout
# logout (or reboot) and log back in
# this tells you all the groups your userid belongs to:
groups
# the output should contain "dialout":
arrizza adm lp dialout cdrom sudo dip plugdev lpadmin
Install CLion
- Note: CLion is a paid IDE. See https://www.jetbrains.com/clion/buy/#edition=personal for a personal version. I installed the Perpetual License (best deal in my opinion)
- See https://www.jetbrains.com/help/clion/2017.1/quick-start-guide.html
- Create a C and/or C++ project to printf "hello world". This tests the CLion installation is correct.
Get LED blinker project source
- get the source code for Arduino LED Blink. This is the "hello world" of embedded projects.
cd ~/projects
git clone git@bitbucket.org:arrizza-public/arduino-led-blink.git
cd arduino-led-blink
finish the installation of necessary packages.
./do_subm_update full # do install of submodule
./do_install full # do install of python, compilers, arduino library
./do_check # check all is installed
For more, see:
Configure CMakeLists.txt
- If you want more information about cmake, arduino and CMakeLists.txt:
- note that the cmake subdirectory I used here was copied from https://github.com/francoiscampbell/arduino-cmake written by Francois Campbell.
- All rights and kudos belong to him for that code.
- It is based on this cmake code https://github.com/queezythegreat/arduino-cmake
- He also has a CLion plugin called "Arduino" https://plugins.jetbrains.com/plugin/7889-arduino I have not checked it out.
Determine the Board ID
Ensure the board id is correct in CMakeLists.txt. Uncomment this line in CMakeLists.txt and run ./do_build
print_board_list()
You'll see a list of boards. Choose the name for the one you are using.
-- 1] -- ARDUINO Boards:
-- 2] -- uno: Arduino Uno
-- 3] -- atmega328: Arduino Duemilanove w/ ATmega328
-- 4] -- diecimila: Arduino Diecimila or Duemilanove w/ ATmega168
-- 5] -- nano328: Arduino Nano w/ ATmega328
-- 6] -- nano: Arduino Nano w/ ATmega168
-- 7] -- mega2560: Arduino Mega 2560 or Mega ADK
-- 8] -- mega: Arduino Mega (ATmega1280)
-- 9] -- leonardo: Arduino Leonardo
-- 10] -- esplora: Arduino Esplora
-- 11] -- micro: Arduino Micro
-- 12] -- mini328: Arduino Mini w/ ATmega328
-- 13] -- mini: Arduino Mini w/ ATmega168
-- 14] -- ethernet: Arduino Ethernet
-- 15] -- fio: Arduino Fio
-- 16] -- bt328: Arduino BT w/ ATmega328
-- 17] -- bt: Arduino BT w/ ATmega168
-- 18] -- LilyPadUSB: LilyPad Arduino USB
-- 19] -- lilypad328: LilyPad Arduino w/ ATmega328
-- 20] -- lilypad: LilyPad Arduino w/ ATmega168
-- 21] -- pro5v328: Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega328
-- 22] -- pro5v: Arduino Pro or Pro Mini (5V, 16 MHz) w/ ATmega168
-- 23] -- pro328: Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
-- 24] -- pro: Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega168
-- 25] -- atmega168: Arduino NG or older w/ ATmega168
-- 26] -- atmega8: Arduino NG or older w/ ATmega8
-- 27] -- robotControl: Arduino Robot Control
-- 28] -- robotMotor: Arduino Robot Motor
-- 29] --
Determine the Port ID
Find out which USB port your Arduino is attached to. This is a special file in the Ubuntu directory structure under "/dev".
To find the correct device id, do this:
- unplug the Arduino and any other USB device (excluding your mouse)
- enter
ls /dev/ttyU*
- you should see none - enter
ls /dev/ttyA*
- you should see none - plug in the Arduino; the LED should light up
- enter
ls /dev/tty*
- you should see a single entry /dev/ttyUSB0. - If not, enter
ls /dev/ttyA*
- you should see a single entry /dev/ttyACM0 - if you see more than one entry, you have some other USB device plugged in
- If you change which USB port the Arduino is plugged into, the device id may change.
Configure cmake
At this point you have the board id and the port value for your arduino.
Set these variables:
set(MY_BOARD nano328)
set(MY_PORT /dev/ttyUSB0)
Check other settings
uncomment this line and run ./do_build
print_board_settings(${MY_BOARD})
Check that the settings are correct for your board:
-- 1] -- Arduino nano328 Board:
-- 2] -- nano328.name=Arduino Nano w/ ATmega328
-- 3] --
-- 4] -- nano328.upload.protocol=arduino
-- 5] -- nano328.upload.maximum_size=30720
-- 6] -- nano328.upload.speed=57600
-- 7] --
-- 8] -- nano328.bootloader.low_fuses=0xFF
-- 9] -- nano328.bootloader.high_fuses=0xDA
-- 10] -- nano328.bootloader.extended_fuses=0xfd
-- 11] -- nano328.bootloader.path=atmega
-- 12] -- nano328.bootloader.file=ATmegaBOOT_168_atmega328.hex
-- 13] -- nano328.bootloader.unlock_bits=0xff
-- 14] -- nano328.bootloader.lock_bits=0xcf
-- 15] --
-- 16] -- nano328.build.mcu=atmega328p
-- 17] -- nano328.build.f_cpu=16000000L
-- 18] -- nano328.build.core=arduino
-- 19] -- nano328.build.variant=eightanaloginputs
Note: if one of them are wrong you can set that value manually:
# just an example, the default value 57600 is correct
nano328.upload.speed=115200
Upload to arduino
At this point, the build is complete and the cmake is configured correctly.
- run
./do_upload
to upload the image to your arduino.
The LED should be off for 1 second, then turn on and blink quickly for about a second, and repeat indefinitely.
devino-app-template
Another project to check out is devino-app-template. This one is template. It is a starting point for creating an arduino project.
This one is similar to arduino-led-blink except the blinking is done based on random values.
When you ./do_upload
multiple times the LED will blink at different rates each time.
CLion Build
- Open the project in CLion
- click File | Open...
- select the project folder e.g. ~/projects/arduino-led-blink
- click on Run | Build; this will probably fail because cmake has not run yet.
- click File | Reload CMake project
- click on Run | Build or ctrl-F9; this should succeed. If it fails, check the other configuration settings in CLion
CLion cmake configuration
- Click File | Settings | Build, Execution, Deployment
- Click CMake
- select Debug
- in the Generator. select Ninja
- click OK to accept the changes
-
Click File | Reload CMake Project
- this creates the cmake-build-debug subdirectory
- it then generates all the other files needed by CMake & make
-
set the C/C++ formatting styles:
- click File | Settings | Editor
- select Code Style | C/C++
- set up as you wish. I prefer:
- no tabs
- indent size 4
- braces on new lines
Setup Serial Monitor in CLion
Install It
- open File | Settings | Plugins
- in the Search bar enter "Serial Port Monitor"
- the Serial Port Monitor plugin should appear in the right hand pane
- you may have to click "Search in repositories"
- if the "Install" button appears, click to install the plugin
- Once it's done installing, the "install" button changes to "Restart CLion", click that.
- If not, restart CLion.
Configure Serial Monitor
- click on File | Settings | Tools
- select "Serial Monitor" (if it's not there, check the installation is correct)
- enter "dev/ttyUSB0" for the port name. Note this may be different for your machine.
- enter 115200 for the Baudrate
- ensure 8n1 i.e. 8 bits, None parity, 1 Stop bit
Open the tool pane
- click on Tools | Serial Port Monitor | the USB0 port set up
- The pane should show up at the bottom the CLion screen
Test It
- compile & upload a project that has serial output e.g. Arduino Serial Output
- click on the "Disconnect" icon in the Serial Monitor pane
- you may have to plug and unplug the blue "plug" icon to get it to connect
- ensure it is disconnected (the icon looks disconnected).
- run
./do_upload
to start the project on the arduino -
click on the "Connect" icon in the Serial Monitor pane
-
Note: if you have the Arduino printing output to the Serial Monitor, you won't be able to do a
./do_upload
to get a new version of code uploaded.- click on the "Disconnect" icon such that it shows the two plugs separated
- do the
./do_upload
- click on the "Connect" icon such that it shows the two plugs connected
- you should see output in the Serial Monitor window
-
To clear the window, press the "garbage can" icon in the Serial Monitor pane
-
To see the hex values of all the characters in the window press the first icon in the Serial Monitor pane "Switch to HEX view"
-
If you only see "garbage" characters on the screen, check if you have it configured for:
- the correct 115200 baud rate i.e. not 9600, 19200, etc.
- the correct 8-bit characters i.e. not 7 bits
- the correct No Parity setting i.e. not Odd or Even Parity
- the correct 1 Stop bits, i.e. not 0 or 2 Stop bits
- the correct USB port. On my laptop, that's /dev/ttyUSB0
Setup PuTTY
PuTTY is a terminal emulator. The "TTY" is capitalized because many years ago, you would communicate with a teletype (abbreviated "TTY") using a modem and a serial port. Some Linux device ids (e.g. /dev/ttyxx) get their name from that bit of history as well.
Install Putty
- open a terminal and enter:
sudo apt install putty
Configure It
- start putty
putty
- you should get a "Configuration" window
-
in the Category left-hand pane, select Serial (at the very bottom, you may have to scroll)
- set "Serial Line to connect to" to "/dev/ttyUSB0"
- set "Speed (baud)" to "115200"
- make sure Data bits is 8
- make sure Stop bits is 1
- make sure Parity is None
- make sure Flow Control is None
-
in the Category left-hand pane, select Session (the top row)
- select "Serial" in the Selection Type radio buttons
- make sure "Serial line" is set to "/dev/ttyUSB0"
- make sure "Speed" is set to 115200
- In "Saved Sessions" enter a name, e.g. "arduino-nano328" (or the boardid you used in cmake)
- click on Save button
Test putty
- plug in the Arduino
- in the "Saved Sessions" ensure that "arduino-nano328" is selected & highlighted
- the Open button should be highlighted too. Press it
- the Configuration window closes and a new window with title "/dev/ttyUSB0 -PuTTY" appears
- the text from your Arduino application should appear in the window
-
Note that to enter values, you do not have to press Enter
-
to close it:
- click on the "x" System Menu button to close the window.
- You will be asked if it's ok to close, click OK
-
to reopen it:
- enter
putty
on the command line - double-click on arduino-nano328 (or whatever you named it in the "Saved Sessions" list).
- The Arduino will reboot, and you should see your text in the window.
- enter