Environment setup: Refer to Arduino IDE Quick Start to complete IDE installation, and install the corresponding board package for your development board as well as the required driver libraries.
Required libraries:
Required hardware products:

G2.
The example below mainly uses the writeMicroseconds function to control the servo's rotation position, suitable for both Servo Kit 180° and Servo Kit 360°.COUNT_LOW and COUNT_HIGH correspond to the minimum and maximum pulse widths of the servo, respectively, while STEP defines the incremental step size for the pulse width, and SERVO_PIN specifies the digital pin connected to the servo's signal wire.
For Servo Kit 180°, COUNT_LOW usually corresponds to the 0° position of the servo, and COUNT_HIGH to the 180° position. After calling writeMicroseconds, the servo rotates to the position matching the given pulse width and holds there. In the example below, servo.writeMicroseconds(1500) moves the 180° kit to the 90° position and holds it.
For Servo Kit 360°, the value passed to writeMicroseconds controls rotation speed and direction: COUNT_LOW corresponds to maximum clockwise speed, COUNT_HIGH to maximum counterclockwise speed, and the midpoint value indicates stop. Values in COUNT_LOW ~ midpoint rotate clockwise (larger values = slower speed), while values in midpoint ~ COUNT_HIGH rotate counterclockwise (larger values = faster speed). In the example below, servo.writeMicroseconds(1500) stops the 360° kit.
#include "M5Unified.h"
#include "M5GFX.h"
#include "ESP32Servo.h"
// Macro definitions for servo control parameters
#define COUNT_LOW 500 // Minimum pulse width in microseconds (μs) for the servo (typically corresponds to 0° position)
#define COUNT_HIGH 2500 // Maximum pulse width in microseconds (μs) for the servo (typically corresponds to 180° position)
#define STEP 100 // Increment step for pulse width (controls how smoothly the servo moves between positions)
#define SERVO_PIN 2 // Digital pin connected to the servo motor's signal wire
Servo servo;
void setup() {
M5.begin();
servo.attach(SERVO_PIN, COUNT_LOW, COUNT_HIGH); // Attach the servo to the specified pin, with defined min (COUNT_LOW) and max (COUNT_HIGH) pulse widths
M5.Lcd.setFont(&fonts::FreeMonoBoldOblique12pt7b);
M5.Lcd.drawCenterString("SERVO", 160, 110);
}
void loop() {
servo.writeMicroseconds(500);
delay(2000);
servo.writeMicroseconds(1500); // 90° for 180° Kit, Stop for 360° Kit
delay(2000);
servo.writeMicroseconds(2500);
delay(2000);
servo.writeMicroseconds(1500); // 90° for 180° Kit, Stop for 360° Kit
delay(2000);
}1. Download mode: Devices must enter download mode before flashing programs; the steps may vary between controllers. For details, refer to the Arduino IDE Quick Start page's device program download tutorial list.
For CoreS3, press and hold the reset button (about 2 seconds) until the internal green LED lights up, then release it. The device has now entered download mode and is waiting for programming. The red light turns off after the button is released, confirming that the device is in download mode.