Servo motors give you precise angular control instead of just spin - command an exact angle from 0° to 180° and the motor holds it there. That makes them a good fit for robotic arms, camera gimbals, or anything that needs to point somewhere and stay put. Here's how to wire one up to an Arduino MKR WiFi 1010 and move it with a few lines of code.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino MKR WiFi 1010 - Servo Motor
What you need
- Arduino MKR WiFi 1010
- Micro USB Cable
- Servo Motor
- Breadboard
- Jumper Wires
- DC Power Jack
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A servo has three wires. Brown or black is GND, connect it to the Arduino's GND pin. Red is VCC - for anything beyond a tiny micro servo, feed it from an external 5V supply rather than the board itself. Yellow or orange is the signal wire, which carries the PWM pulses that tell the servo what angle to go to; connect it to a digital pin such as D9 (the example sketch uses pin 14). If you power the servo externally, you still need to tie that supply's ground to the Arduino's GND - without this common ground the signal has no reliable reference and the servo will behave erratically or not move at all.
How it works
Inside a servo there's a small DC motor, a gearbox that trades speed for torque, and a potentiometer that reports the shaft's current angle back to a built-in control circuit. That circuit compares the angle you've commanded against the angle the potentiometer reports and drives the motor until they match, then holds position - this closed-loop feedback is what lets a servo sit at a precise angle instead of just spinning. You control it with a single PWM signal at 50Hz; the width of each pulse tells the servo which angle to move to, so no separate motor driver chip is needed the way a plain DC motor would require.
Code
The sketch uses Arduino's Servo library, which handles the PWM timing for you so you don't have to generate pulses by hand. You create a Servo object, call attach() on it with the pin number to start sending it control pulses, and then call write() with an angle from 0 to 180 whenever you want it to move. Since a servo moves as fast as it can to whatever angle you send, you can fake slower motion by writing a sequence of small angle changes with short delays between them instead of jumping straight to the target.
arduino-mkr-wifi-1010-servo-motor.ino
/*
* This Arduino MKR WiFi 1010 code was developed by newbiely.com
*
* This Arduino MKR WiFi 1010 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-servo-motor
*/
#include <Servo.h>
#define SERVO_PIN 14 // Arduino MKR WiFi 1010 pin connected to servo motor
Servo servoMotor;
void setup() {
servoMotor.attach(SERVO_PIN); // attaches the servo on Arduino Nano 33 IoT pin
}
void loop() {
// rotates from 0 degrees to 180 degrees
for (int pos = 0; pos <= 180; pos += 1) {
// in steps of 1 degree
servoMotor.write(pos);
delay(15); // waits 15ms to reach the position
}
// rotates from 180 degrees to 0 degrees
for (int pos = 180; pos >= 0; pos -= 1) {
servoMotor.write(pos);
delay(15); // waits 15ms to reach the position
}
}
Things that go wrong
If the servo twitches, stalls, or resets the Arduino when it moves, check your power setup first - small micro servos like the SG90 can sometimes run off USB power, but anything bigger needs its own 5V supply. The most common wiring mistake is forgetting to connect the external supply's ground to the Arduino's GND; without that shared reference the signal pin's voltage levels don't mean anything to the servo and its behavior becomes unpredictable.
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-servo-motor

Top comments (0)