Program PIR Sensor dengan LED menggunakan ESP32 di Wokwi

Halo semuanya! Berjumpa dengan Miniblog dari Inzaghi's Blog! Kali ini kita akan membuat Program PIR Sensor dengan LED menggunakan ESP32 di Wokwi.

Sumber Tutorial : ESP32io.com

Lalu, kita akan membuat Program Arduino PIR Sensor dengan LED menggunakan ESP32 di Wokwi.

Kode :

const int MOTION_SENSOR_PIN = 10;   // Arduino pin connected to the OUTPUT pin of motion sensor
const int LED_PIN           = 5;   // Arduino pin connected to LED's pin
int motionStateCurrent      = LOW; // current  state of motion sensor's pin
int motionStatePrevious     = LOW; // previous state of motion sensor's pin

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  pinMode(LED_PIN, OUTPUT);          // set arduino pin to output mode
}

void loop() {
  motionStatePrevious = motionStateCurrent;             // store old state
  motionStateCurrent  = digitalRead(MOTION_SENSOR_PIN); // read new state

  if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    digitalWrite(LED_PIN, HIGH); // turn on
  }
  else
  if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    digitalWrite(LED_PIN, LOW);  // turn off
  }
}

Gambar (Output) :


Mohon maaf apabila ada kesalahan sedikit pun pada Kode Program ini.

Terima Kasih 😀😊😘👌👍 :)

Post a Comment

Previous Post Next Post