Lesson 5 Knock Sensor

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

A knock sensor (as shown below) is similar to the shock switch except that it’s more sensitive. It can feel small amplitude vibration.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Knock sensor module

– Several jumper wires

Experimental Principle

The principle is similar to that of the shock switch. Connect the knock switch sensor pin S to D3 to build a simple circuit. When the knock switch sensor inducts knock signals, the LED attached to pin 13 will light up.

Experimental Procedures

Step 1: Build the circuit

           Knock Sensor Module              SunFounder Uno

                                      S —————————————-D3

                                      –  ————————————- GND

                                      + ————————————— 5V

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno

Now, knock the sensor and the LED attached to pin 13 on the SunFounder Uno board will light up.

C Code

const int knockPin = 3; // the number of the knock sensor pin
const int ledPin = 13; // the number of the LED pin// variables will change:
int buttonState = 0; // variable for reading the knock sensor statusvoid setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the knock sensor pin as an input:
pinMode(knockPin, INPUT);
}void loop(){
// read the state of the knock sensor value:
buttonState = digitalRead(knockPin);// check if the knock sensor is pressed.
// if it is, the knock sensor is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, HIGH);
}
}