Dec 072013
 

http://tmblr.co/ZF7mJupjcFfv

Capacitive touch sensor with MSP430 Launchpad

I found this interesting article on making a capacitative touch sensor with graphite drawings here, and I wanted to modify it for the MSP430g2331.

The first thing I had to change was the readCapacitative() function. I only allow use of the P1.x pins, and I changed the input from pin number to the x in P1.x, to match the layout of the MSP430 registers.

Here is my modified readCapacitative function:

    // readCapacitivePin
//  Input: x in P1.x pin name 
//  Output: A number expressing
//          how much capacitance is on the pin
//  When you touch the pin, or whatever you have
//  attached to it, the number will get higher
//  In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
//  it up to +5v.
uint8_t readCapacitivePin(int pinToMeasure){
  // This is how you declare a variable which
  //  will hold the PORT, PIN, and DDR registers
  //  on an AVR
  volatile uint8_t* port;
  volatile uint8_t* ddr;
  volatile uint8_t* pin;
  // Here we translate the input pin number from
  //  Arduino pin number to the AVR PORT, PIN, DDR,
  //  and which bit of those registers we care about.
  byte bitmask;

    port = (uint8_t*) &P1OUT;
    ddr = (uint8_t*) &P1DIR;
    bitmask = 1 << pinToMeasure;
    pin = (uint8_t*) &P1IN;

  // Discharge the pin first by setting it low and output
  *port &= ~(bitmask);
  *ddr  |= bitmask;
  delay(1);
  // Make the pin an input WITHOUT the internal pull-up on
  *ddr &= ~(bitmask);
  // Now see how long the pin to get pulled up
  int cycles = 16000;
  for(int i = 0; i < cycles; i++){
    if (*pin & bitmask){
      cycles = i;
      break;
    }
  }
  // Discharge the pin again by setting it low and output
  //  It's important to leave the pins low if you want to 
  //  be able to touch more than 1 sensor at a time - if
  //  the sensor is left pulled high, when you touch
  //  two sensors, your body will transfer the charge between
  //  sensors.
  *port &= ~(bitmask);
  *ddr  |= bitmask;

  return cycles;
}

The important part is changing PORTD, DDRD and PIND to the port names for the MSP430. I also changed the output pin to P1.0 (the red LED). I wanted to produce a speaker tone instead of lighting an LED, but the tone library was way too much to include with this program – this chip only has 2K RAM.

Now I can get the light to turn on by touching the pin end of the resistor directly, but for some reason I haven’t had any luck with a pencil drawing yet. Strangely, it seems to work best when I touch the +5V end with one hand and the pin end with the other. Also very strangely, I had to set the serial monitor to 4800 baud to see proper output.

More info on the MSP430g2231 can be found here, and the IDE I am using, Energia, can be found here.

  1. c-d-e likes this
  2. mandrakeinrochester likes this
  3. thecodemaiden posted this
Short URL for this post: http://tmblr.co/ZF7mJupjcFfv
 Posted by at 1:14 pm

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)