This tutorial explains how to control the rotation of two servos based on the combination of input from a PIR motion sensor and a force sensitive resistor. The scenario set up here ithat when the PIR senses a person nearby and the FSR is not sensing pressure at the same time, the servos will spin 90 degrees from 0, and when the PIR senses a person leaves after showing up nearby, the servos will spin from 90 degrees back to 0.
The components you will need are:
- Arduino Uno
- Breadboard
- Square Force Sensitive Resistor
- PIR Motion Sensor
- Two Micro Servos
- 10k Ohm Resistor
- 14 Wires (11 Male to Male wires, 3 Female to Male Wires)
Setting Up the Hardware
- Use the 3 Female to Male wires to connect the PIR to the Arduino board, when you put the side of PIR with the 3 pin headers at bottom, from left to right, the three pin headers should be connected to GND, 5V, and Pin 2 respectively.
- Connect the left end of the FSR to Pin A0 as well as GND through a 10K Ohm resistor, and connect the right end to 5V.
- Connect the two micro servos to the 5V, GND and a pin on Arduino Board respectively. Here we connect the two servos to Pin 9 and Pin 10.
Not clear enough? Here is a schematic diagram and a photo for the wired circuit!
Now let’s check out the code. The key is to define variables to store the readings generated from the PIR and FSR, and use them in the if statement to create different conditions, so that under those conditions you could give the servos different instructions to spin!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
int fsrAnalogPin = 0; // Connect the Force Sensitive Resistor to analog 0 as input int fsrReading; // Define the variable to store the analog reading from the FSR resistor int inputPin = 2; // Connect the PIR sensor to pin 2 as input int pirState = LOW; // We start, assuming no motion detected int val = 0; // Define the variable for reading the sensing status of PIR #include <Servo.h> // Import servo library Servo servo1; // Create the servo control object Servo servo2; // Create the servo control object void setup() { Serial.begin(9600);// We'll send the motion sensing status to serial monitor. servo1.attach(9);//Tell Arduino to start sending control signals from pin 9 to the servo. servo2.attach(10);//Tell Arduino to start sending control signals from pin 10 to the servo. pinMode(inputPin, INPUT); // Declare the PIR sensor as input } void loop() { fsrReading = analogRead(fsrAnalogPin);//read the values output by the force sensitive resistor val = digitalRead(inputPin); // Read input value from PIR if (val == HIGH) { // Check if the input is HIGH, which means someone is around if(fsrReading == 0){ //We only want the servo start to spin when the force sensitive resistor is not pressed if (pirState == LOW) { // Check if last state of the motion sensor is LOW, which means no one was around Serial.println("Motion detected!");//Print the output change pirState = HIGH;//Update the state //Spin the two servos from 0 to 90 degrees. servo1.write(90); servo2.write(90); delay(1000);//Pause 1000 milliseconds between the two executions of the loop, allowing the servo to have time to move. } } } else { // When the input is LOW, which means no one is around if (pirState == HIGH){ //Check if the last state is HIGH, which means someone was around Serial.println("Motion ended!");//Print the output change pirState = LOW;//Update the state //Spin the two servos from 90 to 0 degrees. servo1.write(0); servo2.write(0); delay(1000);//Pause 1000 milliseconds between the two executions of the loop, allowing the servo to have time to move. } } } |