Photon SleepSense
Tags: #particle #photon #temperature #humidity #gyro #EKG #EOG #sleep #rem #I2C
The Photon SleepSense project brings together a number of the subcircuits in the MetaMorph Subcircuit Library to create a device that can be used to analyze the sleeping habits of a patient. We ordered a single prototype from MacroFab after it was designed by an intern this summer. When the board arrived, we developed a simple Photon firmware that interacted with each of the subcircuits and recorded data for later examination.
Design
The SleepSense incorporates a Tri-Axis Gyro, Humidity and Temperature Sensor, and three subcircuits based around the AD8232 bio-instrumentation chip. Two of the AD3282 subcircuits are being used together to perform an EOG which can detect eye movements. The other is being used to perform and EKG function and record the heartbeat of the patient. By examining data from all of the sensors, it is possible to get a very good picture of a night of sleep and to diagnose a sleeping difficulty.
All of the subcircuits where combined on the MetaMorph Photon Shield platform to allow for a Particle Photon to interface with the different subcircuits and send that data to the cloud. The design was completed using the MetaMorph Desktop Tools and a .zip file with the design files, including all Photon Particle code, is included in the resources section below.
It clocked in just above 5.25 square inches, even given its sparse layout. If you wanted to place all of the circuits on a smaller board to created a project out of it, you could easily fit everything in just <3? sq. in.>. It came ready to go right out of the box, so we plugged our Photon in and got started using it right away.
Photon Code
The Photon platform was chosen for its ease of data acquisition and ability to upload data to the cloud. The Particle platform includes a visualizer that allows us to see all the events that transpire in the course of a night. Programming the Particle Photon turned out to be pretty easy after we get our Photon connected to the internet and linked to our user account. <Should I include something about having to fix the broken GME connectors and an explanation of how that problem can be avoided?>
The Humidity and Temperature Sensor communicates with the Photon over I2C. So we set it up to simply publish data every 1 minute. Here are the snippets of this work:
<HTS code>
The Tri-Axis Gyro allows us to detect the movements of the patient throughout the night. We set the threshold of observation to <> and had it record every incident of movement about this threshold:
<Gyro code>
Lastly the EKG (heartbeat) and EOG (eye movement) monitor subcircuits all connect directly to analog to digital converters, so we simply recorded the data at a rate of around <100?>Hz and analyze this data in realtime on the photon to calculate the heartbeat of the patient. Here is the code that we used to send data over a Serial connection to plot with Processing.org plotter:
#define initial_value 2048 #define debounce_time 200 #define read_theta 400 #define bpm_theta 4 #define jump_limit 12 #define max_value 4096 double average = initial_value; int read = initial_value; int previousRead = initial_value; int rate = 0; float bpm = 60.0; long lastJump = 0; long jump=0; void setup() { Serial.begin(9600); } void loop() { //Do our analysis previousRead = read; read = analogRead(A2); //Adjust the running average average = ((average*read_theta)+read)/(read_theta+1); //Check for pulse in signal if out of debounce range if(millis()-jump>debounce_limit && read-previousRead>jump_limit) { jump=millis(); rate = 60*1000/(jump-lastJump); if(rate>bpm*1.5) bpm = (bpm*bpm_theta+rate)/(bpm_theta+1); lastJump = jump; } Serial.println(read); Serial.println(average+max_value); Serial.println(bpm+2*max_value); delay(1); }
Here is the code used for our plotter. It produces a trace of the EKG and a graph of the heart rate over time.
We had to design some simple code to store and analyze the heartbeat and EOG data. You can see these functions below:
Finally, we put the Photon SleepSense through its paces with a full night's sleep of recording. Here is what we were able to find!
<Pretty presentation of results>
Cost
The Photon SleepSense cost $140.81 to have manufactured from MacroFab.
- 3x SparkFun Single Lead Heart Rate Monitor - AD8232, 3x $19.95 = $59.85, 3x 1.45 = 4.35 sq. in.
- SparkFun Tri-Axis Gyro Breakout - L3G4200D, $49.95, 0.48 sq. in.
- SparkFun Humidity and Temperature Sensor Breakout - HIH6130, $29.95, 0.3 sq. in.
Total cost as SparkFun parts = $139.75
Resources
- Photon SleepSense Project Folder
- components/
- designs/
- Photon SleepSense.xme
- Photon SleepSense.ino