pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Hat Servo Arduino Tutorial

1. Preparation

2. Example Program

Case Description
Hat Servo is a servo motor module for StickC. The servo model is ES9251II, with a motion angle of 165° ± 10°. It uses PWM control to adjust the rotation angle. In this example, StickC-Plus2 outputs a PWM signal through GPIO 26 to control the rotation angle of the Hat Servo, and prints the current angle and status information on the M5StickC Plus2 screen and the computer's serial monitor.

Full Program

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <M5Unified.h>

const int servo_pin = 26;
const int freq = 50;
const int resolution = 16;  // Use high resolution

void setup() {
    M5.begin();
    Serial.begin(115200);
    ledcAttach(servo_pin, freq, resolution);
}

int angleToDuty(int angle) {
    int min_us = 600;
    int max_us = 2400;

    // Ensure angle is within range
    angle = constrain(angle, 0, 165);

    int pulse_us = map(angle, 0, 165, min_us, max_us);
    int duty = (pulse_us * 65535L) / 20000;

    return duty;
}

void loop() {
    M5.Display.fillRect(0, 100, 320, 60, BLACK);
    M5.Display.setCursor(20, 100, 2);
    M5.Display.print("Moving 0->165");
    Serial.println("Moving 0->165");

    for(int angle = 0; angle <= 165; angle ++) {
        int duty = angleToDuty(angle);
        ledcWrite(servo_pin, duty);

         // Display current angle on M5StickC-Plus2
        M5.Display.fillRect(20, 120, 100, 20, BLACK);
        M5.Display.setCursor(20, 120, 2);
        M5.Display.printf("Angle: %3d", angle);
        Serial.printf("Angle: %3d\n", angle);

        delay(50);  // Control rotation speed
    }

    delay(1000);

    M5.Display.fillRect(0, 100, 320, 60, BLACK);
    M5.Display.setCursor(20, 100, 2);
    M5.Display.print("Moving 165->0");
    Serial.println("Moving 165->0");

    for(int angle = 165; angle >= 0; angle --) {
        int duty = angleToDuty(angle);
        ledcWrite(servo_pin, duty);

        M5.Display.fillRect(20, 120, 100, 20, BLACK);
        M5.Display.setCursor(20, 120, 2);
        M5.Display.printf("Angle: %3d", angle);
        Serial.printf("Angle: %3d\n", angle);

        delay(50);  // Control rotation speed
    }

    delay(1000);
}

3. Compile and Upload

  1. Enter download mode: Different Stick devices require the corresponding driver to be installed before flashing. Different controllers may use different drivers and installation steps. For details, refer to the device program download tutorial list at the bottom of the Arduino IDE Getting Started Guide page to see the specific operating method for the corresponding device.

  2. Select the device port, click the Compile and Upload button in the upper-left corner of the Arduino IDE, and wait for the program to finish compiling and uploading to the device.

4. Hat Servo Current Angle and Status Information Display

This program will control the rotation angle of the Hat Servo, and print the current angle and status information on the StickC-Plus2 screen and the computer's serial monitor:

On This Page