Sleep Monitoring Prototype with Arduino and Raspberry Pi

Our daughter, Kate, is 3.5 years old. It is always a challenge for us to send her to sleep. It’s really hard for her to slow done. She always wants to have more fun or hear more stories instead of sleeping. And even when we succeed with making her fall asleep, it often happens that she wakes up in the middle of the night and migrates to our bed. My wife believes that it is caused by some micro-environmental changes in her room: humidity, temperature or something else. It sounded like a challenge for a geeky dad, and I’ve decided to build sensor board that would be collecting certain metrics in her room throughout the night.

The Idea

I already had Raspberry Pi that I might use for this project and luckily, Santa brought me an Arduino Uno last year. So, after googling a bit around, I’ve decided that I would build a sensor board powered by Arduino and it will be sending all readings to Raspberry Pi over USB to provide persistent data storage and motion triggered video recording. I’ve also bumped into this post that really helped with some pieces of code and developed my idea.

The sensor/parts list

Working with sensors

As I’ve mentioned already, I want Arduino to send sensor data over USB to Raspberry Pi. On the development phase, I’ll be connecting Arduino to my laptop to push code to it and for debugging purpose. When you open Arduino IDE, there’s a Serial Monitor button on the right top side of the window. After I upload code, I open serial monitor and should see sensors data being sent by Arduino.

Sample Serial Monitor Output - Arduino IDE

Sample Serial Monitor Output – Arduino IDE

 

Lighting sensor

For lighting sensor I’m using a photo resistor. Working with analog sensors with Arduino is real fun. You just need a pulldown resistor and a sensor.

Sleep Monitor Arduino Photoresistor  Schematic Diagram

Sleep Monitor Arduino Photoresistor Schematic Diagram

Sleep Monitor Arduino Photoresistor Breadboard

Sleep Monitor Arduino Photoresistor Breadboard

Sample sketch that sends lighting sensor readings over USB.

 

Vibration sensor

This is a second sensor that I’m adding to my prototype. I’m suing a MEAS Piezo Vibration sensor. It is basically a piezo element, that I hope will be sensitive enough to capture motion of body in bed. This sensor should help me understand if my daughter has restless minutes before she wakes up. This is an analog sensor, working with it is as easy as with lighting sensor.

The tool that I use for schematic diagrams and breadboard prototyping did not have MEAS elements, so I’ve replaced it with standard piezo speaker.

Arduino with photo-resistor and MEAS Vibration sensor

Arduino with photo-resistor and MEAS Vibration sensor

Arduino with photo-resistor and MEAS Vibration sensor

Arduino with photo-resistor and MEAS Vibration sensor.

Code is growing as I’m adding more sensors to the project:

 

Room Temperature and Humidity

Room temperature and humidity are going to be monitored using DHT11 sensor.  The one that I’ve got, already has a pull-up resistor on it’s board, so I just had to power it up and connect the data pin with Arduino digital pin. This is the first digital sensor in my prototype. Make sure that you download DHT11 library and import it to your Arduino IDE.

 

Arduino Uno with photoresistor, vibration sensor and DHT11, Schematic Diagram

Arduino Uno with photoresistor, vibration sensor and DHT11, Schematic Diagram

 

Arduino Uno with photoresistor, vibration sensor and DHT11, Breadboard Diagram

Arduino Uno with photoresistor, vibration sensor and DHT11, Breadboard Diagram

Adding code sections for DHT11. It’s worth mentioning, that the first time you access DHT11 sensor, it might return 0 value for humidity. In future sketches  you can see that I’m doing DHT11 readings in setup section, so that I get 0 at initialization stage and have real values when I need them.

Here’s the sample output of the Serial Monitor for the three first sensors. The first value is lighting percent, second – vibration sensor, third – humidity, fourth – room temperature in Celsius.

Sample Serial Monitor Output for Arduino Sensors

Sample Serial Monitor Output for Arduino Sensors

 

 

PIR Motion Sensor

If my daughter does wake up in the middle of the night, I’d like to capture video or pictures for further analysis:what if cat is bothering her, or maybe she acts as if she has a nightmare? To achieve that, I’m planning to use PIR motion sensor, similar to what is used in security alarm motion sensors. This is a digital sensor, that has three pins – Vcc, ground and out.  When it detects motion, it sets the out pin to high. Nothing complicated for an Arduino.

Arduino with PIR sensor, Schematic Diagram

Arduino with PIR sensor, Schematic Diagram

 

Arduino with PIR sensor, Breadboard Diagram

Arduino with PIR sensor, Breadboard Diagram

 

At this stage I’ve done some major changes to my Arduino sketch. When I got to motion sensor, I’ve realized that I want to check motion sensor status as often as possible, but I don’t actually need all other environmental sensor readings that often. So I’ve added a loop counter into my code, so that motion sensor is checked every 100ms and if motion state has changed, then it alerts to serial port. All other sensors, are scanned in every 300th cycle (every 30 seconds), and the summary is sent to serial port.

I’m also reading vibration sensor value every 100ms and outputting the average value every 30 seconds.

Bed temperature sensor

The last sensor for my prototype is the bed temperature sensor. This is a DS18B20 digital temperature sensor on a long cable, so that I could put it under bed sheet.

Arduino with DS18B20 Temperature sensor, Schematic Diagram

Arduino with DS18B20 Temperature sensor, Schematic Diagram

Arduino with DS18B20 Temperature sensor, Breadboard Diagram

Arduino with DS18B20 Temperature sensor, Breadboard Diagram

Arduino Uno and breadboard with all sensors

Arduino Uno and breadboard with all sensors

For DS18B20 you would also need to download and import additional Arduino IDE library that is called OneWire. Here’s the final sketch that works with all sensors:

Below you can see the sample data that I see in Serial Monitor. Every time there’s a motion detected, Arduino sends M1 signal over serial interface, when motion stops, it sends M0. Every 30 seconds it also sends the data from all sensors in such order: lighting, vibration, humidity, room temp, motion state, bed temp.

Sample output of Arduino sensor board

Sample output of Arduino sensor board

Now that sensor values are collected, I have to store the data in Raspberry Pi filesystem and start video recording when M1 signal is received.

Raspberry Pi Configuration

Raspberry Pi B+ with IR Camera Module

Raspberry Pi B+ with IR Camera Module

Communication with Raspberry Pi  is made over USB. I’m going to use noIR Camera Module to capture images at night when motion is detected. To help camera “see” at night, I’m using an IR LED lamp, that will be pointed to the ceiling and using it as a diffusor.

IR LED Lamp

IR LED Lamp

On initial setup, after camera is installed and Pi is booted up, you have to enable camera support. For that, run:

and select Enable camera -> Enter -> Finish and then reboot the Pi.

When it’s back up, you should be able to test camera with simple commands like:

It will capture a single frame into cam.jpg file.

Pi-Arduino USB Integration

In order to let my python script listen to USB, I had to install pyserial library.

To do that, download the latest version of pyserial from https://pypi.python.org/pypi/pyserial, unpack it and run python setup.py install as root.

Here’s my script:

It’s fairly simple:

  1. Connect to serial port
  2. Read a line from port
  3. If it is M1 – start capturing 640×480 images
  4. If it is M0 – stop capturing images
  5. If it’s anything else, then it’s the sensors summary, save it to cvs file.

I also pre-created /home/pi/stream directory to store all image captures.

Making it all work together

This prototype is not too much power consuming, so Arduino can be powered directly from Raspberry Pi USB port . As soon as you plug Arduino in, it will start collecting environment metrics. I launch my python script in screen on Raspberry Pi. Every 30 seconds I get a new record in sleep-monitor-data.csv file.

After data is collected, I can build a chart in MS Excel. Here’s a sample chart for one night measurements of humidity, two temp sensors, light sensors and motion. You can see how the temperature and humidity were rising, while the room was closed, and how it dropped when Kate woke up and left her room. Still have no idea what made her wake up, but maybe with more data I’ll be able to figure it out.

Sleep Monitor Example Chart: Humidity, temperature, Lighting, Motion

Sleep Monitor Example Chart: Humidity, temperature, Lighting, Motion

And here’s the sample image capture, triggered by motion sensor – our cat checking out Kate’s bed in the middle of the night.

Cat captured on motion-activated Arduino-Pi camera

Cat captured on motion-activated Arduino-Pi camera

It looks night-vision green, but I have no idea why is it happening. Sometimes images are greenish, sometimes close to black and white.

Conclusions

  • Arduino is awesome!  This is my first Arduino project. It was so easy to work with all the sensors. Like programming on C. I had some experience with Microchip PIC controllers, but those had to be programmed on Assembler. I’m sure it would have taken me months to implement same project on PIC.
  • Raspberry Pi is a nice thing. It gives you linux flexibility wherever you need it. It’s consuming about 300-350mah and thus can be battery/solar powered. Gives you tons of opportunities.
  • Breadboards are good for proof of concept builds only. All those small wires and sensors sticking out of it are not reliable at all and won’t last a second if kid gets to them.
  • In my project, Arduino is connected to Raspberry Pi with a cable, and can’t be too far away. The problem is that you need Arduino sensor board to be as close to monitoring subject as possible, but Pi with camera should be in a distance, to provide a better camera viewing angle.
  • I’m already thinking about more reliable hardware implementation of this project that could be running in Kate’s room on permanent basis.