Page 1 of 2

knock sensor

Posted: Tue Mar 27, 2012 3:04 pm
by eduke
hi. can someone tell me how to use it as 'Knock Sensor' if the piezo is connected to D11? :oops:

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

Re: knock sensor

Posted: Fri Mar 30, 2012 7:21 pm
by eduke
no one knows? where is the moderator? fantastic support that ... : ( :cry:

Re: knock sensor

Posted: Tue Apr 03, 2012 3:48 pm
by eduke

Re: knock sensor

Posted: Wed Apr 04, 2012 12:08 am
by cef
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.

Re: knock sensor

Posted: Wed Apr 04, 2012 1:49 am
by jonoxer
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 ( 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); 

void loop() {
  knockValue = analogRead(knockSensor);     
  if (knockValue >= THRESHOLD) {
    pinState = !pinState;
    digitalWrite(ledPin, pinState);
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.

Re: knock sensor

Posted: Fri Apr 06, 2012 5:08 pm
by eduke
Thanks, I appreciate your response ...

do not worry, I understand, thanks;)

Re: knock sensor

Posted: Sat Apr 07, 2012 4:09 am
by jonoxer
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.


Re: knock sensor

Posted: Mon Apr 09, 2012 5:51 pm
by eduke
For now just trying to learn ... Thanks for your interest :)

Re: knock sensor

Posted: Thu Apr 12, 2012 4:49 am
by geekscape
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
// 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);


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;

Re: knock sensor

Posted: Thu Apr 12, 2012 5:46 am
by jonoxer
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.