Updated: Jun 23, 2022
Following on from looking at commercial products for sleep monitoring using EEG, I thought I’d take a look at more DIY based approaches using some open source sensor boards and a basic microcontroller.
There are a few options I found for this but there isn’t many:
This is quite a classic module in the EEG space as it’s one of the first that was manufactured to support commercial BCI products and toys. Over the years there’s been a lot of DIY projects made using this board.
The issue I have is that the chip on the board is kind of a black box because the signals that come out of it are already digitally processed – although in a roundabout way you can obtain the raw signal too. You wouldn’t really learn much and the options to tweak anything at a hardware level are limited. I do have one of these boards and they are incredibly small, which is great for making a headset but if your soldering isn’t great you might struggle with connecting the relevant wires. I’ll still probably use this at some point but for now I wanted something that allowed for more lower level control.
You can find loads of these TGAM boards and kits on things like Alibaba and eBay for very reasonable rates but shop around because prices do vary.
From a research perspective, this website is fantastic, some really interesting overviews of circuits and materials for building sensors etc. You can purchase a ready built module based on the principles outlined on the site for 99 Euros:
The problem with this setup is it uses a USB port as output so for sleep analysis and keeping all the cabling together it’s going to be cumbersome. Also all the electronic components aren’t on the small side so the overall scale of this thing could be an issue.
Mikroe EEG Click
There’s not much documentation on this board or any examples of what people have done with it but the specs looked great. It’s very similar in principle to the OpenEEG schematic and uses the same instrumentation amp. I love the fact that it has a 3.5 jack so you can easily switch out sensors, which would be handy for trying different setups. I also like the modular approach in that it contains everything up until getting the raw signal and so you can use whichever microcontroller you want that has the relevant Analog to Digital processing. I’m not sure why I can’t see anyone having used these, maybe it’s the pricing or lack of documentation – EEG can get complicated. I think what might have helped Mikroe is if they had this as part of kit then it would be easier for users to put together and get using. It also has the relevant bandpass filtering and an onboard POT to adjust the sensitivity.
A key part of these circuits are instrumentation amplifiers, and these are ultimately made from op-amps. You can create your own instrumentation amp then from op-amp chips but accuracy and stability on these will never be as good as dedicated chips like the Texas Instruments INA114. I think the Muse band actually uses this kind of approach using precision op-amps so it might be ok for sleep analysis or even better in their setup - I don't know. I’ve also seen some cheaper boards the claim to offer EMG and EEG sensing but when you look at the components they’re using, the signal to noise ratio is not going to be great at all for EEG.
OpenBCI boards, such as the ganglion look amazing but just a bit overkill for this project and I think there are more cost effective approaches to start with.
I decided to get the EGG Mikroe Click and then the next thing was to select a microcontroller. I wanted to avoid Bluetooth and wifi to limit any interference issues and ideally just use onboard storage to save the RAW signal.
The easiest thing to do would be to use a dev board that supports the Mikrobus format that the EEG Click uses, you can see some examples here:
These all look quite big though and I’ve got some boards at home already, which would save on costs. I had a Feather M0 Adalogger around from a previous project so decided to go with that. By all accounts the ATSAMD21 chip has a decent ADC and the SD card meant I can record and process data between the board and my PC easily. It’s also pretty small and has a connector for a battery so can be built into a wearable pretty easily.
One of the problems with interfacing the EEG clickboard with the Adalogger is that the Mikroe board outputs a 5V signal, which is too high for what the ATSAMD21 inputs can handle. To avoid any issues I used a resistive voltage divider to bring the signal back down to around 3.3V max. There may be other ways around this but I went with the easiest approach I could make sense of.
Voltage divider values I used:
If you wanted a perfect signal at the end I’d say just find a board with a chip that takes 5V inputs. I like everything Adafruit put out so there might be another Feather or something similar that has a better MCU – they update new things a lot. You could also use an external dedicated ADC but then you’d have a bunch more extra wires and would not be making use of an already fairly decent ADC onboard the ATSAMD21.
The next stage was deciding on materials for the EEG sensors. After a bit of experimenting I went with using gold cup electrodes, which are easy to find online these days. I’ve seen people make electrodes from copper coins and other things – these will all be poor in practice because of how they react once you sweat both short and long term – stick with gold plating or silver.
The Mikroe board takes 3 inputs – though note that it is still just for a single channel. The board uses an instrumentation amplifier so 2 of these distinguish between voltage changes that are incredibly small – such as EEG. The 3rd electrode is used for noise reduction and primarily reducing mains line noise:
I highly advise reading up the OpenEEG website and some of the docs there for a better insight into how this kind of thing works.
In a more ideal situation for EEG, you could keep the positive and negative electrodes on your upper forehead and then the 3rd reference electrode would be clipped to your ear lobe or behind the ear – the reason for this is there’s less noise from the EMG in muscles likely to interfere with anything there. However sleep is not an ideal world because you will move around somewhat, which will affect noise so I opted to just set the electrodes out all across the forehead with some adequate spacing. There are trade-offs that need to
be made around keeping a consistent signal for hours during sleep and the quality and strength of the signal. Both the Muse and Dreem bands use a central reference electrode on the forehead and I think it’s a safe bet for sleep tracking.
I sewed some material onto a thin sweatband to place the electrodes and added Velcro to attach the EEG and Feather, which is in another housing made from a small coin wallet (in red in the photo below).
The coin wallet is rigid so any compression wouldn’t press into the board or battery and I’ve attached it to the back, which works well for me because I know I never lie on my back during sleep.
I essentially made a sandwich like layout with the EEG board and Feather either side of a lipo battery and these were connected using fairly strong double sided sticky tabs. The headers for each board were soldered in a way that made this possible with cleared space on the back of each board.
The electrodes were made as short as possible to avoid noise and I used shielded audio cable to match the 3.5 jack on the EEG board. If you just twist the positive and negative wires from the electrodes and then keep the reference wire separate and hook into a 3 pole 3.5 adaptor, this is probably a better wiring scheme than what I did but I’ve not tested it out.
The wiring is pretty straightforward, a couple of notes:
- The EEG Click is powered from the BAT pin on the Feather so it has the full power from the Lipo, which is around 4V initially.
- I use the resistor divider shown in the circuit diagram earlier before feeding the sensor output to one of the analog inputs on the Feather. You can’t really see it in the photos because the resistors are covered in heat shrink tube.
In terms of costs, this is roughly what it came out to:
EEG Mikroe Click
3.7V Lipo (get one with a connector so it can plug into the Feather)
Adafruit Adalogger Feather M0
16GB SD card
Gold or Silver Cup electrodes (you’ll usually find these in a set of 10 or 20, it’s better to get more than just 3 as back-up, sometimes the plating can crack on these)
sweatband, resistors and wires
For the software side of things on the Feather, I decided on working with Arduino, which I think is a good balance between performance and ease of use. The speed at which we need to record EEG data is too fast for CircuitPython. If you wanted to be ultra efficient and bare metal, you could use Atmel Studio too.
For EEG, I want to record at around 200 or 250 Hertz minimum so at that rate you need to consider storage space and recording speed. A lot of examples for recording to SD Card reference saving data to text and CSV files but this will get very inefficient. I record the samples in raw format, which saves on storage space and is much faster – a whole night’s sleep data is less than 10MB so a 16GB memory card could record over 4 years of sleep data!
Because I use a resistive voltage divider, this affects the signal somewhat and to counter this I adjusted some of the default values that Adafruit set for the Feather M0. Firstly I increased the sampling resolution and then I changed a few other parameters based on a bit of trial and error. Optimizing any ADC is not trivial but there are some great resources online that help you understand some of the options available on the ATSAMD21 e.g.:
Also this one on GitHub:
Using The Band
The band is actually really comfortable, I do get nights where it slips up slightly and affects the readings so it isn’t perfect. EEG cups are usually used in conjunction with a conductive compound which is usually a paste e.g. this Ten20 one:
I’ve yet to try it with paste but I’ve been getting good data with conductive gel and even aloe vera gel. Unlike the paste as much, both of these dry out after some time in the night but as you perspire later, the signal quality continues to hold fine in my experience. I also made a gel that lasts longer by mixing aloe vera with a dash of saline and a drop of glycerine – the glycerine slows down drying time and the saline helps bring the conductance up because glycerine isn’t a great conductor.
Processing The Data
I use Python to read the raw data from the SD Card and display a spectrogram of the night. If you check my previous posts around sleep analysis you’ll see I prefer using spectrograms as a visual aid for sleep.
Here’s an example of one night recently:
The stronger areas of red in lower frequencies represents deeper sleep stages and where thing look more quiet across the spectrum tends to be where REM is. The signal quality I get from this band is comparable to the Muse I have but the downside is that it’s just a single channel so if I lose contact with any of the electrodes the signal can suffer whereas the Muse has more sensors that I could refer to for redundancy or I could average together.
I’ve been working on a software product that can trigger an alarm during REM phases for lucid dreaming or can be used as a smart wake alarm when fed a stream of EEG data. At the moment it’s still work in progress but I’ve been using this to automate calculating sleep stages retrospectively based on a convolution neural network. I’ll have more details on this product soon and it will be available for download eventually this year. In the meantime here’s a dashboard of what the tool can do with the data I had above:
Here you can see the spectrogram again but this time I also have additional charts that show the level of deep sleep more clearly (bottom graph) and a couple of graphs that display sleep stages that are calculated when the file is loaded, this uses the AI algorithm that I've trained before.
The great thing about this setup is that I don’t have to worry about syncing to a phone or connecting to anything else because all I need is the SD Card. So for long term analysis this works well as I can just keep charging and using daily and analyze it all later on. I’ve actually found myself using this more than the Muse and Dreem I have at the moment just for the simplicity.
The code I used on the Featuer and also the Python script to generate the spectrogram from the raw data files is on my GitHub repo here: