<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Simon Dobson (Posts about arduino)</title><link>https://simondobson.org/</link><description></description><atom:link href="https://simondobson.org/categories/arduino.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2025 &lt;a href="mailto:simoninireland@gmail.com"&gt;Simon Dobson&lt;/a&gt; </copyright><lastBuildDate>Thu, 23 Jan 2025 17:16:08 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Citizen sensing with Arduino</title><link>https://simondobson.org/2018/05/24/citizen-sensing-with-arduino/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;Introducing some notes on low-power Arduino programming.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;In 2013 I did a summer project on using the &lt;a href="http://arduino.cc"&gt;Arduino&lt;/a&gt; as a platform for "citizen sensing". This rapidly became an exploration of how to create hardware and software that can do sensing while operating in a very low-power regime, such as one would need for an environmental sensor.&lt;/p&gt;
&lt;p&gt;There were several results from this project -- one of which &lt;em&gt;wasn't&lt;/em&gt; an actual solution to the motivating problem I'd come up with. However, it did generate a lot of notes about low-power Arduino programming, both for hardware and software, and a software library that embodies some of them&lt;/p&gt;
&lt;p&gt;I recently decommissioned the web site I was using to host this content, so I've ported it onto my main blog as a collection of &lt;a href="https://simondobson.org/development/projects/citizen-sensing/"&gt;research and development notes&lt;/a&gt; in case it's still of interest to anyone.&lt;/p&gt;</description><category>arduino</category><category>hardware</category><category>sensor networks</category><guid>https://simondobson.org/2018/05/24/citizen-sensing-with-arduino/</guid><pubDate>Thu, 24 May 2018 12:26:34 GMT</pubDate></item><item><title>First SleepySketch release</title><link>https://simondobson.org/2014/01/07/sleepysketch-release/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;Happy 2014! We're particularly happy to be making the first release of the SleepySketch library for writing low-power Arduino sketches.

&lt;!--more--&gt;

SleepySketch changes the way you write Arduino sketches by letting the library, rather than the main body of the sketch, decide when to run code. The sketch stays asleep as much as possible, with the Arduino placed into a low-power state to preserve battery.

This is a first release of SleepySketch, for comments. It provides a sketch framework, a basic sleep manager, and an example "blinkenlights" demonstration to show how the system fits together. Future releases will provide more flexible sleep management and support for component-level power management for common components like Xbee radios.

You can download SleepySketch v. 0.1 from &lt;a href="https://simondobson.org/download/sleepysketch/"&gt;here&lt;/a&gt;.

 &lt;/p&gt;</description><category>arduino</category><category>making</category><category>power management</category><category>sleepysketch</category><category>software</category><guid>https://simondobson.org/2014/01/07/sleepysketch-release/</guid><pubDate>Tue, 07 Jan 2014 12:36:14 GMT</pubDate></item><item><title>Scratch: visual programming for Arduino</title><link>https://simondobson.org/2013/10/08/s4a/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;For some people, traditional programming is daunting. This needn't be the case, and there are several more visual programming studios available -- one of which has now come to the Arduino.

&lt;!--more--&gt;

&lt;a href="http://scratch.mit.edu/" target="_blank"&gt;Scratch&lt;/a&gt; is a long-running programme from MIT that lets non-programmers -- and especially kids -- get started writing complex programs. It comes from the family of languages that include &lt;a href="http://squeak.org/" target="_blank"&gt;Squeak&lt;/a&gt; and &lt;a href="http://www.squeakland.org/" target="_blank"&gt;Etoys&lt;/a&gt;, all intended to demystify programming and computers.

Scratch has now come to Arduino by way of &lt;a href="http://s4a.cat/" target="_blank"&gt;S4a&lt;/a&gt;, that lets people develop sketches using a very visual approach:

&lt;img alt="" src="http://s4a.cat/img/shot01.png" width="580" height="332"&gt; A Scratch program

It's hard to describe exactly how this sort of visual programming works, and it's definitely not suitable for all tasks: typically it's better for exploratory and experimental, "play" approaches rather than more detailed and complex processes. But it'll be great to see whether this is appropriate for many of the applications the Arduino finds a home in -- and even more so to explore it for what we're trying to do here at Citizen Sensing.&lt;/p&gt;</description><category>arduino</category><category>making</category><category>scratch</category><guid>https://simondobson.org/2013/10/08/s4a/</guid><pubDate>Tue, 08 Oct 2013 07:00:12 GMT</pubDate></item><item><title>Uploading sketches to a breadboard Arduino</title><link>https://simondobson.org/2013/08/28/uploading-sketches/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;It turns out there there are quite a few versions of the "same" components out there. Uploading sketches to an Arduino-on-a-breadboard is trickier than it first appears.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;The &lt;a href="https://simondobson.org/2013/08/27/arduino-breadboard/"&gt;Arduino-on-a-breadboard showed&lt;/a&gt; that we can get a lower power version of the same architecture. However, in doing the measurements I used a microcontroller already loaded with the code I used for the power measurements (sleeping and blinking). Trying to change this code and upload it &lt;em&gt;via&lt;/em&gt; the USB breakout board didn't work -- repeatedly.&lt;/p&gt;
&lt;p&gt;It turns out that the &lt;a href="http://arduino.cc/en/Main/Standalone" target="_blank" rel="noopener"&gt;breadboard tutorial on the Arduino web site&lt;/a&gt; is actually flawed for the current versions of the components concerned. There are actually two problems: the microcontroller needs to be manually reset before uploading a sketch; and the USB breakout board needs slightly more supporting electronics to talk to the microcontroller.&lt;/p&gt;
&lt;p&gt;The first problem stems from the microcontroller needing to be reset before code can be uploaded to it. Essentially the reset makes the bootloader wait for code for a few seconds, and start the existing program if none arrives. On older Arduino models you have to physically reset the board using the reset switch just before uploading a sketch; on newer models, this reset happens automatically. Setting up the breakout board to reset the microcontroller immediately before it tries to talk to it will solve this.&lt;/p&gt;
&lt;p&gt;The second problem is more subtle. The USB breakout board is actually a USB to serial converter. The &lt;a href="http://arduino.cc/en/Main/Standalone" target="_blank" rel="noopener"&gt;tutorial&lt;/a&gt; suggests that it is enough to connect the transmit and receive (Tx and Rx) lines to the microcontroller, but this turns out not to be the case: one also needs to connect some handshaking lines to make the system synchronise and communicate correctly. I eventually found a &lt;a href="http://www.hobbytronics.co.uk/arduino-atmega328-hardcore" target="_blank" rel="noopener"&gt;post&lt;/a&gt; that explains this: however, &lt;em&gt;that post is flawed too&lt;/em&gt;, because it relies on a particular pin-out for the USB breakout board that's different tothe one I have. So here's a debugged explanation of what needs to happen.&lt;/p&gt;
&lt;p&gt;&lt;img alt="FT232r breakout board" src="https://simondobson.org/images/citizen-sensing/ft232r-breakout.jpg"&gt;&lt;/p&gt;
&lt;p&gt;We need to connect the basic TxD, RxD, Vcc and Gnd lines on the breakout board as you'd expect. The picture to the right shows the the underside of my breakout board, with the pins named. If we number the pins counter-clockwise from the top left (so DCD is pin 1, TXD is 9, TXLED is 11, and VCC is 13), we connect pins 3 and 10 to ground, pin 13 to power, pin 9 to pin 2 of the ATMega microcontroller, and pin 5 to ATMega pin 3.&lt;/p&gt;
&lt;p&gt;What now also need to happen is that we need to connect the CTS and DTR lines to something. DTR (Data Transfer Ready) is sent low when the USB has data ready: we want this to trigger a reset of at ATMega. We then need to send CTS (Clear To Send) low so that the board starts sending data. This is basic serial-port handshaking. The timing can be accomplished using an RC circuit consisting of a 100ohm resistor and a 100nF capacitor attached appropriately. Putting this circuit onto the breadboard sorts out the handshaking, and the Arduino IDE happily uploads sketches just as it would to a "real" Arduino.&lt;/p&gt;
&lt;p&gt;The net result of this is to add some more wiring to the USB end of the Arduino breadboard:&lt;/p&gt;
&lt;p&gt;&lt;img alt="USB end" src="https://simondobson.org/images/citizen-sensing/usb-end.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Note the resistor and capacitor. (The red wire crossing the breakout board is a Gnd connection, needed because my breadboard only had single power rails top and bottom.) The circuit involved is as follows:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Handshaking circuit" src="https://simondobson.org/images/citizen-sensing/breadboard-handshake.png"&gt;&lt;/p&gt;
&lt;p&gt;For my particular breakout board shown above, this means connecting pin 7 to the capacitor and pin 15 to the following resistor. (It's this last step that the &lt;a href="http://www.hobbytronics.co.uk/arduino-atmega328-hardcore" target="_blank" rel="noopener"&gt;post&lt;/a&gt; gets wrong -- or at least uses a different pin for CTS.) The net result is an Arduino-on-a-breadboard that looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Second Arduino-on-a-breadboard" src="https://simondobson.org/images/citizen-sensing/breadboard-arduino-2.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Somewhat more complicated, but rather more functional.&lt;/p&gt;
&lt;p&gt;I think you have to maintain a sense of perspective about these issues, annoying as they are: in many ways it's good that the components change and evolve rather than staying exactly the same, as it means that they're being developed and refined over time. On the other hand, it means you have to be &lt;em&gt;very&lt;/em&gt; circumspect about following blindly the tutorials and explanations on blog posts from even a relatively short time ago.&lt;/p&gt;</description><category>arduino</category><category>making</category><category>project:ditch</category><category>usb</category><guid>https://simondobson.org/2013/08/28/uploading-sketches/</guid><pubDate>Wed, 28 Aug 2013 20:50:04 GMT</pubDate></item><item><title>Low-power Arduino-on-a-breadboard</title><link>https://simondobson.org/2013/08/27/arduino-breadboard/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;Putting an Arduino together from scratch lets us look at where the power consumption might be reduced -- and is just an interesting thing to do anyway.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;One of the most exciting things about the Arduino is that it's open-source, so you can build them yourself -- and potentially vary the way they're put together for specific projects, which is very useful as a starting point for people (like me!) who aren't hardware engineers.&lt;/p&gt;
&lt;p&gt;The main challenge for sensing with Arduinos seems to be their power consumption, and the obvious way to address this is to see whether there are things to be done to reduce the power drain, for example by addressing the issue of the &lt;a href="https://simondobson.org/2013/07/31/power/"&gt;quiescent current of the power regulator&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;As a starting point, I used an &lt;a href="http://arduino.cc/en/Main/Standalone" target="_blank" rel="noopener"&gt;on-line guide &lt;/a&gt;to build an Arduino on a breadboard:&lt;/p&gt;
&lt;p&gt;&lt;img alt="An Arduino build from components" src="https://simondobson.org/images/citizen-sensing/breadboard-arduino.jpg"&gt;&lt;/p&gt;
&lt;p&gt;Actually this isn't a "full" Arduino as the analogue to digital converter (ADC) isn't properly set up, but it has the basic components of microcontroller (the same ATmega 328P as on an Arduino Uno), LED, reset switch, power, and USB. The breakout board at the left-hand side is the USB adapter, while the cluster of components on the right is the power regulator. At present I'm powering from batteries; one can also power from the USB, or from a wall power supply via another breakout board, but this way allows the same power measurement regime as &lt;a href="https://simondobson.org/2013/07/31/power/"&gt;earlier&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Measuring power for a simple "blink" program gives the following result:&lt;/p&gt;
&lt;table style="border: 1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Activity&lt;/td&gt;
&lt;td&gt;Power mode&lt;/td&gt;
&lt;td&gt;Current&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nothing&lt;/td&gt;
&lt;td&gt;Deep sleep&lt;/td&gt;
&lt;td&gt;4.5mA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flashing LED&lt;/td&gt;
&lt;td&gt;Awake&lt;/td&gt;
&lt;td&gt;17mA&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;So in deep sleep mode the system draws about a seventh the power as a
"real" Arduino. This is all down to the choice of voltage regulator:
an L7805 with a design maximum quiescent current of 6mA. To put this
into perspective, a system that could last a week on a standard
Arduino board would last the best part of two months in this
configuration. Put another way, we can build a sensor mote with an
Arduino architecture and dramatically increased lifetime by changing a
core component and using SleepySketch to keep the system asleep by
default.&lt;/p&gt;</description><category>arduino</category><category>making</category><category>power management</category><category>project:ditch</category><guid>https://simondobson.org/2013/08/27/arduino-breadboard/</guid><pubDate>Tue, 27 Aug 2013 10:47:45 GMT</pubDate></item><item><title>XBee sleeping</title><link>https://simondobson.org/2013/08/02/xbee-sleeping/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;Clearly saving battery power means getting the XBee radio to sleep at the behest of the Arduino. This turns out to be fairly simple, but does require modifying the XBee shield slightly.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;Our &lt;a href="https://simondobson.org/2013/07/31/power/"&gt;previous measurements&lt;/a&gt; indicate that the XBee draws about 45mA of current -- something we have to save for battery-powered nodes. Fortunately XBee radios have a hardware-controlled sleep mode, so the Arduino can sleep the radio when not in use.&lt;/p&gt;
&lt;p&gt;Since XBees work as a &lt;a href="https://simondobson.org/2013/07/02/mesh-network/"&gt;mesh network&lt;/a&gt;, it's clearly going to be an issue as to &lt;em&gt;when&lt;/em&gt; a radio sleeps, and for how long -- since when asleep the radio can't route packets, and so the network starts to break down. But that's a higher-level concern: for the moment, we'll focus on the mechanics of getting the XBee to sleep.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Pins relating to XBee sleep mode" src="https://simondobson.org/images/citizen-sensing/sleep-pin.jpg"&gt;&lt;/p&gt;
&lt;p&gt;The basic mechanism is simple, and involves hardware and software. At the hardware level, the XBee uses pin 9 (shown in red right) as a control pin. This pin can be used to sleep the radio: setting it to 3,3V ("high" or "asserted") causes the radio to sleep; setting it to 0V ("low" or "deasserted") wakes the radio up. The XBee only takes account of the pin when in certain sleep modes, however: these are analogous to the &lt;a href="https://simondobson.org/2013/07/23/arduino-watchdog/"&gt;Arduino's sleep modes&lt;/a&gt;. This is the software part: the XBee needs to be placed into the correct sleep mode, and can then be controlled from the Arduino.&lt;/p&gt;
&lt;p&gt;Also note I/O line 7, pin 12 (in green): we'll come back to this later.&lt;/p&gt;
&lt;p&gt;We'll deal with the hardware part first, and then the software.&lt;/p&gt;
&lt;h3&gt;Getting access to the sleep pin&lt;/h3&gt;

&lt;p&gt;The XBee shield doesn't connect the sleep pin to anything by default, so to control it we have to connect it. There are several ways we could do this, with the simplest being to solder a wire from the pan on pin 9 to an appropriate header on the shield, which is then connected to a digital pin on the Arduino. For simplicity we'll start with a wire that's long enough to reach &lt;em&gt;any&lt;/em&gt; header: we'll actually plug it into the header for the Arduino's D7 pin.&lt;/p&gt;
&lt;p&gt;There's a slight concern about voltage levels in this approach, as the
Arduino operates at 5V while the XBee uses 3.3V. Experimentally this
doesn't seem to make a difference; for a production system we'd
probably want to create a resistor network to drop the voltage to that
needed by the radio, to avoid any risk of damage. We'd also probably
want to solder a header to XBee pin 9's pad to make it easier to
connect the wiring.&lt;/p&gt;
&lt;h3&gt;Setting the sleep mode&lt;/h3&gt;

&lt;p&gt;The XBee's sleep mode is controlled by a single AT command called (unsurprisingly) SM. The default (SM 0) is for the radio to be on all the time; SM 1 selects the pin-sleep mode described above, and is the one we'll be using as it places the radio under the (hardware) control of the Arduino. (There are other sleep modes where the radio sleeps under the control of its own internal timer. Some projects use the radio's timer to wake the Arduino rather than the other way round: we prefer to keep the Arduino in control.)&lt;/p&gt;
&lt;p&gt;Setting the sleep mode is simply a matter of &lt;a href="https://simondobson.org/2013/08/02/at-commands-2"&gt;issuing the appropriate AT command&lt;/a&gt;. However, as with a lot of things to do with hardware, we have to set things up slightly first.&lt;/p&gt;
&lt;p&gt;The first issue concerns the setting of the XBee's sleep pin. If we
select pin-sleep mode with the pin high, the XBee will sleep
immediately, which might not be what we intended. So if we've
connected the sleep pin to D7, we need to set things up so that the
pin is low to keep the radio awake, and then select its sleep mode:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="cp"&gt;#define XBEE_SLEEP 7                &lt;/span&gt;&lt;span class="c1"&gt;// Xbee sleep pin on D7&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;pinMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;XBEE_SLEEP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OUTPUT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;// sleep control&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;digitalWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;XBEE_SLEEP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;// deassert to keep radio awake when sleep mode selected&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;(Some early mistakes showed that -- contrary to what might be expected -- the sleep pin defaults to high (send radio to sleep) rather than low. So this step is important.)&lt;/p&gt;
&lt;p&gt;The next issue concerns the rather intricate behaviour of the XBee's other pins when sleeping. When the radio enters sleep mode, it asserts its I/O 7 line (pin 12) so that external devices know that it's asleep. This could be used to make sure that external peripherals wake up only when the radio is active, but for some reason the XBee shield's designers have connected this pin to the Arduino's reset line, which means that sleeping the radio will reset (and in fact freeze) the Arduino. &lt;a href="http://rubenlaguna.com/wp/2009/03/05/setting-xbee-to-sleep-causes-arduino-reset/" target="_blank"&gt;Some posts suggest&lt;/a&gt; that solving this requires cutting lines on the shield, which might have been true for earlier shields but now isn't: we simply need to disable the output of this pin, using another AT command:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="n"&gt;atCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This disables the XBee's D7 line (not to be confused with the Arduino's D7 line, which we've attached to the XBee's sleep pin), which is enough to stop the Arduino freezing. on sleep. (Yes, this &lt;em&gt;did&lt;/em&gt; take quite a while to work out, since you ask...)&lt;/p&gt;
&lt;p&gt;We can now put all this together to place the XBee into SM 1  and let the Arduino sleep it at will:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="cp"&gt;#define LED 13&lt;/span&gt;
&lt;span class="cp"&gt;#define XBEE_SLEEP 7                &lt;/span&gt;&lt;span class="c1"&gt;// XBee sleep pin on D7&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;pinMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OUTPUT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;// LED signal&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;pinMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;XBEE_SLEEP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;OUTPUT&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;// sleep control&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;9600&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;radio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setSerial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Serial&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;digitalWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;XBEE_SLEEP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;// deassert to keep radio awake when sleep mode selected&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;atCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"D7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;atCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SM"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="c1"&gt;// AT commands failed, flash frantically&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that we've used local AT commands to set the radio's mode. It's
also possible to do this &lt;a href="https://simondobson.org/2013/07/xctu/"&gt;statically using
X-CTU&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Results&lt;/h3&gt;

&lt;p&gt;&lt;img alt="XBee with sleep control" src="https://simondobson.org/images/citizen-sensing/xbee-sleep.jpg"&gt;&lt;/p&gt;
&lt;p&gt;The results of all this hacking are that the Arduino can put the XBee into sleep mode whenever it wants to simply by asserting D7. The voltages on pins are maintained even when the Arduino itself sleeps, so it can put the radio to sleep and then sleep itself, wake up and wake up the radio.&lt;/p&gt;
&lt;p&gt;Measuring current shows that the sleeping Arduino and XBee draw abour 35mA, the same as an Arduino alone. This makes sense, as the XBee datasheet suggests that when sleeping it draws current in the microamp range -- far too small for a normal ammeter to measure, and dwarfed by the quiescent current of the Arduino board (which still needs some work).&lt;/p&gt;
&lt;p&gt;Waking the radio happens quickly when the sleep pin is deasserted, but it seems to take some time to re-connect to the mesh co-ordinator: around 7s, in fact, which is a little strange and needs some more exploration.&lt;/p&gt;</description><category>arduino</category><category>at command</category><category>making</category><category>power management</category><category>project:ditch</category><category>xbee</category><guid>https://simondobson.org/2013/08/02/xbee-sleeping/</guid><pubDate>Fri, 02 Aug 2013 14:41:30 GMT</pubDate></item><item><title>Issuing AT commands</title><link>https://simondobson.org/2013/08/02/at-commands-2/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;Controlling the XBee requires issuing AT commands. The XBee library has the low-level machinery to do this.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;AT commands are the basis for controlling almost all modems, and the XBee is no different. In API mode, AT commands are issued in a similar manner to sending data. The Arduino XBee library has the low-level code needed, which can be wrapped into a slightly easier-to-use form.&lt;/p&gt;
&lt;p&gt;The basic approach is to send an AT command request packet and then
read a returned packet acknowledging the command. For the moment we'll
stick to "setting" commands, where the AT command takes an integer
parameter: the other are needed less frequently. We construct the
request packet, send it, read the response, and check that all went
well. This isolates the rest of the program from the message exchange,
but also hides the exact nature of any error.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;XBee.h&amp;gt;&lt;/span&gt;

&lt;span class="n"&gt;XBee&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;radio&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;atCommand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// send local AT command&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;AtCommandRequest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AtCommandRequest&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;amp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint8_t&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;radio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// receive response frame&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;AtCommandResponse&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AtCommandResponse&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readPacket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;                               &lt;/span&gt;&lt;span class="c1"&gt;// read packet from radio&lt;/span&gt;
&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getResponse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;getApiId&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AT_RESPONSE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// right type?&lt;/span&gt;
&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="n"&gt;radio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getResponse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;getAtCommandResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isOk&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;                                     &lt;/span&gt;&lt;span class="c1"&gt;// not an error?&lt;/span&gt;
&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;// if we get here, return a failure&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This function can be used to issue the different control codes for the radio. Some parameters can be &lt;a href="https://simondobson.org/2013/07/02/xctu/"&gt;set using X-CTU &lt;/a&gt;when the radio firmware is installed, but commands are sometimes needed at run-time too.&lt;/p&gt;</description><category>arduino</category><category>at command</category><category>making</category><category>project:ditch</category><category>xbee</category><guid>https://simondobson.org/2013/08/02/at-commands-2/</guid><pubDate>Fri, 02 Aug 2013 11:03:54 GMT</pubDate></item><item><title>Basic power measurements</title><link>https://simondobson.org/2013/07/31/power/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;Some initial measurements of power consumption.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;How much power does Arduino sleep mode save? The simplest way to work this out is to power an Arduino from a battery pack and measure the current being drawn in the different modes. A simple program to demonstrate the different modes is:
&lt;/p&gt;&lt;ul&gt;
    &lt;li&gt;Normal &lt;code&gt;delay()&lt;/code&gt; loop&lt;/li&gt;
    &lt;li&gt;Deep sleep for a period (deep sleep)&lt;/li&gt;
    &lt;li&gt;Flash the LED (awake)&lt;/li&gt;
    &lt;li&gt;Flash the LED differently while sending out radio messages (awake and transmitting)&lt;/li&gt;
&lt;/ul&gt;
We perform these tasks repeatedly, keeping them going for 10s each to let the power draw stabilise.
&lt;p&gt;The results are as follows:&lt;/p&gt;
&lt;table style="border: 1"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Activity&lt;/td&gt;
&lt;td&gt;Power mode&lt;/td&gt;
&lt;td&gt;Current&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nothing&lt;/td&gt;
&lt;td&gt;&lt;code&gt;delay()&lt;/code&gt; loop&lt;/td&gt;
&lt;td&gt;43mA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nothing&lt;/td&gt;
&lt;td&gt;Deep sleep&lt;/td&gt;
&lt;td&gt;33mA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Steady LED&lt;/td&gt;
&lt;td&gt;Deep sleep&lt;/td&gt;
&lt;td&gt;34mA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flashing LED&lt;/td&gt;
&lt;td&gt;Awake&lt;/td&gt;
&lt;td&gt;45mA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Xbee (quiet)&lt;/td&gt;
&lt;td&gt;Deep sleep&lt;/td&gt;
&lt;td&gt;72mA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Xbee (quiet)&lt;/td&gt;
&lt;td&gt;Awake&lt;/td&gt;
&lt;td&gt;85mA&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Xbee (transmitting)&lt;/td&gt;
&lt;td&gt;Awake&lt;/td&gt;
&lt;td&gt;87mA&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The good news is that SleepySketch makes it very easy to access the deep sleep mode, and to stay in it by default. This is good, as the normal approach of using &lt;code&gt;delay()&lt;/code&gt; is quite power-hungry. The bad news is that the "at rest" power consumption of an Arduino even in deep sleep  -- the quiescent current being drawn by the voltage regulator and other components on the board, regardless of what the microcontroller is doing -- is about 35mA, with an XBee drawing an additional 40mA.There is very little difference in power whether the radio is transmitting or not (although the current being drawn looked more variable when transmitting, suggesting that there's some variation happening faster than the ammeter's sample time).&lt;/p&gt;
&lt;p&gt;The radio isn't put to sleep when the Arduino is asleep, which is clearly something that needs to happen: it draws power even when the Arduino is incapable of using it. Something to explore. Potentially more serious is the power being drawn when the Arduino is asleep. A battery pack with 4 x 1500mAH batteries will be drained in about 7 days (6000mAH / 35mA) even with the system asleep all the time.&lt;/p&gt;
&lt;p&gt;[UPDATE 1Aug2013: made the table layout a bit clearer.]&lt;/p&gt;</description><category>arduino</category><category>making</category><category>power management</category><category>project:ditch</category><category>xbee</category><guid>https://simondobson.org/2013/07/31/power/</guid><pubDate>Wed, 31 Jul 2013 17:12:55 GMT</pubDate></item><item><title>Understanding Arduino sleep modes: the watchdog timer</title><link>https://simondobson.org/2013/07/23/arduino-watchdog/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;The Arduino has several sleep modes that can be used to reduce power consumption. The most useful for sensor networks is probably the one that uses the watchdog timer.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;Powering-down the Arduino makes a lot of sense for a sensor network: it saves battery power allowing the system to survive for longer. Deciding &lt;em&gt;when&lt;/em&gt; to power the system down is another story, but in this post we'll concentrate on documenting the mechanics of the process. The details are necessarily messy and low-level. (I've been greatly helped in writing this post by &lt;a href="http://www.atmel.com/devices/atmega328p.aspx?tab=documents" target="_blank"&gt;the data sheet for the Atmel ATmega328P microcontroller&lt;/a&gt; that's used in the Arduino Uno, as well as by &lt;a href="http://donalmorrissey.blogspot.ie/2010/04/putting-arduino-diecimila-to-sleep-part.html" target="_blank"&gt;a series of blog posts by Donal Morrissey&lt;/a&gt; that also deal with other sleep modes for the Atmel.)
&lt;/p&gt;&lt;h3&gt;Header files and general information&lt;/h3&gt;
To use the watchdog timer, a sketch needs to include three header files:
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;avr/power.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;avr/wdt.h&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These provide definitions for various functions and variables needed to control the watchdog timer and manage some of the other power functions.&lt;/p&gt;
&lt;h3&gt;Power modes&lt;/h3&gt;
&lt;p&gt;A power (or sleep) mode is a setting for the microcontroller that allows it to use less power in exchange of disabling some of its functions. Since a microcontroller is, to all intents and purposes, a small computer on a chip, it has a lot of sub-systems that may not be needed all the time. A power mode lets you shut these unneeded sub-systems down. The result saves power but reduces functionality.&lt;/p&gt;
&lt;p&gt;Power modes are pretty coarse control mechanisms, and can shut down more than you intend. If your project is basically software-driven, with the Arduino making all the decisions, then a "deep" power-saving mode is ideal; on the other hand, if you rely on hardware-based signals at all, a "deep" sleep will probably ignore your hardware and the Arduino may never wake up.&lt;/p&gt;
&lt;p&gt;The watchdog timer is used to manage the "power-down" mode, the deepest sleep mode with the biggest power savings.&lt;/p&gt;
&lt;h3&gt;Watchdog timer&lt;/h3&gt;
&lt;p&gt;The Arduino's watchdog timer is a countdown timer that's driven by its own oscillator on the microcontroller. It's designed to run even when all the other circuitry is powered down, meaning that the microcontroller is drawing as little power as possible without actually being turned off completely.&lt;/p&gt;
&lt;p&gt;Why "watchdog" timer? The basic function of a watchdog timer is to "bite" after a certain period, where "biting" means raising an interrupt, re-setting the system, or both. A typical use of a watchdog is to make a system more robust to software failures. Since the watchdog is handled by the microcontroller's hardware, independent of any program being run, it will still bite even if the software gets stuck in an infinite loop (for example). Some designers set the watchdog ahead of complex operations, so that if the operation fails, the system will reset in a short amount of time and end up back in a known-good configuration. At the end of a successful operation, the program disables the watchdog (before it bites) and carries on. Of course this assumes that the operation completes before the watchdog bites, which means the programmer needs to have a good idea of how long it will take.&lt;/p&gt;
&lt;h3&gt;Setting the time-out period&lt;/h3&gt;
&lt;p&gt;It's as well to understand how watchdog timers on microcontrollers work. Typically they have a fairly coarse resolution, counting a fixed number of timer ticks before "biting" and performing some function. In the case of the Arduino, the watchdog timer is driven by the internal oscillator running at 128KHz and counts off some multiple of ticks before biting. This value -- the number of ticks counted -- is referred to as the "prescalar" for the timer.&lt;/p&gt;
&lt;p&gt;The prescalar is controlled by the values of four bits in the watchdog timer's control register, &lt;code&gt;WDTCSR&lt;/code&gt;. To set them up, you pick the value of prescalar you want and set the appropriate bits. If the bits contain a number ( i ), then the watchdog will bite after ( (2048 &amp;lt;&amp;lt; i) / 128000 ) seconds. So ( i = 0) means the watchdog bites after 16ms; ( i = 1 ) produces  delay of 32ms; and so on up to ( i = 9 ) (the largest value allowed) means the watchdog bites after about 8s.&lt;/p&gt;
&lt;p&gt;The word "about" is important here: the oscillator's exact frequency depends on the supply voltage to the chip and some other factors, meaning that you should be conservative about relying on the delay time.&lt;/p&gt;
&lt;p&gt;Writing the appropriate value of ( i ) into the control register
involves representing ( i ) as a four-digit binary number and then
writing these bits into four bits of the register -- and unfortunately
these bits aren't consecutive. if ( i = 7 ) for example, then this
is 0b0111 in binary, so we write 1 into bits &lt;code&gt;WDP0&lt;/code&gt;,
&lt;code&gt;WDP1&lt;/code&gt; and &lt;code&gt;WDP2&lt;/code&gt;, and 0 into bit
&lt;code&gt;WDP3&lt;/code&gt;, and 0 into all the other bits:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="n"&gt;WDTCSR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDP0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDP1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDP2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The phrases of the form &lt;code&gt;(1 &amp;lt;&amp;lt; WDP0)&lt;/code&gt; simply takes a binary digit 1 and shifts it left into bit position &lt;code&gt;WDP0&lt;/code&gt;. The &lt;code&gt;|&lt;/code&gt; symbols logically OR these bits together to generate the final bit mask that is assigned to the control register.&lt;/p&gt;
&lt;p&gt;Actually there's a little bit more to it than this, as we can't change the watchdog's configuration arbitrarily. Instead we have to notify the chip that it's configuration is about to be changed, by setting two other bits in the control register and then performing the updates we want:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="n"&gt;WDTCSR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDCE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Setting &lt;code&gt;WDCE&lt;/code&gt; enables changes in configuration to be made in the next few processor cycles, &lt;em&gt;i.e.&lt;/em&gt; immediately. Setting &lt;code&gt;WDE&lt;/code&gt; resets the timer.&lt;/p&gt;
&lt;p&gt;Finally we enable the watchdog timer interrupts by setting bit
&lt;code&gt;WDIE&lt;/code&gt;. When the watchdog timer bites, the microcontroller
executes an interrupt handler, re-starts the main program, and clears
&lt;code&gt;WDIE&lt;/code&gt;. Any further interrupts, if the time is re-enabled,
will then cause a system reset.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="n"&gt;WDTCSR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDIE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So the complete code the setting up the watchdog timer to bite in 2s is:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="n"&gt;set_sleep_mode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SLEEP_MODE_PWR_DOWN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;              &lt;/span&gt;&lt;span class="c1"&gt;// select the watchdog timer mode&lt;/span&gt;
&lt;span class="n"&gt;MCUSR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDRF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;                            &lt;/span&gt;&lt;span class="c1"&gt;// reset status flag&lt;/span&gt;
&lt;span class="n"&gt;WDTCSR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDCE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;               &lt;/span&gt;&lt;span class="c1"&gt;// enable configuration changes&lt;/span&gt;
&lt;span class="n"&gt;WDTCSR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDP0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDP1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDP2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// set the prescalar = 7&lt;/span&gt;
&lt;span class="n"&gt;WDTCSR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDIE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;                            &lt;/span&gt;&lt;span class="c1"&gt;// enable interrupt mode&lt;/span&gt;
&lt;span class="n"&gt;sleep_enable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;                                   &lt;/span&gt;&lt;span class="c1"&gt;// enable the sleep mode ready for use&lt;/span&gt;
&lt;span class="n"&gt;sleep_mode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;                                     &lt;/span&gt;&lt;span class="c1"&gt;// trigger the sleep&lt;/span&gt;

&lt;span class="cm"&gt;/* ...time passes ... */&lt;/span&gt;

&lt;span class="n"&gt;sleep_disable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;                                  &lt;/span&gt;&lt;span class="c1"&gt;// prevent further sleeps&amp;lt;/pre&amp;gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3&gt; Interrupt handler&lt;/h3&gt;
&lt;p&gt;What happens when the watchdog bites? It causes an interrupt that has to be handled before the program can continue. The interrupt could be used for all sorts of things, but there's often no point in worrying about it: but it still has to be there, to prevent the microcontroller just resetting. The following code installs a dummy interrupt handler:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;&lt;span class="n"&gt;ISR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;WDT_vect&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="cm"&gt;/* dummy */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;WDT_vect&lt;/code&gt; identifies the watchdog timer's interrupt vector.&lt;/p&gt;
&lt;p&gt;While this might seem like a waste of time, it's important to have an interrupt handler as the default behaviour of the watchdog timer is to reset the microcontroller, which we want to avoid. It's also worth noting that, once enabled, the watchdog timer will keep biting, so the interrupt handler will be called repeatedly. (Put a print statement in the hander to see.) This doesn't cause any problems.&lt;/p&gt;</description><category>arduino</category><category>making</category><category>power management</category><category>project:ditch</category><category>sleep</category><guid>https://simondobson.org/2013/07/23/arduino-watchdog/</guid><pubDate>Tue, 23 Jul 2013 14:21:27 GMT</pubDate></item><item><title>API communications now working</title><link>https://simondobson.org/2013/07/06/api-communications/</link><dc:creator>Simon Dobson</dc:creator><description>&lt;p&gt;A small sensor network now working, with two edge devices talking to a base station.&lt;/p&gt;
&lt;!--more--&gt;

&lt;p&gt;&lt;img alt="Network" src="https://simondobson.org/images/citizen-sensing/network.jpg"&gt;&lt;/p&gt;
&lt;p&gt;This step of the project accomplishes two things: is gets API networking mode working for the XBee radios, and makes sure the the interaction between software on the Arduinos and software running on the base station work too.&lt;/p&gt;
&lt;p&gt;The data stream is simple enough: each Arduino counts up from 0 to 255 every 5s, passing the result up to the co-ordinator radio. A Processing program on the laptop collects the numbers and prints them. Naturally they become somewhat intertwined as their clocks aren't quite synchronised.&lt;/p&gt;
&lt;p&gt;Actually this is enough to perform a simple radio survey to check transmission distance: we can move the radios away from the base station until they lose contact (nominally 100m for these 2mW radios, in reality probably substantially less), then move back into range, and then move one of the radios again to check that it meshes with the intermediate node in reaching back to the base station. This will also check that battery power works.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Meshing" src="https://simondobson.org/images/citizen-sensing/processing-xbee-api.png"&gt;&lt;/p&gt;
&lt;p&gt;The software is quite straightforward, and the &lt;a href="https://simondobson.org/2013/07/03/xbee-arduino/"&gt;xbee-arduino library&lt;/a&gt; handles all the low-level communications -- although it's very low-level, fine for the experienced programmer but probably all but mystifying to anyone not used to this kind of software. The corresponding Java xbee-api library is slightly more friendly, but &lt;em&gt;only&lt;/em&gt; slightly: they probably both need wrapping into a framework that hides the radio nastiness.&lt;/p&gt;
&lt;p&gt;I think the biggest hurdle for this sort of system is the data format -- or, more precisely, the need (or desire, at least) to to use C at one end and Processing/Java at the other, which means that the data on the wire is being described twice. A framework approach could use (for example) &lt;a href="http://www.json.org/" target="_blank"&gt;JSON&lt;/a&gt;, although there'd still be a need to make sure it was compactly encoded and transmitted.&lt;/p&gt;</description><category>arduino</category><category>making</category><category>processing</category><category>project:ditch</category><category>wireless</category><category>xbee</category><guid>https://simondobson.org/2013/07/06/api-communications/</guid><pubDate>Sat, 06 Jul 2013 11:12:05 GMT</pubDate></item></channel></rss>