Programming an Arduino Uno — FreeBSD/VirtualBox/WinXP

Yesterday I received my Arduino Uno R3 clone. Today, I programmed it to do a few basic tasks. The most notable thing is that I ran the Arduino Windows IDE (version 1.0.1) under Windows XPsp3 as a guest VM inside VirtualBox 4.1.8 under FreeBSD 8. And it worked! 

The environment

My home server (gjabkup2) runs FreeBSD 8 (specifically FreeBSD 8.3-PRERELEASE #1 r232754) on which I have a number of virtual machines running inside VirtualBox (specifically VirtualBox 4.1.8_OSE r75467). One of my VMs is Windows XPsp3 with latest patches. So I decided to try plugging my Uno into the FreeBSD host and programming it from my WinXP VM. Yes, convoluted, but I wanted to try. The fact that it worked is a major bonus!

Plugging the Uno into FreeBSD 8.3-PRELEASE

The Uno utilises the USB connection as a serial-over-USB communications link (programming and I/O) and for power (drawing power from the host PC). The Uno R3 variant utilises an Atmega16U2 as the on-board USB-to-serial converter.

With WinXP already running under VirtualBox, I plugged my Uno into gjabkup2’s USB port and saw the following on console:

  May 31 18:44:56 gjabkup2 kernel: ugen6.4:  at usbus6
  May 31 18:44:56 gjabkup2 kernel: umodem0:  on usbus6
  May 31 18:44:56 gjabkup2 kernel: umodem0: data interface 1, has CM over data, has break

My VirtualBox instance was not yet configured to pass through the Arduino USB device to the WindowsXP guest VM, so nothing happened inside the WinXP VM at this point.

Installing the necessary driver under WinXP guest

The VBox GUI allowed me to add the Uno as a recognised USB device in the USB filter list for the running WinXP VM. Having done this, I unplugged and replugged in the Uno and WinXP immediately recognised it as new hardware.

Following the Windows installation instructions for the Uno, I directed the New Hardware dialog to use the file "..\arduino1.0.1\drivers\Arduino UNI REV3.inf“. The process ultimately added the Uno as a COM&LPT port device attached to COM3 inside the WinXP VM.

The Arduino IDE

The Arduino IDE does not need to be ‘installed’ in any specific sense — simply launch the arduino.exe executable from the unpacked zip file. Before programming the Uno, the IDE needs to be told which port the Uno is plugged into (COM3 in my case) and what type of Arduino board it is (Tools->Board->Arduino Uno)

The “Blink” example program

The Arduino IDE comes with a collection of example programs, one of which (entertainly named “Blink”) simply turns on and off at one second intervals the on-board LED attached to I/O pin 13. (My clone Arduino started blinking its LED at one second intervals as soon as it was turned on, so I assume the Blink program was installed at the factory.) Nevertheless, my first experiment is to load, change and run a variant of the Blink example program found under File->Examples->1.Basics->Blink.

The Blink example program is quite simple:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  //delay(1000);               // wait for a second
  delay(500);               // wait for a half second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  //delay(1000);               // wait for a second
  delay(500);               // wait for a half second
}

I edited it to a 0.5 second on/off cycle, replacing delay(1000) with delay(500), so it would differ from the program pre-loaded into my new Uno. Pressed the “Upload” button on the IDE to compile the source  (or recompile, as required), transfer the resulting executable to the Uno over the USB interface and trigger a board reset that starts the new program running.

Within a few tens of seconds the compile and upload was complete and my Arduino’s built-in orange LED started flashing on/off every 0.5 seconds. This confirmed that (a) the Blink program could be uploaded to the Arduino Uno via the WinXP VM’s control of the FreeBSD host’s USB interface, and (b) my changes worked. Yay!

The “serial monitor” and analog inputs

The Arduino IDE has a Serial Monitor window (Tools->Serial Monitor) that emulates a console attached to the “serial port” provided by the Uno’s serial-over-USB interface. I decided to experiment with the Serial Monitor using the “ReadAnalogVoltage” example program (File->Examples->1.Basics->ReadAnalogVoltage) and my new joystick (x-y) potentiometer (“Thumb Joystick Module“).

“ReadAnalogVoltage” reads from the Uno’s analog port A0 and prints the nominal voltage to the Uno’s serial-over-USB port. I edited the program slightly to read from two analog ports (A0 and A1) and regularly print both nominal voltages to the “serial port”. Connected the joystick’s 5v & GND to similar pins on the Uno, and the joystick’s VRx and VRy to pins A0 and A1 on the Uno.

Upload’ed the program to my Uno, opened the Serial Monitor, and watched (in amazement, naturally) as two columns of numbers rolled down the screen, varying as I moved my joystick around on both axes.

Yay+!

Controlling servo-motors

Loaded up the example “Knob” in the Servo examples ( File->Examples->Servo->Knob), left my joystick connected to analog ports A0 and A1 (from the preceding experiment), and connected my new tiny servo motor (a “SG90 Mini Servo”, but labeled “Tower Pro micro servo 9G”) to the Uno.

The servo’s wiring is:

  • brown->GND
  • red->5V
  • orange->Signal (pin9 on Uno)

The Knob program (below) relies on the “Servo” library to hide the details of having the Uno send regular pulses to drive the servomotor. The width of these pulses controls the angle to which the servo motor turns itself. In the Knob program all we have to do is worry about defining a Servo object (Servo myservo), tell the Uno the servo motor is controlled by output pin 9 (myservo.attach(9)) and write an angle between 0 and 179 (using myservo.write()) based on the current analog value on input pin A0. The program loops to catch changes in the analog input value, it doesn’t need to keep writing to hold the servo in place.

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include 

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}

With the Knob program compiled, uploaded and started the servo faithfully tracked the position of one axes of the joystick. Also tried the “Sweep” demo application, which also worked just fine (causing the servo to continuously sweep back and forth without any other intervention).

Yay++!

Conclusion

It works. Programming the Uno using the Arduino Windows IDE running inside a WinXP VM under VirtualBox on a FreeBSD host works. Nice.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: