UDOO Board : Your First Sketch

My first exposure to Kickstarter was seeing the UDOO Board project. (Talk about a good first impression!) At the time I was not able to back the project, but I have been eyeing it with envy ever since! Advertised as the power of four Raspberry Pis and an Arduino Due in one board, it makes quite the promise!

One of the main reasons technology like the Raspberry Pi is attractive is because it provides not only a high level Linux environment that is more accessible and flexible than an Arduino, but also still gives access to low level world of electronics through a GPIO interface. However, while the Raspberry Pi provides that GPIO interface, its number of pins can be a limiting factor, and the lack compatibility with the Arduino community's extensive collection of shields and expansion boards can be restricting. 

UDOO is an attempt to change that! It provides the high level computing platform of either Linux or Android, combined with a fully pin compatible Arduino experience. 

So let's dive in and experience the UDOO board from first setup all the way to running an Arduino sketch!

Setting Up

UDOO does a good job of making the setup process very streamlined. Like the Raspberry Pi, the UDOO's OS is flashed to an SD card. Available OS's include a version of Ubuntu as well as Android 4.3. There are also several unofficial OS's including a version of Debian (familiar to those Raspberry Pi folks among you!) and XMBC. Downloads can be found here.

NOTE: Important! On Windows you absolutely must unzip the OS images using 7zip. The native windows unzip utility either thinks the OS images are corrupt or are several hundred petabytes in size. I downloaded the OS image three times before figuring this out...

Once downloaded and extracted, the OS image can be flashed to an SD card using these instructions.

Insert the SD card into your UDOO and attach it to a display, power source, and a network cable. Congratulations! You are ready to get started!

NOTE: In the latest default Ubuntu distribution there is a "UDOO Configuration" shortcut on the Desktop GUI. It is very similar to the Raspberry Pi's raspi-config tool. I recommend running that and reviewing it to see if there are any options you want to change!

Static IP

The first thing I like to do with my Linux powered electronics is enable SSH access and set a static IP so they can be attached headlessly to my home network and accessed via PuTTY or some other command line tool. (I do not have any spare monitors in my house and hooking up each and every Linux board to my TV is tedious...) 

Thankfully, on the UDOO SSH access is enabled by default on the default Ubuntu OS distribution! The first time you boot up your UDOO though, it will get assigned a dynamic IP address by your router's DHCP service. You will need to use this address to connect the first time, so you can then assign a static IP.

via GUI

If you want to avoid having to find the dynamic IP address for the first time SSH connection and want the simplest approach, then you can set up the static IP in the GUI. If you want to understand how to change things under the covers, then proceed to the "via Command Line" section.

To set a static IP in the Desktop GUI, go to Preferences -> Network Connections. Select the tab corresponding to the type of network connection you are using (Wired, Wireless, etc.) Under IPv4 Settings there is a Method dropdown that allows you to select Manual, and enter your own IP, Gateway, etc. 

See! That was easy...

via Command Line

As more challenging alternative (or if you have no spare monitor you can use) you can also find your UDOO's IP address by logging into your router, finding it in your list of connected devices, and seeing what IP address was assigned to it by DHCP. Using this IP address you can connect to your UDOO via PuTTY. 

You will first need to determine what interface your ethernet port is using. To do so, run this command:

ifconfig  

If should give you a listing of available network interfaces. Find the one starting with eth. Now, edit your interfaces file.

NOTE: I found that different versions of the default Ubuntu distribution had different interfaces defined. The beta2 release had eth6 as the default ethernet interface, while the version 1.0 release used eth0. Your mileage may vary!

sudo vi /etc/network/interfaces  

NOTE: If you get a prompt about there being a swap file already present (this occured for me in the version 1.0 release of the default Ubuntu distribution), you can proceed. I removed the swap file by running sudo rm /etc/network/.inferaces.swp.

You should see these lines:

auto lo  
iface lo inet loopback  

Add the following lines, substitute eth0 for the name of your ethernet interface determined from ifconfig.

auto eth0  
iface eth0 inet static  
address <YOUR STATIC IP>  
gateway <YOUR GATEWAY IP>  
netmask 255.255.255.0  

For much more extended discussion on this, consult my blog post where I demonstrate how to do this same thing on a Raspberry Pi. If you are interested in establishing a static IP address for your WIFI adapter from the command line, you can also use this other blog post as a starting point.

Your First Sketch

So how does this Linux / Arduino thing work? Out of the box, UDOO comes preinstalled with an Arduino IDE that is already setup to talk to the onboard Arduino Due. In a future blog post I will tackle the vastly more difficult challenge of compiling and uploading sketches from the command line... For this post we will use the provided Arduino IDE! (One step at a time...)

Wanting a slightly more substantial project than just illuminating a single LED, I decided to build a binary counter that uses LEDs to count from 1 to 16 in binary in a loop. 

First, I built a circuit of four independently controlled LEDs onto a breadboard. A close up photo of the board can be found here

NOTE: The long leg (anode) of the LED is on the left, closest to the incoming current. The short leg of the LED is on the right, closest to the resistor. I always get that wrong...

This circuit was wired into the Arduino Due side of the UDOO like so.

NOTE: I chose pins 23, 25, 27, and 29 for control pins, just because of their position. Any four pins on the board will do.

Now it is time for some code! 

int PIN_1 = 23;  
int PIN_2 = 25;  
int PIN_3 = 27;  
int PIN_4 = 29;

int counter = 0;

void setup() {  
  pinMode(PIN_1, OUTPUT);
  pinMode(PIN_2, OUTPUT);
  pinMode(PIN_3, OUTPUT);
  pinMode(PIN_4, OUTPUT);
}

void loop() {  
  ++counter;

  int remainder = counter;

  if (remainder >= 8) {
    digitalWrite(PIN_1, HIGH);
    remainder = remainder - 8;
  } else {
    digitalWrite(PIN_1, LOW);
  }

  if (remainder >= 4) {
    digitalWrite(PIN_2, HIGH);
    remainder = remainder - 4;
  } else {
    digitalWrite(PIN_2, LOW);
  }

  if (remainder >= 2) {
    digitalWrite(PIN_3, HIGH);
    remainder = remainder - 2;
  } else {
    digitalWrite(PIN_3, LOW);
  }

  if (remainder >= 1) {
    digitalWrite(PIN_4, HIGH);
    remainder = remainder - 1;
  } else {
    digitalWrite(PIN_4, LOW);
  }

  delay(1000);

  digitalWrite(PIN_1, LOW);
  digitalWrite(PIN_2, LOW);
  digitalWrite(PIN_3, LOW);
  digitalWrite(PIN_4, LOW);

  delay(1000);

  if (counter == 15) {
    counter = 0;
  }
}

First we create variables for the pins we are using and the loop counter:

int PIN_1 = 23;  
int PIN_2 = 25;  
int PIN_3 = 27;  
int PIN_4 = 29;

int counter = 0;  

If you want to rewire the circuit onto different pins on the Arduino you would need to change these pin variables.

In the setup() function we set each of the four pins for output:

void setup() {  
  pinMode(PIN_1, OUTPUT);
  pinMode(PIN_2, OUTPUT);
  pinMode(PIN_3, OUTPUT);
  pinMode(PIN_4, OUTPUT);
}

In the loop() function we set maintain a counter that repeatedly runs from 0 to 15. To calculate what lights to enable, we create a second variable that carries the remainder between each light. The status of each light is calculated from highest position (binary 8 in this case) to lowest position (binary 1).

  if (remainder >= 8) {
    digitalWrite(PIN_1, HIGH);
    remainder = remainder - 8;
  } else {
    digitalWrite(PIN_1, LOW);
  }

The logic here is that if the remainder is greater than the binary value of this LED (8, 4, 2, or 1), then subtract the value of this LED from the remainder and enable the light. If the remainder is not greater than the value of this LED, then disable the light (to clear its state from previous runs.) This will go through all four LEDs and set them to the right state for the counter. By the time all the lights are evaluated, the remainder will be zero!

Then you must pause for a second, so the lights will stay lit long enough to see them:

delay(1000);  

Then, to cause the "blink" effect, you need to turn all the lights off, and wait for another second (again, so that the lights will stay dark long enough to see them.) 

digitalWrite(PIN_1, LOW);  
digitalWrite(PIN_2, LOW);  
digitalWrite(PIN_3, LOW);  
digitalWrite(PIN_4, LOW);

delay(1000);  

NOTE: This last step is optional if you want the counter to smoothly count between values without blinking.

To run this sketch, save it in a file called BinaryCounter.ino and save it in a directory on your UDOO also called BinaryCounter. Open the Arduino IDE and go to File -> Open. Select BinaryCounter.ino.

Now click the upload button. It should transfer the sketch to the Arduino Due and your binary counter will start running!

Questions? Comments? Email me at: [email protected]!