• 这个教程示范了怎样用一个压力元件来监测震动,就是说,在门,桌子或者其他固体表面的敲击。
  • 压力元件是一个由形变(振动,音波,机械拉扯等引起的)产生电压的元件设备。同样地,当你在压力元件上加上一个电压,它会发生振动,并产生一个声调。压力元件可以同时用来产生音调和检测音调。
  • 这个程序通过 analogRead() 命令读取压力输出,并在一个涉及到模电转换(或者ADC)的过程里匹配这个电压从0-5V到一个数字范围0-1023。
  • 如果感应器输出超出这个固定的阈值,你的开发板将会通过串口发送字符串“Knock!”到电脑。
  • 打开串口监视器观察文本。
  • Arduino or Genuino 开发板
  • 压电陶瓷片
  • 1 MΩ 电阻
  • 坚硬的表面
  • 压电元件是极化的,意思是说电压往一个方向通过元件。连接黑色线(较低电压)到地,而红线(较高电压)连接到模拟引脚pin0。总的来说,并联一个1M欧姆电阻到压电元件来限制由压电元件产生的电压和电流,从而保护模拟输入引脚。
  • 可以不带塑料罩就用压电元件。这些看起来像一个金属片,而且比用作输入传感器更简单。当紧紧挤压或者黏合到感应表面时,压电传感器的工作效果最好。
  • 一个压电元件通过1MΩ电阻连接到模拟引脚pin0.

    这下面的代码里,输入压电数据和由使用者设置的阈值比较。尝试提高或者降低阈值来提高你感应器的整体灵敏度。

    /* Knock Sensor
       This sketch reads a piezo element to detect a knocking sound.
       It reads an analog pin and compares the result to a set threshold.
       If the result is greater than the threshold, it writes
       "knock" to the serial port, and toggles the LED on pin 13.
       The circuit:
        * + connection of the piezo attached to analog in 0
        * - connection of the piezo attached to ground
        * 1-megohm resistor attached from analog in 0 to ground
       created 25 Mar 2007
       by David Cuartielles 
       modified 30 Aug 2011
       by Tom Igoe
       This example code is in the public domain.
    // these constants won't change:
    const int ledPin = 13;      // led connected to digital pin 13
    const int knockSensor = A0; // the piezo is connected to analog pin 0
    const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not
    // these variables will change:
    int sensorReading = 0;      // variable to store the value read from the sensor pin
    int ledState = LOW;         // variable used to store the last LED status, to toggle the light
    void setup() {
      pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
      Serial.begin(9600);       // use the serial port
    void loop() {
      // read the sensor and store it in the variable sensorReading:
      sensorReading = analogRead(knockSensor);
      // if the sensor reading is greater than the threshold:
      if (sensorReading >= threshold) {
        // toggle the status of the ledPin:
        ledState = !ledState;
        // update the LED pin itself:
        digitalWrite(ledPin, ledState);
        // send the string "Knock!" back to the computer, followed by newline
        Serial.println("Knock!");
      delay(100);  // delay to avoid overloading the serial port buffer
    						
    				
    			

    最新评论

    PREVNEXT