GSR Reader

Galvanic skin response readings are simply the measurement of electrical resistance through the body. Two leads are attached to two fingertips. One lead sends current while the other measures the difference. This setup measures GSR every 50 milliseconds. Each reading is graphed, while peaks are highlighted and an average is calculated to smooth out the values. A baseline reading is taken for 10 seconds if the readings go flat (fingers removed from leads).


GSR Reader from che-wei wang on Vimeo.


GSR readings in processing from che-wei wang on Vimeo.

Arduino Code:


void setup(){
  Serial.begin(9600); 
}

void loop(){
  int a=analogRead(0);
  if (Serial.available() > 0) {

    byte inbyte=Serial.read();
    if(inbyte=='a'){
      Serial.print(a,BYTE);


    }
  }
}

Processing Code:


import processing.serial.*;
Serial myPort;  

int hPosition = 1;     // the horizontal position on the graph
float currentReading;
float lastReading;
int count=0;
int zeroLinePos=0;

float gsrAverage,prevGsrAverage;
float baseLine=0;
long lastFlatLine=0;
color graphColor=color(255,255,255);
int baselineTimer=10000;
int gsrValue;
int gsrZeroCount=0;
float gsrRange=0;
int downhillCount=0;
int uphillCount=0;
boolean downhill;
boolean peaked=false;
float peak, valley;

void setup () {
  size(900, 450);
  // List all the available serial ports
  //println(Serial.list());

  myPort = new Serial(this, Serial.list()[0], 9600);
  currentReading=0;
  lastReading=0;
  gsrAverage=0;
  background(0);

  smooth();
}

void draw () {
  //best delay setting for gsr readings
  delay(50);
  //image(myMovie, 0, 0);

  if (gsrValue<15 &&gsrValue>-15){
    if( gsrZeroCount>10){
      currentReading=0;//flatline
      gsrAverage=0;
      baseLine=0;
      lastFlatLine=millis();
      gsrZeroCount=0;
      // println("reset");

    }
    gsrZeroCount++;
  }
  else{
    currentReading=gsrValue-baseLine;
    gsrZeroCount=0;
  }

  if(millis()-lastFlatLine>baselineTimer){
    baseLine=gsrAverage;
  }

  //graph colors
  if(gsrAverage>0 && gsrAverageheight/2.0*.25 && gsrAverageheight/2.0*.5 && gsrAverageheight/2.0*.75) graphColor=color(255,100,0);

  gsrRange=peak-valley;

  // at the edge of the screen, go back to the beginning:
  if (hPosition >= width) {
    hPosition = 0;

    //cover last drawing
    fill(0,200);
    noStroke();
    rect(0,0,width,height);
  }
  else {
    hPosition+=1;
  }

  gsrAverage=smooth(currentReading,.97,gsrAverage);

  //draw stuff

  //spike
  noStroke();
  if(gsrRange>200){
    fill(255);
    ellipse(10,10,20,20); 
  }
  else{
    fill(0);
    ellipse(10,10,20,20); 
  }

  //graph
  strokeWeight(.5);
  stroke(graphColor);
  line(hPosition-1, height/2.0-lastReading, hPosition, height/2.0-currentReading);
  stroke(255,0,100);
  line(hPosition-1,height/2.0-prevGsrAverage,hPosition,height/2.0-gsrAverage);

  //draw peaks
  int thres=7;

  noFill();
  stroke(255,0,0);
  strokeWeight(2);

  if (currentReading-thres>lastReading&& peaked==true){
    downhill=false;
    //println(downhillCount);
    uphillCount++;  
    downhillCount=0;
    point(hPosition-1, height/2.0-lastReading);
    valley=lastReading;
    peaked=false;

  }
  if(currentReading+thres 1){      // check to make sure param's are within range
    filterVal = .99;
  }
  else if (filterVal <= 0){
    filterVal = 0;
  }
  smoothedVal = (data * (1 - filterVal)) + (smoothedVal  *  filterVal);
  return (int)smoothedVal;
}

45 comments

  1. Hi,
    Love your work. I’v been looking for instruction on how to make a GSR reader with my arduino for a long time and can’t seem to find anything. I was wondering if you might give me a quick run down on how its done. I’m very new to electronics as well so I’m only just learning. Any help would be truly appreciated.

  2. My name is Pu Tai.

    I am a student from Central Saint Martins in London. I am trying to do a project that involves sensing mood and came across your GSR reader. Being an art student I don’t know a huge amount about how to go about this but I have been learning arduino and processing, it is extremely useful that you’ve shared your Arduino and Processing code so I thank you for your generosity.

    I don’t wish to take up too much of your time so I have made every effort to work this out myself but the only bit that is not clear is the circuit. I only can recognize some resistors and a capacitors on your circuit. I have tried to work it out by looking at your video but it would be so helpful if you would be kind enough to share a cicuit diagram, or a clear description. I am excited to use this technology and I’m deeply appreciative of any support you can give me. Of course, you will be credited in the final work.

    I am looking forward to hearing from you and I thank you for your help so far

    Pu

  3. Hi Pu. I basically have 300k worth of resistors between the lead going to analog (0) and ground. The capacitor is not necessary.

    So I have 5 volts going to one lead soldered to a copper penny. The other copper penny and lead splits to two wires. One wire goes to analog(0) and the other goes through a 300k resistor and then to ground. What’s read in analog(0) is the change in resistance of the voltage going through your body.

    There are better ways to do this for better accuracy, but this is a simple hack and it works. More here http://ltc.cit.cornell.edu/courses/ee476/FinalProjects/s2006/hmm32_pjw32/index.html1

  4. Hey Che-wei, the project looks great! I’m looking at doing a project with some kind of GRS reader and I already have an Arduino. My question is how I would get a reading from the circuit if I just wanted the value and not all the graphing stuff. Anyway, this project has been a great help.

  5. the arduino code waits for an ‘a’ and just sends the value it’s reading via serial, so you can just turn on your serial monitor in arduino and type ‘a’ and send. You should get the value at that moment back.

    Keep in mind the speed at which you read the GSR readings affects the smoothness and accuracy of the graph. There’s a bunch of code in processing that’s smoothing out the values so it’s not jumping all over the place.

  6. Ok so I’ve build the circuit and copied the code, but when I went to upload, I got “error: variable or field ‘serialEvent’ declared void’. Did I miss a library or something?

  7. I’m not sure what’s wrong. make sure you have the right serial port selected in myPort = new Serial(this, Serial.list()[0], 9600); you can check which on your arduino is connected to by uncommenting the line above //println(Serial.list());

  8. HI Che-Wei, I’ve built your GSR, run the processing code and get a graph but the scale seems quite off (small, i see peaks but they very tiny). Any suggestions? Also tried the link above for the more precise version but it appears to have been taken down. I’d like to know more if you can send me the hacks and details I’d love the help.

    Best

    Nathaniel

  9. Nathaniel, I would play around with the sample rate of the code in processing. Change the delay(50) in the draw loop to other values. You can also try out different values of resistors. Good luck!

  10. Che-Wei, Ok I have adjusted the delay in either direction and no luck there. I cleaned my pennies thinking that I may have a conductivity issue and I do get a fractionally better reading if I spit on my fingers and then touch the pennies, but the only time I get a sizeable reading is if the pennies touch. Then i get a large spike. Could the wiring have anything to do with it? It seems so simple that I am doubtful. I have tried different resistance as well as a pot too. Not a lot of luck besides making the lines follow the swiping of the pot. Dont know if you have access to the info you pointed putai towards but once figured out I’d like to make this more precise.

    thanks again

    nathaniel

  11. Nathaniel, I’m not sure what the problem might be. If the circuit is set up right, then i would try attaching the pennies to other parts of the body.

  12. Hi,
    Just wanted to thank you for providing information on a simple GSR reader project. I’m a research student at Newcastle Uni and have just developed your code and reader for part of my work.
    The code I developed uses the data received from the GSR reader to prduce an aura effect around the user, I shall be posting the annotated processing code very soon on my blog, http://dm.ncl.ac.uk/davysmith/blog

    Keep up the good work

    Davy Smith

  13. Hey Che-Wei,

    I’ve been trying to figure out your GSR circuit from your photos, but I’m not getting really far.
    Could you maybe explain how you build it, where the wires are going ect.?

    Thanks alot!

    Best,
    Leen

  14. Hi Che-Wei,

    I have been working with my tutor to get your GSR circuit with some success! However I have been using an Arduino Uno with no luck, the circuit is correct and working, however the processing code graph doesn’t respond to it (remains flat lined.).

    We tried my tutor’s older Arduino Duemilanove and it worked perfectly first time. Is there a possible fix or a user error that is causing the Arduino UNO not to work with the processing code correctly?

    Thanks.

    Meurig.

Comments are closed.