Arduino Setup

From wiki

Jump to navigation Jump to search

Previous ⇦ Arduino Arduino ⇫ Up Software Basics ⇨ Next

Under Contruction! Some items on this page may not work!


  • See below

Arduino and CLion

These instructions are for installation on Ubuntu 16.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 Common Build tools

  • install build tools (make, gcc & g++ and others), cmake and Arduino:
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install arduino
sudo apt-get install gcc-avr
sudo apt-get install avr-libc
sudo apt-get install avrdude
sudo apt-get install binutils-avr
sudo apt-get install gdb-avr

# some sites indicated this component needs to be removed, 
# but others claim it has no effect.
sudo apt-get -y remove brltty  

Install Mercurial

    sudo apt-get install mercurial
    hg --version

Check Installation

  • invoke each tool and display it's version string
gcc -v
g++ -v
make -v
cmake -version
avrdude -v
avr-size -v

hg --version

Other Tasks

  • I put everything in to a "projects" sub-directory 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*.
 whoami       # this conveniently tells you your_userid
 sudo adduser your_userid dialout
 # logout (or reboot) and log back in
 # this tells you all of the groups your userid belongs to:
 # the output should contain "dialout":
 arrizza adm lp dialout cdrom sudo dip plugdev lpadmin

Install CLion

Configuration: set default build directory

  • set the default build directory to be "build"
  • Click File | Settings | Build, Execution, Deployment
  • Click CMake
  • select Debug
  • in the Generation Path text area enter "build" (no quotes)
  • click OK to accept the changes
  • Click File | Reload CMake Project
    • this creates the build sub-directory
    • it then generates all of the other files needed by CMake & make

Configuration: .ino File Type and Formatting

  • add the .ino extension to the C++ settings:
    • click File | Settings | Editor
    • select File Types & find C/C++ in the "Recognized File Types'
    • in the Registered Patterns pane, click on the green + and add "*.ino"
  • 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 2
      • braces on new lines

Get LED blinker project source

  • get the source code for ArduinoLedBlink. This is the "hello world' of embedded projects.
    cd ~/projects
    hg clone ssh://
    cd arduinoledblink
  • An hg clone only needs to be done once. However to refresh the project (i.e. if I've made changes to it on the web), you can pull those changes in:
 hg pull -u

Build it

  • If you want more information about cmake and Arduino and CMakeLists.txt,
  • run make to create the build directory, generate some necessary files and kick off a build
  • the first time invoking this step you will get a lot of output
  • subsequent times the output is much reduced
$ make
-- Generating ArduinoLedBlink
-- Using /usr/bin/avr-objcopy for converting firmware image to hex
-- Configuring done
-- Generating done
-- Build files have been written to: /home/arrizza/projects/arduino/ArduinoLedBlink/build
[ 90%] Built target mega2560_CORE
Scanning dependencies of target ArduinoLedBlink
[ 95%] Building CXX object CMakeFiles/ArduinoLedBlink.dir/ArduinoLedBlink_ArduinoLedBlink.cpp.obj
Linking CXX executable ArduinoLedBlink.elf
Generating EEP image
Generating HEX image
Calculating image size
Firmware Size:  [Program: 1652 bytes (0.6%)]  [Data: 9 bytes (0.1%)] on atmega2560
EEPROM   Size:  [Program: 0 bytes (0.0%)]  [Data: 0 bytes (0.0%)] on atmega2560

[100%] Built target ArduinoLedBlink
  • there will be a new directory called build with all of the files needed to upload and run the sketch
$ ls -l
total 24
-rwxrwxr-x 1 arrizza arrizza  970 Apr 14 20:14 ArduinoLedBlink.ino
drwxrwxr-x 3 arrizza arrizza 4096 Apr 14 20:19 build
drwxrwxr-x 3 arrizza arrizza 4096 Apr  6 21:31 cmake
-rwxrwxr-x 1 arrizza arrizza  370 Apr  6 22:13 CMakeLists.txt
-rw-rw-r-- 1 arrizza arrizza  217 Apr 14 20:14 Makefile
-rw-rw-r-- 1 arrizza arrizza  364 Apr  6 21:56 readme.txt

CLion Build

  • open the project in CLion
    • click FIle | Import Project...
    • select the project folder ~/projects/ArduinoLedBlink
    • you'll get a popup asking if you want to overwrite the CMakeLists.txt.
    • Select Open Project (not Overwrite...)
  • 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

Check USB device setup

  • unplug the Arduino board if it's plugged in
  • check the /dev list
 $ ls /dev/ttyA*
 ls: cannot access /dev/ttyA*: No such file or directory
  • plug the Arduino board via a USB cable into your PC
 $ ls /dev/ttyA*
  • if your device is not ttyACM0, you will need to change the CMakeLists.txt file to match the tty device id:
   BOARD mega2560
   PORT /dev/ttyACM0

Upload it

  • run upload. This takes the compiled and generated and installs it ("uploads") to your Arduino board.
$ make upload
[ 90%] Built target mega2560_CORE
[100%] Built target ArduinoLedBlink

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801
avrdude: reading input file "/home/arrizza/projects/arduino/ArduinoLedBlink/build/ArduinoLedBlink.hex"
avrdude: writing flash (1652 bytes):

Writing | ################################################## | 100% 0.29s

avrdude: 1652 bytes of flash written
avrdude: reading input file "/home/arrizza/projects/arduino/ArduinoLedBlink/build/ArduinoLedBlink.eep"
avrdude: writing eeprom (0 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 0 bytes of eeprom written

avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done.  Thank you.

[100%] Built target ArduinoLedBlink-upload
[100%] Built target upload
  • The LED should be off for 1 second, then turn on and blink quickly for about a second, and repeat indefinitely

Clean It

  • to delete all build artifacts and start from scratch run make clean
  • you can then build (see above)
$ make clean
rm -rf build

Unit Tests


Install googletest (unit test framework):

sudo apt-get install libgtest-dev
cd /usr/src/gtest
sudo cmake .
sudo make
sudo mv libg* /usr/lib/

Install lcov. lcov is a front end to gcov. It creates a nice HTML set of pages for the coverage information gathered by gcov. Note that gcov is part of gcc/g++ so there is no extra installation work there.

sudo apt-get install lcov

Try it

The simplest way to try google test out is to use an existing project, see ArduinoAccuratePulser. Clone it and then run these commands

cd ArduinoAccuratePulser
cd ut
make lcov

An HTML page should show up in your default browser.

Terminal Emulator

Find the device id for the USB port

Each of the terminal emulators requires a device id. 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/ttyA* - you should see none
  • enter ls /dev/ttyU* - you should see none
  • plug in the Arduino; the LED should light up
  • enter ls /dev/ttyA* - you should see a single entry /dev/ttyACM0.
  • If not, enter ls /dev/ttyU* - you should see a single entry /dev/ttyUSB0
  • 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..

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 It

  • click on File | Settings | Tools
  • select Serial Monitor (if it's not there, check the installation is correct)
  • enter "dev/ttyACM0 for the port name. Note this may be different for your machine.
  • enter 115200 for the Baudrate

Test It

  • click on View | Tool Windows | Serial Monitor
  • the Serial Monitor pane should show up at the bottom of the CLion screen
  • compile & upload a sketch that has serial output see below #Check It.
  • click on the blue "plug" icon in the Serial Monitor pane
    • you may have to plug and unplug the blue "plug" icon to get it to connect
  • Note: If it is connected, then unconnecting and reconnecting will cause the Arduino to reboot.
  • Note: if you have the Arduino printing output to the Serial Monitor, you won't be able to do a make upload to get a new version of code uploaded.
    • click on the blue "plug" icon such that it shows the two plugs separated
    • do the make upload
    • click on the blue "plug" 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 of the characters in the window press the first icon in the Serial Monitor pane "Switch to HEX view"

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 It

  • open a terminal and enter:
sudo apt-get install putty

Configure It

  • start 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/ttyACM0"
    • 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/ttyACM0"
    • make sure "Speed" is set to 115200
    • In "Saved Sessions" enter a name, e.g. "Arduino"
    • click on Save button

Test It

  • plug in the Arduino
  • in the "Saved Sessions" ensure that "Arduino" is selected & highlighted
  • the Open button should be highlighted too. Press it
  • the Configuration window closes and a new window with title "/dev/ttyACM0 -PuTTY" appears
  • the text from your Arduino application should appear in the window see below #Check it
  • 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 (or whatever you named it in the "Saved Sessions" list. The Arduino will reboot and you should see your text in the window.

Setup screen

Install it


Configure it


Test It


Get Arduino Serial Output source

The simplest sketch to test Serial Terminal behavior and its use is ArduinoSerialOutput

  • clone, compile and upload it
  • upload it to the Arduino
  • turn on putty, screen or the Serial Monitor in CLion
  • If it complains that the port is already in use:
    • check if you have another terminal emulator already connected to that port e.g. PuTTY and screen.
    • figure out what other devices are plugged into your PC and unplug them
  • If you 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/ttyACM0
Personal tools