Sleep n’ Tweet

The Sleep n’ Tweet is basically just an Arduino with an Ethernet shield that keeps track of your vitals (movement + heart rate) as you sleep. I haven’t push it nearly as far as it could go, think of it more as a working prototype than a polished product – but the potential is huge! It could be used to track vitals at any time and tweet anything you want according to whatever filters you put on it. It could be manufactured to be very, very small. And it could also be stuck to just about anything to check for movement and IR or flow changes – but for now we’ll stick to sleep.

Now I am lucky enough to work at the greatest company in the world as a University of Waterloo, co-op student. It might not even be a company you’ve heard of yet. They’re named Upverter and we are trying to change the way people design, build, share and collaborate on electronics. My job (also the coolest job in the world) is to find interesting problems that either have been or could be solved with electronics. I showcase what we do at Upverter and what the community is building. And I not only publish and share my experiences but I also design the solutions to these problems and open source my experience, discoveries and my designs.  I’m helping to build the core content of a community sharing electronics. Like I said, its a pretty cool job.  So one day, here I am at work – doing the coolest job ever, when an email gets sent around the office about a twitter competition. And so I started thinking to myself… What is a problem that twitter and an Arduino can solve?  But its even more than that – what REAL problem can I solve, that hasn’t been solved before, or at least hasn’t been open sourced, using twitter and Arduino?  I’m gonna come back to this, but at just that very moment here I am deep in brainstorming thought and my co-worker Jaco starts boasting about what a great night sleep he got, how much he loves his Wakemate, yadda, yadda, yadda.

WAIT! It kinda goes without saying, but sleep is very important.  All of us need it and for the most part do it differently.  Problem is, technology still has a ways to go before we can detect exactly what is happening to someone while they sleep without ruining their sleep, or wiring them up like Frankenstien.  For example if you go to a professional to analyse your sleep you will be covered in sensors like EEGsEMGs or EOGs. The up side is they measure everything your body does while you sleep… But the down side is they are far from pleasant to wear to bed. Maybe there is something here I can make a little bit better…

As a result of bad tech, Actigraphy has become the more common method of getting a bit of insight into how people sleep and its really the only option when it comes to personal sleep tracking.  For Actigraphy there are no head or chest sensors but just a device worn around the wrist on the non-dominant hand. The device normally contains an accelerometer which is then used to determine what sleep state the user is in. All of this data is then synced to your mobile phone, which you can also then upload to their website to analyse your movements during the night. Worst case = a fuzzy wrist widgit, best case keep on going!

So now that I’ve bored you with the background – I can imagine you know what I’m gonna say next… Why not build my own? Anyone can go buy a WakeMate. But how many people have built their very own, designed in the cloud and open sourced to the world? Zero! Well, that’s not true… I guess its at least one now! haha. And for the haters – as for why to open source it? Because I believe that someday the innovation that becomes possible from me giving back to the community will affect my life in a positive way. Something that never would have been possible otherwise. And thus, the Sleep n’ Tweet was born.

STEP 1: How it works


Overview

So here I’m going to give you a rundown of how the device works and functions so you at least understand what you’re about to make. If you’re just keen on making your own you can skip this section (I promise I won’t be hurt if you do).  Now, I’m also gonna go all engineering lab report here, so prepare for the monotone… So this device uses the Arduino for its base.  The Arduino knows and does all once we program it. It doesn’t know a thing until we do that. On top of the base is the Arduino Ethernet shield which lets the Arduino talk to all the internets out there. Thus allowing the Arduino to communicate with Twitter with a little bit of programming.  Steaming from the Arduino are two sensor circuits; the accelerometer and the IR heart rate sensor. We’ll get to those in a minute though.

Arduino

For this project I used the Arduino Uno and an Ethernet shield on top of this.  You need to make sure that pins 12 and 13 (point to pins in picture) are open because these will be used for communications between the Arduino and the Ethernet shield. Which sucks because pins 12 and 13 are my favourite to use.  I don’t know what it is about them just like the numbers, but that took me 30 minutes to find out. So I used pin 9 to control whether or not the Infrared LED and red LED are active and I used pin 8 to read whether or not there was a change in the IR photo transistor. Pins A0, A1 and A2 were used to take care of the accelerometer.  For power, I connected the 5v pin to the heart rate circuit and connected the 3.3v pin to the accelerometer circuit. That is basically how I hooked up the brains of the device. Still plenty of pins open for other things, maybe a moisture sensor to see if you’re sweating buckets in your sleep. Or a pressure sensor to see if you’re crushing a limb for hours and hours every night.

Heart Rate Sensor

The heart rate sensor is comprised of an op amp connected to an IR LED and an IR photo transistor.  In short, the design works by shinning an IR LED onto your finger and receiving some amount of light through the photo transistor. When your heart pumps blood, the volume of blood in your finger will change, and so will the amount of light picked up by the photo transistor. This value change will be pretty small so we’ll need to kick the change up a couple of notches with an op amp.  The one I’ve set up should roughly multiply the signal by about 10 000 times.  Now what we can do with this amplified signal is pump it into the Arduino and read the changes as ones and zeroes. I’ve also hooked up a LED between the Arduino and the op amp so you can directly view the signal going into the Arduino. What I’ve done is measured the time in between heart rates and after 5 beats, I take the average to compose a beats per minute.  I also have filters in place in the program to rule out any unreasonable frequencies. For example, if the heart rate comes out to be 100ms between beats, I know this reading is false because the human heart cannot beat 600 times per minute. I made the valid range 30BPM to 250BPM. I found the IR sensor works on multiple parts of the body, but best on the finger.

Accelerometer

I used an accelerometer pre-assembled from Sparkfun called the ADXL335, but you can build your own which isn’t that hard.(The schematic is here) The accelerometer is directly hooked up to the analog pins of the Arduino. Constant readings are given about it’s orientation and I basically I detect the changes in the orientation and log them as movements. I had to do a couple of things in the program to combat irregularities such as twitches. I take a time frame in which the number of movements read is compared to the number of non-movements read. Then, I also change the sensitivity. So if you move by 5 it will detect a positive movement. Basically, more movement equals a higher change in number.

Put it all together and you have a way of reading someones heart rates and movements. Lets move on to what you actually have to get to build this bad boy.

Step 2 : Parts/Tools And Skills

To do this project you should have a basic knowledge of soldering and have access to soldering equipment or solder paste. You could just wear the breadboard to sleep too, but that might be a tiny bit uncomfortable. A glue gun would also be nice in case you need to strengthen or insulate anything. If you have electronics experience great, but it is not necessarily required to build your own. With these tools at my disposal I knew Jaco’s Wakemate would be rendered obsolete.  As long as I made the Sleep n’ Tweet soft and fuzzy.

I’ve put together the following list of materials in a spreadsheet which include price, where to get one, and what it is.  I ordered everything from digikey for consistency sake, but you can shop around and probably do it for cheaper than I did.

For the base you’re going to want to have something to build a base with for the heart rate monitor and sensor. I used a clear box I found laying around for the heart rate monitors case and I just used some Styrofoam which came with my Ethernet shield and electrical tape for the pad of the heart rate sensor kind of cheap, kind of ghetto, but what’s more awesome? You always want people to believe you built it yourself when they look at it.

A couple of notes on the parts list. I used a pre-assembled Sparkfun product for the accelerometer, I just so happened to have one lying around which probably won’t be the case for most of you so if you’re looking to save some money then you can get the schematic for it here. I’ve included both the materials to build it and the breakout board in the parts list.  For the IR sensor and IR LED, you can either buy separate LED’s and photo transistors or get a neat little combo pack which I found works better since you don’t have to mess about with spacing between the LEDs and sideways flying light. Enough talk anyways, here’s the URL for the list: https://spreadsheets.google.com/spreadsheet/ccc?key=0AiIbSAcpx0hkdEI3dEdpRE42SldEM2Vxc0Uxd0o0ZUE&hl=en_US

Step 3: Schematics

To make these I used the Upverter EDA tool – which is a pretty new and different, so I’m going to spend a minute explaining it.  You should really just check it out at upverter.com, but for lack of a better analogy its Google Docs vs. Microsoft Word.  There is a lot less feature bloat, it runs in your web-browser, its collaborative, its open source hardware focused, and its the first tool ever that allows me to share my designs on the web without resorting to PDFs.  I can actually take the designs I build in Upverter and embed them into other sites a’la YouTube – which is very, very cool. My friends can then play with my design, export a BOM, order the parts or even fork it and make their own changes, and then something cool is possible… What I’ve designed becomes a building block of future electronics rather than a dead end project in my basement. Now Upverter is far from finished, and there are a lot of features coming down the pipe, but its to the point where we have tons of users signing up, forking content, creating parts, and just generally designing cool electronics. You should really check it out, join up and help shape the future of electronics design. You can find the design file here.

Step 4: The Build

The parts list I’ve given you are the best combination I came across.  I encourage you to try and find a better one, but this one worked well for me. It’s a relatively simple circuit to build. The only thing that takes some time is setting up the heart rate monitor. One thing I found that worked perfectly was I had 8 connections going to the Arduino; exactly the amount of cables in a cat5 Ethernet cable. It’s no surprise what someone sleeping does when 8 small wires are hooked up to them. They rip them to shreds in the middle of their sleep. So I hooked up a female connector to my circuit so I could hook up an Ethernet cable between the Arduino and the sensor unit.  Make sure the cable is not a crossover cable.  This will cause many terrible things to happen.  I was reading the output from my accelerometer and was only getting zeros and was scared I fried company property and they beat us here. Turns out the cable I snagged was a crossover and it really messed with my head. I received a minor beating, but nothing too serious.

If you want to make sure your IR LED is working properly grab a camera.  The human eye is incapable of detecting IR light, but it will show up on any camera as a bright whitish purple.
This is what IR looks like through a camera

Utility Functions

The only utility I have is taking the 5 beat read times and averaging them then converting them to a beats per minute.

Twitter

This is where the twitter message is processed and sent. You are alerted if something goes wrong and given the error message from twitter in the serial monitor.  This is an important one because networks are always a pain. Also not twitter gets upset when you send duplicates. So when you’re heart is the same for two tweets in a row and you are or aren’t moving.  It will not tweet unless something has changed.

Sensors

In the check movement function you can see that past and present values are compared to see if there has been a change. The change being a movement of some kind. I have if statements set up so movement is detected in all three dimensions and I have a sensitivity set up so you can change exactly how much movement sets off the movement function. The number of times moved is then compared to the total number of movement reads and then a final verdict is passed whether there was or was not movement during the time period. These time periods are like windows. So if I move in the 30 second window, the Arduino will tweet that I have moved within 30 seconds. If I’m still, it will say I remain still until the next window. It’s also been filtered so going to itch your nose or things like that won’t be counted as total movement.

The heart rate sensor counts the amount of time between each heart beat for 5 beats and then averages them. The time between beats will not be recorded if it is outside the range of a human heart. Otherwise every twitch or touch of the sensor would register as a wild beat per minute value.  Unless you’re Lance Armstrong or a humming bird your BPM should be between 30 and 220. In studies done below 30BPM is extremely unlikely and over 250BPM usually means a doctor would like shock you with a defibrillator. This range can easily be changed in the program if you just so happen to be Mr. Armstrong. Or a humming bird squirrle man thing.

Output

This is where you will spend a lot of time if any mistakes are made.  I spent a couple of hours blankly staring at this window for many hours during this project.  I hope you do too, because if it works the first time you didn’t do it right.

The output simply takes all recorded information and outputs it to the serial console in the printStatus function. This is for any troubleshooting or inquiry.

The tweetStatus takes the information gathered from the sensors and tweets a message depending on what state you are in during your sleep.

Arduino Functions

Just the setup and the main loop. Pretty self explanatory.

Here’s the Code:

     #if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later#include <SPI.h>#endif#include <stdio.h>#include <string.h>#include <Ethernet.h>#include <EthernetDNS.h>#include <Twitter.h>int x, y, z;// Accelerometer readingsboolean movementHistory [5] = {false,false,false,false,false};// The time of the last heart beatunsigned long lastMovementReadingTime = 0;// The number of reads takenint movementReads = 0;// The number of times moving when readint movementCount = 0;int irSensorState = 0;// Heart rate readingsint pulseTime [5] = {0,0,0,0,0};// The time of the last heart beatunsigned long currentTime = 0;int SensorPin = 8;int LEDpin = 9;unsigned long lastPrint = 0;unsigned long lastTweet = 0;byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };byte ip[] = { 192, 168, 0, 250 };Twitter twitter("Put your token in here.");// Utility Functions//=============================int calcPulseRate(){int pulseRate;pulseRate = pulseTime[0] + pulseTime[1] + pulseTime[2] + pulseTime[3] + pulseTime[4]; //then takes 60/average to get BPMpulseRate = 60000/ (pulseRate/5);return pulseRate;}// Twitter//=============================void tweet(char msg){Serial.println("connecting ...");if (twitter.post(msg)) {// Specify &Serial to output received response to Serial.// If no output is required, you can just omit the argument, e.g.// int status = twitter.wait();int status = twitter.wait(&Serial);if (status == 200) {Serial.println("OK.");} else {Serial.print("failed : code ");Serial.println(status);}} else {Serial.println("connection failed.");}}// Sensors//=============================boolean checkMovement() //function compares old values to new ones and detects only if a certain ammount of movement is happening{boolean ret = false;int sensitivity = 3;int newX, newY, newZ;newX = analogRead(0); // read analog input pin 0newY = analogRead(1); // read analog input pin 1newZ = analogRead(2); // read analog input pin 2if(x-newX > sensitivity || x-newX < -sensitivity){ret = true;}else if(y-newY > sensitivity || y-newY < -sensitivity){ret = true;}else if(z-newZ > sensitivity || z-newZ < -sensitivity){ret = true;}{for(int i = 4; i>0;i--){movementHistory[i] = movementHistory[i-1]; //Updates the array so the first value can be changed}movementHistory[0] = false;if (10 <= ((movementReads/movementCount)100)){movementHistory[0] = true;}movementCount = 0;lastMovementReadingTime = millis();}{movementCount++;}movementReads++;x = newX;y = newY;z = newZ;Serial.print(" ");Serial.print(newY);Serial.print(" ");Serial.println(newZ);/// return whether there is movementreturn movementHistory[0];}int getHeartRate(){// Used to stor the status of the photo transistorint newState = digitalRead(SensorPin);if(newState != irSensorState){if(newState == 1){ //beat is 1 or 0 this if statment is only entered when a 1 turns to a 0 or a 0 to a 1for(int i = 4; i>0;i--){pulseTime[i] = pulseTime[i-1]; //Updates the array so the first value can be changed} This is the filter for the system. The heart-rate is not allowed to go above 250BPM or below 30BPM Other filtering methodes. Check if the value between beats is greater than a certain level. A big change doesn't work, weighted mean. delay stuff./if(millis() - currentTime > 240 && millis() - currentTime < 2000 /&& isMoving() == false/){pulseTime[0] = millis() - currentTime; //keeps the time between beats to a reasonable range to avoid confusion}}{currentTime = millis();}irSensorState = newState;}return calcPulseRate();}// Output//=============================void printStatus(boolean isMoving, int pulseRate){if (millis() - lastPrint >= 1000){// Print movement statusSerial.print("Movement: ");if (isMoving){Serial.println("True");}else{Serial.println("False");}if(pulseTime[4] == 0){Serial.println("Calibrating"); //if 5 readings have not been taken the device is still calibrating} else{Serial.print(pulseTime[0]); //Prints the dataSerial.print(", ");Serial.print(pulseTime[1]);Serial.print(", ");Serial.print(pulseTime[2]);Serial.print(", ");Serial.print(pulseTime[3]);Serial.print(", ");Serial.print(pulseTime[4]);Serial.print(", ");Serial.print("BPM: ");Serial.println(pulseRate);}lastPrint = millis();}}void tweetStatus(boolean isMoving, int pulseRate){char msg[140];//Is the length of time between tweets and other filters to decide which message should be sent to twitterif (millis() - lastTweet >= 300000) //5 minutes{if(pulseRate > 120 && isMoving){sprintf(msg, "Awake with a pulse rate of: %d", pulseRate);}else if(pulseRate > 120 && ! isMoving){sprintf(msg, "Exciting dream with a pulse rate of: %d", pulseRate);}else{sprintf(msg, "Sleeping with a pulse rate of: %d", pulseRate);}//updates the last time since a tweet was madelastTweet = millis();}}// Arduino Functions//=============================void setup(){delay(1000);Ethernet.begin(mac, ip);Serial.begin(9600);pinMode(LEDpin,OUTPUT);pinMode(SensorPin,INPUT);digitalWrite(LEDpin,HIGH); //Controlls weather the IR LED is on or off}void loop(){boolean isMoving = false;int heartRate = 0;isMoving = checkMovement();/ calculates the average heart rate in beats per minute according to differences detected by the IR tranistor/heartRate = getHeartRate();printStatus(isMoving, heartRate);/ sends a message to twitter depending on the users heart rate and movement/tweetStatus(isMoving, heartRate);}// Accelerometer state// Heart rate sensor status// Heart rate pins// needed for twitter// Ethernet Shield Settings// substitute an address on your own network here// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)//=============================/ Takes an average of the time between heart beats ///=============================/tweets the inputted message to twitter///=============================/ Checks if there has been a change in accelerometer position// Read the new values // Check for changes /if (millis() - lastMovementReadingTime > 30000)// Duty cycle = 10%movementReads = 0;if (ret)// Store new values/ Serial.print(newX);/ Reads the IR sensor and gets the time between heart beats /// There has been a change/if(newState == 0)// Store the new state// calculates the pulse rate at the end//=============================/ Prints all information to the serial monitor /// Print pulse status/ Finds the apprpriate message according to movements and pulse rate and sends the message to the tweet function /tweet(msg);//=============================/ The Arduino setup function // The Arduino run loop that checks the sensors and then sends a tweet depending on the status of the sensors// checks the accelerometer for changes too see if the device is moving or not// sends a message to the serial console/

Step 6: Casing

For the case I used a minty box I picked up from a hardware store near Upverter’s location.  I wish I’d found something that was a bit prettier, but I couldn’t find anything. I would have sewed a pillow case around it to make it soft, but I build electronics and can’t sew. I also wouldn’t have been able to show you guys if I had either. SoI cut a small slot in the front using a hacksaw so I could put the sensor through it.  I then hot glued a cat5 female jack on the side so the wrist unit could be connected to the Arduino using a cat5 Ethernet cable.

I started with just a header and connected the two using the bare wiring from the cable, but when I tested this the wires were broken and the connector was removed after sleeping with it.  I remember waking up tangled in cables because the header got disconnected which made me panic and break more stuff before quickly before falling back to sleep. So I decided on just using the RJ45 connector which is nice and secure with lots of glue. After that I modified another female connector to fit on top of the Arduino so I could hook the two up together.  Then I was done!

Lessons

I found that 15 minutes per tweet isn’t quite frequent enough. When you are looking over it the next morning, 32 lines just isn’t enough data to plot out 8 hours of sleep. This can easily be changed in the program under the tweet section. One thing that I need to change is using a more flexible wire for the sensor. The wiring I used doesn’t quite allow ones finger to bend which when half asleep makes me take it off which then makes my tweets duplicates resulting in me wearing the wristband to bed for nothing. With proper PCB manufacturing I’m sure the device could also be made much smaller and more comfortable to wear.

Final Thoughts an v2.0 ideas

The Sleep n’ Tweet worked well overall, but it could definitely use a couple of changes.  One thing I noticed was the subconscious removal of the heart rate monitor during the night.  I feel something a little more comfortable needs to be designed and implemented. As everyone who has worn it has told me. A couple of sewing lessons would solve this I’m sure.

A second cool addition would be to make the Arduino read tweets that say “WAKE UP!” or something along those lines.  This would then trigger a motor mounted on your wrist and wake you up.  Probably not the best thing to have if you’re looking for a good nights sleep, but a cool addition non the less.

You could also add a thermo sensor to the Sleep n’ Tweet as well as any other biological sensors to add to your data. If you also put a pressure sensor on the finger you would be able to neglect any readings that do not include pressure.  So the heart rate monitor would only be activated if someone was wearing the device.

Also no one really likes wearing wires to bed.  If I had a way to etch PCBs I’m sure the device could be miniaturized and therefore be more comfortable to the wearer.  Maybe slip it in a sweatband.

Lastly some people might find the blinking LED distracting while they are trying to fall asleep. Made me feel like a satellite when I was trying to go to sleep blinking like sputnik.  So maybe you could make the light switchable.  Give it an on mode for monitoring or and off mode for sleeping.

Those are just some thoughts I had, but the sky’s the limit.  So take this and fork it to pieces. It’s really easy to do in upverter just click fork design!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s