Halo semuanya! Berjumpa dengan Miniblog dari Inzaghi's Blog! Kali ini kita akan membuat Program PIR Sensor dengan Servo Motor menggunakan ESP32 di Wokwi.
Sumber Tutorial : ESP32io.com
Pertama, merilah kita buka Situs Wokwi di sini (Wokwi.com). Kemudian, buatlah Project-nya.
Lalu, kita akan membuat Program Arduino PIR Sensor dengan Servo Motor menggunakan Wokwi.
Kode :
#include <Servo.h>// constants won't changeconst int MOTION_SENSOR_PIN = 6; // Arduino pin connected to motion sensor's pinconst int SERVO_PIN = 9; // Arduino pin connected to servo motor's pinServo servo; // create servo object to control a servo// variables will change:int angle = 0; // the current angle of servo motorint lastMotionState; // the previous state of motion sensorint currentMotionState; // the current state of motion sensorvoid setup() {Serial.begin(9600); // initialize serialpinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input modeservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo objectservo.write(angle);currentMotionState = digitalRead(MOTION_SENSOR_PIN);}
void loop() {lastMotionState = currentMotionState; // save the last statecurrentMotionState = digitalRead(MOTION_SENSOR_PIN); // read new stateif (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGHSerial.println("Motion detected!");servo.write(90);}elseif (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOWSerial.println("Motion stopped!");servo.write(0);}}
Gambar (Output) :
Mohon maaf apabila ada kesalahan sedikit pun pada Kode Program ini.
Terima Kasih 😀😊😘👌👍 :)
Tags
Kode Program