Hack Kit Series: Closer look at SG90 Micro Servos

Hello once again,

In this post, I want to draw your attention to the couple of servos included in your awesome Meadow Hack Kit. These are pretty straight forward to set up using the Meadow.Foundation driver library.

For these peripherals, you’ll need to search and install the Meadow.Foundation.Servo NuGet package in your Meadow Application project.

Once added, all that’s left is learn how to wire them up on your board and instantiate a Servo object and start using them with no fuzz digging through some datasheets.

Testing Circuit sample

Servos can draw a lot of current, especially under load. Additionally, most common hobby servos require 6V. For this reason, an external power supply should be used when they’re used in practical applications.

For just testing however, they can be powered via the 5V rail on the Meadow board.

Circuit with Power Source

When powering with an external power source, you must connect the external GND to the GND rail on the Meadow or the PWM control signal will not work.

Code Sample

For the vast majority of Meadow.Foundation drivers, there’s at least two ways you can instantiate a peripheral:

Option 1: Passing the Device and Pins

var servo = new Servo(
    pwm: Device.Pins.D04, 
    config: NamedServoConfigs.SG90
);

int min = servo.Config.MinimumAngle;
int max = servo.Config.MaximumAngle;

for(int angle = min; angle < max; angle++)
{
    servo.RotateTo(angle);
    Thread.Sleep(100);
}

When you pass the Device along with the Device.Pins, the driver will basically create the required ports with default values. If you would like to customize or have more control on the behavior of your driver, you might want to go with Option 2.

Option 2: Passing the pre-configured Ports

var servo = new Servo(
    pwm: Device.CreatePwmPort(Device.Pins.D04), 
    config: NamedServoConfigs.SG90
);

int min = servo.Config.MinimumAngle;
int max = servo.Config.MaximumAngle;

for(int angle = min; angle < max; angle++)
{
    servo.RotateTo(angle);
    Thread.Sleep(100);
}

As you can see in this option, you can create a PWM port yourself and pass it to the driver as you create a servo object.

Named Servo Configurations

Servos are controlled via PWM signals and respond differently depending on the duration of pulse during the PWM duty cycle. Each servo has a particular pulse duration range that they operate under to set their angle, as in the case with fixed-range servos, or their rotation direction and speed, as the case with continuous rotation servos.

To simplify the use of the wide range of servos out there, we created a NamedServoConfig class that allows you to specify the parameters of the servo in use, such as MinimumPulseDuration and MaximumPulseDuration. This allows you to use nearly any servo with the Meadow.Foundation.Servo library.

For the servos included in the Hack Kit, you can simply use the NamedServoConfigs.SG90 configuration

That’s a wrap

This is all I wanted to show you in this quick post. If you want to read more in depth about the Servos API, feel free to check to our official API docs. Also, here’s some cool Hackster projects that use SG90 micro servos:

Happy hacking and see you on the next post!

Jorge