Bird
Raised Fist0
Arduinoprogramming~20 mins

Servo motor control with Servo library in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Servo Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output behavior of this servo control code?

Consider this Arduino code controlling a servo motor. What will the servo do when this code runs?

Arduino
 #include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);
  myServo.write(0);
}

void loop() {
  for (int pos = 0; pos <= 180; pos += 30) {
    myServo.write(pos);
    delay(500);
  }
}
AThe servo moves continuously from 0 to 180 degrees without stopping.
BThe servo stays at 0 degrees and does not move.
CThe servo moves from 180 degrees to 0 degrees in steps of 30 degrees.
DThe servo moves from 0 degrees to 180 degrees in steps of 30 degrees, pausing 0.5 seconds at each step, then repeats.
Attempts:
2 left
💡 Hint

Look at the for loop and the delay inside loop().

❓ Predict Output
intermediate
2:00remaining
What error does this Arduino code produce?

Look at this code snippet controlling a servo. What error will it cause when compiled?

Arduino
#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);
  myServo.write(90);
}

void loop() {
  myServo.write(200);
  delay(1000);
}
ANo error; the servo will move to 200 degrees.
BCompilation error: 'write' argument out of range causes error.
CWarning: servo angle should be between 0 and 180 degrees, but code compiles and runs.
DRuntime error: servo ignores the 200-degree command and stays at 90 degrees.
Attempts:
2 left
💡 Hint

Check the valid range for servo angles in the Servo library.

🔧 Debug
advanced
2:00remaining
Why does this servo code not move the servo as expected?

This code is supposed to sweep the servo from 0 to 180 degrees and back. Why does the servo stay still?

Arduino
#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);
}

void loop() {
  for (int pos = 0; pos <= 180; pos++) {
    myServo.write(pos);
  }
  for (int pos = 180; pos >= 0; pos--) {
    myServo.write(pos);
  }
}
AThe code lacks delay, so servo commands are sent too fast to move physically.
BServo is not attached to pin 9 correctly, causing no movement.
CThe for loops have incorrect conditions causing infinite loops.
DServo library is not included, so write() does nothing.
Attempts:
2 left
💡 Hint

Think about how fast the servo can move and how the code sends commands.

📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this servo code?

Find the option that corrects the syntax error in this Arduino sketch.

Arduino
#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9)
  myServo.write(90);
}

void loop() {
  // empty loop
}
AAdd a semicolon after myServo.attach(9);
BChange myServo.attach(9) to myServo.attach(9); myServo.write(90);
CRemove the myServo.write(90) line.
DAdd parentheses after loop: void loop()() { }
Attempts:
2 left
💡 Hint

Look carefully at the end of the line with myServo.attach(9).

🚀 Application
expert
2:00remaining
How many distinct positions does this servo sweep cover?

This code sweeps a servo motor from 0 to 180 degrees in steps of 45 degrees, then back to 0. How many distinct positions does the servo move to in one full sweep cycle?

Arduino
#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);
}

void loop() {
  for (int pos = 0; pos <= 180; pos += 45) {
    myServo.write(pos);
    delay(500);
  }
  for (int pos = 135; pos >= 0; pos -= 45) {
    myServo.write(pos);
    delay(500);
  }
}
A8 positions
B5 positions
C9 positions
D10 positions
Attempts:
2 left
💡 Hint

Count the positions in the first loop and the second loop, then combine without duplicates.

Practice

(1/5)
1. What is the main purpose of the Servo library in Arduino?
easy
A. To control servo motors easily by setting angles
B. To read temperature sensors
C. To connect to Wi-Fi networks
D. To display text on an LCD screen

Solution

  1. Step 1: Understand the Servo library role

    The Servo library is designed to control servo motors by sending signals to set their position.
  2. Step 2: Compare options with library purpose

    Only To control servo motors easily by setting angles matches the library's function of controlling servo motors by setting angles.
  3. Final Answer:

    To control servo motors easily by setting angles -> Option A
  4. Quick Check:

    Servo library controls servo motors = D [OK]
Hint: Servo library controls motors by angle commands [OK]
Common Mistakes:
  • Confusing Servo library with sensor or display libraries
  • Thinking it manages Wi-Fi or other hardware
  • Assuming it reads data instead of controlling motors
2. Which of the following is the correct way to attach a servo to pin 9 using the Servo library?
easy
A. servo.attach(9);
B. servo.attachPin(9);
C. attachServo(9);
D. servo.connect(9);

Solution

  1. Step 1: Recall the Servo attach syntax

    The Servo library uses the method attach(pinNumber) to connect the servo to a pin.
  2. Step 2: Match the correct method call

    Only servo.attach(9); matches the correct syntax.
  3. Final Answer:

    servo.attach(9); -> Option A
  4. Quick Check:

    Attach servo with attach(pin) = C [OK]
Hint: Use attach(pin) to connect servo to pin [OK]
Common Mistakes:
  • Using incorrect method names like attachPin or connect
  • Forgetting the object name before attach
  • Using wrong syntax without parentheses
3. What will be the output behavior of this Arduino code snippet controlling a servo?
  #include <Servo.h>
  Servo myServo;
  void setup() {
    myServo.attach(10);
    myServo.write(90);
  }
  void loop() {
    // no code here
  }
medium
A. The servo continuously rotates back and forth
B. The servo moves to 90 degrees and stays there
C. The servo does not move at all
D. The code causes a compile error

Solution

  1. Step 1: Analyze the setup function

    The servo is attached to pin 10 and immediately set to 90 degrees using write(90).
  2. Step 2: Check the loop function

    The loop is empty, so no further commands change the servo position.
  3. Final Answer:

    The servo moves to 90 degrees and stays there -> Option B
  4. Quick Check:

    Servo set once in setup = stays at 90 [OK]
Hint: Servo holds last written angle if loop is empty [OK]
Common Mistakes:
  • Thinking servo moves continuously without commands
  • Assuming loop is needed to move servo once
  • Confusing servo rotation with continuous rotation motors
4. Identify the error in this code controlling a servo motor:
  #include <Servo.h>
  Servo servo1;
  void setup() {
    servo1.attach(5);
    servo1.write(180);
  }
  void loop() {
    servo1.write(0);
    delay(1000);
    servo1.write(180);
    delay(1000);
  }
medium
A. The delay function is missing a semicolon
B. The servo object is not declared correctly
C. No error; the code will run correctly
D. The servo is attached to an invalid pin number

Solution

  1. Step 1: Check servo declaration and attach

    The servo object is declared as Servo servo1; and attached to pin 5, which is valid.
  2. Step 2: Review write and delay usage

    The write() commands set angles 0 and 180 with delays of 1000 milliseconds, all correctly terminated with semicolons.
  3. Final Answer:

    No error; the code will run correctly -> Option C
  4. Quick Check:

    Correct syntax and logic = A [OK]
Hint: Check semicolons and valid pin numbers [OK]
Common Mistakes:
  • Assuming pin 5 is invalid without checking board specs
  • Missing semicolons after delay calls
  • Incorrect servo object declaration syntax
5. You want to sweep a servo motor smoothly from 0 to 180 degrees and back repeatedly. Which code snippet correctly implements this using the Servo library?
  #include <Servo.h>
  Servo myServo;
  void setup() {
    myServo.attach(6);
  }
  void loop() {
    // Which code goes here?
  }
hard
A. myServo.write(0); delay(1000); myServo.write(180); delay(1000);
B. myServo.attach(6); myServo.write(90);
C. while(true) { myServo.write(90); delay(500); }
D. for(int pos=0; pos<=180; pos+=1) { myServo.write(pos); delay(15); } for(int pos=180; pos>=0; pos-=1) { myServo.write(pos); delay(15); }

Solution

  1. Step 1: Understand smooth sweeping

    Sweeping smoothly means moving the servo in small steps with short delays between each step.
  2. Step 2: Analyze each option

    for(int pos=0; pos<=180; pos+=1) { myServo.write(pos); delay(15); } for(int pos=180; pos>=0; pos-=1) { myServo.write(pos); delay(15); } uses two for-loops to increment and decrement the angle by 1 degree with 15 ms delay, creating a smooth sweep. Options A, C, and D do not create smooth back-and-forth movement.
  3. Final Answer:

    for(int pos=0; pos<=180; pos+=1) { myServo.write(pos); delay(15); } for(int pos=180; pos>=0; pos-=1) { myServo.write(pos); delay(15); } -> Option D
  4. Quick Check:

    Smooth sweep needs incremental steps and delays = B [OK]
Hint: Use small angle steps with delay for smooth servo sweep [OK]
Common Mistakes:
  • Using only two positions without gradual steps
  • Missing delay causing jerky movement
  • Re-attaching servo inside loop causing errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard