Trossen Arduino Top Thread

Tutorial: Use a piezo buzzer as a BB sensor

  1. ScuD's Avatar
    ScuD ScuD is offline Positronic Brain
    Category
    How To, DIY
    Views
    18,771
    Replies
    0
     

    Use a piezo buzzer as a BB sensor

    Difficulty
    Easy!
    Estimated Time
    less than half an hour typically
    Skills Required
    some form of programming, soldering.
    Parts Required
    Piezo buzzer, 5.1v zener diode, 10k to 100k resistor (value isn't critical)
    This tutorial is a simple introduction on using a piezo buzzer as an impact sensor.

    A piezo buzzer is nothing more than a piezo crystal trapped between two metal plates. It works due to the piezoelectric effect.

    When an electric potential is applied to a piezo crystal, it will deform. The larger the potential, the larger the deformation.
    The reverse is also true, when a piezo crystal is deformed, it will generate electricity.

    So that's what we're using here, the BB hits the piezo buzzer, it deforms, and in turn generates an electric voltage that we can detect as a hit.

    Now the harder the piezo is hit, the higher the voltage will be that comes off it.
    The buzzer I used gave spikes of up to 10 volts when hit with a BB, even with it taped to a plastic box and not hitting it directly, so it's clear we need some kind of way to protect the inputs of our microcontroller, since they usually cannot handle more than the VDD voltage + a small amount (e.g. VDD+0.5V)

    For this reason, we'll be using a zener diode to make sure the voltage doesn't get above the VDD power of the microcontroller.
    I used a PIC with a VDD of 5 volts, so I'll be using a 5V1 zener (remember,there's a slight overhead, check your datasheet or take a value lower to be on the safe side, like 4v7).

    You'll need to put a resistor in series with the zener to make sure the piezo isn't shorted,since this will drop the generated voltage so low we can no longer detect it.
    The value isn't too important, just make sure it's high enough (eg. 10- 100kOhms).


    From this point on, we can basically treat the sensor as a switch.
    See, I told you it was easy!

    Here's a snippet of C code to show how it works:
    Code:
    */
    This code reads pin A0 to check for a high level input, ie. the sensor being triggered.
    Once triggered, it will check to see if it stays triggered for 200 iterations. If it is, a hit is registered.
    If not, the input is discarded.
    The output is a series of 8 leds on Port B, which start with a lit led on B7.
    After a successfull hit, the led will go out and the led on B6 will light.
    Looks something like this in a byte sequence:
    10000000 => hit => 01000000 => hit => 00100000
    /*
    
    
    void main(){
          unsigned char debounce;          //setup a 1-byte variable
          
          TRISB = 0x00;                         //make PORTB all outputs
          TRISA = 0xFF;                         //make PORTA all inputs
          CMCON = 0X07;                       //turn off comparators
          PORTB = 0x80;                        //load portb with 0x80
          
          while(1){
                   while(PORTA.F0) debounce++;   //increase counter
                                                              //while pin is high
    
                   if (debounce > 200){
                    PORTB = PORTB >> 1;             //Shift PORTB data one bit right
                    if(PORTB==0) PORTB = 0x80;    //reload PORTB if zero
                   }
                   debounce = 0;                 //reset debounce variable 
                                                       //if the debounce counter went
                                                       //over 200 counts, a hit is
          }                                           //registered and PORTB led
                                                      //indicator decreased
     }
    Don't laugh, I'm still learning C

    What this code does is continuously poll PORTA.0, when it goes high it increases a counter untill it goes low again.
    The count is an indication of how long the pulse was there, thus a simple software debounce.
    If the pulse was long enough, we rotate a bit on PORTB to the right, which gives a sort of knightrider-ish display of the hits that were registered.

    E-Z as pie.

    Here's the interesting part. As the generated voltage is somewhat proportional to the pressure involved, we can use analog measurements to see exactly how hard the sensor was hit, and as such we can set the sensitivity in software. But that might be for another tutorial

    For the hardware part, all you really need is a zener diode and a resistor to keep from frying your controller, everything else can be done in software quite easily.


    Last but not least, a crappy video of some boggled together hardware to show the thing in action:
    [ame="http://www.youtube.com/watch?v=GyoYF84mhfs"]YouTube - Piezo BB sensor[/ame]
    Attached Files
    • piezo-buzzer
    • BBsensor
    • bbsensor1
    Artificial Intelligence is no match for Natural Stupidity

    "For a list of all the ways technology has failed to improve life, press three" - Alice Kahn

    Resistance is futile! (if < 1)