Introduction: DIY EEG (and ECG) Circuit
EEGs are a noninvasive way to look into your brain. While the brain is extremely complex, areas of it can lock into circular firing patterns, resulting in telltale brain waves that one can observe with the right equipment. Intensity of these waves change depending on your internal state. The waves we will be most easily able to distinguish are alpha and beta waves -- alpha waves occur at around 8-12 Hz and when measured from the frontal lobe provide an estimate of how relaxed a person is, while beta waves are around 12-30 Hz and correspond to how much a person is concentrating or how alert they are.
The concentration of each wave can also tell more specific things about your thought patterns depending on where you measure them from. For example, alpha concentrations on the left motor cortex increase when you think about moving your right hand. Regardless of where you're taking measurements, looking at the concentrations of waves in real time - a process called biofeedback - can give you much greater control over them.
This tutorial is an in-depth guide on how to make your own simple EEG circuit. Along with monitoring brain wave concentration, the final circuit can also be used as an ECG, as a way to see your heartbeat trace. The circuit will use 3 electrodes - 2 to measure a voltage difference across your scalp, and one as a reference to ground. Depending on how many parts you already have, the circuit could only set you back around $10.
The aim for this project is to be easily available and understood by people of every technology background. For those electronically savvy, I will include up front a finalized schematic so you can jump right into making it yourself. For those that want more guidance, I will include a detailed description / explanation of every section of the circuit, showing you what it does and why you need it.
Then, I'll move onto the software (Processing based), which is a very important piece in actually interpreting the raw data you receive.
So - let's start!
Step 1: Parts
I purchased most of my parts from Digikey (and Amazon). Their layout might seem slightly intimidating at first glance, but they seem like the cheapest place to get parts. And they have the USPS first class shipping option ( < $3 for small orders, choose this! It will save you a lot.), meaning you don't have to spend the same amount on parts as shipping, as it is on some websites.
- 1x Instrumentation Amplifier - AD620AN - This is the most expensive, and most important part. While technically you can make your own instrumentation amplifier from 3 op-amps, I could never get my own to give me good results. Precision cut resistors in this ensure that it'll do its job.
- 2x Quad Op-Amp - TL084CN - Any Op-Amp will do. You need 5 single amps, this one just includes 4 in each chip.
I would strongly suggest buying a capacitor bundle from ebay or the like, espcecially if you plan on ever doing some other sort of electronic project. One bundle and you're basically set for life. Regardless, whether you buy them in a pack or individually, make sure to include these capacitors :
- 1x 10 nF, ceramic
- 1x 20 nF, ceramic
- 1x 100nF, tantalum
- 5x 220nF, tantalum
- 1x 1uF, electrolytic
- 2x 10uF, electrolytic
Same as capacitors, I suggest a bundle. This is a very good one, has all the values you need (minus the potentiometer). The individual values you'll need, though, are:
- 1x 1kΩ Potentiometer - via Digikey - very useful to adjust your gain on the fly.
- 2x 12Ω
- 1x 220Ω
- 1x 560Ω
- 2x 22kΩ
- 1x 47kΩ
- 2x 100kΩ
- 2x 180kΩ
- 1x 220kΩ
- 2x 270kΩ
- 1x 1MΩ
- A breadboard to wire everything on. This one is large enough, and comes with useful jumper wires. I suggest saving the jumper wires specifically for connecting the various stages of the design. This will make it very modular, and easy to reorganize/reorder if you end up needing to.
- Wires for everything else. I like that pack, since it's pre-cut and keeps your board tidy. You can also get plain wire and cut it yourself.
- 3.5mm audio cable.
- 2x 9V batteries for power.
- Ambu Neuroline Cups seem to be the most cost-effective method, found here. Thanks to user jonencar for the link in the comments.
- electrode gel
Step 2: Complete Design
The attached picture is the final schematic. After the instrumentation amplifier, each box is a single op-amp (couldn't find a non-dual op-amp using this schematic program). I've also included the frequency response of the finished circuit (taken with the NI myDAQ, a good all-purpose oscilloscope) -- excluding the ~90x amplification the data receives by going through the instrumentation amplifier. For those not too familiar with dB, a nice conversion calculator with dB to linear gain/attenuation can be found here.
Regarding power: the easiest way to power the circuit is with 2 9V batteries. To feed your op-amps -9V to 9V of power, connect one battery the correct way, and one backwards. That is, connect the positive lead of one battery to your positive power supply line and its negative lead to GND (ground). With the other battery, connect its positive lead to GND, and its negative lead to the negative power supply line. To "set" GND, you will eventually connect an electrode from your leg directly to the GND line. This will ensure that "0V" is your leg's voltage (unaffected by any head activity), and that all readings will vary from there.
The biggest design goal for this circuit is to obtain the data, then reduce noise by enough to get a good signal into the computer, where we will process the data a bit more. As we will be using our computer's sound card to get the data in, we have to cut noise enough that the signal with noise does not spike above or below +1V and -1V, as this is the point where the sound card clips the data off. As we will be using +-9V through batteries to power the circuit, we also have to make sure that as our data is going through the circuit, it never peaks above or below this value.
I've also included an example of how to lay out the components on a breadboard, one with notes on it and one without. It isn't exactly how I did it, but should give you a general idea if you haven't worked with breadboards before. Still, I do NOT suggest using that picture as a strict guide on how to wire everything. Follow the actual schematic -- the breadboard layout looks like a pretty jarbled mess (it gets really hard to avoid that when you try to fit everything on a 30-column board). The program also didn't always layer the components correctly, and since there's no way to test the "breadboard schematic", there's a possibility that it's not perfect. Use it as a general guideline, if you need it.
To clarify some specific colors used on the breadboard view -- all red wires are power lines, and all black are basic interconnections between circuit pieces (most are connections to ground). The green wire is the output of the first notch filter, the yellow is the output of the 8 Hz HPF, the blue is the output of the 30 Hz LPF, and the white wire is the output of the gain stage. The box at the bottom represents the audio cable going to the sound card.
In general, things to remember are that in breadboards the power lines (top and bottom rows) are connected horizontally, and the rest is connected vertically, with breaks in connections across the middle gap. When looking at the schematic, take care not to connect wires together unless a black circle is there with the connections - a cross without a dot doesn't indicate a connection.
Step 3: Stage 1 - Instrumentation Amplifier
An instrumentation amplifier takes as its inputs 2 voltages, and outputs the difference between the two multiplied by some gain, G. Instrumentation amplifiers, however, are not perfect. On real amplifiers, the output is slightly skewed if both input voltages are offset the same by some amount. A perfect amplifier would take as inputs 2.1V and 2.2V, and output 0.1V*G. A real one is influenced by this common offset, and will change the output slightly accordingly. The Common Mode Rejection Ratio (CMRR) is a value given to the amplifier that corresponds to how well it ignores the common offset between inputs. A higher CMRR is better, and will output something closer to what a perfect amplifier would. It is possible to make your own instrumentation amplifier (usually with 3 op-amps), but unless you make it with precision resistors, it will suffer from a low CMRR. I personally couldn't get a good reading with a self-made instrumentation amp.
Using an instrumentation amplifier chip, the gain is changed by altering the value of the resistor between pin 1 and 8.
The datasheet for the AD620AN (our instrumentation amplifier) is here. From it, you can see that the formula for gain using this chip is G = 1 + 49,400 / Rg, which equates to a gain of 89.2 with a 560 ohm resistor. This is a good number to get the data into a not-miniscule range; we'll add a way to adjust gain on the fly a bit later. You should also see on this datasheet that on your actual circuit, your active electrodes (ones that are not the ground electrode) will be connected to pin 2 and 3 (-IN and +IN).
Step 4: Stage 2 - 60 Hz Notch Filter
The biggest source of noise in our system will be centered at 60 Hz, due to power line interference. Even if you use batteries to power your circuit, your circuit will still experience this noise. For this reason, we will have 2 "notch" filters - filters that have a severe reduction of gain around 1 particular frequency. We will use one now, to cut out as much interference as we can before we apply any more gain to our circuit, and one at the very end, to cut out any more interference we may have picked up.
The notch filter is most sensitive to changes in the 12 ohm resistor. To ensure the notch is centered at 60Hz, take a reading (detailed on the last stage) at the end of the instrumentation amplifier, then do so again once the signal has gone through the notch filter. You should see a significant reduction in the amplitude of the 60Hz (light grey) frequency band. If it is not centered at 60Hz, try a 10 ohm resistor, or even a different 12ohm one, as at that resistance it can easily vary by a considerable amount.
Step 5: Stage 3 - 7Hz High Pass Filter
As we are measuring data across the skin, our final data will also contain voltage from our galvanic skin response across our head. This will obscure the brain data we want, and as this interference is primarily low frequency, it can be fairly easily filtered out with a high pass filter (HPF). The trade-off doing this is that we also filter out a lot of gamma/delta wave data (the brain waves that are about 8 Hz and less), but if our main focus is alpha/beta wave monitoring, this isn't much of a problem.
This filter is a 2-pole HPF with a cutoff frequency of 7.23Hz. A cutoff frequency of 7.23Hz means that at this frequency, the circuit outputs data that is reduced to about 71% of its original value. As it is a high pass filter, frequencies above this cutoff will approach a gain of 1, while frequencies below will be continually reduced. The filter having 2 poles means that in the region below the cutoff frequency, the gain falls off much faster than a simpler resistor/capacitor circuit. More specifically, in this circuit, our double pole design reduces data by a factor of about 56 by the time it gets to 1Hz, while a single pole would only reduce it by a factor of about 7.5.
Along with the circuit design, I've also included the frequency response of this particular section.
Step 6: Stage 4 - 31Hz Low Pass Filter
Next up, we want to filter out data above the frequencies we are interested in. More specifically, as beta wave information stops out at 30Hz, we want to get rid of anything above that, as combined it can contribute a good amount of noise to our data. The circuit design is very similar to the high pass filter from stage 3 - it has a gain of .71 at 31.23Hz, and decreases from there at a rate such that by 300Hz it has attenuated the data by about a factor of 100.
Again, I've included the frequency response of this section here.
Step 7: Stage 5 - 1 Hz HPF and Gain of 83-455
The beginning of this circuit contains a quick HPF of cutoff frequency 1Hz (Fc = 1/(2*pi*R11*C11), just for some extra attenuation of unwanted noise. On the other end, the resistor and capacitor in parallel provide some extra filtering of high frequencies (Fc = 1/(2*pi*10nF*100kΩ) = 160Hz on a low-pass filter).
The main purpose of this section, however, lies below this, with the 220Ω resistor and potentiometer (pot for short). This op-amp is a non-inverting amplifier, and so has a gain of G = 1 + R12/(R13+R14), (ignoring the 10nF capacitor, as it's a small value and won't contribute much to the gain). The potentiometer is a variable resistor - when the input is connected to the first pin and the output to the second, turning the wiper changes its resistance linearly between 0 and 1000 ohms. This means that when the pot is turned all the way to the left, the gain of this circuit is G = 1 + R12/(R13 + 0) = 1 + 100k/(220 + 0) = 455. When it is turned all the way to the right, the gain is G = 1 + R12/(R13 + 1000) = 1 + 100k/(220 + 1k) = 83.
Remember, this 83-455 gain is on top of the 89.2x gain from the instrumentation amplifier. Alpha wave amplitude varies from person to person, from about 10 to 30 uV. Using a middle value of 20 uV, this means the ending voltage reading could range from 83*89.2*20e-6 = .148V to 455*89.2*20e-6 = .81172V. Once you've started taking readings, adjust the potentiometer such that when you're not moving at all, voltages don't fluctuate offscreen (over 1V). It doesn't have to be maximized such that the amplitude is the highest possible without clipping - just know that if you make it too small, you'll increase the error incurred from digitally reading the data into the computer.
Step 8: Stage 6 - Another 60Hz Notch Filter (and Into the Computer!)
Even with all the previous filtering stages, the data will still at this point contain a good amount of 60 Hz noise. To fix this, we will process it through another notch filter centered at 60 Hz, identical to the previous one. The final data will still have a small amount of noise, but that can be ignored through software once the data is loaded into the computer.
To get the data into the computer, we will be using a 3.5mm male-to-male cable (this is the same size ending as any headphone jack). On the cable, the first 2 notches are the right and left channels, and the one furthest down is GND. As shown in the picture, you should connect the end of the cable between the 22k resistor and 220nF capacitor (the yellow alligator clip), and the base of the cable to the GND line of your circuit -- the same line you connected your GND electrode to (the red alligator clip in the image). I suggest connecting the other end of these alligator clips to jumper wires, inserting these into their appropriate place in the circuit. Connect the other end of the cable into the microphone port of your computer, and you're good to go!
Now, onto the electrode setup and code.
Step 9: Getting Electrodes, and Proper Placement
For the electrodes themselves, these are known to be very good ones. Check this page for updates, though, as I'm currently investigating cheaper ways to make your own electrodes.
There are many possible electrode placements - which one you choose will depend on a mix of convenience and what you wish to measure. For a trial demonstration, we will measure alpha waves originating from the occipital lobe. This is because these waves are the easiest to produce, are quite large in amplitude, and only require 1 electrode on a section of the scalp with hair.
Parts of your head you'll need to know for this are the mastoid, nasion, and inion. The mastoid is the bone behind your ear: you can easily feel it by rubbing that area. The nasion is the ridge between your nose and forehead, just between your eyebrows. The inion is the point where your skull ends at the back of your head.
For this setup, you'll need a bandana and some tape (I used electrical tape, but scotch tape or even a band-aid would probably work). First, find your left mastoid. Clear away any hair, and tape the ground electrode to the skin there. Next, tie the bandana tight around your head, such that it is above both the nasion and inion. The bandana will be used to secure in place the remaining 2 electrodes. Put one electrode about 1 inch above and 1 inch to the right of your nasion, and the second the same distance from your inion. Using this top down view, your electrodes should be roughly in the area of Fp2 and O2.
As mentioned, we will be primarily measuring alpha waves with this setup. Alpha waves are around 8-12 Hz waves, and in general increase in amplitude when you are relaxed or in a more meditative state. Because one electrode lays on your occipital lobe (the part of your brain used for visual processing), whenever you close your eyes there should be a distinct increase in alpha wave concentration. The increase should be even more apparent if you relax and try to clear your mind while your eyes are closed.
Step 10: "Processing" the Data
CAUTION: I highly suggest doing this on a laptop, because there is a danger involved if there's a spike in voltage from the wall outlet. Regardless, BE VERY CAREFUL WITH THE PROBES GOING TO THE SOUND CARD. If they accidentally touch a high voltage source (i.e. you stick them into a power outlet), you can fry your computer's sound card.
First thing to do is to download Processing, available at http://processing.org. It doesn't require any installation; just unzip the download, open the folder, and run it. Download the sketch (what programs are called in processing), and open it up. The program should be good to go as is, and while there should be enough documentation to understand what's going on in the sketch, I want to make a few notes and give a general outline of what it does here.
I encourage you to tinker with the program - change things, make your own, etc. Don't be afraid to break it, since a working version can always be found here. If you're new to programming, the guys that made processing have some really great basic tutorials here. One thing to note is that Processing is case sensitive - if at some point you type in FFTHeight instead of FFTheight, the program will give you an error and take you to the line where you typed in the former. I didn't document every single function that I used - if you're unsure about what a part of code does, you should look up the function being used at processing.org, so that you can see what its meant to do, as well as exactly what it takes as inputs and produces as outputs. Audio classes won't be found as easily there (minim, FFT, AudioInput, etc). To find documentation for those pieces, look here, specifically at the manuals under the tools tab at the top.
Also, a little background on the FFT. Data can be represented in many ways, two common ways being in time and frequency. Representing information into frequency domain usually amounts to showing data as the combination of a lot of sine waves with various frequencies and amplitudes. If you have a pure sine wave, say oscillating at 1 Hz, you would see the sine wave we all know and love in the time domain, but in the frequency domain would just see one line at f = 1. If in time you had a wave that was made by adding one sine wave at 1 Hz, and one that was half the amplitude the first but at 2 Hz, in frequency you would see two lines - one at 1 Hz with a height of 1, and one at 2 Hz with a height of 0.5.
From this, you can represent very complicated signals (any signal!) as a combination of a number (sometimes an infinite number) of sine waves. The most common way to convert signals from the time domain to frequency is with the FFT (Fast Fourier Transform). It does exactly what I just described -- it takes as input a section of time domain signal, and outputs bands corresponding to the concentration of certain ranges of frequencies in that signal. This data can easily be visualized by displaying each band as a bar with a certain height, as I did in the code.
This program is really just a data acquisition / visualization one. There are a LOT of things you can do with this circuit -- I encourage you to really play around with it and make something of your own! The next step is an optional one detailing something a little more fun that I made with the type of data acquired in this program.
Code available here.
Step 11: Playing Pong With Your EEG
I also decided to make a quick demo game of something more fun you can do with the EEG circuit. You can read most of this in the attached processing code, but the general concept is a simple pong game where you control the paddles with your concentration of alpha waves. The easiest way to manage the controls is to close your eyes and relax to make the paddle move up, and to open your eyes and focus to make it move down. The electrode setup is the same as the previous code slice - GND behind left ear, and active electrodes on the right frontal and occipital lobes.
The associated code is much neater than the data-reading one. Because the game code incorporates object oriented programming, experienced programmers will likely find it easier to follow along, while those that have not worked with object oriented code before may have a harder time. If you fall into the latter category, I highly suggest reading this tutorial to get familiar with the basics.
The circuit used for this one is exactly the same, except you need two, since you have two players. To get it in via the audio cable, connect one of the circuits the usual way (outlined in the previous steps), and the other circuit to the other audio channel (the middle section of the audio cable). You can use one set of batteries for both circuits, so they should share the same ground.
Code available here.
Quick video demo of me using it against a wall here.
Step 12: Going Further - Using Arduino to Get More Inputs
By using the microphone input to your computer, you can monitor two EEG nodes at once (right and left channels). To monitor more simultaneously, however, you need to use a microcontroller such as the Arduino Uno to get the data into your computer. The Uno has 6 analog inputs, so you can observe 6 channels at once without any extra circuitry. If you want to observe more, you need a multiplexer chip such as the MAX4051. This chip will take as input a number of EEG channels and a number from 0-7, and output only the channel corresponding to the number. Rapidly cycling through the channels samples all of them at a rate necessary for good data acquisition.