Abhinav Krishna
Published © MIT

Analysing EEG with TinyML

Using Wio Terminal, EEG, and TinyML via Edge Impulse to control home automation with brain signals

IntermediateWork in progressOver 1 day68
Analysing EEG with TinyML

Things used in this project

Hardware components

Wio Terminal
Seeed Studio Wio Terminal
×1
EEG SENSOR
×1
Grove - 4-Channel SPDT Relay
Seeed Studio Grove - 4-Channel SPDT Relay
×1

Software apps and online services

Edge Impulse Studio
Edge Impulse Studio
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Jumper Wire, Bundle
Jumper Wire, Bundle

Story

Read more

Schematics

archi2_7roeqwrjeo_zAGNrPw29p.png

Code

EEG

C/C++
#define SAMPLE_RATE 256
#define BAUD_RATE 115200
#define INPUT_PIN A0


void setup() {
	// Serial connection begin
	Serial.begin(BAUD_RATE);
}

void loop() {
	// Calculate elapsed time
	static unsigned long past = 0;
	unsigned long present = micros();
	unsigned long interval = present - past;
	past = present;

	// Run timer
	static long timer = 0;
	timer -= interval;

	// Sample
	if(timer < 0){
		timer += 1000000 / SAMPLE_RATE;
		float sensor_value = analogRead(INPUT_PIN);
		float signal = EEGFilter(sensor_value);
		Serial.println(signal);
	}
}


float EEGFilter(float input) {
	float output = input;
	{
		static float z1, z2; // filter section state
		float x = output - -0.95391350*z1 - 0.25311356*z2;
		output = 0.00735282*x + 0.01470564*z1 + 0.00735282*z2;
		z2 = z1;
		z1 = x;
	}
	{
		static float z1, z2; // filter section state
		float x = output - -1.20596630*z1 - 0.60558332*z2;
		output = 1.00000000*x + 2.00000000*z1 + 1.00000000*z2;
		z2 = z1;
		z1 = x;
	}
	{
		static float z1, z2; // filter section state
		float x = output - -1.97690645*z1 - 0.97706395*z2;
		output = 1.00000000*x + -2.00000000*z1 + 1.00000000*z2;
		z2 = z1;
		z1 = x;
	}
	{
		static float z1, z2; // filter section state
		float x = output - -1.99071687*z1 - 0.99086813*z2;
		output = 1.00000000*x + -2.00000000*z1 + 1.00000000*z2;
		z2 = z1;
		z1 = x;
	}
	return output;
}

Credits

Abhinav Krishna
9 projects • 56 followers
Maker | IoT Enthusiast | Electronics hobbyist

Comments