LilyPad Arduino – sensing ambient temperature

This project uses a temperature sensor and five LEDs sewn into a wrist warmer to respond to ambient temperature.

If the temperature drops below 12°C, the lights start fading in and out. If the temperature is 1°C or below, the lights twinkle.

This guide is written for Mac OS X.

(tl;dr: Wrist warmers are practical warming devices, they can also tell you when it’s cold enough to wear them, through the application of pretty twinkling lights.)

What you’ll need

The following LilyPad Arduino components. I’ve used Proto-Pic to buy these components.

These LilyPad components come bundled in the LilyPad e-textile Beginner’s Kit.

     

  • A LilyPad Simple board
  • Temperature Sensor
  • 5 white LEDs
  • 110mAh Lithium Polymer Battery
  • LilyPad FTDI Basic (for connecting to computer using a USB cable – the rectangular component in the first picture above)
  • Conductive thread
  • Sewing needle

Additional components

I used Maplin and my existing sewing / electronics kit stashes for these.

         

  • Crocodile clips
  • Mini B USB Lead
  • Multimeter (I used a digital multimeter)
  • Scissors

Getting started: downloading software

1. Download the Mac OS X software from Arduino.cc. Unzip and copy Arduino.app to your Applications folder.

2. Next, you’ll need to install the USB drivers. The instructions on the Arduino site suggest that these come with the package you’ve just downloaded – they don’t, but can be downloaded from ftdichip.com.

3. Download the Mac OSX 2.2.16 drivers (they’re all the same), and mount the disk image (double click to mount the disk).

4. Install FTDIUSBSerialDriver_10_4_10_5_10_6.mpkg (this seems to work with Mac OS 10.7 too)

Using the Arduino software

1. Connect your computer to the LilyPad Arduino. Attach the FTDI Basic clip to the Lilypad and plug in the USB cable form this to your computer.

2. Open the Arduino software in your Applications folder.

Your window will look something like this (click to see large version):

3. Select your board type. For the Lilypad simple board , it’s ‘Lilypad Arduino w/ ATmega382‘. Go to Tools > Board in the Menu:


4. Now select your serial port. It should be ‘/dev/tty.usbserial-A900J3D6′. Go to Tools > Serial Port in the Menu:

5. Programs are called ‘sketches’ in the Arduino software. You can create a new sketch in the Menu bar from File > New, or open previous sketches from File > Sketchbook.

Arduino sketches are written in C. If you’re new to coding in C (like me) there’s lots of code you can grab to play around with. I’d really recommend playing around with the examples available in the Arduino software (look in File > Examples on the Menu bar) to get started. There’s also some great examples of code to look at on different websites. Try Leah Buechley’s Arduino Introduction and Ladyada’s Arduino Tutorials.

Coding the wrist warmer for responding to ambient temperature

1. Here’s the code I wrote to sense and respond to ambient temperature. Copy and paste this in to your sketch. Feel free to play with, extend and reuse this code to create your own stuff. Try changing the temperatures:

[c]

/* Aly Fielding / @alysonf / http://alysonfielding.com Temperature sensor – flashing and blinking lights
with big thanks to Ladyada’s light temp code. See: http://www.ladyada.net/make/logshield/lighttemp.html
*/

#define aref_voltage 4.08         // This is a reference voltage for your power supply: as 4.08
//but measure it with a multimeter when running and add in the correct voltage.

// ‘int’ is short for ‘integer’ = whole number

int tempPin = 18;        // the analog pin is connected to temp sensor
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
int tempReading;        // the analog reading from the sensor

int lightPin1 = 5;       //put the LED light on pin 5
int lightPin2 = 6;       //put the LED light on pin 6
int lightPin3 = 9;       //put the LED light on pin 9
int lightPin4 = 10;      //put the LED light on pin 10
int lightPin5 = 11;      //put the LED light on pin 11

int groundPin1 = 16;    //setting 16 to be another – (ground) pin
int groundPin2 = 17;    //setting 17 to be another – (ground) pin
int positivePin = 19;   //setting 19 to be a positive pin.

int brightness1 = 0;    // how bright the LED is
int fadeAmount1 = 5;    // how many points to fade the LED by

int brightness2 = 25;    // how bright the LED is
int fadeAmount2 = 5;    // how many points to fade the LED by

int brightness3 = 45;    // how bright the LED is
int fadeAmount3 = 5;    // how many points to fade the LED by

int brightness4 = 70;    // how bright the LED is
int fadeAmount4 = 5;    // how many points to fade the LED by

int brightness5 = 90;    // how bright the LED is
int fadeAmount5 = 5;    // how many points to fade the LED by

void setup(void) {
// To send debugging information via the Serial monitor
Serial.begin(9600);

pinMode(lightPin1, OUTPUT); //Setting LED 1 (lightPin1) to be an output. Ditto for each of the other 5 LEDs
pinMode(lightPin2, OUTPUT);
pinMode(lightPin3, OUTPUT);
pinMode(lightPin4, OUTPUT);
pinMode(lightPin5, OUTPUT);
pinMode(groundPin1, OUTPUT);
pinMode(groundPin2, OUTPUT);
pinMode(positivePin, OUTPUT);
digitalWrite(groundPin1, LOW);    //digitalwrite is ‘set the value of this pin to be …. LOW = minus , HIGH = positive’
digitalWrite(groundPin2, LOW);    //digitalwrite is ‘set the value of this pin to be ….’
digitalWrite(positivePin, HIGH);  //digitalwrite is ‘set the value of 19 to be positive
}

void loop(void) {

tempReading = analogRead(tempPin);  //Get a temperaure reading from the temp sensor

Serial.print("Temp reading = "); // allow the temp reading to appear in debugging > Serial Monitor
Serial.print(tempReading);     // the raw analog reading

// converting that reading to voltage, which is based off the reference voltage
float voltage = tempReading * aref_voltage;
voltage /= 1024.0;

// print out the voltage
Serial.print(" – ");
Serial.print(voltage);
Serial.println(" volts");

// now print out the temperature
float temperatureC = (voltage – 0.5) * 100 ;  //converting from 10 mv per degree with 500 mV offset
//to degrees ((voltage – 500mV) times 100)
Serial.print(temperatureC);
Serial.println(" degrees C");

// now convert to Fahrenheight
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.println(" degrees F");

if (temperatureC >= 12) { // above 12C the light goes off

digitalWrite(lightPin1, LOW);
digitalWrite(lightPin2, LOW);
digitalWrite(lightPin3, LOW);
digitalWrite(lightPin4, LOW);
digitalWrite(lightPin5, LOW);

}

else if (temperatureC >= 1) { //below 12C and above or equal to 1C, the lights fade in and out

// set the brightness of all five LEDS:
analogWrite(lightPin1, max(brightness1, 0));
analogWrite(lightPin2, max(brightness2, 0));
analogWrite(lightPin3, max(brightness3, 0));
analogWrite(lightPin4, max(brightness4, 0));
analogWrite(lightPin5, max(brightness5, 0));

// change the brightness for next time through the loop:
brightness1 = brightness1 + fadeAmount1;
brightness2 = brightness2 + fadeAmount2;
brightness3 = brightness3 + fadeAmount3;
brightness4 = brightness4 + fadeAmount4;
brightness5 = brightness5 + fadeAmount5;

// reverse the direction of the fading at the ends of the fade:
// Also allow the brightness down to -20 to make the LED fade to black for longer
if (brightness1 == -20 || brightness1 == 100) {
fadeAmount1 = -fadeAmount1;

}

if (brightness2 == -20 || brightness2 == 100) {
fadeAmount2 = -fadeAmount2;

}

if (brightness3 == -20 || brightness3 == 100) {
fadeAmount3 = -fadeAmount3;

}

if (brightness4 == -20 || brightness4 == 100) {
fadeAmount4 = -fadeAmount4;

}

if (brightness5 == -20 || brightness5 == 100) {
fadeAmount5 = -fadeAmount5;

}

}
else //If temp drops below 1C, the lights will change status to blink randomly (in sequence)

{

digitalWrite(lightPin1, HIGH);   // set the LED on
delay(random(100, 400));              // blink randomly
digitalWrite(lightPin1, LOW);    // set the LED off
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin2, HIGH);   // set the LED on
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin2, LOW);    // set the LED off
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin3, HIGH);   // set the LED on
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin3, LOW);    // set the LED off
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin4, HIGH);   // set the LED on
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin4, LOW);    // set the LED off
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin5, HIGH);   // set the LED on
delay(random(100, 400));               // blink randomly
digitalWrite(lightPin5, LOW);    // set the LED off
delay(random(100, 400));               // blink randomly

}

}

[/c]

2. Save the sketch – this will save your sketch into a folder called ‘Arduino‘. When working on the sketch above, I saved each new version out as ‘temperature_v1‘ ‘…_v2‘ etc, so I could refer back if I made a mistake and/or wanted to revert to an earlier model.

3. You’ll need to Verify the code before you upload to your LilyPad Arduino. To do this, click the tick icon at the top of your sketch:

4. Now you’re ready to Upload to your Lilypad. To do this, click the right arrow at the top of your sketch (next to the Verify button).

Testing your design

One of the best pieces of advice I read was to use crocodile clips to test your design before sewing. DO THIS. Really. It’s worth it to save possible tears and frustration later on.

For my code, here’s the diagram for wiring. Just connect the clips to each component. Several components can share the same ground (e.g. several clips can go to the same minus pin). Click for a larger image.

And here’s the real world one:

(Note in this picture, I’d connected the LEDs to both ground pins 16 and 17. When I came to sewing, it just looked prettier connecting all the LEDs  to pin 17 only. But the code works for either way).

Also, it’s really important at this stage to use the multimeter to test the voltage. Connect up the battery and place one of the multimeter’s testing prongs on pin 19 (which is set to positive) and the other on the minus pin (-). Take the reading you get and check if it’s the same as the one in the code:

[c]

#define aref_voltage 4.08

[/c]

Mine was 4.08 as above, but change this if you get a different reading. Just replace #define aref_voltage 4.08 to whatever reading you get. If you need to change, Verify and Upload your code again to your LilyPad.

You can also debug by checking the temperature changes in the Serial Monitor – this will show the temperatures your temp sensor is detecting.

          

All happy and working? Good-o. Time to sew your design.

Sewing your design

Use the sketch above to plan out your design on your wrist warmer. There’s a good guide to sewing which is worth reading first. Two important points:

  • Sew the conductive thread several times over the pins to make a good connection.
  • It’s important not to cross the streams threads. This will make badness happen: your design will not work.

1. Unplug the USB leads and detach the LilyPad FTDI Basic. Remove the crocodile clips.

2. Work out how best / where to place the battery. Attach this to the LilyPad.

3. Sew all the components as per the design.

4. When everything’s done, test it works again. Chuck the wrist warmer (with battery) in the freezer for a few minutes to debug. Remember to change the switch on the LilyPad to On if it’s not already.

It works? Excellent news.

4. Have a cup of tea and Tunnock’s Teacake. Put the wrist warmer on. Head out for a walk in the snow and watch the pretty LED lights twinkle and fade in your hand.

2 thoughts on “LilyPad Arduino – sensing ambient temperature

  1. Pingback: Interfacing and Programming Temperature Sensors – Tutorials and Resources - Into Robotics

  2. Thanks for this well documented Tutorial. I just started hacking with a Lilypad and this was super helpful. Thx.

    Like

Comments are closed.