Battery powered sensors

Now that i have 2 JeeNodes up and running for more than a week and everything’s working fine, it’s time to do some calculations on power usage of the sensors. First thing i wanted to know was how much power a combination of a sensorless Jeenode and XBee module was using, so i created a sketch that would give me some ‘inside’ information about what was going on inside the Jeenode. The sketch i made informs me about a couple of things, like:

– the timespan the JeeNode is awake to power up the XBee, send a packet to the XBee and wait for it to finish transmission;

– the timespan the XBee is powered to send that packet of 10 bytes.

Battery powered JeeNode

With these numbers i should be able to roughly calculate the power usage of this sensor. The power that is used by the JeeNode while it’s awake is about 35 mA, while the XBee uses around 40 mA while transmitting. When both the JeeNode and XBee are in deep sleep, the combination of both uses only 60 μA.

The time measurements i did to find out the timespans mentioned above gave me the following figures: the total time the JeeNode is awake to read a sample from the Pressure sensor, wake the XBee, transmit the data and go back to sleep is 40 ms. The time that the XBee is awake is around 25 ms. With a sample interval of 30 seconds, and a 2000 mAh battery, i have calculated a battery life of more than a year; increase the interval to 60 seconds (still good enough for this type of information) and batteries should survive more than 2 years… i think.

Just ask me in a year or so ..

But good enough to go ahead with the expedition! 🙂

Digital Code Lock

Last Friday a Digital Code Lock arrived. I want to see if i can do something useful with it. It is intended for use with a door opener.
Above the keypad there are 3 LEDs of which 2 are freely usable; the 3rd (middle) bi-color LED shows the status of the code lock.  The code lock comes with a manual with description of all connections and some connection examples.

Digital Code Lock

Digital Code Lock

Code Lock Internals

Code Lock Internals

It’s one of the cheaper code locks, but if all goes well and i’m satisfied i might buy another more eye-catching one.

This model has a sabotage switch inside just like the tamper switch on the inside of Visonic sensors. I’m going to connect this switch to a Visonic MCT302 aux input located inside my house and any attempt to sabotage the code lock will fire the alarm. One of the freely usable LEDs is going to display the status of the alarm, the other free LED doesn’t have a function assigned yet. Just a matter of time…

The Code Lock will be supervised by an Arduino so i can make full use of all the functionality it has to offer.

To be continued…

The power of Arduino and Zigbee

Today i had some spare time to combine a few of the things i’ve been working on lately:

  • My Zigbee API;
  • Arduino;
  • Pan & Tilt mechanism

I wanted to create a small executable that would enable me to control the position of the 2 servo’s.

XBee module

I started with programming the XBee: Zigbee End Device AT firmware, the right PAN, Node Identifier ED5 (as in End Device 5), Sleep Mode=1 (Pin Hibernate). I put the module on a breadboard by use of a Breakout Board. Grounded pin 9 and used the 3.3V output of the Arduino to power the module.

Arduino

I strapped the Arduino Duemilanove to the same breadboard and wired the 2 servo’s. Pins 9 and 10 of the Arduino are used as PWM output for the 2 servo’s. Arduino pins 2 & 3 were connected to the DIN and DOUT pins of the XBee module.

Sketch

Now it was time to write a sketch that would receive commands from the XBee and move the servo’s accordingly. The sketch is a real quick and dirty one, low on error handling and validation. I made up a small ‘protocol’: commands have to end with the exclamation mark (“!”) and can be either 1 or 2 characters long.

P+! would Pan right 3 degrees, P-! would Pan left 3 degrees, T+! would Tilt up 3 degrees and so on. H! is the command to go back to the Home position. Of course you can extend this as much as you like, e.g. add absolute positioning, presets, you name it.

This is the sketch:

#include <NewSoftSerial.h>
#include <ServoTimer1.h>
#include <PString.h>

ServoTimer1 PanServo;
ServoTimer1 TiltServo;

int PanAngle = 90;
int TiltAngle = 90;
byte DeltaPan = 3;         // move 3 degrees
byte DeltaTilt = 3;        // move 3 degrees
char cbuffer[40] = "";     // receive buffer
NewSoftSerial XBSerial = NewSoftSerial(2, 3);
PString buffer(cbuffer, sizeof(cbuffer));

void setup() {

  XBSerial.begin(9600);         // set up XBee at 9600 bps
  delay(10000);                 // wait 10 sec. for possible joining

  PanServo.attach(9);
  TiltServo.attach(10);

  PanServo.write(PanAngle);
  TiltServo.write(TiltAngle);
}

void waitforcommand()
{
  while (true)
  {
    if (XBSerial.available())
    {
      int c = XBSerial.read();
      if (c == 0x21)     // command terminator = "!"
      {
        break;
      }
      else
      {
        // add to buffer
        buffer.print(c,BYTE);
      };
    };
  };
}

void loop() {

  buffer="";
  waitforcommand();

  // what's the first character?
  switch (buffer[0])
  {
    case 0x50:             // P for Pan
      {
        // what's the next character?
        switch (buffer[1])
        {
          case 0x2B:       // +
            PanAngle += DeltaPan;
            break;

          case 0x2D:       // -
            PanAngle -= DeltaPan;
            break;
          };
          break;
      };
    case 0x54:            // T for Tilt
      {
        // what's the next character?
        switch (buffer[1])
        {
          case 0x2B:       // +
            TiltAngle += DeltaTilt;
            break;

          case 0x2D:       // -
            TiltAngle -= DeltaTilt;
            break;
          };
          break;
      };
    case 0x48:            // H for Home
      {
        PanAngle=90;
        TiltAngle=90;
        break;
      };

  };

  // check boundaries for Pan
  PanAngle=(PanAngle < 0)?0:PanAngle;
  PanAngle=(PanAngle > 180)?180:PanAngle;

  // check boundaries for Tilt
  TiltAngle=(TiltAngle < 90)?90:TiltAngle;
  TiltAngle=(TiltAngle > 180)?180:TiltAngle;

  // Set dervo's accordingly
  PanServo.write(PanAngle);
  TiltServo.write(TiltAngle);

}

PC software

Next item was a small program that would use the API to address the XBee module.

XBee servo control

XBee servo control

4  buttons for Left, Rigth, Up and Down, a Home button and a button to connect to the Zigbee network. Not very spectacular, I’d say.

There are 2 important API functions that are used to communicate with the Zigbee module:

  • Node Discovery
  • XBee Transmit Request.

The code that is executed when the “Connect” button is pressed, sends out a Node Discovery command. This will trigger all joined Zigbee modules to respond with information about themselves, like 16- and 64-bit address, node identifier and some more interesting stuff. The code goes something like this:

  XB:=TXBEE.Create(14,9600);
  XB.Open;
  gXBeeModules.Clear;
  cmd:=TXBee_NodeDiscover.Create('');
  XB.SendPacket(cmd.GetPacket);

The Node Identifier is used to see if the Zigbee module that is connected to the Arduino, is “in the air”. Cause it doesn’t have to be, it could be disconnected from power or whatever…

When the Zigbee module with Node Identifier “”ED5” has responded, the 5 buttons are enabled and clicking them will move the servo’s. This is done with the XBee Transmit Request function; a string is being sent to the XBee and the XBee will take care of transferring it out on the UART.  Below the code for the “Home” button:

i:=gXBeeModules.Indexof('ED5');
if i >=0
then begin
Module := TXBeeModule(gXBeeModules.Objects[i]);
Addr16:= Module.Addr16 ;
Addr64:= Module.Addr64 ;
TR:=TXBeeTransmitRequest.Create(1,Addr16,Addr64,"H!");
X.SendPacket(TR.GetPacket);
end;

Et voila, the servo’s start moving!

To see how (good or bad) it works, i’ve made a small video.
The power of Arduino and Zigbee!!!

ATMega328 and its Watchdog Timer

Now that i managed to put an XBee ZB module to sleep, all i had left to do was being able to put the RBBB to sleep for a certain amount of time (and have it wake up again!).  It would be very nice if i could use 2 triggers for waking up the controller: based on a predefined interval and by some external source (for example an attached motion detector that detects motion).

Zigbee module

Low-power Zigbee & Arduino

Both are actually equally important, so i tossed a coin with the outcome that i started with trying to accomplish to wake up the RBBB on a predefined interval. I found an article that pretty much did what i wanted. The maximum time that the Watchdog Timer can power down the microcontroller is 8 seconds, after which it will wake up again and carry on with it’s job. I modified some things so that the power down routine is called multiple times, which results in a power down mode in multples of 8 seconds. Currently the wake up interval is set at 32 (4*8) seconds and it has run flawlessly for 16 hours now. I still have to dig in deeper to the Watchdog code see if my ‘modifications’ can be made more efficient, but for now this will be OK.

Now i wonder what the power consumption of this combination of RBBB and Zigbee module is… should i let it run on a battery and wait till it stops? How long will that be…. no; i think it’s better to find a way to estimate battery life by actually measuring the power consumption. That will probably mean i will need extra hardware and tools.

Sleepy Arduino and Zigbee

While working on the experiments I’ve done in the past months with the Arduino Duemilanove, RBBB, XBee Series 1 and 2 modules the thought of a wireless, battery powered, ‘intelligent’ sensor kept me busy.

RBBB controlling a XBee ZB module

RBBB controlling a XBee ZB module

Finally the time had come  to do some experiments  in that direction. I’ve found several very well written articles, all by Jean-Claude Wippler regarding Low-power techniques. This was a very good starting point; one of the things that i concluded is that running on 3.3V is the best choice: it seems it’s much easier to interface with sensors at 3.3V then 5V. So i really gotta have me some JeeNodes soon now! 🙂

But it wouldn’t hurt to already start searching the net for code that would support bringing the ATMega to a state in which it would consume much less power.  I had already found a way to bring an XBee module to deep sleep by setting Sleep Mode (SM) to 1 and grounding pin 9, so now it was time to start sketching and produce something that could put both the RBBB and the attached XBee to sleep for a fixed period, wake them up, take samples of whatever sensor is attached, send the data to the Zigbee Coordinator and go back to sleep again. I didn’t want to use any external trigger, so the solution had to be found inside the MCU itself.

Putting XBee to sleep

First i wrote a sketch that periodically woke up the XBee, send some data and put the XBee back to sleep. That shouldn’t be that hard.  How wrong could i be….

The job to do comes down to the following sequence:

  • wake up the XBee (use a Digital Output from the RBBB to set XBee pin 9 low);
  • wait for the XBee to be ready (CTS pin to go low);
  • send some dummy data;
  • wait a little more and put the XBee back to sleep (set XBee pin 9 back to high);
  • delay the loop for a number of seconds.

At first run all worked fine. However, after increasing the interval at which the job listed above were to be executed, i started losing packets. Because i enclosed a counter value into the sent data, i could see a very strange behavior: depending on the interval i saw packets coming only once every 2 or 3 times a packet was sent… What’s that?? By playing around with the interval i could determine that something strange was happening when the interval was set above 5 seconds. Why ? It took me some time to figure this out.

Sleeping XBee

Sleeping XBee

I  remembered the 5 seconds to be the default value for the “Time Before Sleep” (ST) Command. But this Command and the value set, are only applicable for Cyclic Sleep End Devices. And indeed, changing the Time Before Sleep didn’t make things better. OK, so now it was time to start reading the manual really well 🙂

And there it was: the “Child Poll Timeout” must be what is bugging me. This is what the manual has to say about Child Poll Timeout:

“Router and coordinator devices maintain a timestamp for each end device child indicating when the end device sent its last poll request to check for buffered data packets. If an end device does not send a poll request to its parent for a certain period of time, the parent will assume the end device has moved out of range and will remove the end device from its child table. This allows routers and coordinators to be responsive to changing network conditions.”

So, if an End Device doesn’t let it’s parent (= Router, Coordinator) know that it’s still there from time to time, the End Device will be thrown out of the PAN, and has to start joining all over again; which takes a relatively long time, which it doesn’t really have being powered down all the time..

Increasing the value for the Child Poll Timeout on the Coordinator (to a higher value then the minimum of 5 seconds) resolved everything.

On to the next ‘easy’ job; using the ATMega328 Watchdog Timer to put the RBBB to sleep 🙂

Testing Zigbee, Arduino and Domotica interface

This weekend i’ve been working on a setup to be able to test various things with the exciting Digi XBee modules that i’ve purchased some time ago.

XBee ZB modules and a RBBB

XBee ZB modules and a RBBB

As you can see there are 2 different types of XBee modules placed on the breadboard; there are 3 (a bit smaller) XBee Series 2 modules and 3 (larger) XBee-PRO modules. I also included an RBBB, a very small Arduino compatible device, that is located on the lower half of the board and is connected to the 6th XBee module. On the lower right side of the board there’s a 3.3V regulator to power the XBee modules.

First thing that had to be done was programming the upper row of 5 Digi XBee modules with the right firmware to let them all act as a End Device in AT/Transparent mode, and do some wiring. The XBee breakout board had already been soldered some time ago. I also added some LEDs for visualizing the status of the modules. After that, it was time do do some testing of my Domotica software to interface with these modules. Commands like “IS” for sampling the Digital/Analog IO ports on the modules. Here the first problem came up; the command did execute correctly cause i got a response back, but the payload of the frame was empty.. of course, after some time i realized i forgot to enable any of the I/O on the modules… 🙁

Next command on the list was the ZigBee Transmit Request Command. This command is used for sending data to a module where it will be sent to the UART. This way it will arrive at the device that is attached to the XBee, in my case the RBBB. This allows me to communicate with the Arduino wirelessly. All worked well, i got the right responses back and i saw data arriving on the RBBB.

Now it was time to start sending data from the RBBB to my Domotica system. Here the default sleep mode of the Zigbee took some time to resolve. The first sketch for the RBBB sent some text to the XBee every 15 seconds. But this only arrived on my PC once every 2 minutes or so… and i didn’t know why. Changing the sketch and now sending the same text every second, made the problem go away… Suddenly i realized is was the default sleep mode that was bugging me! The default sleep mode for an XBee with this firmware is “Cyclic Sleep”, which means the module is only awake periodically. Ofcourse, sending data to the UART of a sleeping XBee doesn’t work… I tried to change Sleep mode to 0 (“always awake”) but that’s not even supported! Hmm, fortunately there’s a workaround for this: set sleep mode to 1 (“Pin Hibernate”), ground Pin 9 (the yellow wire in the picture does that) and you’re done. While trying to change the sleep mode on a specific module the use of the LED really payed off, cause suddenly the LED for the wrong module went off.. it seemed i had addressed the wrong module with the Sleep mode set to Pin Hibernate command. Typically a case of where you can keep on searching for hours and hours why it’s not working as expected, and all you did wrong was addressing the wrong module…

Now i have a sketch running on the RBBB that transmits a small text every 10 seconds and it will echo anything that is received. This way i can start testing how reliable the combination of XBee and Arduino really is.

RBBB board tryout

Today i soldered a RBBB (Really Bare Bones Board) kit. This is an Arduino-compatible board, but much smaller than for instance the Duemilanove.

USB BUB powered RBBB running the Blink sketch

USB BUB powered RBBB running the Blink sketch

A few weeks ago i realized that if i were actually going to use an Arduino somewhere in my house, i would want the board to be much smaller then the Duemilanove is.
That’s when i found the RBBB at Modern Devices. I ordered 2 RBBB’s, a USB-BUB and a USB cable.

Putting all pieces together was pretty straightforward, the instructions were very clear and within an hour i had my first RBBB up and running it’s first sketch.

The dimensions of the RBBB are 15 x 76 mm, but you can make it even smaller if you can live without the power jack or even the voltage regulator.
Just cut a piece off of the right side of the board: 15 x 57 mm is the minimal board size.

Another thing i found out is that the ATMega microcontroller will run fine at 3.3V, which seems to be a better choice then 5V when you want to connect all kinds of sensors to it.
For that i will soon get me some JeeNodes which seem to be specifically designed for 3.3V.

One thing i’m still a bit worried about is what to choose for communication; 433, 868 MHz or XBee?
433 MHz is where i already have a lot of sensors transmitting; sometimes sensor signals are lost, which is not a big deal for things like temperature sensors, but if i want my ATMega to lock the front door, i don’t think 433 MHz is a good choice.
The same goes for 868 MHz: that’s the frequency on which all my security sensors are operating; i don’t want to over-populate that frequency either.
XBee module on 2.4 GHz then? Maybe.. but will i be able to reduce power consumption of ATMega + XBee to an acceptable level so that i won’t have to change batteries every week?  🙁 There’s still a lot of work to do…

Arduino and PIR Motion Sensor

This evening i have been playing with a PIR motion detector and connected it to my Arduino. I wanted to see how this motion detector behaved and performed. Maybe to create my own motion sensors.
Left=MS13, right=tested PIR:

Regular MS13 and PIR compared

So i hooked it up to the 5V output from the Arduino and connected the PIR output to one of the Arduino’s digital inputs and added a LED to have a visual confirmation of motion detection:

Arduino, XBee and PIR

And i wrote a sketch that switches the LED and writes some text to the XBee that was also connected to the Arduino.

//
// PIR motion detector with LED indicator
// Status info by XBee to PC
// 07 Oct 2009
//

#include <NewSoftSerial.h>

NewSoftSerial XBSerial = NewSoftSerial(2, 3);

int PIRpin = 4;		// digital pin 4 for PIR
int LEDpin = 9;		// digital pin 9 for LED
int PIRval;             // PIR HIGH or LOW value

void setup() {
  pinMode(LEDpin, OUTPUT);    // set the LED pin as ouput
  pinMode(PIRpin, INPUT);     // set the PIR pin as an input

  // Setup SoftwareSerial port
  XBSerial.begin(9600);
  // send informational message to terminal window
  XBSerial.println("Setup finished.");
}

void loop() {

  PIRval = digitalRead(PIRpin);    // read PIR
  if (PIRval == HIGH) {
    XBSerial.print(millis());      // show uptime
    XBSerial.println(" High!");
    digitalWrite(LEDpin, HIGH);    // LED ON
  }
  else {
    XBSerial.print(millis());
    XBSerial.println(" Low!");
    digitalWrite(LEDpin, LOW);     // LED OFF
  }
  delay(100);
}

And here some sample output, captured with Hyperterminal:

...
205564 Low!
205679 Low!
205793 High!
205908 High!
...
...
209485 High!
209601 High!
209716 Low!
209830 Low!
209945 Low!
...

(those numbers are milliseconds)

From what i have seen so far, detected motion results in a high output value for about 1.5 – 2 seconds minimal. And as long as there is motion detected, the output will remain high.

In the very short time i tested this PIR, sensitivity looks very good.

Actually this is very nice; with this PIR you have almost realtime information about whether there is motion detected or not. Hook it up to an Arduino which will do the polling and for example only forward changes in motion detection.

Perhaps even add extra functionality in when to forward changes, like motion has to have a minimal time of x seconds before it is considered motion. Anything is possible. 🙂

Next thing on the list is combining a PIR and a XBee module to make the PIR wireless.

Experimenting with XBee and Arduino

Recently i bought 2 XBee RF Modules from Digi.

For those who have no clue what these modules can do, here some specs:
– Power output: 1mW
– Indoor/Outdoor range: Up to 30/100 m
– RF data rate: 250 Kbps
– Interface data rate: Up to 115.2 Kbps
– 6 x 10-bit ADC inputs
– 8 x Digital I/O
– Configuration: API or AT (Hayes) Commands.
– Operating frequency: 2.4 GHz
– Networking topology: Point-to-point, point-to-multipoint, & peer-to-peer
– Addressing: 65,000 network addresses available for each channel
– Supply voltage: 2.8 – 3.4 VDC
– Transmit current: 35/45 mA @ 3.3 V for normal/boost mode
– Receive current: 50 mA @ 3.3 V
– Sleep current: <10 A
– Interface options: 3V CMOS UART
– Size: 24 x 28 mm
– Weight: 3g

Primary goal for getting these 2 XBee modules was to get familiar with the XBee/Zigbee concept and do some experiments with these modules. For example, i wanted to see if I could get my Arduino wireless. After a lot of reading I decided to give it a try; and here is a summary of how surprisingly easy it was to get the 2 XBee modules working and getting my Arduino wireless as an extra bonus.

Buying
The first experiment was to connect one Xbee module to my PC and another one to my Arduino.
For that I bought the following items:

2 x Digi XBee 802.15.4 (Series 1) module (Digi Article Number XB24-ACI-001)

2 x Xbee Adapter kit
This adapter is used to plug in the XBee modems and takes care of:
– Onboard 3.3V regulator to power the XBee;
– Level shifting circuitry that makes it easy to connect it to 5V circuitry such as an Arduino;
– Two LEDs, for activity (red) and power (green)
– 10-pin 2mm sockets included to protect the modem and allow easy swapping, upgrading or recycling
http://www.adafruit.com/index.php?main_page=product_info&cPath=29&products_id=126
– Specifically created for use with the FTDI cable mentioned below.

1 x USB TTL-232 cable – TTL-232R 3.3V
This is a cable with a FTDI FT232RL usb/serial chip embedded in it. It has a 6-pin socket at the end that connects perfectly to the Adapter Kit mentioned above.

Soldering & Configuration
After soldering the 2 Adapter kits and plugging the Xbee modules into the adapter, connecting the USB cable to an adapter and the PC, the green LED started blinking right away; so far so good! Make sure you have the latest FTDI drivers on your PC, otherwise the cable wont work.
After testing both combinations of adapter and module, I could have started working with the Xbee modules right away, but I chose to change a few things in the modules before doing that:
– updating the Xbee modules firmware to the latest version;
– selecting my own PAN ID instead of leaving it to the factory default.

Changing the baud rate is another useful option to consider at this point. I left the baud rate to 9600 for now.
Configuration can be done in different ways. Either by use of a terminal program (Hyperterminal or whatever your favorite is) or by using a (Windows only) tool made by Digi, called X-CTU. If youre using a terminal program make sure you set the COM port settings to 9600,8,N,1. Connect, and type +++. The Xbee module should respond with an OK. Sounds familiar, right? Yep ,the XBee module can be configured with AT commands. For example, ATBD 3 means setting the baud rate to 9600, ATBD 4 will set it to 19200 baud. ATWR saves the new settings to the flash memory.

If you dont want to look up all the AT commands you need to configure the Xbee module the way you want, the X-CTU tool from Digi is the easiest way. This is what X-CTU looks like:
XCTU Screenshot

Very easy to use; you can perform firmware upgrades with it, save configuration profiles and reload those into the Xbee module, it has some testing tools like a terminal window, etc. It works very easy; no need for F1. (maybe thats why it doesnt even have a help file). For Linux users, AFAIK, the only option is by means of a terminal program. More experienced users should be able to make a script to send all the necessary AT commands to the serial port.

XBee module connected to PC:

XBee Adapter Kit

“The other side”
Ok, now we have 2 Xbee modules, configured and ready to use. Now it’s time to prepare the other side, my new Arduino. So i placed 1 XBee adapter on the breadboard next to the Arduino that was still running its silly 3 blinking LEDs test sketch.

Now it was time to make that sketch a bit more interactive so i created a new sketch that will scan the Xbee Serial data arriving at the Arduino for ASCII characters R, Y, G and (space). The R will toggle the Red LED, the Y will toggle the Yellow LED, the G will toggle the Green LED and the (space) will toggle all LEDs. Just a simple sketch to start with.


//
// LED switching by means of XBee
// Sep 2009
//
#include <NewSoftSerial.h>

NewSoftSerial XBSerial = NewSoftSerial(2, 3);

int ledPinR = 10;    // Red LED connected to digital pin 10
int ledPinY = 11;    // Yellow LED connected to digital pin 11
int ledPinG = 12;    // Green LED connected to digital pin 12
int Rval = LOW;      // initial state of Red LED
int Yval = LOW;      // initial state of Yellow LED
int Gval = LOW;      // initial state of Green LED

void setup()  {

  // Set all pins for OUTPUT
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinY, OUTPUT);
  pinMode(ledPinG, OUTPUT);

  // Set all LEDs to initial state
  SetPin(ledPinR, Rval);
  SetPin(ledPinY, Yval);
  SetPin(ledPinG, Gval);

  // Setup SoftwareSerial port
  XBSerial.begin(9600);
  // send informational message to terminal window
  XBSerial.println("Setup finished.");
}

void loop()
{
  if (XBSerial.available()) {
      // read serial data byte by byte
      byte inByte = XBSerial.read();
      // scan for specific characters: R,Y,G and space.
      switch (inByte) {
        case 82:    // R toggles Red LED
          Rval = Rval==HIGH?LOW:HIGH;
          SetPin(ledPinR, Rval);
          break;
        case 89:    // Y toggles Yellow LED
          Yval = Yval==HIGH?LOW:HIGH;
          SetPin(ledPinY, Yval);
          break;
        case 71:    // G toggles Green LED
          Gval = Gval==HIGH?LOW:HIGH;
          SetPin(ledPinG, Gval);
          break;
        case 32:    // Spacebar toggles all LEDs
          Rval = Rval==HIGH?LOW:HIGH;
          SetPin(ledPinR, Rval);
          Yval = Yval==HIGH?LOW:HIGH;
          SetPin(ledPinY, Yval);
          Gval = Gval==HIGH?LOW:HIGH;
          SetPin(ledPinG, Gval);
          break;
      }
      // echo the inByte
      // (no specific purpose)
      XBSerial.print((char)inByte);
  }
  delay(10);
}

void SetPin(int pin, int value)
{
  digitalWrite(pin, value);
}

Verify, change code, Verify, change code, Verify, change code, Verify, (hey, were talking C here 🙂  )
Another cup of coffee and finally: Compile, Upload, Running!

Here’s the Arduino board with the other XBee module:

The Xbee adapter is now powered with 5V from the Arduino (red and black wires) and the adapters RX/TX lines go to pins 2 and 3 (green and white). Normally you would use pins 0 and 1 for that, cause those 2 pins are designated for serial I/O, but that would mean I would lose the USB connection to upload sketches to the Arduino. And with very little programming experience on the Arduino that didnt look like a good idea. Fortunately theres a library called NewSoftSerial that makes it possible to do Serial I/O on other pins as well.

Now, when I have a terminal window open and connected to the Xbee module, I can use the keyboard to toggle the 3 LEDs. Yeah, I know, this wont win any “most innovative project” price, but at least I can see my 2 Xbee modules communicating with each other, and my Arduino is wireless. Mission accomplished!
And what has actually been created here? The basics for a wireless, programmable interface with 14 Digital I/O Pins (of which 6 can provide PWM output) and 6 Analog Inputs! If that aint nice

Total costs (in EUR):


Arduino                  22,50
USB cable                18,50
2 XBee modules           31,30  ($ 46,00)
2 XBee adapters          13,60  ($ 20,00)
Total                 85,90

Whats next?
– Getting hold of another kind of Xbee adapter that will Ethernet-enable a Xbee module so I can talk to it from my HA system;
– Integrating Xbee/Arduino combo in my HA system as an additional Interface so I can make use of all of the capabilities the Arduino has to offer from within my HA system;
– Buying a Pan & Tilt mechanism (to cheap to DIY) and hooking it up to the Arduino to learn more of the I/O capabilitys of the Arduino;
– And probably the most important one to result in more new ideas: exploring the capabilities of a stand alone Xbee module (as in: what can you do with the onboard Analog and Digital I/O an Xbee module has to offer)!

To be continued…

My first steps with Arduino

Last weekend i ordered a Arduino Duemilanove.

The Arduino platform is a real easy to use platform. It is intended for those who want to work with microcontrollers; especially those who have no experience in that area and need an environment that deals with all the ‘hard stuff’ to get things working.
The Duemilanove is the latest model in the Arduino product range.

Arduino Duemilanove

It comes on a small board that fits in the palm of your hand. As reference i placed a Visonic MCT302 next to it, a sensor which most of us will know. The largest component is the Atmel ATmega328 microcontroller. There’s also a USB connector on the board to connect it to your PC, and a power connector. It has 14 digital I/O pins, 6 analog inputs, a reset button and some more components needed to support the microcontroller. Connecting to the PC is as simple as connecting a USB cable to the board and your PC.

The software you need to write code for the Arduino board is open source and can be downloaded and installed in just a matter of minutes. It contains everything to develop, test and upload your code to the Arduino board.
Arduino IDE

Today the Arduino arrived. This is really Plug and Play! After connecting the USB cable and power, starting the Arduino development environment on my PC and opening an example program (called a sketch) and uploading it to the Arduino i was ready to go!
dsc_1650_resize

The Arduino hardware can be extended with additional shields that can be plugged on top of the Arduino board. Shields for XBee, Ethernet, Motor control, DMX, RFID, GPS and much more.

Next step will be building 2 XBee Adapter Kits i bought and try to get a wireless connection up and running between my PC and the Arduino, so that i can communicate with the Arduino while it’s disconnected from my PC.

dsc_1646_resize

Once that is working, i’ll try and make my own home-brew Webcam Pan & Tilt mechanism. If i succeed, i think many more interesting projects will follow 🙂