Bird
0
0
Arduinoprogramming~20 mins

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

Choose your learning style9 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.