Comments system

การใช้งาน โมดูลวัดระยะทาง Ultrasonic Module Distance Measuring Transducer Sensor กับ Arduino ภายใน 3 นาที

การใช้งาน โมดูลวัดระยะทาง Ultrasonic Module Distance Measuring Transducer Sensor กับ Arduino ภายใน 3 นาที

การใช้งาน โมดูลวัดระยะทาง Ultrasonic Module Distance Measuring Transducer Sensor กับ Arduino

การวัดระยะทางโดยใช้ โมดูล Ultrasonic ร่วมกับ Arduino สามารถทำได้ง่าย อุปกรณ์โมดูล Ultrasonic มีความแม่นยำในการวัดระยะทาง การทำงานเป็นแบบคลื่นสะท้อนกลับแล้วนำมาคำนวน จึงเหมาะสำหรับมาใช้ในการหลบหลีกสิ่งกีดขวาง ตรวจจับวัตถุที่อยู่ในรัศมีที่ต้องการ

วิธีการต่อขา ใช้งาน โมดูลวัดระยะทาง Ultrasonic Module Distance Measuring Transducer Sensor กับ Arduino

  • Vcc - 5v
  • Gnd - Gnd
  • Trig - 13
  • Echo - 12
ตัวอย่างโคด Arduino ใช้งาน โมดูลวัดระยะทาง Ultrasonic Module Distance Measuring Transducer Sensor
const int pingPin = 13;
    int inPin = 12;

     
    void setup() {
      Serial.begin(9600);
    }
     
    void loop()
    {
      long duration, cm;
     
      pinMode(pingPin, OUTPUT);
     

      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);
      pinMode(inPin, INPUT);
      duration = pulseIn(inPin, HIGH);

      cm = microsecondsToCentimeters(duration);

      Serial.print(cm);
      Serial.print("cm");
      Serial.println();     
      delay(100);
    }
     
    long microsecondsToCentimeters(long microseconds)
    {
      // The speed of sound is 340 m/s or 29 microseconds per centimeter.
      // The ping travels out and back, so to find the distance of the
      // object we take half of the distance travelled.
      return microseconds / 29 / 2;
    }

แสดงความคิดเห็น

0 ความคิดเห็น