25 January 2011

A simple wireless sensor network

A lot of pieces come together in this project.  It seems to be working well; I'm pretty happy with it.  I'll jump right in and give a high level description of the pieces.

I started this blog with digital clocks and this project starts with one as the sensor unit.  It consists of a Arduino Pro Mini 328 (check the size of that guy!), a "Chronodot" Real-Time Clock (RTC) module from Macetech based on the Maxim DS3231 chip, and an XBee ZigBee module from Digi International (part no. XB24-Z7WIT-004).  Just for fun and to generate a little more data to collect, I connected a simple Cadmium-Sulfide (CdS) photocell to one of the Arduino's A/D inputs to measure light intensity wherever the sensor unit happens to be.  The readout is a 16x2 LCD from Sparkfun.  See the picture below.

Sensor Unit.  L to R: Arduino, RTC, XBee, Photocell and XBee status LEDs, power supply.

A few words on the parts.  The Arduino Pro Mini has the same ATmega328 microprocessor and basic specs as the Arduino Uno, but is much smaller!  I soldered on headers so it would plug into a breadboard.  What it doesn't have is a power supply or a USB interface.  Both Sparkfun and Adafruit make nice little breadboard power supplies that work great and accept the same wall wart input as the Uno.  As for the USB connection, you'll need an FTDI breakout board (about the size of a small postage stamp) and FTDI drivers to do the interface.  Once you have that (and it's pretty easy), the USB interface works pretty much the same as the Uno.

The DS3231 RTC is pretty cool too.  If you keep it between freezing and about 100°F, it will keep time within two parts per million, which is about a minute per year.  It accomplishes this by monitoring its own temperature (a major cause of oscillator drift) and switching capacitors in and out of the crystal oscillator circuit to trim the frequency.  The crystal itself is on-chip which also helps accuracy.  A very sweet piece of engineering.  In addition to the date and time, we can also read the temperature from it (in °C to the nearest quarter degree).

Last we have the XBee module, which uses the ZigBee specification, based on the IEEE 802.15.4 standard.  These modules are low power and don't offer terribly high bandwidth, which is just fine for sensor networks which tend to send very small amounts of data at any one time.  The really neat thing is that XBee modules form themselves (with just about zero help from you!) into self-healing mesh networks.  They can be configured either as network coordinators, routers, or end devices.  Every network must have exactly one coordinator.  Routers can do anything an end device can, plus forward messages between devices.  End devices cannot route traffic, and must do all their business through a parent node which is either a coordinator or a router.  Additionally, end devices can sleep in a low-power mode which would be very good for battery-powered remote sensors.  If an end device is sleeping, its parent node will store traffic for it.  The XBee in my sensor module is configured as a router.

Now we come to what I call the "base" unit.  This consists of an Arduino Uno, with an Ethernet shield added on top and connected to my internet router.  Also we have an XBee module, configured as the network coordinator, and a seven-segment LED display.

Base unit. Arduino and Ethernet shield at top. XBee, status LEDs, temperature display on breadboard.

The whole thing works like this.  Once a minute, the sensor unit sends the temperature and light intensity data to the base unit.  The base unit sends the data to Pachube (click here to go to the Pachube feed for this project) and displays the temperature on the LED display.  We can then download the data from Pachube, or have it create charts like below.  These are live charts and will refresh every few minutes (not sure how often exactly, haven't found the spec on Pachube yet).  So if you refresh this page, you will see current data, assuming that a cat hasn't chewed through any wires on my end in the meantime.

DS18B20 Temperature Sensor

It would be straightforward to add additional sensor units to this project.  One idea I have in mind is to measure the temperature of my wood stove with a thermocouple.  Maybe send a text message to my mobile phone if it got over a certain temperature.

Hard work!

Boy this blogging is hard work.  Takes a lot of time.  I've been having way too much fun with the technology to blog about it.  My intent was to document steps along the way.  I'm going to break away from that for now, then come back and catch up later.  So the next post will be where I'm at currently, which is a sensor network which consists of two Arduinos talking together via XBee radios, and sending data up to Pachube.com.

12 January 2011

What, actual numbers?

Binary clocks are lots of fun, but most people appreciate base ten.  I used to have a binary clock on my desk at work, and people would ask, "What is that?"  Before they could recover from the cognitive dissonance caused by the short answer ("It's a clock, can you tell what time it is?"), I'd rattle off the time to them, down to the second.  They'd check their watches and shake their heads.  I'd say, "It's binary.  You can read binary?  This is a computer company..."  (Actually it was a BCD clock, but that was a finer point I usually didn't want to get into.)

In order to move on with the clock project, we'll need to add hardware, namely a display, and software to run it.  I always like to test things out by themselves to make sure I understand them before using them in conjunction with other components.  This is called unit testing.  The only objective here is to understand how to hook up a display and talk to it, so that we can make it display whatever we want.

I chose to use a Sparkfun 7-segment serial LED display.  This seems to be a simple enough display, not very imposing, just four digits.  Appearances are deceiving.  This display can communicate in two different ways (!) and it uses the same microprocessor as the Arduino (!!) to manage the communications and drive the LEDs.  So maybe not so simple.  Witness the abundant, ummm, grousing in the forums regarding defective displays of various type and manufacture.  There seem to be lots of displays that just don't work correctly right out of the box, they just display gobbledegook and otherwise behave strangely.  Well like I said, not so simple, we are not dealing with a light bulb here.  Several things have to be done correctly.  Wiring it up for starters, and then getting the software right.  Depending on how a display communicates, there may be a couple or many parameters that have to be correctly set, and if any one of them is off, then we won't get anything useful on the display.

Now I certainly had my share of gobbledegook at first with this display.  But I assumed (correctly ;-) that the display was OK and that the trouble was in my program, and indeed it was.  Bit of a learning curve.  Maybe by reading this, yours will be shorter.  Anyway, I like this display a lot.  I can operate it in either communications mode and it works nicely.  So let's get on with it and demonstrate both methods.

The first method is sometimes called just "serial" communication, or (better) "serial TTL" communication.  This is a variation of the RS-232 communication standard that uses the 0-5 volt levels associated with transistor-transistor logic (TTL) integrated circuits.  For two devices to communicate in two directions, serial TTL requires two wires, one to send on and one to receive on.  One device's send line is the other's receive line and vice versa.  Since we will only be sending commands to the display (it's polite and never talks back), we'll only need to connect a single wire to the display in addition to the power connections.  Note that you will see the abbreviations RX and TX for receive and transmit.

The second method is called a Serial Peripheral Interface bus or SPI.  This is similar to serial TTL in that there are two lines for receive and transmit, but there are two more lines, one called a (serial) clock, and one called slave select.  The clock is used to time and synchronize the data bits sent over the receive and transmit lines.  But they're not called that; when you invent a new communication protocol, you need new terminology, so SPI doesn't use transmit and receive, but Master In Slave Out (MISO) and Master Out Slave In (MOSI).  One big difference is that SPI can connect more than two devices on the same set of wires (which is what makes it a bus): One master and one or more slaves.  Of course they cannot all talk at the same time, or things would get all confused.  So to ensure polite conversation (one at a time, please!) each slave device has a slave select line which the master controls to tell the slave that it's ok to communicate back to the master.

There are pros and cons to the two methods.  SPI can send data faster, but requires more wires.  For this project, there really isn't much advantage to one method over the other, since we won't be pushing the limits of either.  But it's a great learning opportunity to become familiar with both methods, so we will do just that!

With that as background, check out my sketch demonstrating serial TTL communication.  I tried to include lots of comments, not just on the software, but also a description of how to wire up the hardware.  This sketch can use either the Arduino SoftwareSerial library which comes with the Arduino software, or the NewSoftSerial library which has many improvements (see my Arduino Libraries and Hardware page for a link to download NewSoftSerial).  I've read a lot of less-than-flattering comments on the SoftwareSerial library.  In fact I'm not sure if they still apply, whether issues have been addressed, or whether it has just sort of been abandoned and is no longer being actively developed.  Many people prefer the NewSoftSerial library instead, which works the same from a programming standpoint.  I tried both, and they both work fine for this project, but this is a simple project not likely to test library limitations.

Display connected for serial TTL communication.  Note only one wire for the data and two for power.

Yet to come: SPI comments, picture, link to sketch...

11 January 2011

First project

I gave the kids Arduino Inventor Kits for Christmas, you can read all the gory details on my About page.  If you'd rather cut right to the chase, I was way more impressed than I had expected to be, to the point that I decided to jump in to the world of Arduino myself.

I needed a first project and really didn't have to think.  I've always liked digital clocks so that seemed like a natural.  (This may go back to high school, when I built a digital clock from small-scale TTL integrated circuits.  I still have it, and I think it still works.  Maybe a topic for another time.)

So I ordered up a bunch of hardware, and luckily the Arduino Uno came first, along with a real-time clock (RTC) module.  The seven-segment LED and LCD displays were taking longer.  So I borrowed eight LEDs and a shift register from my son's kit and went at it.  I could have used some old individual 7-segment LEDs I had kicking around, along with some 7447 TTL BCD-to-7-segment decoder/drivers, but that gets to be a lot of hardware on a small breadboard and I had better ideas coming anyway.

The requirements for the first project were therefore:
  1. Read the date and time from the RTC via an I2C interface,
  2. Write to the shift register via an SPI interface, in order to
  3. Display the seconds in binary on six discrete LEDs, and finally,
  4. Write the complete date and time back to the Arduino serial monitor.
Good enough to know it's working, and also to check the accuracy of the RTC.  More on that later.

Lessons learned

Libraries needed for the project

Connecting the shift register and talking to it with SPI

Connecting the RTC, setting it, and talking to it with I2C

Sketch code
void setup() {

void loop() {