Skip to content

Instantly share code, notes, and snippets.

@MexsonFernandes
Created April 5, 2019 13:39
Show Gist options
  • Select an option

  • Save MexsonFernandes/37d56c2d3a0adaf7d54f7d3faa5b0c80 to your computer and use it in GitHub Desktop.

Select an option

Save MexsonFernandes/37d56c2d3a0adaf7d54f7d3faa5b0c80 to your computer and use it in GitHub Desktop.
Matlab code for data collection from Neurosky Mindwave Mobile EEG headset
%clear everything before loading
clc;
clear ALL;
pause on;
%loading dll library, header file and other libraries
%(libfunctionsview thinkgear to view dll)
if ~libisloaded('Thinkgear')
loadlibrary('Thinkgear.dll');
else
unloadlibrary Thinkgear
loadlibrary('Thinkgear.dll');
end
disp('libraries loaded successfully.')
path = 'E:/StartInnovating/Projects/1_Neurosky_robot_control_EEG/Matlab_Code/';
%to compute data of 14 columns
data = ones(61440,14);
portnum1 = 7; %COM Port #
comPortName1 = sprintf('\\\\.\\COM%d', portnum1);
% Baud rate for use with TG_Connect() and TG_SetBaudrate().
TG_BAUD_115200 = 115200;
% Data format for use with TG_Connect() and TG_SetDataFormat().
TG_STREAM_PACKETS = 0;
% Data type that can be requested from TG_GetValue().
TG_DATA_POOR_SIGNAL = 1;
TG_DATA_ATTENTION = 2;
TG_DATA_MEDITATION = 3;
TG_DATA_RAW = 4;
TG_DATA_DELTA = 5;
TG_DATA_THETA = 6;
TG_DATA_ALPHA1 = 7;
TG_DATA_ALPHA2 = 8;
TG_DATA_BETA1 = 9;
TG_DATA_BETA2 = 10;
TG_DATA_GAMMA1 = 11;
TG_DATA_GAMMA2 = 12;
TG_DATA_BLINK_STRENGTH = 37;
% Get a connection ID handle to ThinkGear
connectionId1 = calllib('Thinkgear', 'TG_GetNewConnectionId');
if ( connectionId1 < 0 )
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
error(sprintf( 'ERROR: TG_GetNewConnectionId() returned %d.\n' , connectionId1 ));
end
% Attempt to connect the connection ID handle to serial port "COM8"
errCode = calllib('Thinkgear', 'TG_Connect', connectionId1,comPortName1,TG_BAUD_115200,TG_STREAM_PACKETS );
if ( errCode < 0 )
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
error(sprintf('ERROR: TG_Connect() returned %d.\n' , errCode) );
end
%recording data
fprintf( 'Connected. Reading Packets...\n' );
for i=1:120 %loop for 120 seconds
if (calllib('Thinkgear','TG_ReadPackets',connectionId1,1) == 1) %if a packet was read...
for j=1:14
if(j == 1)
data(i,j) = now;
end
if(j == 2)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_POOR_SIGNAL) ~= 0) %if RAW has been updated
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_POOR_SIGNAL);
end
end
if(j == 3)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ATTENTION) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ATTENTION);
end
end
if(j == 4)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_MEDITATION) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_MEDITATION);
end
end
if(j == 5)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_RAW) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_RAW);
end
end
if(j == 6)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_DELTA) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_DELTA);
end
end
if(j == 7)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_THETA) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_THETA);
end
end
if(j == 8)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ALPHA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ALPHA1);
end
end
if(j == 9)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_ALPHA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_ALPHA2);
end
end
if(j == 10)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BETA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BETA1);
end
end
if(j == 11)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BETA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BETA2);
end
end
if(j == 12)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_GAMMA1) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_GAMMA1);
end
end
if(j == 13)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_GAMMA2) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_GAMMA2);
end
end
if(j == 14)
if (calllib('Thinkgear','TG_GetValueStatus',connectionId1,TG_DATA_BLINK_STRENGTH) ~= 0)
data(i,j) = calllib('Thinkgear','TG_GetValue',connectionId1,TG_DATA_BLINK_STRENGTH);
end
end
end
end
pause(1)
end
fprintf( 'Completed reading data.\n' );
%save data to csv file
csvwrite(path + 'data.csv',data);
dlmwrite(path + 'data.csv',data,'precision', '%.6f');
% disconnect
calllib('Thinkgear', 'TG_FreeConnection', connectionId1 );
@MexsonFernandes

Copy link
Copy Markdown
Author

Make sure that you have the DLL file of Think Gear.

@haseebzaib

Copy link
Copy Markdown

hello sir i need to ask about this path line
on line 15 if i paste this code on matlab what path i need to write there ?
i am making wheelchair using mindwave mobile and i need blink strength data

@chyavanphadke

Copy link
Copy Markdown

Where exactly we can find that dll file ?

@MexsonFernandes

Copy link
Copy Markdown
Author

http://sendanywhe.re/LLY7JPPG

You can use the above link to download the DLL files. I am sure you will find the same one from developer documentation of Neurosky.

@MexsonFernandes

Copy link
Copy Markdown
Author

hello sir i need to ask about this path line
on line 15 if i paste this code on matlab what path i need to write there ?
i am making wheelchair using mindwave mobile and i need blink strength data

you can write any location there. The processed data will be saved in this path.

@chyavanphadke

Copy link
Copy Markdown

@MexsonFernandes, Perfect! thanks for the file and details. Works fine now.
I have one last query. Does this code work for "Neurosky mind wave mobile 2" headsets?
Because I am getting all empty data.

@MexsonFernandes

Copy link
Copy Markdown
Author

It does for Neurosky Mind Wave Mobile 2 headset. You can refer below repository. Although it's not well documented, you can still refer to some code.

https://github.com/MexsonFernandes/EEG-controlled-Robot-using-Neurosky-Mindwave-Mobile2

@MexsonFernandes

Copy link
Copy Markdown
Author

@MexsonFernandes, Perfect! thanks for the file and details. Works fine now.
I have one last query. Does this code work for "Neurosky mind wave mobile 2" headsets?
Because I am getting all empty data.

https://gist.github.com/MexsonFernandes/c08ad378e7cad5c98b5c2dc912797dc1 Refer this gist as well.

@edansama

Copy link
Copy Markdown

Good night, I need some help with code, please, if you can tell me how I can to increase of data number for second that the code can to capture.

@AmerBasem

Copy link
Copy Markdown

hello sir
I faced a problem every time run project it is ...

Error using eeg (line 54)
ERROR: TG_Connect() returned -2.

can you help me, please?

@zhra120

zhra120 commented Nov 14, 2021

Copy link
Copy Markdown

http://sendanywhe.re/LLY7JPPG

You can use the above link to download the DLL files. I am sure you will find the same one from developer documentation of Neurosky.

sorry , can you please send this link again? ...it isn't available.

@urmi2927

Copy link
Copy Markdown

image
Sir Can I get some help ?

there are some issues for which I can't execute the code. Is there any way to solve this without installing 32-bit Matlab?

@haseebzaib

Copy link
Copy Markdown

@urmi2927 your only problem is that your are using wrong library. Use the correct one and it will work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment