Advertisement
belrey10

[KY-002] Shock Sensor

Nov 9th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.42 KB | Source Code | 0 0
  1. /*
  2.  * inventr.io 37 in 1 Sensor Kit (https://learn.inventr.io/product/37-in-1-sensor-kit/)
  3.  * Sensor Course (https://learn.inventr.io/course/sensor-training/)
  4.  *
  5.  * Lesson - [KY-002] Shock Sensor
  6.  *
  7.  * The KY-002 is a simple shock sensor module that detects vibrations or impacts.
  8.  * or movements in a variety of applications, such as detecting doors opening and
  9.  * closing or detecting knocks on a table.
  10.  *
  11.  * Code contributions:
  12.  *    David Schmidt (davids@inventr.io)
  13.  */
  14.  
  15. /* This project just needs a Digital pin.
  16.  * On the Hero (Arduino Uno compatible) we *could* use: D0-D13, A0-A5.
  17.  * Skip: A0-A5 (save for Analog),
  18.  *       D0/D1 (used by USB),
  19.  *       D2/D3 (save for interrupts),
  20.  *       D13 (used by LED_BUILTIN and SPI Clock),
  21.  *       D5, D6, D9, D10 and D11 (save for PWM)
  22.  *       D11 (SPI MOSI)
  23.  *       D12 (SPI MISO)
  24.  * Recommended for fewest conflicts:
  25.  *    D4, D7 or D8
  26.  */
  27. const uint8_t KY_002_PIN = 10;        // Good digital pin not used for other purposes
  28.  
  29. void setup() {
  30.   pinMode(LED_BUILTIN, OUTPUT);  // define LED as output interface
  31.   pinMode(KY_002_PIN, INPUT);    // output interface defines vibration sensor
  32. }
  33.  
  34. void loop() {
  35.   int val = digitalRead(KY_002_PIN);  // read digital interface is assigned a value of 3 val
  36.   if (val)                    // When the shock sensor detects a signal flash LED
  37.     digitalWrite(LED_BUILTIN, HIGH);
  38.   else
  39.     digitalWrite(LED_BUILTIN, LOW);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement