some projects

DIY Sleep Monitor / Logging EEG

leave a comment »

Figure 1. First try with Arduino sleep monitor. In the middle of the graph you can see when the MindFlex headset fell off (!) during the night, and when I put it back on. (Note the signal quality goes from 'fantastic' (near 0) to 'no signal' (200) at that point.)


Building blocks of this project:


Adafruit Data Logging Shield

Mattel MindFlex EEG toy headset

I based most of my work on Eric Mika’s “How to Hack Toy EEGs,” including using his Arduino Brain library to interpret the data from the MindFlex:


Arduino Brain Library (required if you want to duplicate this)

(Wire from the T pin on the NeuroSky board to the Arduino RX pin)

I did make one adjustment — I put a 6N138 optoisolator between the MindFlex headset and the Arduino. This means that not only is there no question of noise/ground loops from the Arduino messing with the EEG signal, but there’s also no longer the chance of frying my brain if I plug the Arduino into a wall-powered laptop.

6N138 connections are as follows:

Mindflex V+ to 6N138 LED anode (pin 2)
Mindflex T pin to 470 ohm resistor to 6N138 LED cathode (pin 3)
Arduino 5V to 6N138 Vcc (pin 8)
1K resistor between 6N138 Vb (pin 7) and GND (pin 5)
Arduino RX pin to 6N138 Vo (pin 6)
Arduino GND to 6N138 GND (pin 5)

After a lot of messing about with the demo code for the Data Logger Shield, I found an incompatibility between the Real Time Clock libraries and the Brain library. So I left out the RTC. Here’s the resulting Arduino sketch: http://pastebin.com/Ye43V8ca

That creates a LOGGERnn.CSV file on the SD card, with all the values from the EEG.

As a test, I used the Arduino/MindFlex as a sleep monitor last night, producing a 3.4MB CSV data file spanning ~9 hours of sleep.

To generate a graph from the CSV file with gnuplot, I created a command file <eeg.gpl> containing the following instructions: (remove the # to graph attention/meditation values, I found them superfluous)

set datafile separator “,”
set key autotitle columnhead
set autoscale x
set yrange [0:1500000]
set y2range [0:100]
set parametric

plot for [i=6:13] file using 1:i with lines smooth bezier, file using 1:3 with lines axis x1y2 smooth csplines #, for [i=4:5] file using 1:i with lines lw 2 axis x1y2 smooth bezier

I then called gnuplot from the command line with the following command:

gnuplot -e “file=’/Path/to/LOGGER00.CSV'” /Path/to/eeg.gpl

The resulting graph (generated through AquaTerm, since I’m on a Mac), looks like this:


In the middle of the graph you can see when the MindFlex headset fell off (!) during the night, and when I put it back on.


Written by 333dan

May 27, 2012 at 7:33 pm

Posted in Uncategorized

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

%d bloggers like this: