Thursday, August 23, 2012

Getting the CMPS03 compass to work with the Arduino

Code for testing a digital compass using Arduino UNO

CMPS03 with arduino I2C example

This will display a value of 0 - 359 for a full rotation of the compass.

The SDA line is on analog pin 4 of the arduino and is connected to pin 3 of the CMPS03.
The SCL line is on analog pin 5 of the arduino and is conected to pin 2 of the CMPS03.
Both SDA and SCL are also connected to the +5v via a couple of 1k8 resistors.
A switch to callibrate the CMPS03 can be connected between pin 6 of the CMPS03 and the ground.


#define address 0x60 //defines address of compass

void setup(){
  Wire.begin(); //conects I2C

void loop(){
  byte highByte;
  byte lowByte;
   Wire.beginTransmission(address);      //starts communication with cmps03
   Wire.write(2);                         //Sends the register we wish to read

   Wire.requestFrom(address, 2);        //requests high byte
   while(Wire.available() < 2);         //while there is a byte to receive
   highByte =;           //reads the byte as an integer
   lowByte =;
   int bearing = ((highByte<<8)+lowByte)/10; 

Thursday, August 16, 2012

Getting started with Arduino robots

The Arduino microcontroller has finally made robotics work fun. I've worked with other microcontrollers such as RoBoard, Freescale, Parallax, and found them to be more frustrating than fun. The Arduino has changed all that in my case. This shows what I've been able to do with just a few evenings.

In the first evening, I was able to get an Arduino board to drive some LEDs within just a few minutes.

A little more work, an I have motors and servos working. By the third evening, I had my first autonomous robot using one ultrasonic sensor for obstacle avoidance:

I made version 2.0 of my robot, so that I could have more breadboard space, giving me an LCD display and two ultrasonic sensors:

The ultrasonic sensors had to be polled separately so they did not confuse each other. A bigger issue was the the robot no longer turned well. The front wheel does not turn, and the robot was now heavy enough that the front wheel no longer easily skidded sideways, like it did in version 1.0 of my robot.

Yesterday in the mail I got a different wheel I ordered that solves this problem, by having small rollers on the wheel allowing it to slide sideways. So here is version 2.1 of my robot:

I'll be adding a magnetic compass soon, and eventually starting version 3.0 of my robot.