knock sensor

A shrunk down Leonardo-compatible board, thumb drive sized with native USB support. [Product info]
eduke
Posts: 8
Joined: Tue Mar 27, 2012 2:58 pm

knock sensor

Post by eduke » Tue Mar 27, 2012 3:04 pm

hi. can someone tell me how to use it as 'Knock Sensor' if the piezo is connected to D11? :oops:
thanks

p.d.hello to everyone as this is my first post :)

eduke
Posts: 8
Joined: Tue Mar 27, 2012 2:58 pm

Re: knock sensor

Post by eduke » Fri Mar 30, 2012 7:21 pm

no one knows? where is the moderator? fantastic support that ... : ( :cry:

eduke
Posts: 8
Joined: Tue Mar 27, 2012 2:58 pm

Re: knock sensor

Post by eduke » Tue Apr 03, 2012 3:48 pm

:shock:

cef
Freetronics Staff
Freetronics Staff
Posts: 106
Joined: Wed Nov 09, 2011 12:53 am

Re: knock sensor

Post by cef » Wed Apr 04, 2012 12:08 am

eduke wrote::shock:
Sorry about the spammers eduke. Spammer and the offending post now deleted.

Unfortunately it's a war with the bots and some days we have lots of luck, and others we don't.

I'm one of the moderators here, but I don't know the answer to your question (I'm a volunteer, don't work for Freetronics, and haven't used the knock sensor myself).

I know that Jon and Marc (the guys behind Freetronics) have been busy with new products that are in the pipeline. I'll poke them and see if I can get one of them to take some time out to answer your question.

User avatar
jonoxer
Freetronics Staff
Freetronics Staff
Posts: 298
Joined: Sat Oct 15, 2011 11:31 am
Location: Melbourne, Australia
Contact:

Re: knock sensor

Post by jonoxer » Wed Apr 04, 2012 1:49 am

Using a Piezo element as a knock sensor is quite simple: you just need to connect one side to GND, the other side to an analog input, place a 1M resistor across the piezo terminals and read from the analog input.

If you're using our SOUND module (http://www.freetronics.com/sound) you don't even need to add the 1M resistor, because we put it on the PCB ready to go. Just plug one pin of the SOUND module into GND, the other pin to an analog input, and you're sorted.

The example code below should get you going if you have one side of the SOUND module connected to analog input 0:

Code: Select all

int ledPin = 13;
int knockSensor = 0;               
byte knockValue = 0;
int pinState = LOW;
int THRESHOLD = 100;

void setup() {
 pinMode(ledPin, OUTPUT); 
 beginSerial(38400);
}

void loop() {
  knockValue = analogRead(knockSensor);     
  if (knockValue >= THRESHOLD) {
    pinState = !pinState;
    digitalWrite(ledPin, pinState);
    Serial.println("Knock");
  }
  delay(100);
}
Note: if you're lazy like me you can even cheat by plugging the SOUND module into two analog pins that are side by side, and using DigitalWrite to set one of the pins to LOW (0V) and reading from the other one. That way you can plug the module straight in and don't even need any jumper wires.

eduke
Posts: 8
Joined: Tue Mar 27, 2012 2:58 pm

Re: knock sensor

Post by eduke » Fri Apr 06, 2012 5:08 pm

jonoxer,
Thanks, I appreciate your response ...

cef,
do not worry, I understand, thanks;)

User avatar
jonoxer
Freetronics Staff
Freetronics Staff
Posts: 298
Joined: Sat Oct 15, 2011 11:31 am
Location: Melbourne, Australia
Contact:

Re: knock sensor

Post by jonoxer » Sat Apr 07, 2012 4:09 am

Oh yeah, I forgot to ask what project you're using this for. I love to hear about things people build, so if you get it working it'd be really cool if you posted some pics or something and a description.

Cheers
--
Jon

eduke
Posts: 8
Joined: Tue Mar 27, 2012 2:58 pm

Re: knock sensor

Post by eduke » Mon Apr 09, 2012 5:51 pm

hi,
For now just trying to learn ... Thanks for your interest :)

User avatar
geekscape
Posts: 7
Joined: Tue Nov 01, 2011 4:58 pm
Location: Melbourne, Australia
Contact:

Re: knock sensor

Post by geekscape » Thu Apr 12, 2012 4:49 am

To make the LeoStick into a working knock sensor, you have to jumper the piezo speaker output pin (D11) across to an analog input pin (A0). In the code, setting pin D11 HIGH and changing it's mode to INPUT means that it won't interfere with the analog read of pin A0.

The on-board piezo buzzer is a somewhat insensitive vibration sensor ... you need to give it a decent knock. I attach my LeoStick via a USB extension cable, to make hitting it against a desk for testing ... a bit easier. I've also tried a SparkFun SEN-09197 vibration sensor and it is much more sensitive.

The following code also provides a simple debounce mechanism, so that the knock sensing and count is more accurate ...

Code: Select all

// Simple knock sensor for Freetronics LeoStick
// http://www.freetronics.com/products/leostick
//
// The piezo crystal is connected to digital pin 11,
// which is fine for digital input, but not for analog input.
// You need to connect a jumper wire between digital pin 11
// and analog pin 0 for the following code to function.
//
// You can just directly connect a Sparkfun SEN-09197 with a
// 1 MOhm resistor in parallel between the analog pin 0 and
// ground ... to get a much more sensitive vibration sensor.

const int knockThreshold = 1000;  // built-in piezo buzzer
//const int knockThreshold = 10;  // SparkFun SEN-09197
const int timeThreshold  =  100;  // milliseconds

const byte pinKnock = 0;  //analog input
const byte pinPiezo = 11;

const byte pinGreen = 9;
const byte pinBlue  = 10;
const byte pinRed   = 13;

int  counter       = 0;
long lastKnockTime = 0;
int  ledState      = LOW;

void ledOutput(
  byte redValue,
  byte greenValue,
  byte blueValue) {

  digitalWrite(pinRed,   redValue);
  digitalWrite(pinGreen, greenValue);
  digitalWrite(pinBlue,  blueValue);
}

void setup(void) {
  digitalWrite(pinPiezo, HIGH);
  pinMode(pinPiezo, INPUT);

  pinMode(pinRed,   OUTPUT);
  pinMode(pinBlue,  OUTPUT);
  pinMode(pinGreen, OUTPUT);
  ledOutput(0, 0, 0);

  Serial.begin(38400);
}

void loop(void) {
  int knockValue = analogRead(pinKnock);

  if (knockValue >= knockThreshold) {  // debounce
    long timeNow = millis();

    if (timeNow > (lastKnockTime + timeThreshold)) {
      ledState = ! ledState;
      ledOutput(ledState, ledState, ledState);
      Serial.println(++ counter);
    }

    lastKnockTime = timeNow;
  }
}

User avatar
jonoxer
Freetronics Staff
Freetronics Staff
Posts: 298
Joined: Sat Oct 15, 2011 11:31 am
Location: Melbourne, Australia
Contact:

Re: knock sensor

Post by jonoxer » Thu Apr 12, 2012 5:46 am

setting pin D11 HIGH and changing it's mode to INPUT means that it won't interfere with the analog read of pin A0
Good tip, Andy.

Another way to do this is to electrically isolate the Piezo element from D11 entirely. I included a cut-track jumper specifically for that purpose on the bottom of the board: if you look near the USB tab you'll see a pair of gold contacts with the label "Sound Enable" next to it. There's a tiny track joining those two contacts, which links the Piezo to D11. Use a knife to cut the track, then you can solder a jumper wire from the gold contact on the "Sound Enable" side (not the "D11" side) to an analog input such as A0.

That way it doesn't matter what you do to D11 in software, it won't bias the analog input. And you can still use it for other purposes.
--
Jon
Attachments
LeoStick--isolate-d11.png

Post Reply