Sleep n’ Tweet Using Arduino

Sleep n Tweet

Step 1: The Sleep n’ Tweet

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 EEGs, EMGs 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…Sleep n Tweet

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! We have 12 more months of money and we need to use it to the max!

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.

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.


Step 2: How it works

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 having your own you can skip this section, I won’t be hurt if you do. I’m going to go all engineering lab report here. In 3…2….1….GO! 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. Steming 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.

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 4: Schematics

embed code: 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, 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 directly take a look at the schematic and edit it here:’-Tweet/.

Step 5: 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.Sleep n Tweet schematic

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.A couple of things I found were flattening the 1uF capacitors really lowered the profile of the device so I could get it into the plastic case I found. Without doing this the device will jab you when you sleep. I used a hack saw to trim the PCB down to a reasonable size so I could fit everything in the case and used some nice wiring to extend the reach of the IR sensor. 

I then soldered the components all together and that’s all there was to do. I hot glued the wires to the top of the PCB too so they wouldn’t break off. When I first had my boss sleep with it he mangled the wires and broke a few. So I lathered the hot glue on the connectors to Zak proof it. I was also fiddling around with the sensor when the wire snapped and it was a real pain to get it back on so I recommend gluing the top. Even though it doesn’t look the prettiest, it prevents a lot of pain, suffering and loss of time.

Step 6: Programming

The program has 5 main sections. All twitter libraries must be included for this to work. You can get the libraries and a sweet quick tutorial here .

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.

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.

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.

Read more: Sleep n’ Tweet

Leave a Comment

Your email address will not be published. Required fields are marked *