Reading brain waves with Espruino

Posted on
  • Anybody else working on brain waves?

    I have successfully hooked up a TGAM (a tiny EEG module) to my Espruino board. I extracted my TGAM from a Mattel MindFlex game that I bought used for 20€ on ebay.

    As electrodes I am using 1 cent coins from the US whose coating I removed with a dremel rotation tool. I have glued them onto a strap of tissue to be worn on the forehead. In order to measure brain waves, at least three electrodes are needed: a signal electrode, a ground electrode and a referece electrode. The TGAM basically measures the voltage between ground and signal and uses the voltage between ground and reference to determine noise.

    Communication with the TGAM is nasty, but I developed an Espruino module for that. I just created a pull request for it, so hopefully it will soon be merged and available for the benefit of all.

    So far I'm successfully dumping brain wave spectral data to console. My first goal with this project is to build a dream recorder which can be worn on the head at night and which simply dumps brain waves to an SD card. Once that is working, I want to develop an algorithm that detects REM sleep phases. The final goal is to turn it into a lucidity device, i.e. a device that detects when a person is dreaming and then informs that person that they are dreaming (without waking them up). The dreamer can then consciously interact with the dream and direct it. I have had a few short lucid dreams and they're awesome, but it's very hard for me to induce them, hence I need to build this tool :) The cue to the dreamer should be a short audio message played through an integrated speaker.


    4 Attachments

    • brainwaves.jpg
    • IMG_6755.jpg
    • IMG_6757.jpg
    • IMG_6759.jpg
  • Fantastic! :D

  • Very, iteresting. I've always wanted to demo/test reading brainwaves, but I have always been skeptical about how well a device could read brainwaves. I wonder how well this device would work: https://www.sparkfun.com/products/12805

    Although, I really like your diy approach and would probably feel less bulky than the unit that sparkfun sells and less $$$!

  • Wow, looks awesome! I love the idea of detecting dreams and playing audio to prompt people!

    Just pulled the module in - I'll stick it online next time I update the website.

  • @d0773d, the NeuroSky Mindwave Mobile (sold by sparkfun) uses the exact same ASIC module (TGAM) and is actually made by the company who developed that chip. I know this headset because I have that one as well. The idea behind the product is that anybody should be able to develop EEG applications without having to care about hardware or interfacing a board. Besides the TGAM, the headset includes a Bluetooth module which can be paired with a computer or a smartphone. It also comes with a driver for PC and Mac which can be used from your own applications so you don't have to mess with the low level protocol. That product makes sense (this is why I bought it) and you can easily develop computer or mobile applications which use it.

    However, for my lucidity project, I want to make an integrated self-contained device. I don't want to stream data over bluetooth, I want to have my Espruino directly in the unit. Furthermore I wouldn't want to sleep with that headset on, I'll rater make my own which should be more comfortable. A good thing about the headset is that it has great signal quality through the integrated electrodes. My own design with the penny electrodes still triggers a "poor signal" message and the chip refuses to calculate attention and meditation values if "poor signal" is greater than 0. I need to keep working on my electrodes.

    If you're interested in the headset, I'd sell mine for a good price. I'm in Germany.

  • @Dennis Thank you for wanting to sell the headset a good price. Right now I have so many projects that I am working on that need to get finished at some point before I take on another. Thank you, though.

  • Are someone still working with brainwaves and Espruino? I have a Neurosky Mindwave Mobile 2 Headset and would like to read the meditation and attention data.

    Can someone help?

  • I'm not sure if anyone is currently doing anything, but there is a module to allow communications between Neurosky devices and Espruino: http://www.espruino.com/Brain

  • Hi Gordon,

    thanks. So without that module do you think its not possible to get a bluetooth connection + reading out data from the NeuroSky Headset directly on a MDBT42Q or Puck?

    Greetings

  • I see thats the module which is integrated in the headset.

  • Is there a possibility to install the module onto MDBT42Q.
    I tried it via github and from a local folder but had no success.

    Its my first time, working with modules on espruino.

  • Interesting project, I have a Muse S headband, which seems to work well for sleep tracking and brainwave data. On the subject of tracking sleep and general hypnogram cycles I do think indirect data points such as changes in heartrate and movement provide decent info when I benchmark against the Muse - it's not 100% of course but I find it's more practical for regular use. I've been working on a sleeptracker for the Bangle.JS that takes HRM data and movement to attempt charting sleep cycles, it wouldn't take all that much more to put in a smart alarm feature as well as possibly a REM alarm in theory - REM and wakefulness would be towards the peaks of these cycles so you just need to detect peaks and trigger an alarm soon after the descent.

    A REM alarm on this would probably need to be via bluetooth to another device as the sound wouldn't be loud enough on the Bangle, and I suspect the motor wouldn't have much effect because of sleep paralysis in REM.


    1 Attachment

    • sleeptracker.png
  • Ahh - so that code is there to talk to this hardware by the look of it: https://store.neurosky.com/products/eeg-­tgam

    If you had that hardware there's no need to mess around with GitHub, you just copy/paste the code from that page into the right hand side of the Web IDE, wire up as suggested, and it'd work.

    However it seems the Neurosky Mindwave Mobile 2 is a completely wireless solution - so realistically I don't think that code is going to work for you.

    What you'd need to do is either find some information on the Bluetooth protocol used online and try and implement it in Espruino (we could help with that), or to actually connect with an app like 'NRF Connect' on your phone and see if you can figure out how it works yourself.

  • Hi Gordon,

    thanks for your answer.

    I have been able to establish a Bluetooth connection with the device. Found a service and characteristics UUID in an Arduino forum.

    However, how I should read the data now (with what code), I'm stumped. Had tried it with the combination of the module suggested by you and about the known to me BLE connection setup in the WEB IDE.

    var gatt,characteristic;
    NRF.requestDevice({ filters: [{ adress: 'c4:64:e3:e8:d6:e9' }] }).then(function(device) {
      console.log("Found");
      return device.gatt.connect();
    }).then(function(g) {
      console.log("Connected");
      gatt = g;
      return gatt.getPrimaryService(
        "039afff0-2c94-11e3-9e06-0002a5d5c51b");­
    }).then(function(service) {
      return service.getCharacteristic(
        "039afff8-2c94-11e3-9e06-0002a5d5c51b");­
    }).then(function (c) {
      console.log("Got Characteristic");
      characteristic = c;
      startReading();
    });
    function startReading() {
      var busy = false;
      var i = setInterval(function() {
        if (!gatt.connected) {
          clearInterval(i);
          return;
        }
        if (busy) return;
        busy = true;
        function processBrainData(data) {
         console.log(data.field, data.value);
       }   brain.on('data', processBrainData);
      }, 50);
    }
    
    
  • Hi, that's great. What I'd suggest is to maybe try this and see if it works:

    ...
    }).then(function(service) {
      return service.getCharacteristic(
        "039afff8-2c94-11e3-9e06-0002a5d5c51b");­
    }).then(function (c) {
      console.log("Got Characteristic");
      characteristic = c;
      characteristic.on('characteristicvaluech­anged', function(event) {
         processBrainData(event.target.value.buff­er);
      });
      return characteristic.startNotifications();
    }).then(function() {
      console.log("Connected.");
    });
    
    function processBrainData(buf) {
      var d = new Uint8Array(buf);
      print(d.slice().map(x=>x.toString(16).pa­dStart(2,0)).join(" "));
    }
    

    At least then you should get a hex dump whenever the data changes. Maybe you could post the results up here and it might give us some idea of how to pull the data out.

    Do you have a link to the Arduino forum? Did they give any hints about how to decode the data?

  • Hey, sure I will try it.

    Yes, please find here the link, its Post#5 in the forum:

    https://forum.arduino.cc/index.php?topic­=695579.0

  • So the connection works but I don´t get any data.
    Thats all what I get:

    Found
    Connected
    Got Characteristic
    Connected.

  • Ok, thanks! It looks like there are some other characteristics reported there:

    Characteristic 039afff8-2c94-11e3-9e06-0002a5d5c51b, properties 0x12, value 0x34240000FC2300007E
    Characteristic 039affa0-2c94-11e3-9e06-0002a5d5c51b, properties 0x8
    Characteristic 039afff4-2c94-11e3-9e06-0002a5d5c51b, properties 0x10
    Characteristic 039affd3-2c94-11e3-9e06-0002a5d5c51b, properties 0xA, value 0x000000000000000002
    

    I can't remember offhand what the properties mean, but it might be worth trying some of those and seeing if you get data from them?

  • Hi Gordon,

    so I just get zeros as values. I tried it with the hex code 0x04 (attention level).
    In the documentation in the link below I saw there are also a hex code to enable the outputs but I don´t konw how to integrate that into the espruino program.

    I added var buf = 0x04 in the code.

    Found this documentation on the thinkgear website with the codes:
    http://developer.neurosky.com/docs/doku.­php?id=thinkgear_communications_protocol­#data_payload

    
    var buf = 0x04 ;
    var gatt,characteristic;
    setWatch(function(e) {
    NRF.requestDevice({ filters: [{ adress: 'c4:64:e3:e8:d6:e9' }] }).then(function(device) {
      console.log("Found");
      return device.gatt.connect();
    }).then(function(g) {
      console.log("Connected");
      gatt = g;
      return gatt.getPrimaryService(
        "039afff0-2c94-11e3-9e06-0002a5d5c51b");­
    }).then(function(service) {
      return service.getCharacteristic(
        "039afff8-2c94-11e3-9e06-0002a5d5c51b");­
    }).then(function (c) {
      console.log("Got Characteristic");
      characteristic = c;
      characteristic.on('characteristicvaluech­anged', function(event) {
         processBrainData(event.target.value.buff­er);
      });
      return characteristic.startNotifications();
    }).then(function() {
      console.log("Connected.");
      enableOutput(buf1);
    });
          }, BTN, { repeat: true, edge: 'rising' });
    function processBrainData(buf) {
      var d = new Uint8Array(16);
      setInterval(function() {
      print(d.slice().map(x=>x.toString(16).pa­dStart(2,0)).join(" "));
    }, 500);
      }
    
  • So did you try any other characteristics, or just 039afff8-2c94-11e3-9e06-0002a5d5c51b?

    To write data, you'd need to do something like :

    var service; 
    // ...
    }).then(function(g) {
      console.log("Connected");
      gatt = g;
      return gatt.getPrimaryService(
        "039afff0-2c94-11e3-9e06-0002a5d5c51b");­
    }).then(function(s) {
      service = s;
      return service.getCharacteristic(
        "039afff8-2c94-11e3-9e06-0002a5d5c51b");­
    }).then(function (c) {
      console.log("Got Characteristic");
      characteristic = c;
      characteristic.on('characteristicvaluech­anged', function(event) {
         processBrainData(event.target.value.buff­er);
      });
      return characteristic.startNotifications();
    }).then(function() {
      return service.getCharacteristic("039affa0-2c94­-11e3-9e06-0002a5d5c51b"); //<--- might be a different one
    }).then(function() {
      return c.writeValue([0x04]);
    }).then(function() {
      console.log("Connected.");
      enableOutput(buf1);
    });
    

    But as they have developer docs, I wonder whether you could actually just get in touch with them and ask them if they have an explanation of their Bluetooth protocol?

  • I've asked about it, but I'm still waiting to hear back.

    You can read the hex addresses from the documentation.

    Do you think I could try it via a module?
    The combination of Bluetooth connection and readout should also work with a module.

    However, I have not really understood how this works with the modules on a local storage location. I know your documentation but don't know how it is meant to put the module in when you have created a new empty folder via Settings >>> Projects.

  • Do you think I could try it via a module?

    I'm not sure I understand the question? If you were getting actual data over Bluetooth then maybe you could have pushed it into the software module and got something useful, but you say you're just getting all zeros?

    I have not really understood how this works with the modules

    Honestly, there's no need. You just do require("modulename") and the module is pulled in from the internet automatically.

    All you need to do is copy the few lines on http://www.espruino.com/Brain and everything will work automatically.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Reading brain waves with Espruino

Posted by Avatar for Dennis @Dennis

Actions