« January 2012 | Main | December 2012 »

Main > Diary > Development

February 20, 2012

Arduino DHT22 Library Improvement

Rob Faludi and I have been teaching a class at the International Center for Theoretical Physics in Trieste, Italy. Marco Zennaro furnished us with a number of DHT22-based Grove Temperature and Humidity Sensors for Seeed Studio. The example code on the Seeed wasn't very robust nor parameterized to work on any Arduino pin so I took a few moments to make some changes. There are now two files:


#ifndef DHT22_H
#define DHT22_H

enum DHT22_Err_t {
  DHT22_ERR_NONE = 0,
  DHT22_ERR_HUNG = 1,
  DHT22_ERR_SYNC = 2,

DHT22_Err_t getDHT22(int pin, float *temperature, float *humidity);



#include "DHT22.h"

static byte _read_dht22_dat(volatile uint8_t *in, uint8_t bitmask)
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
    while(!(*in & bitmask));  // wait for 50us
    if(*in & bitmask) 
      result |=(1<<(7-i));
    while((*in & bitmask));  // wait '1' finish
  return result;

DHT22_Err_t getDHT22(int pin, float *temperature, float *humidity)
  byte dht22_dat[5];
  byte dht22_in;
  byte i;

  uint8_t bitmask = digitalPinToBitMask(pin);
  volatile uint8_t port = digitalPinToPort(pin);
  volatile uint8_t *reg, *out, *in;

  // setup registers and pins
  reg = portModeRegister(port);
  out = portOutputRegister(port);
  in =  portInputRegister(port);
  *reg |= bitmask;
  *out |= bitmask;

  // start condition
  // 1. pull-down i/o pin from 18ms
  *out &= ~bitmask;
  *out |= bitmask;
  *reg &= ~bitmask;
  dht22_in = *in & bitmask;
    Serial.println("dht22 start condition 1 not met");
    return DHT22_ERR_HUNG;
  dht22_in = *in & bitmask;
    return DHT22_ERR_SYNC;
  // now ready for data reception
  for (i=0; i<5; i++)
    dht22_dat[i] = _read_dht22_dat(in, bitmask);
  *reg |= bitmask;
  *out |= bitmask;
  byte dht22_check_sum = dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];
  // check check_sum
  if(dht22_dat[4]!= dht22_check_sum)
    return DHT22_ERR_CHKSUM;
  return DHT22_ERR_NONE; 

This can be used as in in the following example:

#include "DHT22.h"

void setup() {
  delay(2000); // Recommended delay before sensor can be used

void loop() {
  DHT22_Err_t result;
  float temperature = 0, humidity = 0;
  result = getDHT22(4, &temperature, &humidity);  
  if (result != DHT22_ERR_NONE)
  Serial.print("Current humdity = ");
  Serial.print("%  ");
  Serial.print("temperature = ");
  Serial.println("C  ");

You may download the file ICTP_DHT22_XIG.zip. It contains a slightly enhanced example which transmits the information to the XBee Internet Gateway.

Posted by jordanh at 6:46 AM | Comments (6) | TrackBack |

Main > Diary > Technology

February 14, 2012

XBee Pulse I/O

What is an XBee Pulse I/O?

A Fully Populated XBee Pulse I/O Board

The XBee Pulse I/O is a simple, general purpose electronic design which can be used for many automation and monitoring tasks. It includes up to two relays which may be driven through a one-shot pulse generator, simulating pressing and releasing a button, or it may be connected and controlled directly by the XBee’s digital output pins. There are also two powered digital inputs which can be used with closure sensors or leak detectors. It’s open-source hardware: use our design, improve it, sell it to your friends, or give it as a gift to win over your enemies.

The project is a collaboration between myself and my friend Andy Warner.


The XBee Pulse I/O is open-source hardware.

Where is the XBee Pulse I/O Used?

The XBee Pulse I/O is used in automation projects such as the XBee Garage Door project; a project which connects an automatic garage door lift to a mobile phone for remote monitoring and control using iDigi.

Where can I get an XBee Pulse I/O?

At present, there are three options for obtaining an XBee Pulse I/O board.

The first is to obtain a blank XBee Pulse I/O PCB and components and solder a board together yourself. This option is good if you want to use the XBee Pulse I/O in a permanent installation.

The second option is to build a simpler XBee Pulse I/O for prototyping purposes on a boardboard. To build this design you order a smaller set of components from somebody such as Digi-Key.

The third option is to obtain the design files used to create the XBee Pulse I/O PCB and make one yourself: either by fabricating the board using your own method or ordering one from a manufacturer of your choice. This option is for advanced users or users who which to modify the XBee Pulse I/O design for their own purposes.

Ordering a Blank PCB and Components from Digi-Key

You order a manufactured board from BatchPCB here:

The bill-of-materials for the board can be found below. Please note that if you are building a breadboard design, these is likely not the component list you want, unless you'll be be building a PCB version later. The breadboard BOM can be found an additional section further down.

You may order this complete PCB bill-of-materials from Digi-Key by clicking this link which will add Digi-Key part number 6958220-KIT-ND to your order.

The all important assembly instructions can be found at Make: Projects XBee Pulse I/O Single Port Assembly Instructions.

Breadboarding a Simple, Prototype XBee Pulse I/O

The breadboard version of the XBee Pulse I/O has a reduced bill-of-materials and eliminates several of the components used to make the design more robust. Namely the breadboard design does not include a power-on protection circuit to keep the timer from triggering at power on and does not contain protection diodes on the sensor inputs.

The bill-of-materials for the breadboard can be found below.

You may order this XBee Pulse I/O bill-of-materials from Digi-Key by clicking this link which will add Digi-Key part number 6843976-KIT-ND to your order.

Complete, step-by-step assembly instructions are available from Make: Projects.

Obtaining the Design Files for the XBee Pulse I/O

The CadSoft EAGLE PCB files are available for download here:

The Dual Channel XBee Pulse I/O

For users who wants an XBee Pulse I/O with two relays and two digital inputs, here are the relavant links:

Posted by jordanh at 2:43 PM | Comments (0) | TrackBack |

Main > Diary > Technology

February 1, 2012

XBee Garage Door


The XBee Garage Door is an automation project which allows you to connect your automatic garage door lift to your mobile telephone via an Android or a mobile web application. This project was featured in Make Magazine Vol. 30 in the article "12,000-mile .

The XBee Garage Door project uses a Digi XBee to communicate and attaches to the door using a hardware design called the XBee Pulse I/O—a general purpose, open-source hardware design for simple automation projects.


There are four components to making your own XBee Garage Door:

  1. An Breadboarded or populated printed circuit board (PCB) XBee Pulse I/O
  2. A door-closure sensor and length of wire to connect to your existing automatic garage door lift button
  3. A ConnectPort X gateway running the XIG application or a computer running the XIG with an XBee module attached
  4. Using the Android or Web application to monitor and control your door remotely

Building Your Own XBee Pulse I/O Board

You can build your own your XBee Pulse I/O from a prepared PCB or by breadboarding your own. Complete instructions are available on the XBee Pulse I/O page.

Attaching the Sensors and Garage Door Lift

Installing the XBee Pulse I/O into your garage is easy. Follow these simple steps:

  1. Affix the free end of the door closure sensor to the bottom of your garage door
  2. Attach lead wires to the other half of the sensor; attach it to the wall opposite the sensor when the door is in the fully closed position.
  3. Connect this sensor to input 1 of the XBee Pulse I/O PCB, or between XBee pin AD1 and ground on the PCB.
  4. Attach two wires in parallel to the button used to open your garage door
  5. Attach the other side of these two wires to the pulsed output of the XBee Pulse I/O or between the normally open and common terminals of the relay on your breadboard.
  6. Power the XBee Pulse I/O with 4.5v-9v power.
  7. You're done installing!

Obtaining the XIG and Using a Gateway

Obtain a Digi ConnectPort X2 gateway from someplace such as SparkFun or order a second XBee Series 2 module and attach it to your computer using a USB or serial adapter such as the SparkFun XBee Explorer USB.

Download and install the XBee Internet Gateway from the XIG site on Google Code and follow the documentation to get the XIG running on your Digi ConnectPort gateway or PC.

Using the Android or Web Application

If you have an Android phone install this application from the Android Market. The source for the application can be found on the GitHub repository jordanh/xbee-garage-door-android.

If you have another smartphone or if you'll be experimenting with a web browser, use this website. The source, by Margaret McKenna, can be found on GitHub.

Next follow these steps:

  1. Create a free developer account on iDigi, the iDigi webservice will be used to connect your gateway and make it securely accessible from anywhere. This will make it so not just anybody can control your garage door.
  2. Add your gateway to the iDigi Device List by using the "plus" button, selecting "Add Manually" and then choosing your Digi ConnectPort gateway (found on a sticker on the gateway) or XIG-on-a-PC's MAC address (found in the UI)
  3. Start the Android or navigate to the web application
  4. Select your gateways device id as it is specified from the iDigi device list
  5. Select the XBee address which is installed in the XBee Pulse I/O attache to your garage door lift
  6. Voila! you can now monitor and control your garage door from anywhere!

Posted by jordanh at 5:14 PM | Comments (17) | TrackBack |