[Diao Ye learns programming] Arduino hands-on (20) - KY-017 mercury switch center of gravity tilt sensor module 2

insert image description here

The reference to 37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensors and modules on hand, according to the concept of true knowledge (must be done by hands), for the purpose of learning and communication, I am going to try and do experiments one by one here, and will record them whether they are successful or not—Xiao Xiao The progress or problems that cannot be solved, I hope to be able to throw bricks and spark jade.

[Arduino] 108 sensor module series experiments (data + code + graphics + simulation)

Experiment 20: Mercury switch module tilting one-way dumping plate gravity rolling angle center of gravity tilt sensor KY-017

insert image description here

Instructions for using the module:
1. When the product is not tilted, the vibration switch is on, the output terminal outputs low level, and the green indicator light is on;
2. When the product is tilted, the vibration switch is disconnected, the output terminal outputs high level, and the green indicator light is on. The light is on;
3. The output terminal can be directly connected to the single-chip microcomputer, and the high and low levels can be detected through the single-chip microcomputer, so as to detect whether the environment is tilted, and play the role of an angle tilt alarm!

insert image description here

Schematic scene diagram of the experiment

insert image description here

/*

【Arduino】108种传感器模块系列实验(20)

   实验二十:水银开关传感器模块(KY-017)

  源代码

*/

 

void setup()

{

      pinMode(3,INPUT); 

      pinMode(13,OUTPUT); 

}

 

void loop() {

  if (digitalRead(3)) {

      digitalWrite(13,HIGH);

      delay(1000);

  }

  else {

      digitalWrite(13,LOW);  

  }

}


insert image description here

/*

【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)

实验二十:水银开关传感器模块(KY-017)

*/

 

// constants won't change. They're used here to 

// set pin numbers:

const int buttonPin = 2;     // the number of the pushbutton pin

const int ledPin =  13;      // the number of the LED pin

 

// variables will change:

int buttonState = 0;         // variable for reading the pushbutton status

 

void setup() {

  // initialize the LED pin as an output:

  pinMode(ledPin, OUTPUT);      

  // initialize the pushbutton pin as an input:

  pinMode(buttonPin, INPUT);     

}

 

void loop(){

  // read the state of the pushbutton value:

  buttonState = digitalRead(buttonPin);

 

  // check if the pushbutton is pressed.

  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {     

    // turn LED on:    

    digitalWrite(ledPin, HIGH);  

  } 

  else {

    // turn LED off:

    digitalWrite(ledPin, LOW); 

  }

}

Experiment program: View the status of the mercury switch module through the serial port
Experiment with open source graphics programming (Mind+, Mixly, learn by playing)

insert image description here
(2) The return status of the experimental serial port

insert image description here

Test the KY-017 mercury switch vibration sensor module
Reference open source code (Arduino):

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验程序七:测试KY-017水银开关震动传感器模块
*/

// 设置引脚号:
const int buttonPin = 2;     // 水银开关输入引脚的编号
const int ledPin =  13;      // LED 引脚的编号

// 变量会改变:
int buttonState = 0;    // 用于读取水银开关状态的变量

void setup() {
    
    
  // 将 LED 引脚初始化为输出
  pinMode(ledPin, OUTPUT);
  // 将水银开关引脚初始化为输入
  pinMode(buttonPin, INPUT);
}

void loop() {
    
    
  // 读取水银开关的状态
  buttonState = digitalRead(buttonPin);

  // 检查水银开关输入值是否为0
  // 如果是,则 buttonState 为 高电平
  if (buttonState == HIGH) {
    
    
    // 打开 LED
    digitalWrite(ledPin, HIGH);
  }
  else {
    
    
    // 否则关闭LED:
    digitalWrite(ledPin, LOW);
  }
}

Experimental scene graph

insert image description here
Precautions for experiment:
Mercury is poisonous to the human body and the environment, so when using the mercury switch, please be careful to avoid breaking it; when it is no longer in use, it should also be disposed of properly. Prevent the mercury switch from falling from a high place, avoid it from being in contact with hard objects, or being squeezed, which will cause the glass bubble to break.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132501621