11. Motion Sensor
Motion Sensor เป็นอุปกรณ์ตรวจจับการเคลื่อนไหว การทดลองนี้ใช้อุปกรณ์ AMN34112 ซึ่งเป็น Passive detection sensor with built-in amp ของ Panasonic
รูปที่ 1 ไดอะแกรมเวลาของ AMN34112
การทำงาน เมื่อมีการเคลื่อนไหว จะปรากฎพัลส์ 1 ออกมา ตามรูปที่ 1 การทำงานโดยละเอียดศึกษาได้จาก AMN34112.PDF และ bltn_eng_mp.pdf
การทดลอง
เนื่องจากอุปกรณ์มีราคาแพง (ราคาตัวละ 428 บาท) ควรตรวจสอบดูขาอุปกรณ์ ให้ชัดเจน (รูปที่ 2) ก่อนประกอบวงจร
รูปที่ 2 ตำแหน่งขาของ Motion sensor
รูปที่ 3. วงจรสำหรับการทดลอง
รูปที่ 4. การประกอบวงจรบนโปรโตบอร์ด
หมายเหตุ ตำแหน่งขา LCD มีการเปลี่ยนแปลง
โปรแกรมที่ 1 ตรวจสอบสัญญาณโดยอ่านค่าดิจิตอลที่พอร์ท D2
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
int ledPin = 13; // LED connected to digital pin 13
int motionPin = 2;
int val = 0; // variable to store the read value
void setup(void) {
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.setCursor(0, 0);
lcd.println("Motion Sensor");
}
void loop(void) {
val = digitalRead(motionPin); // read the input pin
if(val == 1) {
lcd.setCursor(0, 1);
lcd.print("OK");
}else{
lcd.setCursor(0, 1);
lcd.print("NO");
}
digitalWrite(ledPin, val); // sets the LED to the button's value
delay(1000); // maybe 750ms is enough, maybe not
}
โปรแกรมที่ 1 ตรวจสอบสัญญาณโดยใช้การอินเตอร์รัพท์ ที่ขา INT0 (D2)
การทดลองนี้ต้องใช้ความรู้เรื่องอินเตอร์รัพท์ สามารถดูได้จาก http://narong.ece.engr.tu.ac.th/micro/document/avr/index.php
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);
int ledPin = 13; // LED connected to digital pin 13
int motionPin = 2;
int val = 0; // variable to store the read value
void setup(void) {
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
attachInterrupt(0, blink, RISING);
lcd.setCursor(0, 0);
lcd.println("Motion Sensor");
}
void loop(void) {
if(val == 1) {
lcd.setCursor(0, 1);
lcd.print("OK");
}else{
lcd.setCursor(0, 1);
lcd.print("NO");
}
digitalWrite(ledPin, val); // sets the LED to the button's value
delay(1000); // maybe 750ms is enough, maybe not
}
void blink(){
val = !val;
}