0% found this document useful (0 votes)
171 views

KY-031 Hit-Knock Sensor Module ENG 2

The document provides instructions for setting up and using a KY-031 hit-knock sensor module with an Arduino Uno or Raspberry Pi. It includes specifications for the sensor module, pinout diagrams for connecting it, examples of Arduino and Python code to detect knocks or hits, and step-by-step instructions for installation and setup. The sensor module detects hits by producing a voltage pulse on its output pin when a spring inside makes contact with a metal connector.

Uploaded by

Jason Suthers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

KY-031 Hit-Knock Sensor Module ENG 2

The document provides instructions for setting up and using a KY-031 hit-knock sensor module with an Arduino Uno or Raspberry Pi. It includes specifications for the sensor module, pinout diagrams for connecting it, examples of Arduino and Python code to detect knocks or hits, and step-by-step instructions for installation and setup. The sensor module detects hits by producing a voltage pulse on its output pin when a spring inside makes contact with a metal connector.

Uploaded by

Jason Suthers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Welcome!

Thank you for purchasing our AZ-Delivery KY-031 Hit-Knock Sensor Module.
On the following pages, you will be introduced to how to use and set up this
handy device.

Have fun!
Table of Contents

Introduction......................................................................................................3
Specifications...................................................................................................4
The pinout........................................................................................................4
How to set-up Arduino IDE...............................................................................5
How to set-up the Raspberry Pi and Python....................................................9
Connecting the module with Uno...................................................................10
Sketch example..........................................................................................11
Connecting the module with Raspberry Pi.....................................................12
Python script...............................................................................................13

-2-
Introduction

The KY-031 hit-knock sensor module is a sensor that can detect knocking or
hits. The module has three pins: signal output pin, power supply or VCC pin
and the ground or GND pin. The sensor on-board the KY-031 module is made
of a spring that is in the transparent plastic casing. On one side, spring is
connected to the VCC pin. Between VCC pin and the spring, there is a
resistor. The other side of spring hangs freely near the metal connector. If the
sensor is being hit, the spring swings and it connects with the metal
connector. This closes the circuit, which results in a short voltage pulse at the
signal pin, which than can be picked up by a microcontroller as a trigger
event.

When the KY-031 module is not detecting a knock or a hit, the output on the
signal pin is in the HIGH state (default state). But when it detects a hit, the
output on the signal pin is in the LOW state for a short period of time.

-3-
Specifications

» Operating voltage range: from 3.3V to 5V DC


» Default output: HIGH (default state)
» Dimensions: 19 x 15 x 10mm [0.73 x 0.6 x 0.4in]

In order for a sensor to detect a knock or a hit, it has to be hit it a little bit
harder. Do not overdo it, because this could damage the plastic casing of the
sensor.

The pinout

The KY-031 hit-knock sensor module has three pins. The pinout diagram is
shown on the following image:

-4-
How to set-up Arduino IDE

If the Arduino IDE is not installed, follow the link and download the installation
file for the operating system of choice.

For Windows users, double click on the downloaded .exe file and follow the
instructions in the installation window.

-5-
For Linux users, download a file with the extension .tar.xz, which has to be
extracted. When it is extracted, go to the extracted directory and open the
terminal in that directory. Two .sh scripts have to be executed, the first called
arduino-linux-setup.sh and the second called install.sh.

To run the first script in the terminal, open the terminal in the extracted
directory and run the following command:
sh arduino-linux-setup.sh user_name
user_name - is the name of a superuser in the Linux operating system. A
password for the superuser has to be entered when the command is started.
Wait for a few minutes for the script to complete everything.

The second script called install.sh script has to be used after installation
of the first script. Run the following command in the terminal (extracted
directory): sh install.sh

After the installation of these scripts, go to the All Apps, where the
Arduino IDE is installed.

-6-
Almost all operating systems come with a text editor preinstalled (for
example, Windows comes with Notepad, Linux Ubuntu comes with
Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text
editors are perfectly fine for the purpose of the eBook.

Next thing is to check if your PC can detect an Arduino board. Open freshly
installed Arduino IDE, and go to:
Tools > Board > {your board name here}
{your board name here} should be the Arduino/Genuino Uno, as it
can be seen on the following image:

The port to which the Arduino board is connected has to be selected. Go to:
Tools > Port > {port name goes here}
and when the Arduino board is connected to the USB port, the port name can
be seen in the drop-down menu on the previous image.

-7-
If the Arduino IDE is used on Windows, port names are as follows:

For Linux users, for example port name is /dev/ttyUSBx, where x


represents integer number between 0 and 9.

-8-
How to set-up the Raspberry Pi and Python

For the Raspberry Pi, first the operating system has to be installed, then
everything has to be set-up so that it can be used in the Headless mode.
The Headless mode enables remote connection to the Raspberry Pi, without
the need for a PC screen Monitor, mouse or keyboard. The only things that
are used in this mode are the Raspberry Pi itself, power supply and internet
connection. All of this is explained minutely in the free eBook:
Raspberry Pi Quick Startup Guide

The Raspbian operating system comes with Python preinstalled.

-9-
Connecting the module with Uno

Connect the KY-031 module with the Uno as shown on the following
connection diagram:

KY-031 pin > Uno pin


D0 > D2 (external interrupt pin, MUST!) Blue wire
Middle pin (VCC) > 5V Red wire
GND > GND Black wire

- 10 -
Sketch example

#define HIT_PIN 2 // external interrupt pin


#define LED_PIN 13
volatile byte state = 0;

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(HIT_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(HIT_PIN), hit_led,
FALLING);
}

void loop() {
digitalWrite(LED_PIN, state);
delay(100);
}

void hit_led() {
state = !state; // toggling the led state
}

In the sketch, the external interrupt on digital pin 2 is used, to changle the
state of on-board LED of the Uno. You have to use this pin because exertnal
interrup feature is available only on digital pins 2 or 3.

Upload the sketch to the Uno, the on-board LED connected to digital pin 13
should toggle when the KY-031 module is hit.

- 11 -
Connecting the module with Raspberry Pi

Connect the KY-031 module with the Raspberry Pi as shown on the following
connection diagram:

KY-031 pin > Raspberry Pi pin


S > GPIO22 [pin 15] Blue wire
Middle pin (VCC) > 3V3 [pin 17] Red wire
GND > GND [pin 20] Black wire

- 12 -
Python script

import RPi.GPIO as GPIO


from time import sleep

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Pin setup for the module


Signal_PIN = 22
GPIO.setup(Signal_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def hit(channel):
print('Hit detected!')

GPIO.add_event_detect(22,GPIO.FALLING,callback=hit,bouncetime=100)

print('[Press CTRL + C to end the script!]')


try: # Main program loop
while True:
sleep(0.0001)

# Scavenging work after the end of the program


except KeyboardInterrupt:
print('\nScript end!')

finally:
GPIO.cleanup()

- 13 -
Save the script by the name knock.py. To run the script open terminal in the
directory where the script is saved and run the following command:
python3 knock.py

The result should look like the output on the following image:

To end the script press CTRL + C on the keyboard.

- 14 -
Script starts by importing two libraries, RPi.GPIO and time. Then the GPIO
pin names are set to BCM and all warnings regarding GPIO interfaces are
disabled.

Next, the pin mode of the GPIO pin 22 is set to input.

After that, the function called hit() is created. The function has one
argument and returns no value. The argument is not used, so it will not be
explained here. The function is executed on the digital signal change on the
hit pin. In the function, the print() function with message Hit detected
is used:
def hit(channel):
print('Hit detected!')

After this function, the interrupt routine is created with the following line of
code:
GPIO.add_event_detect(22,GPIO.BOTH,callback=hit,bouncetime=100)

Where number 22 represents the hit pin; GPIO.BOTH represents on which


edge of the digital signal the hit() function is executed, in this case on the
both signal changes (falling and rising); callback=hit sets which function
is executed on the signal change; bouncetime=100 represents how long is
the pause between the trigger (digital signal change) and the beginning of the
function execution, in this case it is 100 milliseconds. With this pause the
bouncing part of the signal is skipped and only the stable state of the digital
signal is read (after the bouncing).

- 15 -
Next, the try-except-finally block of code is created. In the try block
an indefinite loop is created (while True:). In the indefinite loop there is
one pause of 100 microseconds so that the loop itself does not block the
Raspberry Pi.

The except block of code is executed when CTRL + C on the keyboard is


pressed. This is called the keyboard interrupt. When the except block of
code is executed, the message Script end! is displayed in the terminal.

The finally block of code executes when the script ends, and when this
happens, a function called cleanup() is executed. This function is used to
release any previously used GPIO pins or interfaces.

- 16 -
Now it is the time to learn and make your own projects. You can do that with
the help of many example scripts and other tutorials, which can be found on
the internet.

If you are looking for the high quality products for Arduino and
Raspberry Pi, AZ-Delivery Vertriebs GmbH is the right company to get
them from. You will be provided with numerous application examples,
full installation guides, eBooks, libraries and assistance from our
technical experts.

https://az-delivery.de
Have Fun!
Impressum
https://az-delivery.de/pages/about-us

- 17 -

You might also like