Skip to content

Instantly share code, notes, and snippets.

@slmcmahon
Created November 21, 2018 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slmcmahon/842f3d6a8c09ea1fd6f9db3c9dafaf4d to your computer and use it in GitHub Desktop.
Save slmcmahon/842f3d6a8c09ea1fd6f9db3c9dafaf4d to your computer and use it in GitHub Desktop.
Arduino Mini Motion Sensor with LCD
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Connection details for LCD:
// SDA --> A4
// SCL --> A5
// VCC --> 5V
// lcd is initialized with
// IC2C slave address: 0x27
// Columns: 20
// Rows: 2
// (optional) CharSize: LCD_5x8DOTS (default) or LCD5x10DOTS
LiquidCrystal_I2C lcd(0x27,20,2);
// Connection details for PIR:
// OUT --> D2
// VCC --> 5V
int pir = 2;
// Initial state
int state = LOW;
// Holds the state of the PIR (0 or 1)
int val = 0;
void setup()
{
lcd.init();
pinMode(pir, INPUT);
}
void loop()
{
val = digitalRead(pir);
if (val == HIGH) {
delay(100);
if (state == LOW) {
showMessage("Motion Detected!");
state = HIGH;
}
} else {
delay(200);
if (state == HIGH) {
hideMessage();
state = LOW;
}
}
}
void showMessage(char message[]) {
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(message);
}
void hideMessage() {
lcd.clear();
lcd.noBacklight();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment