pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Servo Kit 180°/360° Arduino Tutorial

1. Preparation

2. Example Program

  • In this tutorial, the main controller used is the CoreS3, paired with Servo Kit 180°/360°. The servo needs to be connected to the host through an adapter, as shown in the figure below. Modify the pin definitions in the program according to the actual wiring. Once connected, the control IO is 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 counterclockwise speed, COUNT_HIGH to maximum clockwise speed, and the midpoint value indicates stop. Values in COUNT_LOW ~ midpoint rotate counterclockwise (larger values = faster speed), while values in midpoint ~ COUNT_HIGH rotate clockwise (larger values = slower speed). In the example below, servo.writeMicroseconds(1500) stops the 360° kit.

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
#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);
}

3. Compile and Upload

  • 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 AtomS3R, press and hold the reset button (about 2 seconds) until the internal green LED turns on, then release. The device is now in download mode, ready for flashing.

  • 2. Select the device port, click the compile/upload button in the top left of Arduino IDE, and wait for the process to complete.

4. Servo Control Effect

Note
Do not manually rotate the servo during operation to avoid damaging the motor.
  • Servo Kit 180° will rotate sequentially to positions 0°, 90°, 180°, and 90°, repeating this cycle.

  • Servo Kit 360° will rotate at minimum speed counterclockwise, stop, rotate at minimum speed clockwise, stop, repeating this cycle.

On This Page