Jump to content
Sign in to follow this  
iragdoll

Connecting Piezo Vibration Sensor to LaunchPad

Recommended Posts

u guys should be on a chat channel.

 

what was the last question?

 

would u like some quick example code? i use this for the spectrum analyzer project.

 

i would run it and run a jumper between Vcc and p1.0 (we use as adc channel 0), the led would light up immediately.

 

   // adc setup, note here i am using 2.5v internal reference
   ADC10CTL0 = SREF_1 + REFON + REF2_5V + ADC10SHT_2 + ADC10ON + ADC10IE;
   ADC10CTL1 = INCH_0 + ADC10DIV_3;    // adc channel 0
   ADC10AE0 |= (1<<0);                 // use p1.0

  while (1) {
       ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
       __bis_SR_register(CPUOFF + GIE);        // LPM0, ADC10_ISR will force exit
       // slept and wake up when adc done, continue to next line
       if ((ADC10MEM > CUTOFF)
            // turn on your led, u need to define your cutoff, say 1.25v would be 512
 }

// interrupt handler
#ifdef MSP430
interrupt(ADC10_VECTOR) ADC10_ISR(void) {
#else
#pragma vector=ADC10_VECTOR
__interrupt void Adc10_Vector(void) {
#endif
   __bic_SR_register_on_exit(CPUOFF);  // Clear CPUOFF bit from 0(SR)
}



Share this post


Link to post
Share on other sites
Can you guys sort of edit the code and I will test it out and sort of guide me through it?

You should post your code. I'm about to hit the sack, but we have members from around the world. You should be able to get some help with your code, and it won't cost you anything but some effort on your part.

 

Best of luck. I'll check back in the morning (CST).

Share this post


Link to post
Share on other sites
#define __MSP430G2231__
#include 

#define PHOTOCELL INCH_0 // photocell divider on P1.0/A0


int analogRead(unsigned int pin);

void main() {
 volatile unsigned int val;

 WDTCTL = WDTPW + WDTHOLD; // kill wdt+
 P1OUT = 0x00;

 ADC10CTL0 = ADC10ON + ADC10SHT_0 + SREF_0; // ACD10 on, 4 clock cycles per sample, Use Vcc/Vss references

 // change frequency to ~15.25mhz (DCO(15,3))
 BCSCTL1 |= (BIT0 + BIT1 + BIT2 + BIT3);
 DCOCTL &= ~(BIT7);
 DCOCTL |= (BIT6 + BIT5);

 for (; {
   val = analogRead(PHOTOCELL);
  }
 }  

int analogRead(unsigned int pin) {
 ADC10CTL1 = ADC10SSEL_2 + pin;// Select MCLK and pin
 ADC10CTL0 |= ENC + ADC10SC; // enable and start conversion
 while (1) {
   if ((ADC10CTL1 ^ ADC10BUSY) & ((ADC10CTL0 & ADC10IFG)==ADC10IFG)) { // ensure conversion is complete
     ADC10CTL0 &= ~(ADC10IFG +ENC); // disable conversion and clear flag
     break;
   }
 }
 return ADC10MEM;
}

Share this post


Link to post
Share on other sites

i tried the code and it's fine, what's the problem.

 

i changed it so it will show led on and if i connect vcc to p1.0 and check for adc > 800, it did light up

 

note: i connect an led between gnd and p1.1

 

....
 P1DIR = 0x02;
 for (; {
   val = analogRead(PHOTOCELL);
   if (val > 800) P1OUT |= 0x02;
   else P1OUT &= ~0x02;
  }
 }

 

things to check.

 

. did u put a G2231 in launchpad, or is it a G2211 which has no adc

. did u remove the p1.0 jumper to disable to on board led.

Share this post


Link to post
Share on other sites

Wait did you connect a Piezo Vibration Sensor to it?

 

Yea I know it works now, thanks tho, but I am still trying to figure out how to read the Vibration Sensor...

 

Any ideas?

Share this post


Link to post
Share on other sites

it depends on the device, if say the sensor outputs a voltage very briefly (say less than a micro second), it might escape the adc, even running at 16 mhz the adc may be cannot capture voltage change in such a sort period of time.

 

are u trying to detect a "snap" on the sensor, or are u trying to detect a burst of vibration, if it is the later, u may want to attach a capacitor to "accumulate" your brief voltage change to detection.

 

i know nothing of the sensor except earlier in this thread that says it's +90v, but i think u may need to pre-condition it somewhat before the adc can detect it. i will google some and see if i can help.

 

but may be try short your Vcc to adc0 and make sure your firmware is good 1st.

Share this post


Link to post
Share on other sites

I can confirm that the code works. It's from my LaunchPad Glitchamin v2 (link http://blog.suspended-chord.info/?c=17) It's also been released under the GPLv3, which means that if it's used in any end project that you provide to anyone else, you must provide a copy of the license, and a copy of your modified source code, and also the new source is immediately covered by the GPLv3. I really don't care about this bit, just don't modify the code without providing it to the next person.

 

The piezo sensor you're using is probably just fluctuating wildly to minor vibrations. Some piezos are extremely sensitive. Try attaching a small weight to it, this stabilizes it and isolates rogue vibrations.

 

EDIT: Just heard from the #43oh irc that it works! NatureTM helped a bunch. Glad it's working for you.

Share this post


Link to post
Share on other sites

according to the manual, u might need to have an op-amp to condition your signal (it cited a car-passing-the-road application), or some sort of fet amplification.

 

http://www.sparkfun.com/datasheets/Sens ... echman.pdf

 

 

but i think u could make sure your code turn on the led and stays on (like a latch) so that u won't miss the read as at 16mhz and sampling in 100+khz rates, u can't really catch it w/ your eyes.

 

i am on easter time so good night everyone.

Share this post


Link to post
Share on other sites

I still see something messed up. When I hook up the transistor to the LED connected to P1.6, the LED does not turn off. Its very strange. Also, whenever I put my finger close to the ground wires, the LED lights up. Any ideas?

 

NatureTM: Are you sure that the code is working the way it is? It seems like it is while turning on the on-board LED's.

 

Also, Do I need to configure the timers on the pins in my code? I need to input a value on P1.0 and output an LED signal on P1.6.

 

The code that I am currently using:

 

#define __MSP430G2231__
#include 

#define PHOTOCELL BIT0 

int analogRead(unsigned int pin);

void main() {
 volatile unsigned int val;
 int threshold = 90;
 int delay;

 WDTCTL = WDTPW + WDTHOLD;
 P1DIR |= BIT6;
 //P1OUT = 0;

 ADC10CTL0 = ADC10ON + ADC10SHT_0 + SREF_0; // ACD10 on, 4 clock cycles per sample, Use Vcc/Vss references

 // change frequency to ~15.25mhz (DCO(15,3))
 BCSCTL1 |= (BIT0 + BIT1 + BIT2 + BIT3);
 DCOCTL &= ~(BIT7);
 DCOCTL |= (BIT6 + BIT5);

 for (; {
   val = analogRead(PHOTOCELL);
   if(val > threshold){
       P1OUT |= BIT6;
       delay = 0;
       while(delay < 1000)
               delay++;
       P1OUT &= ~BIT6;
   }
  }
 }

int analogRead(unsigned int pin) {
 ADC10CTL1 = ADC10SSEL_2 + pin;// Select MCLK and pin
 ADC10CTL0 |= ENC + ADC10SC; // enable and start conversion
 while (1) {
   if ((ADC10CTL1 ^ ADC10BUSY) & ((ADC10CTL0 & ADC10IFG)==ADC10IFG)) { // ensure conversion is complete
     ADC10CTL0 &= ~(ADC10IFG +ENC); // disable conversion and clear flag
     break;
   }
 }
 return ADC10MEM;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×