Arduino uno programing

Please help me to make the programing code.
Actually am working on a project.
Am using arduino uno board, pir sensor and MG996R servo motor.

I want to rotate the servo motor 90 degrees and come back to starting position and then stop after pir sensor sense.

Am not trained programme

Please help me.
I will be grateful

We will not write code for you. We will help you to write code. You must make an attempt first to write the code. If you cannot make it work, post what you have and we will guide you. When you post code, always use code tags.

Start by looking at the example sketches in the IDE file menu. Perhaps there is an example sketch to use a servo that will make a good starting point for you, such as the "Sweep" example.

This tutorial is exactly what you are looking for: Arduino - Motion Sensor - Servo Motor tutorial

PaulRB:
We will not write code for you. We will help you to write code. You must make an attempt first to write the code. If you cannot make it work, post what you have and we will guide you. When you post code, always use code tags.

Start by looking at the example sketches in the IDE file menu. Perhaps there is an example sketch to use a servo that will make a good starting point for you, such as the "Sweep" example.

Ok paul....
I wrote a code.... But it is not working as i want..

I will post the code..
Thanks a lot for the quick response

IoT_hobbyist:
This tutorial is exactly what you are looking for: Arduino - Motion Sensor - Servo Motor tutorial

Thanks a lot buddy…
It is exactly what I want…

Reeve_barua:
I wrote a code.... But it is not working as i want..

When you post the program please also tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

...R

Robin2:
When you post the program please also tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.

…R

#include <Servo.h>

// constants won't change
const int MOTION_SENSOR_PIN = 7; // Arduino pin connected to motion sensor's pin
const int SERVO_PIN         = 9; // Arduino pin connected to servo motor's pin

Servo servo; // create servo object to control a servo

// variables will change:
int angle = 0;          // the current angle of servo motor
int lastMotionState;    // the previous state of motion sensor
int currentMotionState; // the current state of motion sensor

void setup() {
 Serial.begin(9600);                // initialize serial
 pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
 servo.attach(SERVO_PIN);           // attaches the servo on pin 9 to the servo object

 servo.write(angle);
 currentMotionState = digitalRead(MOTION_SENSOR_PIN);
}

void loop() {
 lastMotionState    = currentMotionState;             // save the last state
 currentMotionState = digitalRead(MOTION_SENSOR_PIN); // read new state

 if (currentMotionState == LOW && lastMotionState == HIGH) { // pin state change: LOW -> HIGH
   Serial.println("Motion detected!");
   servo.write(90);
 }
 else
 if (currentMotionState == HIGH && lastMotionState == LOW) { // pin state change: HIGH -> LOW
   Serial.println("Motion stopped!");
   servo.write(0);
 }
}

This is the code…
It is working with SG servo motor(rotates 90 degree and then goes back to starting position) but not working with MG servo motor… I have to use the MG servo motor.

The MG servo keeps rotating and doesn’t go back to initial position…
And also by this program it keeps rotating but i want to stop the motor after the sensor senses servo will move 90 degrees and then come back to starting position… And thn stop

Please help me

PaulRB:
When you post code, always use code tags.

If you have that MG996R servo connected to the Arduino 5V pin then that is one reason why it doesn’t work correctly. You need to use a separate power supply like 4 x rechargable AA batteries. The Arduino cannot supply enough current for that servo. You sometimes get away with it with a little SG90 but not with the much larger servo.

But if the servo really keeps on rotating i.e. round more than 360 degrees then you have the wrong type of servo. That sounds like a continuous rotation (360 degree) servo which cannot be accurately positioned like a normal servo. The MG996R is made in both versions and you may simply have the wrong one.

Steve