Arduino Getting Started

26Oct 2012

Arduino Getting Started

Arduino Uno Logo

Arduino is an open source prototyping platform with an 8-bit Atmel microcontroller. An important factor is that many hardware modules, called shields, exist for the platform. These allow to easily implement a variety of tasks. Also, the accessibility of hardware and software makes using the Arduino fun.

This article shows you how to get started. Since here at wisol we like Linux and prefer Vim to the Arduino IDE for programming, we will setup a simple and convenient environment consisting of Debian as the operating system, Vim for programming, and CMake for building.

Installation

We assume that a Linux installation is present. We use Debian, but also your preferred Linux distribution will do. Make sure CMake is installed.

We then download the current version of the Arduino software and unpack it somewhere convenient. For this tutorial, we assume the software is in $HOME/programs/arduino. However, this is not required and can be changed.

Thats all, let’s get going.

Programming Environment

The Arduino Uno Board

The Arduino software has everything required for programming, to compile the code, and to download it to the board. We make direct use of much of this software, but do not use the IDE itself. Instead, we use our preferred editor for programming. This is not difficult to achieve and essentially only requires a Makefile that correctly references the binaries and includes from the Arduino SDK. We feel that CMake is slightly more comfortable and more powerful to create this Makefile than doing it by hand. So this is what we are using.

Since CMake is a Makefile generator, this procedure should work on Linux, Mac, and Windows. It has the additional benefit of decoupling the compilation and download process from the programming environment.

CMake the Makefile

Our CMake file is straightforward and does the following tasks. It should be usable as a template for your own projects.

  • Set board specific options
  • References all required executables (e.g. avr-gcc, avrdude) from the Arduino SDK
  • References all required include directories (stdlib, compiler, Arduino) from the Arduino SDK
  • Set own source files
  • Set compiler / linker options
  • Create executable

Note that we do not require any additional programs to be installed on the system. The Arduino SDK suffice. Also, we do not create a separate Arduino core library, but instead simply compile our files together with all the Arduino sources. This is simpler and works well.

To make working more comfortable, the Makefile includes a download target which converts the compiled executable to the correct object format and downloads it to the Arduino.

Remember to adjust the board specific options and the path to the Arduino SDK in CMakeLists.txt.

A simple blinking program

For testing purposes, there is a simple, non-blocking blinking program. The program, including the CMake source, can be downloaded from Github.

Clone the repository, adjust the board settings as required in CMakeLists.txt, connect your board, and then compile and download the program.

$ git clone https://github.com/wisoltech/arduino-getting-started.git
$ cd arduino-getting-started/build
$ cmake .. && make download

If all the above worked, you can start adding your code. Remember to add new source files to CMakeLists.txt.

Here is the blinking code for reference.

#include <Arduino.h>

// LED connected to digital pin 13
const int led_pin =  13;
// Blink interval in milliseconds
const unsigned long interval = 1000;
unsigned long prev_time = 0;
int led_state = LOW;

void setup() {
	// Initialize the digital pin as an output:
	pinMode(led_pin, OUTPUT);
}

void loop() {
	const unsigned long cur_time = millis();
	if(cur_time - prev_time > interval) {
		prev_time = cur_time;
		led_state = led_state == LOW ? HIGH : LOW;
		// Write output
		digitalWrite(led_pin, led_state);
	}
}

Tags: programming