A lab notebook of my explorations with microcontrollers and other small, intelligent electronics. It exists mostly so I'll have a record of what I figured out and how I configured things, but it may help if you're trying to solve similar problems.

Tuesday, 11 February 2014

It would be nice to know the temperature in the greenhouses at Made in the Shade without having to be there. The obvious first solution step is a commercial remote monitor, good enough to read 300 ft away, which just manages to display temperatures from 3 different greenhouses on an LCD display if the base station is by the back window of the house. It doesn't do much good if you are off the property, and doesn't have an obvious way to hijack the readings from the base station. To get them off the property, we'll have to reacquire the temperature signals, preferably with a battery powered wireless gadget. The obvious solution is some kind of Arduino / XBee combination, except an UNO with an XBee attached consumes about 100 mA and would burn through an AA battery pack in a day or less.

Low Power Means Putting Things to Sleep!

Plugged in a new XBee on the FTDI cable and couldn't remember the right terminal settings (9600, Raw, Echo, then +++ with no return should get an OK.) I used ATID2711, ATMY0, ATDL1, ATBD6 to match the one at the sailboat end of the IOM controller so the TX can listen to either one at 57600 baud. Don't forget the ATWR to write it all out! Upgraded the IOM TX program to echo any serial it gets back on the XBee to the console.

Now to get a trinket talking to the XBee... I couldn't get software serial to work reliably on the trinket at 57600, so backed all the XBees down to 19200. I created SleepyTrinket based on the code from Nathan Seidle at https://github.com/sparkfun/H2OhNo/tree/master/firmware/WatchDogTest. That code uses pins 3 and 4 for the software serial, but I switched it to 0 and 1, then used pin 2 for the XBee sleep line, which leaves 3 (A3) and 4 (A2) for something analog useful. I cut the current draw on the trinket by 3 mA by breaking off the green power LED. The XBee sleeps when pin 9 is set high if sleep mode is enabled (ATSM1 for hibernate or ATSM2 for doze, plus ATWR).

I first powered the Trinket and the XBee directly on the 3V line from a 3.7 V 100 mAh LiPo, hoping to bypass any regulator losse, but that worried me a little so I switched to the regulated battery input. Either way, with everything awake it used about 60 mA and with everything asleep that fell to about 40 uA! That's about 1 mAh per day while sleeping and 1 mAh per minute while awake.

How quickly can I wake everything up, spit out a serial burst and go back to sleep? It works with delays in the code of 1 s, 500 ms, 100 ms, so 50 ms is probably fast enough with a total time around 200 ms per burst. To split the power consumption between awake and asleep would mean reporting about 300 times a day or about once every 5 seconds.

When I enabled the ADC and started reading a TMP 36 the sleep load came up to about 270 uA, or about 6 mAh per day, which would still get months on a 1300 mAh battery. Still, enabling and disabling the ADC each cycle brings the sleep load back down to about 60 uA. Better stability for the TMP 36 comes from a 2 M resistor and a 104 ceramic cap bridging the signal pin to ground. Based on these measurements, the little 500 mAh LiPo in the background should be good for more than 6 months between recharges. The next step is a little tidier packaging and something at the house end that will listen, repeat the data out to the world, and maybe send an SMS if alarm conditions come up.

A little more testing shows that the accuracy of the temperature measurements from this combo are a little dodgy, so time to track down where the error comes from. (Yes I know that the TMP36 spec says +-2C, but I want better ;-) )

/*Use with Adafruit Trinket 3V - remove green power LED - compile as trinket 8MHzabout 6 mA awake and 25 uA asleep, 10 mA if the LED is turned on for just the trinketsleep current goes up to about 1 mA if connected to FTDI cable2014-02-11Rick Sellens adapted from:

Thursday, 6 February 2014

select a transducer to measure something physical like temperature or illumination

download and install libraries to support components

connect neopixel smart RGB LEDs to your Arduino and change their colour and intensity under program control (some soldering required)

Use the neopixels to respond to the measured quantity, and more as time permits

or so I promised in the advertising. The simplest way to get some data would be following this lesson with a photocell. (Ignore all the parts about the LEDs for now and just make sure you can read an analog value that changes with the light. Take note of the extremes values for bright and dark. You can adapt the code at the bottom of this post.)Adafruit NeoPixels are smart RGB LEDs that let you control a whole lot of LEDs with a single data line from the Arduino, and you don't have to worry about all those little current limiting resistors (although, if you are playing with more than a couple of neopixels, you should pay close attention to the capacitor and power supply comments in the uberguide). The complicated part is you need to control that one data line in a fairly complicated way. Fortunately somebody else has already done the work and there is a software library to make it easy. Download the zip file of the library and expand it, then install it in the libraries folder of the Arduino IDE. (You may have to create the libraries folder -- it goes in the same place as your Arduino sketchbook folder.) Be sure to remove the "-master" from the library folder name so it matches the name of the library. Then restart the Arduino IDE so it can find the library.Put together a string of 2 or more NeoPixels (this will probably involve some soldering, either for the Flora or Breadboard versions), then hook them up to +5, Ground and pin 6 for a signal. Open the "strandtest" example in the NeoPixel library and set the number of NeoPixels you have in this line:Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, PIN, NEO_GRB + NEO_KHZ800);
When you run it you should see the pixels continuously changing colour and intensity independently of each other.Modify the code to have the pixels respond to the analog input reading from from the photocell.

Other Sensors and Responses

You could measure temperature, or temperature and humidity, or colour, or orientation, with one of these sensors and use that measurement to control the colours and intensity of the NeoPixels. Download and install the library for your sensor, test that you can read it with the example programs, then combine the code from multiple examples to read the sensor, then control the NeoPixels based on what you read.

Take it further and get your sensor inputs to control a servo-motor, maybe opening and closing a greenhouse vent in response to changes in temperature or humidity.

Coding Hints

Pick one of the examples as your starting place and save it with a new name so you don't break the example.

Copy the header code from the top of all the files with the #include and variable declarations.

Copy any functions other than setup() and loop()

Copy and paste code from each of the setup() functions to make sure everything gets started properly.

Copy and paste code from the loop() functions to do the individual things you want.