Arduino 101: Using a Knock Impact sensor (KY-031)

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn what a Knock Impact sensor (KY-031) is.
  • You will learn how the sensor works and how to use it with the arduino uno board.
  • You will learn the programming code used to make this sensor work.

Requirements

  • Arduino Uno

image.png

Image source

  • Knock Impact sensor (KY-031)

image.png

Image source

  • LED

image.png

Image source

  • Resistor (220 ohms)

image.png

Image source

  • Breadboard

image.png

Image source

  • Jumper Wires

image.png

Image source

  • USB type A to B cable

image.png

Image source

  • Computer

image.png

Image source

Software

  • Arduino Software/ Arduino IDE

image.png

Knowledge

  • Basic electronics and programming

Difficulty

  • Basic

Project description

  • This project uses a Knock Impact sensor (KY-031) that is connected to the arduino uno board. The KY-031 knock sensor is set to be the input device and detect shock in the area while the LED will be the output device to indicate the status of the sensor. Once shock has been identified it will then send a HIGH signal to the arduino uno board then turns the LED on.

Component description

Arduino Uno - is a type of arduino board that is commonly by specialist and hobbyist because of its robust design. It is equipped with a microcontroller board that is based on ATMega328P with 14 advanced I/O pins (6 are PWM outputs), 6 analog inputs, 16 Mhz quartz crystal, a power jack, a USB port, an ICSP header and a reset button.Reference

Knock Impact sensor (KY-031) - is a device that is capable of detecting shock or vibration in the area. It is a great alternative for the SW-420 vibration sensor. It consists of a spring sensor that will detect the vibration,a pull up resistor and three header pin that is allocated for the VCC (+5 Volts), GND (Ground) and S (Signal).

image.png

Image source

Tutorial Contents

Using a Knock Impact Sensor(KY-031)

Step 1: Gather the Parts

  • You can get the components in your nearby electronics shop or you can go ahead and order it online.
    image.png

Step 2: Construct the Circuit

Connect the Sources

  • Connect the 5V pin of the arduino uno board into the top/bottom slot of the breadboard. The connections here are horizontally connected making it an ideal for the sources.

  • Connect the GND pin of the arduino uno board into the top/bottom slot of the breadboard but make sure not to place them together to avoid shorting the arduino board.

image.png

Connect the LED

  • Connect the Anode(+) of the LED to a 220 ohm resistor that is connected to pin 2 of the arduino uno board. This resistor will protect the LED from over supply of current.
  • Connect the Cathode(-) of the LED to the common ground.

image.png

Connect the Sensor

  • Connect the VCC(+) pin of the knock sensor to the voltage source in the breadboard.
  • Connect the GND(-) pin of the knock sensor to the common ground in the breadboard.
  • Connect the S(signal) pin of the knock sensor to pin number 3 of the arduino uno board.

image.png

Step 3: Programming

  • Connect the arduino uno board to the computer using the USB type A to B cable.

image.png

  • Once connected, open the Arduino IDE and click Tools > Board: > then select the Arduino/Genuino Uno.

image.png

  • Copy the code below into your sketch in the arduino IDE.
int ledPin = 2 ;//declares the LED @pin 2
int knockPin = 3; //declares the Knock sensor @pin 3
int value;      //defines the variable value 
   
void setup ()
{
  pinMode (knockPin, INPUT) ; //sets the sensor as INPUT
  pinMode (ledPin, OUTPUT) ; //sets the LED as the OUTPUT 
}
void loop ()
{
  value = digitalRead (knockPin) ; //value will read the status of the knock sensor
  if (value == HIGH)  //if it reads a HIGH value
  {
    digitalWrite (ledPin, HIGH); //the LED will turn on
    delay(5000);   //duration of 5 seconds
  }
  else  //otherwise
  {
    digitalWrite (ledPin, LOW); //the LED is turned off
  }
}

1st code block: Declares the variables of the sensor and the LED.

int ledPin = 2 ;//declares the LED @pin 2
int knockPin = 3; //declares the Knock sensor @pin 3
int value;      //defines the variable value 

2nd code block: In the setup function it sets the knock sensor as the input while the LED as the output.

void setup ()
{
  pinMode (knockPin, INPUT) ; //sets the sensor as INPUT
  pinMode (ledPin, OUTPUT) ; //sets the LED as the OUTPUT 
}

3rd code block: In the loop function it sets the 'value' to read the status of the knock sensor. If it reads a HIGH value it will turn the LED on. Otherwise, the LED is turned off.

void loop ()
{
  value = digitalRead (knockPin) ; //value will read the status of the knock sensor
  if (value == HIGH)  //if it reads a HIGH value
  {
    digitalWrite (ledPin, HIGH); //the LED will turn on
    delay(5000);   //duration of 5 seconds
  }
  else  //otherwise
  {
    digitalWrite (ledPin, LOW); //the LED is turned off
  }
  • After typing the code in your sketch in the arduino IDE, click the Verify button to save it and start compiling the sketch. This will also check for any errors in the program.

image.png

  • Once saved and no errors are found, click the Upload button to start installing the program into the arduino uno board.

image.png

Step 4: Testing

  • After the programming is done, remove the arduino uno board from the computer and connect it with a 9 volts battery pack to power up the device.
  • Now, knock/shock the area around the sensor, this should make the LED turn on for 5 seconds because the sensor will detect the vibration in the surrounding and the LED is indicating the sensors status.
  • Now stop knocking the area, the LED should be turned off to indicate that the sensor is not detecting any vibration in the surrounding.

Curriculum

Check out my other arduino tutorials below:



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thank you very much👍

Hey @creon, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Following you!

Hey @ted7 I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63061.76
ETH 2602.70
USDT 1.00
SBD 2.75