The Keyes KY 031 Hit Sensor Knock or Hit Sensor Keys 031 is a variation on the vibration sensors, except that the sensitivity is much reduced. It is designed to register a hit or contact on for example a robot.
The sketch below turns the onboard LED connected to pin 13 ON when the sensor is gently tapped. To allow the sensor to be simply plugged into the Arduino header I have used pins 6 and 7 to supply power, the sensor output being connected to pin 7. However, the sensor can just as easily be powered from the Gnd and 5 volt pins.
/********************************************************
Keyes KY 031 Hit or Knock Sensor
Chris Rouse December 2015
This sensor is less sensitive than the vibration sensors
Connections::
- on Sensor Board to Arduino Gnd (or pin 5)
Middle pin on Sensor Board to Arduino 5 volts (or pin 6)
S pin on Sensor Board to Arduino pin 7
********************************************************/
void setup() {
pinMode(13, OUTPUT); // onboard LED
digitalWrite(13, LOW); // turn off onboard LED
pinMode(5, OUTPUT); // connect to Vibration Sensor S pin
pinMode(6, OUTPUT); // connect to Vibration Sensor MIDDLE pin
pinMode(7, INPUT); // connect to Vibration Sensor - pin
digitalWrite(5, LOW); // Gnd connection
digitalWrite(6, HIGH); // 5 volts connection
}
void loop() {
if (digitalRead(7) == LOW){
digitalWrite(13, HIGH); // turn ON LED
delay(500);
}
else{
digitalWrite(13, LOW);
}
}