Introduction: Mind Control 3: EEG Monitor
An electroencephalogram (EEG) is a test used to evaluate electrical activity in the brain.
This instructables explains how to build an EEG waveform monitor using an Arduino UNO R3, a TFT graphics shield, and a modified AD8232 heart monitor.
The TFT graphics shield is described in my instructable https://www.instructables.com/id/Arduino-TFT-Grap...
Due to its small size the TFT display can only show 4 out of the 32 possible channels at any one time ... but the choice of channels is yours.
Processing software is included for displaying all 32 EEG channels simultaneously on your PC screen.
Arduino EEG software covering each of the following frequency ranges is also presented :
- DC .. 32Hz
- DC .. 64Hz
- DC .. 128Hz
- DC .. 256Hz
- DC .. 512Hz
- DC .. 1024Hz
- DC .. 2048Hz
- DC .. 3200Hz
The estimated cost of parts for this project is less than $30.
- The cover photo shows the EEG Monitor display
- Photo 2 shows the completed monitor
- The video shows the EEG Monitor in operation
DC stands for Direct Current which equates to a frequency of 0Hz
Step 1: Parts List
The following parts were obtained from https://www.aliexpress.com/
- 1 only Arduino Uno R3
- 1 only AD8232 ECG heart monitor module
- 1 only 2.2 Inch TFT SPI LCD Display Module 240*320 ILI9341 with SD Card Slot for Arduino Raspberry Pi 51/AVR/STM32/ARM/PIC 
- 1 only Prototype PCB Expansion Board For Arduino ATMEGA328P UNO R3 Shield FR-4 Fiber PCB Breadboard 2mm 2.54mm Pitch
The following parts were obtained locally:
- 5 only 2K2 ohm 1/8 watt metal film resistors
- 5 only 3K3 ohm 1/8 watt metal film resistors
- 2 only 10K ohm 1/8 watt metal film resistors
- 1 only 1uF tantulam capacitor
- 1 only 40 pin header terminal strip 0.1"/2.54mm pitch for PCBs
- 10 amp tinned copper fuse wire
Misc items already on hand:
- stranded copper wire
The estimated cost of parts is less than $30
Step 2: Circuit
The circuit diagram for my EEG display is shown in photo 1.
Construction details for the matching shield (photo 2) are given in my instructable https://www.instructables.com/id/Arduino-TFT-Grap...
Step 3: AD8232 Modifications
An unmodified circuit board is shown in photo 1
When used as a heart monitor the ECG leads are connected as follows:
- Right arm is connected to RA
- Left arm is connected to LA
- Right leg is connected to RL
A modified circuit board is shown in photo 2
After the modifications the leads become:
- Right eyebrow is connected to RA
- Left eyebrow is connected to LA
- Forehead is connected to RL
The headband details are described in my instructable https://www.instructables.com/id/Wink-Detector/
The original circuit
A simplified block diagram of the original heart monitor is shown in photo 3.
This diagram was created by matching the component values in the Sparkfun “Heart Monitor” schematic  to the AD8232 “Functional Block Diagram” 
When used as a heart monitor, both inputs to the AD8232 instrumentation amplifier are tied to the 3.3 volt supply rail via 10M resistors. The instrumentation amplifier, however, can’t operate unless the two input leads are around mid-rail potential.
Mid-rail potential is obtained by injecting a tiny (10uA) current from the RLD (right leg drive) lead into your leg. We have effectively created a voltage divider using your body as one of the resistors.
The actual purpose of the RLD lead is explained in the AD8232 data sheet ... I’m just looking at it from a different viewpoint.
The modified circuit
A block schematic of the circuit modifications is shown in photo 3.
Instead of looking for heart beats, the EEG Monitor is looking for differences in electrical potential. As such it needs to be fully operational at all times ... both inputs of the instrumentation amplifier must be tied to a mid-rail potential such as Vref (1.5 volts)
This is achieved by cutting the track that connects both 10M resistors to the 3.3 volt supply and joining the cut end to Vref by means of a small wire link. Both inputs of the instrumentation amplifier are now at mid-rail potential which means the AD8232 output hovers about 1.5 volts DC.
We also don’t need the RLD lead ... let’s use this lead to improve the CMRR (common mode rejection ratio) of the system by raising your body to midrail potential. This is achieved by cutting the track to the AD8232 RLD pin and joining the cut end to Vref.
The AD8232 data sheet recommends that the RLD and RLDF (right leg drive feedback) pins be shorted when using a two-lead circuit. This is achieved by shorting the capacitor joining these two pins.
Step 4: Theory
Complex waveforms comprise many sinewaves each with a different frequency, amplitude. and phase. These frequencies may be identified using a fast fourier transform (FFT).
Spectrum analysers, such as my instructable https://www.instructables.com/id/Mind-Control-2-S...and this EEG monitor both use FFT ... the difference is in how they display the FFT output data,
Spectrum analysers display the signal amplitudes against frequency whereas EEG monitors display frequency and amplitude against time.
The fix_FFT software used in this EEG Monitor outputs numbers. A large number indicates the presence of a frequency. 
- Photo 1 shows the numbers for a 6Hz sinewave ... there is only one large number
- Photo 2 shows the numbers for a 6Hz squarewave ... there are three bins with a large number.
Note that the 3rd harmonic at 18Hz has 1/3rd of the fundamental amplitude, and that the fifth harmonic at 30Hz has 1/5th of the fundamental amplitude.
The number of frequencies that are assigned to a bin depends on the sampling-frequency and the number of samples.
If the sampling frequency is 64Hz and the number of samples is also 64 then each bin will be 1Hz wide.
If we increase the sampling frequency by a factor of 4 then each bin will be 4Hz wide. For example the number in bin would represent 24Hz, 25Hz, 26Hz, and 27Hz.
The examples shown in the above photos have a bin-width of 1Hz for ease of explanation.
Sampling theory (Nyquist) says that we only need look at the first 32 bins if the number of samples is 64.
This Youtube video explains the Fourier transform in a visual manner
Step 5: Design Considerations
The Arduino analog-to-digital convertors have a number of design requirements
The waveform peaks to your Arduino analog-to-digital (A/D) converters must not go below zero volts or exceed +5volts.
Assuming a 2 volt peak-peak input waveform, then a DC bias of at least 1volt is required.
This may be achieved using:
- a resistive voltage-divider and a coupling capacitor. The down-side to coupling capacitors is they limit the low frequency response
- op-amp offset voltages connected to the A/D.
This EEG Monitor uses the 1.5 volt reference voltage from the AD8232 breakout board. Since coupling-capacitors are not required bin of the EEG monitor will respond to changes in DC.
The A/D convertor outputs 1023 for a +5 volt input, and 0 for zero volts.
The fix_FFT.ino library expects numbers in the range 0..255.
The following code lines address these requirements:
- short value = analogRead(adc_input);
- data[i] = map(value, 0, 1023, 0, 255);
Maximum sampling frequency
“For a 16 MHz Arduino the ADC clock is set to 16 MHz/128 = 125 KHz. Each conversion in AVR takes 13 ADC clocks so 125 KHz /13 = 9615 Hz.
That is the maximum possible sampling rate, but the actual sampling rate in your application depends on the interval between successive conversions calls.” 
In practice I have found the maximum sample rate to be 8192Hz when sending FFT (fast fourier transform) data to my PC
The DC offset from the input voltage divider only affects bin.
My code automatically subtracts the startup reading from all subsequent readings which allows me to track any changes.
Step 6: Software
Software covering each of the following frequency ranges is attached
Copy the contents of your chosen package into a fresh arduino sketch, save it using the same name but without the .ino extension, then upload it to your Arduino.
The bin-widths in Hz may be found by dividing the maximum package frequency by 32.
- The bin-width for fix_FFT_EEG_DC_256.ino is 256/32=8Hz
- The lowest frequency that will appear in say bin of fix_FFT_EEG_DC_256.ino will be 4*8=32Hz
- Binwill respond to all frequencies between 32Hz and 39Hz since it is 8Hz wide.
- Bin will respond to all frequencies between 5x8 = 40Hz and 47Hz
In addition to the above software I have included two Processing sketches for displaying all 32 frequency bins (photo 1) against time:
Both packages display what is sent to them.
There is, however, a slight difference ... fix_FFT_EEG.pde automatically removes any DC offset regardless of whether the DC offset has already removed.
Use "fix_FFT_EEG_2.pde" if you specifically want to see the offset.
To run these sketches:
- Download and install Processing 3 from https://processing.org/download/
- Copy the contents of your chosen package into a fresh Processing sketch, and save it using the same name but without the .pde extension.
- Connect your Arduino to one of your PC's USB ports
- Finally, click the top-left button on your processing screen 
You will get a run-time error message if Processing can't find your Arduino. Follow the instructions in the fix_FFT_EEG.pde header if this should happen.
Step 7: Summary
This instructable explains how to make a EEG analyser.
The operation has also been explained with the aid of design notes, videos and photos.
Eight frequency ranges of Arduino software have been provided.
Two sets of Processing software have also been provided.
The processing software allows you to simultaneously view all 32 waveforms
The estimated cost of parts is less than $30.
Click here to view my other instructables.