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 sensorconst int LED_PIN = 5; // Arduino pin connected to LED's pinint motionStateCurrent = LOW; // current state of motion sensor's pinint motionStatePrevious = LOW; // previous state of motion sensor's pinvoid setup() {Serial.begin(9600); // initialize serialpinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input modepinMode(LED_PIN, OUTPUT); // set arduino pin to output mode}void loop() {motionStatePrevious = motionStateCurrent; // store old statemotionStateCurrent = digitalRead(MOTION_SENSOR_PIN); // read new stateif (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGHSerial.println("Motion detected!");digitalWrite(LED_PIN, HIGH); // turn on}elseif (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOWSerial.println("Motion stopped!");digitalWrite(LED_PIN, LOW); // turn off}}
Gambar (Output) :
Mohon maaf apabila ada kesalahan sedikit pun pada Kode Program ini.
Terima Kasih 😀😊😘👌👍 :)
Tags
Kode Program