MG90D Servos with Arduino

2025-Mar-12, Wednesday 12:00 pm
[personal profile] chebe
Take 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 write() would only sometimes work.

Illustration of Arduino Mega board, with 9V power source, connected to a servo with power through a breadboard with capacitor, and connected to Digital/PWM pin 09.

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
}
(will be screened)
(will be screened if not validated)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

Profile

chebe: (Default)
chebe

June 2025

M T W T F S S
      1
23 45678
9101112131415
16171819202122
23242526272829
30      

Expand Cut Tags

No cut tags

Style Credit