MG90D Servos with Arduino
2025-Mar-12, Wednesday 12:00 pmTake one Arduino Uno, and run some servos. Seemed straight-forward. Seemed.
Firstly, I couldn't find any Unos or Duemilanoves, I must have used them all. Guess it's time to finally break out the Mega 2560.
The servos are the MG90D micro servos. And using the Servo.h library.
Basic circuit set up like the sweep tutorial; on a breadboard, using a 100 µF capacitor (servo is 4.8V to 6V DC voltage), and external 9V power supply. Sweep worked fine, but setting exact angles via
After some poking around I learned that I needed to set the minimum and maximum pulse lengths for these particular servos. (The values from the product description on Adafruit. Also note, this servo will 'hold' the position even when no signal is sent.) And then I could set the degree of rotation (0°-180°) as expected.
Firstly, I couldn't find any Unos or Duemilanoves, I must have used them all. Guess it's time to finally break out the Mega 2560.
The servos are the MG90D micro servos. And using the Servo.h library.
Basic circuit set up like the sweep tutorial; on a breadboard, using a 100 µF capacitor (servo is 4.8V to 6V DC voltage), and external 9V power supply. Sweep worked fine, but setting exact angles via
write()
would only sometimes work.
Servo Circuit
Made with Fritzing
After some poking around I learned that I needed to set the minimum and maximum pulse lengths for these particular servos. (The values from the product description on Adafruit. Also note, this servo will 'hold' the position even when no signal is sent.) And then I could set the degree of rotation (0°-180°) as expected.
#include <Servo.h>
Servo myServo;
int minPulseWidth = 750; // µs == 0.75ms
int maxPulseWidth = 2250; // µs == 2.25ms
void setup()
{
myServo.attach(9, minPulseWidth, maxPulseWidth); // digital pin (with PWM) 9
}
void loop()
{
myServo.write(90); // angle in degrees, 0-180
delay(1000); // ms
myServo.write(180); // angle in degrees, 0-180
delay(1000); // ms
myServo.write(0); // angle in degrees, 0-180
delay(1000); // ms
}