Servo moves inconsistent amounts (time-sensitive)

My son is working on a science fair project that is due on Tuesday! I’ve had a lot of great help from these forums so far.

He is currently stuck with a problem that he thinks is servo-related. He programmed the servo motor to swing up when the motion sensor detects movement. The motor is attached to a piece of cardboard that covers and uncovers a clock (thus creating his idea of a motion-sensor clock). It’s all worked the way he planned except he cannot get the servo motor to swing a consistent amount.

When he first turns it on, it will start in the position that he puts it but each time he activates the sensor, the arm will move different amounts, thus, the clock is sometimes uncovered, sometimes partially covered. I’ve attached some pictures and video of the device and his current code.

Essentially he wants the motor to start horizontal, move 90 degrees with motion and then return to horizontal.


void setup() { //sets up code for loop section


void loop() { //repeats over and over

int pos; //varable for position

for (pos=50 ;pos<220;pos-=1) { //tells servo how to move and for how long
analogWrite(5,150); //tells servo how fast to move

for (pos=50;pos<220;pos+=1) { //tells servo how to move and for how long
analogWrite(5,150); //tells servo how fast to move


Hi @rosiegirl,
I can see on your pictures that the motion sensor and the dimmer and the servo are all connected to the output of the Arduino. And the program you use only puts a certain constant value on this output …
(you write a value of 150 which represents appr. 3 Volt, because a value of 255 would be 5 Volt).
Can you tell us what you want to achieve with the Arduino? In the setup you show us the arduino does in fact nothing…
I tried leaving out the Arduino, just clicking the following bits in a row:
power-motion sensor-dimmer-servo. Then you can move the servo by waving to the sensor and you can adjust the angle the servo is moving with the dimmer. When activated it moves and then waits a few seconds before returning to its rest postion. Is that what you want or do you want more? If you could tell a bit more about the Arduino I could maybe help you with the program…

Thanks @alexpikkert…I’m going to get my son to let you know what he’s trying to do. He’ll respond in a few minutes!

1 Like

I’ve also tried leaving out the Arduino. I don’t think it changes anything with the consistency of the servo motor, although it may be useful for programming it to do what I would like.

What I want to achieve is for the servo to move to its max position, then return to the starting position. Is it possible to do it using the Arduino with power-arduino-motion trigger-dimmer-servo? (The dimmer was previously used to slow the speed of the servo so it returns to approximately the same position each time).

Hi @rosiegirl,
If you only want to move the servo to its max position you do not need the Arduino I think. Just use the power bit connected to the motion sensor and then the servo.
When waving to the sensor the servo will move to its maximum position, waits for a few seconds and returns to its original position.
There are a few things however to take care of:
The motion sensor is very very sensitive so I think you should cover it until it works. And do not let it see the motion of your piece of cardboard else it will react on this movement also, I think you do not want this to happen…
And last but not least the powersupply must not be depleted, a low battery will be causing a lot of trouble (instable situation, the servo needs a lot of power and could send a lot of noise into the system)…
Let me know if you need more info, glad to help!

I do not understand the Arduino program, it only puts a stable value on the output D5…

Hi @alexpikkert,

Originally he wanted to create a clock that only turned on when it sensed motion but didn’t lose time. He managed to program quite a bit through the Arduino but couldn’t get past the powering off/losing time stuff. So he switched to wanting to cover and uncover the clock based on a motion sensor. He really wanted to be able to program something since he couldn’t finish his original idea and he feels like just using the bits isn’t “science fair” enough! Is there a way to program it to just rotate 90 degrees and back? Or anything else he can program through the Arduino?

Hi @rosiegirl,
With the Arduino you can do a lot of programming…
The only thing which can be programmed to do something in your project is the servo.
Maybe add a LED bit to the project ?
I can imagine something like this:
Connect the servo to an output that switches between zero and 90 degrees when the motion sensor is activated. For this the sensor must be connected to one of the inputs of the Arduino.
Then you can also program the time the sensor will stay on 90 degrees.
Adding a led bit (or the bargraph bit) to another output could be used as a warning light that flashes a few times before the servo moves back to warn that the servo will return to its resting position ?
Could your son write this program ?

Thanks @alexpikkert, I think he could but I’m not sure. Before I lose you, can you let me know what he would do and then I can at least help him troubleshoot if it goes wrong?

Any thoughts on why the motor doesn’t always return to the same resting spot?

You could test the servo and the power supply by using a pushbutton instead of the motion sensor.
Just click the following bits together:
This moves the servo between min and max and should always return to min.

lt is no problem for me to troubleshoot if he comes up with some programming, or do you want me to write it?
It’s his project ofcourse…

@alexpikkert he seems to understand how to code it but you lost him with:

Connect the servo to an output that switches between zero and 90 degrees when the motion sensor is activated. For this the sensor must be connected to one of the inputs of the Arduino.

He is set on the motion sensor because the problem he’s solving for the science fair is that he sometimes wants to know the time in the middle of the night without also staring at the time all night!

I think he’s confused as to what he’s actually coding in this case?

This is what I had in mind:
You can connect the servo to the Arduino output D5.
And connect the motion sensor to input A0. Then program this output to have a value of 150 when the A0 input is high (this is the case when the motion sensor is activated).
The value of 150 can be changed to a different value to let the servo move exactly 90 degrees.
I think ( without checking it) that a value of 255 is appr. 120 degrees)…

He’s going to go try it right now! Fingers crossed! He said he tried something similar at some point but the motion sensor didn’t work on pin A0. The servo is always on, regardless as to if the motion trigger is activated?

Let him make a program and I will help if it is not working as expected.
It is now 22:30 here in the Netherlands, so I will take a look at it tomorrow morning. Good luck!

You need to connect the power to the motion sensor and the motion sensor to A0.
Then what you write in the program will for sure move the servo…

Thank you so much! He’s working on it now (we’re in New York, so still day!)

1 Like

Based on the video, it looks like the servo is in swing mode. Just switch to turn mode, and all will be fine. (And the Arduino isn’t needed.)

1 Like

Hi again @alexpikkert,

My son posted the code he had in a separate post here: Programming servo with motion trigger


I am trying to program the servo motor to move 105 degrees and back every time the motion trigger is activated. However, the result is nothing happening. My code is below:

void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
pinMode (0, INPUT);
pinMode (5, OUTPUT);

void loop() {
// put your main code here, to run repeatedly:
int sensorValue= analogRead(A0);
if (sensorValue) 1023;
analogWrite (150, 5);

Is there anything noticeably wrong that may be causing nothing to happen. I’m not really sure how to do an “if” statement. Finally, should I have included the code below?

(hashtag)include Servo
Servo myServo;


I have read your posts that you are helping your son with his science fair project. Kudos to all of the effort he is putting into his project (and to you for all of the help)!

I am still learning myself and I don’t have the motion trigger bit, but I hope the following code will work for you (it works with both the dimmer and button in place of the motion trigger).

/*pin numbers*/
const int motionTriggerPin = A0;
const int servoPin = 5;

int motionTriggerValue = 0;

void setup() {
  pinMode(motionTriggerPin, INPUT);
  pinMode(servoPin, OUTPUT);

  analogWrite(servoPin, 0);

void loop() {
  motionTriggerValue = analogRead(motionTriggerPin);
  if(motionTriggerValue > 0) {
    //Serial.println("motion was triggered");
    analogWrite(servoPin, 200);
  } else {
      //Serial.println("motion is NOT being triggered");
      analogWrite(servoPin, 0);

Two things:

(1) Your son may have to change the information in the if statement to make it work with the motion trigger. If so, it might be helpful if he includes the lines I commented out and looks at the serial monitor to see what the values are that come from the motion trigger. (In order to see the values and after you get rid of the “//” comment slashes, you have to click on the little magnifying glass in the upper right hand corner of the coding area. Just another FYI, you have to close and reopen the window each time you load the program to your little bits or else you will get lots angry orange text and the program won’t load to the little bits. If it gets really mad then you may have to restart your computer for it to cooperate again…)

(2) I have been fighting with the servo all afternoon and I don’t understand why it twitches around before returning to the starting position, but if you let it do its twitching then it does eventually go to the correct position (I’ll let you know if I figure this issue out).

Good luck finishing up the project. If you all have any questions on the code, I can try to help. Also, here is a link to the reference page for how the if loop works:

I forgot to mention, you can change the value 200 in this code from 0 to 255:

analogWrite(servoPin, 200);

If you know the maximum degrees the servo turns, it should be possible to do a little math to get the exact angle you want, but for now I suggest just using trial and error to get it to turn to the position he wants.

Sorry, one more thing. I noticed in one of your pictures that the motion trigger was placed in the circuit after the arduino bit. I believe it should be set up like this: power (blue) to motion trigger (pink) to arduino (orange) to servo (green).

I think the pink bits should almost always come before the orange arduino bit and the green bits after the orange arduino bit. That way, the pink block can send its information through the orange arduino bit to your computer program, the computer program can use that information to make a decision or do some kind of calculation and then the computer program can send some information back through the orange arduino bit to the green bit telling it what to do.