Monday, November 18, 2013

OpenBCI Alpha Wave Detector

Part of what excites me so much about EEG hacking is the idea that maybe I can control things with just my mind.  Once you gain any experience at all with EEG signals, however, you realize just how hard a task that can be.  So, start simple.  Start with what's easy.  Build from there.  With EEG, "easy" are Alpha waves.  Alpha waves is nice a simple EEG waveform that shows up fairly strongly around 10 Hz.  It is a great target for making one's first brain controlled hack.  And I just did it myself...check it out!  It's completely computer-free and feels like magic!


Electronics Setup

For this hack, I used the parts shown in the picture below.  It's an OpenBCI board sitting on an Arduino Uno (the Uno is completely hidden in this photo under the white OpenBCI board), a cheap peizo buzzer, and a basic red LED.  I put a 330 ohm resistor in series with the LED and in series with the buzzer to limit the current (a practice recommended in all of the Arduino learning examples).  Because I wanted to be completely computer-free, the whole thing is powered by a 9V battery adapter.

OpenBCI plus Arduino Uno plus LED, Piezo Buzzer, and 9V Battery.

EEG Setup

This hack uses two off-the-shelf reusable EEG electrodes.  I plug them into my Open BCI board using the adapter cable that I made.  I placed the first electrode (the reference) on the top of my head, a little towards the front ("Fz").  I placed the second electrode on the back of my head, just above the bump on the back of the head (the "inion") and a little to my left (aka "O1").  I worked the electrodes under my hair and stuck them in place using ten20 conductive paste.  This time, I used the small jar of it instead of the tube...the jar is much easier to work with.

My setup, including the colorful homemade adapter cable for
attaching my two EEG electrodes.
Arduino Processing Software

Unlike all of my previous experience with EEG hacking, where a PC was involved for doing the signal processing, today I will be doing all of the EEG processing on the Arduino itself.  The Arduino is not a computational power house, so we have to be reasonable in our expectations when doing signal processing on the Arduino.

To detect Alpha waves in EEG signals, there are several things that you need to do:

  1. Filter to remove strong interfering signals (60 Hz and DC drift)
  2. Filter to focus on the frequencies of interest (in this case, Alpha are ~10 Hz)
  3. Decide if there is enough Alpha (and just Alpha) to activate the LED and buzzer

The full details of my signal processing chain is a long story that will get its own post.  The Arduino's limited bit depth (32-bits is the maximum data type) and limited speed with floating point operations means that you cannot use sharp filters.  Instead, you have to use relatively gentle filters such as "biquad" filters (which are a form of 2nd order IIR filter).  To generate the filter coefficients, I used code that I found here.

After doing my filtering, I then compute the RMS power of the signal.  I simply square the single, apply a low-pass filter (another biquad from the code above), and take the square root.  This gives you a running RMS estimate of the signal amplitude.  Because of my filtering, this gives me a running estimate of the Alpha amplitude.  I then light my LED and sound my buzzer based on the amount of Alpha energy.

Thinking Things to Action

So, with this setup, you've got yourself a self-contained device that responds to your brain waves.  It doesn't have to be just an LED or buzzer, either.  It could be a robotic arm swinging a sword (to fight off pirates, of course), or a cool flashing hat for dance parties, or an animatronic flower that blooms with your thoughts.  Or maybe you like the idea of getting this kind of neurofeedback to see what is happening when you meditate (change the filter from 10 Hz Alpha up to 20 Hz Beta).  Or, maybe you could use it simply for weirding-out your friends and co-workers...what with the strange wires coming out of your head and all.  There are so many possibilities.

To me, controlling things with your brain in this way seems a bit magical...but I made this thing myself, so I know that it's not magic...it's hacking!

Follow-Up: A long time has passed, but I think that I found the code for this hack.  This was prompted by folks on the OpenBCI forum trying to do it themselves!  Cool!

Follow-Up:  I extended this work to control a six-legged robot with my brain waves!

4 comments:

  1. Hi Chip,

    I am currently using an OpenBCI v3, and am wondering how to configure the board to even start to code outputs - there are only A0-A7 pins on the actual octagonal board and am wondering which pins to use to control/program outputs (such as simple blinking an LED on an Arduino red board).

    Thanks! I love all of your work!

    Johanping

    ReplyDelete
  2. hallo,
    i bought a 8 bit openBCI v3 (I think) in order to use it in the same way you show here.
    my question is how do you connect openBCI board with your Arduino UNO, do you have a sketch...
    thanks a lot for your help.

    Adriano

    ReplyDelete
    Replies
    1. The openBCI v3 board has the Arduino built-in. So, if you want to do a hack like the one above, you'll have to reprogram the OpenBCI board using the OpenBCI_8Bit firmware from the OpenBCI GitHub site. You'll have to merge my Alpha detection Arduino software with the OpenBCI_8bit software. If you're inexperienced, this could be a bit of a challenge (but challenges are fun!).

      Chip

      Delete
  3. Hi i'm a novice in arduino uno designs and read deeply impressed by your post. i also designed using the buzzer that you are. but the buzzer sound is too small. i wonder that your coding of buzzer not the fillter. i'm looking forward to your reply! thank you ^^

    ReplyDelete