Friday, 30 September 2011

Remote Sensing with XBees, Hardware

The schematic below shows a battery-powered wireless remote temperature and humidity sensor. The wireless part is provided by an XBee Pro Series 2, the humidity sensor is an HIH-4030 and the temperature sensor is an LM335a.


The XBee is configured as an 'end-device' and periodically transmits three analog samples to its 'coordinator': humidity on AD0, temperature on AD1 and supply voltage on AD2.

The most important design decision was to run the XBee standalone, without the aid of a microcontroller, which had several consequences for the resulting circuit:
  1. It constrained the choice of humidity sensor to the relatively-expensive Honeywell part, which outputs a voltage proportional to temperature. (Cheaper parts, such as this one, output digital data requiring a microcontroller to present it to the XBee.) 
  2. The Honeywell part demands a 5v supply, and the XBee no more than 3.3v, entailing the further purchase of an Explorer Regulated to step-down the supply voltage for the XBee. (This is not shown in the diagram.)
  3. The analog inputs on the XBee can only read up to 1.2v, so each sensor's output must be passed through a resistive divider to scale it down.
  4. To conserve power, the XBee spends most of its time asleep, waking only to read the sensors. Its ON/SLEEP pin is asserted when it wakes but, of course, doesn't supply the correct voltage for the sensors, so a transistor is needed to switch the 5v supply.
Had the decision to use a microcontroller been taken instead (using an ATtiny85v, say) the entire circuit could have run from 3v, considerably reducing cost and hardware-complexity.

(The diagram was made with Eagle for which Sparkfun provides a library of circuit elements for most of the parts it supplies.)

The software side of this project will be described in a subsequent posting.

Saturday, 10 September 2011

Open Source is Wonderful, part 94

A couple of years ago, I bought an ICE Tube clock kit from Adafruit Industries. Not having touched a soldering iron in twenty years meant that I found this quite a tricky build, wiring the tube in particular. (However the online instructions were excellent, and can only have improved since.)


At the time, I'd spotted that at its heart was a programmable chip of some sort but having no idea what an ATMega was: I was happy enough to have built the thing and bask in the reflected admiration of visitors.

Until last night, that is, when I noticed that it was out by five minutes, having only been adjusted six months earlier! Comparing this with my ancient (though also digital) radio alarm-clock whose time is only adjusted after a power-cut, I wondered if I didn't know enough about AVRs now to modify the firmware to implement some sort of drift adjustment.

So I googled "icetube firmware" to find the software it had come with, and discovered there are at least four different firmware versions available, two of which have a drift adjustment feature. I went with jsgf's one, which has a bunch of other cool features, such as day- and night- brightnesses, animated transitions and the ability to turn off the seconds' display altogether (this was a deal-breaker for me originally to use it as an alarm clock).

So I ripped out the chip, discovered it was an ATMega 168v, and threw together a programmer for it, based on the In-System Programming article below --- I've updated the table there with pins for the ATMega series. Total time to implement this feature thanks to OSS? 30 minutes, as determined by the amount I had to update the clock by when I powered it back on!

Friday, 12 August 2011

Twilight


The circuit above implements a light switch which is activated by darkness and movement, using an LDR and a PIR sensor respectively. It draws its power from the voltage it's switching, using a 7805 voltage regulator (on the left). A manual override switch is also provided.

The sketch below implements the brains of this circuit. Highlights are:
  • It smooths the light reading using an array of samples to prevent accidental triggering.
  • Hysteresis also helps with this, a light reading below the light threshold is definitely daylight, while one above the dark threshold is definitely night-time.
  • The light level is tunable using a potentiometer, when it's between the light and dark thresholds, an LED is lit indicating dusk.
  • Another LED is lit, using the signal from the PIR, when motion is detected. This is useful when positioning the box in daylight!
  • A manual override is provided to turn on the light for half an hour when it's daytime, and turn it off for 15s at night-time. (The latter allows the light to be turned off and the operator time to get out of the way.)
Several iterations were required to get the switching behaviour just right for the way it was to be used (in a kitchen). Had it been implemented using discrete hardware, it would not have been so easy to make these changes.



Wednesday, 13 July 2011

Observations on the ATtiny44

Back here, where I discussed Arduino IDE support for the ATtiny core, I mentioned that I didn't think the -44 was properly supported (although I'd had success with the -84); since then I've confirmed this.

I became sure that the source of the problem was just the various switches passed by the IDE to the native compiler and linker after a day's fiddling with them (you can inspect them by starting the IDE from a shell) and soon afterwards, rather than writing one of my own, I discovered Martin Oldfield's excellent Makefile.

In order to use it, I had to tweak it a bit (you can find my version here), principally to produce, and link against, an Arduino core library; Martin's version links all of the objects into the final image which made it too big for the -44.

One last -44 hint: never use floating-point mathematics: since none of the AT chips has a floating point unit, gcc has to insert software floating-point operations which are not cheap! They add about 1.5kB to the final image, leaving very little space for your precious program!

Sunday, 5 June 2011

XBees are Great - Part 1

I wanted to create some remote sensors for a project recently. I needed a Passive Infra-Red (PIR) sensor, an RFID card reader/writer and a gesture sensor based on photo resistors. I wanted them to be wirelessly connected so that I could place the boxes wherever I wanted without having to worry about wiring issues.
I chose to use XBee wireless modules for the communications. They use a protocol called ZigBee designed for low power applications with limited distance communications, rather like Bluetooth but better designed. I had heard favourable reports about XBees and decided to try them out.

The selling features for me were that they:
  • Form themselves into a mesh network without any commands from me.
  • Can send serial data from one radio to another
  • Have up to ten digital I/O ports
  • Have up to four analogue input ports
  • Can periodically send input readings to a controller radio.
  • Input pins can be interrogated remotely
  • Output pins can be set remotely

The radios run off 3.3V but can tolerate 5V on the I/O pins. The analogue input ports measure 0v to 3.3V and produce a number in the range 0-255.

These features mean that the radios can be used stand-alone for many applications (switched, LEDs, voltage measurement) or with an Arduino for more complex applications.

I bought some from eBay. I got Series 2 radios as they have more features than Series 1 and I would recommend getting Series 2 if you buy some; Series 1 and Series 2 radios are not compatible. Obviously you need a minimum of two radios! As you can see in the picture above, XBee radios are small printed circuit boards with a wire or chip antenna on top and two rows of connectors on the bottom.

I also bought at the same time some XBee shields for my Arduinos. These shields look like this:


This was a mistake. I certainly did not need more than the number of Arduinos that I had. Also I discovered that none of the I/O pins on the XBee, once it is plugged in to the shield, are connected to the Arduino, wires have to be soldered in to achieve this. Another thing I did not like about the shield was that some of the Arduino's pins are hidden and become unavailable.

Mistake two was not having a way of connecting a USB cable to an XBee for programming it as a controller or a router. You need one controller in a network, the others, the routers, obtain addresses from the controller.

I found some Sparkfun boards that fitted the bill and bought some of those. The one below connects a USB cable with a standard mini-USB connector to the XBee. This board can be used for reflashing the firmware to con figure the radio or it can be used to connect a desktop or laptop to the radio system to give access to the other radios. The only way to reflash the firmware for reconfiguration or upgrading the firmware is to use a special configuration program (XCTU) on a Windows computer. The site says that it will not work on 64 bit systems but it does, I have had no problem using Windows 7 on a 64 bit system.












The other boards I bought for the XBees were simply breakout boards allowing me to plug the XBee into a breadboard if I wanted to. These boards also have a 5V to 3.3V converter allowing 5V to be used to power everything if desired.


These boards have to have header strips added to them to be able to plug them into a breadboard. I used one board with a four pin header on the end of the board for serial communications and power and ground.

(This article will be continued with some simple use cases)

Wednesday, 25 May 2011

One-Wire Mains Switch



The schematic shows a one-wire controlled mains switch. One-wire is a simple protocol which is typically used for networks of temperature sensors. (The diagram is a simplified version of a circuit by Simon Atkins.)

Features of this circuit are:
  • The diodes on the left provide surge protection, for those occasions when your network is hit by lightning.
  • The DS2406 is active-low, the LED is entirely optional.
  • The RC pair at the far right is called a snubber. It is used for inductive loads (e.g., electric motors) when the current is out of phase with the voltage. The resistor should be rated at 1W and the capacitor 400v.
  • The resistor R3 is required to limit the current through the opto-isolator during the instant of time when it is conductive but before the triac has switched on.

Frankenstein's Thermometer, redux

Finally managed to find a diagramming program I didn't dislike: a freeware version of Eagle. It produced the following circuit diagram for the thermometer:

There's not too much to say about that circuit, except that an ATtiny24 probably doesn't have enough storage to run the program. I used an -84. (Note that I don't think that arduino-tiny supports -44 chips yet.)



Here's a picture of the finished "product" in mid-measure:

It runs on two AAA batteries.