// Include libraries at the beginning
#include <Servo.h>
// Define your variables
const int pirPin = 0; // Pin for the PIR motion sensor (GPIO15)
Servo motor; // Create a Servo object to control the motor
const int motorPin = 1; // Pin for the servo motor (GPIO16)
int pirState = LOW; // Variable to store the current state of PIR (motion detected or not)
void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
Serial1.println("Hello, Raspberry Pi Pico!");
// Setup pins and start the motor object
pinMode(pirPin, INPUT); // Set PIR pin as input
motor.attach(motorPin); // Attach the servo motor to the motor pin
// Initialize the motor to a neutral position
motor.write(90); // 90 degrees = motor is stationary
Serial.begin(9600); // Start serial monitor for debugging
}
void loop() {
// put your main code here, to run repeatedly:
// Read the PIR sensor value
pirState = digitalRead(pirPin);
// Check if motion is detected (PIR gives a HIGH signal when motion is detected)
if (pirState == HIGH) {
Serial.println("Motion detected!");
// Move the motor to 180 degrees (or any desired angle)
motor.write(180);
} else {
Serial.println("No motion.");
// Move the motor back to 90 degrees (neutral position)
motor.write(90);
}
delay(10); // Delay for stability
}