Automated eye blink detection online

It is known that EEG data is often corrupted by different types of artifacts. Many efforts have been made to enhance its quality by reducing the artifact. The EEG contains the technical artifacts (noise from the electric power source, amplitude artifact, etc.) and biological artifacts (eye artifacts, ECG and EMG artifacts). This blog post is focused on eye-blinking artifact detection online from the OpenBCI GUI. Detection of eye artifacts is not a simple process and therefore there are many efforts to develop an optimal method for eye artifact detection. We will continue to work on this to find in a near future a way to eliminate it. In this post, there is described a simple and legit detection method based on the raw data from the OpenBCI 32bit board.

You can find and download the OpenBCI GUI version here.

The Approach

Coding Part

The following lines of code of this part need to be written in the “EEG_Processing.pde” tab from the OpenBCI GUI.

STEP 1: Create a threshold to detect eye blinks

Pre-set the channels that you want to use and create the boolean variable that will detect the eye blinks.

 float ch1_mean = 0; 
 float ch2_mean = 0; 
 Boolean BLINKED = false;

Create the function to create the eye blink detection. We believe that an eye blink happens when the absolute value of the newest EEG data in general minus the mean EEG data from the channels that we are interested is bigger than 100 mV.

 public void blink_detect(float[][] data_newest_uV) {
  float ch1_sum = 0;
  float ch2_sum = 0;

  if (((abs(data_newest_uV[0][0])-ch1_mean) > 100) & ((abs(data_newest_uV[0][0])) > 100)) {
   BLINKED = true;
   } else {
    println("NO BLINK");
    BLINKED = false;

  for (int i=0; i<data_newest_uV.length; i++) {
   ch1_sum = ch1_sum + abs(data_newest_uV[0][i]); 
   ch2_sum = ch2_sum + abs(data_newest_uV[1][i]);
  ch1_mean = ch1_sum/8;
  ch2_mean = ch2_sum/8;

STEP 2: Add blink_detect in Process() function

Note that you need to add the boolean value “BLINKED” (determined as false) to the very top of “EEG_Processing” tab in order to become a general variable that can be used among all the OpenBCI GUI tabs.

boolean BLINKED = false;

STEP 3: Make BLINKED a global variable

Also, make sure that you write the following command inside the function process() or the function blink_detect that we just wrote won’t run.

void process(){

Design Part

The following lines of code of this part need to be drawn up in the “Playground.pde” tab from the OpenBCI GUI.

STEP 1: Create the Animation class

We need to create a new class to display the gif images correctly. We can write the class at the very bottom of the Playground file.

// Class for animating a sequence of GIFs
 class Animation {
 PImage[] images;
 int imageCount;
 int frame;

 Animation(String imagePrefix, int count) {
 imageCount = count;
 images = new PImage[imageCount];

 for (int i = 0; i < imageCount; i++) {
  // Use nf() to number format 'i' into four digits
  String filename = imagePrefix + nf(i) + ".gif";
  //String filename = imagePrefix + nf(i, 4) + ".gif";
   images[i] = loadImage(filename);
   images[i].resize(0, 200);

 void display(float xpos, float ypos) {
  frame = (frame+1) % imageCount;
  image(images[frame], xpos, ypos);

 int getWidth() {
  return images[0].width;

STEP 2: Upload images

You need to create the type or variables that you are going to use in the draw function at the very top of the Playground class.

 // Images
 PImage img; // Declare variable "a" of type PImage
 PImage img2;

A few lines late, you must associate the variables that we created earlier with the images that you want to upload for each variable.

 // Load Images
 img = loadImage("eyes_open.png");
 img2 = loadImage("eyes_closed.png");
Figure. Images used to simulate the eye blink.


STEP 2: Plot the images

Since BLINKED variable is now a general variable, we can use it as the trigger to display the eye blink animation when an eye blink is detected in the EEG_Procesing tab. You need to write the following lines of code inside the draw() function.

  if ((BLINKED == true) || (mousePressed)) {
 image(img2, x+50, y+70);
 img2.resize(350, 250);
 } else {
 image(img, x+50, y+70);
 img.resize(350, 250);


Voilà! Here is the demonstration of how legit is our code regarding automated eye blink detection online in the OpenBCI GUI.



Future Thoughts

We would like to go some steps further and try to correct the EEG data when an eye blink is detected. Also, we could do some research about the threshold that we should use.



Written by Gabriel Ibagon and Irene Vigue Guix (Summer interns at OpenBCI).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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 )

Connecting to %s