LightBook: a movement-sensing book

This project is to create a bedside light that can switched on or off by moving a book. It uses an accelerometer sewn into the book to measure the book’s movement.

When the book lies flat, the LED lights are off. When the book is tipped up above a 20 degree angle in any direction, the lights fade up to full. To keep the lights on, stand the book upright, or prop up on an angle. To turn the lights off, lie the book flat.

What you’ll need
I used Proto-Pic for the LilyPad Arduino components.

  • A LilyPad Simple board (shown with LED lights in the first picture)
  • 6 white LEDs
  • Accelerometer (the middle picture above)
  • 110mAh Lithium Polymer Battery
  • FTDI Basic (for connecting to computer using a USB cable – the rectangular component in the first picture above)
  • Conductive thread
  • Sewing needle

Additional components

  • Crocodile clips
  • Mini B USB Lead
  • Scissors
  • A hardback book
  • Scalpel / Stanley knife
  • Pencil
  • Bradawl
  • Metal ruler (or similar to help cut out pages)

If you’re new to LilyPad Arduino, see the sensing ambient temperature project for a guide to getting started and setting up the Mac OS X software.

[Update 26/03/12: Here’s a separate guide to setting up the Arduino software for Mac OS X]

Coding the book

Here’s the code I wrote for the LightBook project. Feel free to play with, re-use and extend this code to create your own stuff.

[c]

/*
LightBook code V1. Alyson Fielding / @alysonf / alysonfielding.com

With thanks to David A Mellis’ Analog Devices ADXL3xx code (modified by Tom Igoe) – Arduino Tutorial:

http://www.arduino.cc/en/Tutorial/ADXL3xx

*/

// these constants describe the pins. They won’t change:
const int xpin = A2;                  // x-axis of the accelerometer
const int ypin = A3;                  // y-axis

int lightPin1 = 5;       //put the LED light on pin 5
int groundPin1 = 6;    //setting 6 to be another – (ground) pin
int lightPin2 = 9;       //put the LED light on pin 9
int groundPin2 = 10;    //setting 10 to be another – (ground) pin
int val = 0;
int xReading; // reading the xpin – on the x axis
int yReading; // reading the ypin – on the y axis
int xdiff; // how far away this value is from the flat book
int ydiff; // how far away this value is from the flat book
int maxdiff; //the biggest difference of xdiff and ydiff

void setup()
{
// initialize the serial communications:
Serial.begin(9600);

pinMode(lightPin1, OUTPUT); //Setting LED (lightPin) to be an output.
pinMode(groundPin1, OUTPUT);
digitalWrite(groundPin1, LOW);

pinMode(lightPin2, OUTPUT); //Setting LED (lightPin) to be an output.
pinMode(groundPin2, OUTPUT);
digitalWrite(groundPin2, LOW);

}

void loop()
{

xReading=analogRead(xpin);
yReading=analogRead(ypin);

xdiff=500-xReading;
ydiff=500-yReading;

xdiff=abs(xdiff);
ydiff=abs(ydiff);

maxdiff=max(xdiff,ydiff);

if (maxdiff < 20){
analogWrite(lightPin1,0);
analogWrite(lightPin2,0);
}

else if (maxdiff >80){
analogWrite(lightPin1,100);
analogWrite(lightPin2,100);
}

else {

analogWrite(lightPin1, (maxdiff-20) * 1.6666);
analogWrite(lightPin2, (maxdiff-20) * 1.6666);
}

// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("t");
Serial.println();
// delay before next reading:
delay(100);
}

[/c]

Save the sketch and verify before uploading (more about how to do this in the sensing ambient temperature project).

Here’s my design for wiring. Click for a larger image.

Sewing the book

Find a good hardback book that a) will stand up unsupported in portrait and landscape and b) you don’t mind altering for the project. I had a scoot around a second hand book shop for a suitable book and settled on Plays and Longer Poems by Percy Bysshe Shelley, published by Everyman’s Library in 1936.

Draw a quick pencil sketch to mark out where the components will fit in the book. The LilyPad and accelerometer will sit on the inside of the cover, with the LED lights on the front cover of the book.

Then on the first page of the book, sketch out a mirror image of the LilyPad and accelerometer components and draw a rectangle around this (making sure there is extra space for the battery).

This space will be dug out into the pages of the book, to give space for all the components and allow the book front cover to lie flush with the pages.

Next, position your LED lights on the front cover of the book, to work out where you want them to sit. Once decided, mark out in pencil. Then take your bradawl and – be brave – punch through the twelve holes needed for the lights on the front cover.

Fit the LilyPad and accelerometer in place with tape (this is to raise it up slightly from the surface of the inside cover). You can glue or tape down further later if needed.

Sew in the components – I found it easiest starting with the accelerometer and leaving the LEDs until last. Tip: double check the polarity of the LEDs is the right way round as you sew them onto the front cover through the holes you made.

Make a good connection over the pins by sewing the thread around several times. Also, it’s important not to cross any threads. I used a bit of tape to hold some of them down to make sure they weren’t going to touch anything else.

When it’s all sewn in, connect the battery and check it all works. Debug if necessary in the Arduino software by going to Tools > Serial Monitor while the sketch is running.

Finally, use a scalpel or Stanley knife to cut out the rectangle through the pages in the book. You won’t need to cut all the way through the book (unless it’s fairly thin) – just enough to fit the components so that the book can close fully.

Enjoy your LightBook!