пятница, 16 мая 2014 г.

Arduino. Датчик движения HC-SR501

Дело было вечером, делать было нечего... Решил пересмотреть свою копилку материалов по датчикам к Arduino. Наткнулся на схему проверки работоспособности датчика движения HC-SR501

Вместо рекомендуемого 4.5 кОм резистра воткнул 15 кОм (ну, что нашел). Датчик весело замигал светодиодом при движении. Много интересного про датчик можно прочитать, например, на этом ресурсе.

До подключения к плате осталось пара шагов - подключил выход Out к 8 разъему (Digital) на плате микроконтроллера. По совету, увиденному в одном из форумов подвязал Out резистором к земле. Номинал резистора оставил 15 кОм. Питание (+5В) и землю взял с платы.

Простейший скетч для проверки работы датчика с Arduino

int pirPin = 8;
int val;

void setup() {
    pinMode(pirPin,INPUT);
    Serial.begin(9600);
}

void loop() {
    val = digitalRead(pirPin); //read state of the PIR
    if (val == LOW) {
      Serial.println("No motion"); //if the value read is low, there was no motion
    }
    else {
      Serial.println("Motion!"); //if the value read was high, there was motion
    }
    delay(1000);
}

Когда все заработало, перецепил все на схему к уже собранному УСТРОЙСТВУ. Модифицировал скетч - при появлении сигнала от датчика включается подсветка экрана и горит 5 секунд.

#include Wire.h
#include DS3231.h
#include LiquidCrystal.h
/*
The circuit:
* 5V to Arduino 5V pin
* GND to Arduino GND pin
* SDA (data) to Analog #4
* SCL (clock) to Analog #5
*/

// Connect via i2c, default address #0 (A0-A2 not jumpered)
LiquidCrystal lcd(0);
DS3231 Clock;
bool h12=false;//True is 12-h, false is 24-hour.;
bool PM;
bool Century=false;

int pirPin = 8;
int val;
int msec;

void setup() {
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
lcd.clear();
lcd.setBacklight(LOW);
Wire.begin();
pinMode(pirPin,INPUT);
}

void loop() {
int second,minute,hour,date,month,year,temperature;

val = digitalRead(pirPin); //read state of the PIR
if (val == LOW){
lcd.setBacklight(LOW); //if the value read is low, there was no motion
}
else {
lcd.setBacklight(HIGH);
msec=millis()/1000;
}
if (millis()/1000-msec<5) lcd.setBacklight(HIGH);

year=Clock.getYear();
month=Clock.getMonth(Century);
date=Clock.getDate();
hour=Clock.getHour(h12,PM);
minute=Clock.getMinute();
second=Clock.getSecond();
temperature=Clock.getTemperature();

lcd.print("20");
lcd.print(year,DEC);
lcd.print('-');
if (month<10) lcd.print('0');
lcd.print(month,DEC);
lcd.print('-');
if (date<10) lcd.print('0');
lcd.print(date,DEC);

// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
if (hour<10) lcd.print('0');
lcd.print(hour,DEC);
lcd.print(':');
if (minute<10) lcd.print('0');
lcd.print(minute,DEC);
lcd.print(':');
if (second<10) lcd.print('0');
lcd.print(second,DEC);
lcd.print(' ');
lcd.print("Temp:");
lcd.print(temperature);
delay(500);
lcd.setCursor(0,0);
}

тут завтра будет фотография УСТРОЙСТВА


Комментариев нет:

Отправить комментарий