Skip to content

Example 22: Servo Control (SG90) ​

Experiment Overview ​

SG90 is a commonly used micro servo that controls rotation angle (0°~180°) via PWM signal. FastBee has a built-in PWM_SERVO peripheral type (type: 41), directly supporting angle control.

Hardware Wiring ​

Board LabelGPIO PinConnected Device
SIGNALGPIO17SG90 signal line (orange)

SG90 wiring: Red→5V, Brown→GND, Orange→GPIO signal line.

JSON Configuration Example ​

json
{
  "peripherals": [
    {
      "id": "servo_01",
      "name": "Servo SG90",
      "type": 41,
      "enabled": false,
      "pins": [13],
      "params": {}
    }
  ]
}

Peripheral Execution Linkage ​

Scenario 1: Remote Angle Setting (Platform Trigger) ​

Function: Control servo to specified angle via cloud platform command

Web Interface Configuration Steps ​

Step 1: Create Rule

  1. Click Peripheral Execution in the left menu
  2. Click New Rule button
  3. Fill in basic configuration:
    • Rule Name: Servo to 90 Degrees
    • Report Data: ✅ Enable
    • Enabled: ✅ Enable

Step 2: Configure Trigger

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Platform TriggerReceive cloud platform commands
    OperatorSelect Set ModeReceived data as action parameters

Step 3: Configure Action

  1. Click Add Action button

  2. Fill in:

    • Action Type: Select Set Servo
    • Target Peripheral: Select servo_01
    • Action Params: 90 (angle, 0-180)
  3. Click Save button


Scenario 2: Button Controls Servo (Event Trigger + Script) ​

Function: Each button press increases servo by 30 degrees, cycling 0-180

Web Interface Configuration Steps ​

  1. Create rule, name: Button Control Servo
  2. Trigger configuration:
    • Trigger Type: Select Event Trigger
    • Event Type: 100 (GPIO interrupt)
    • Event Source: key1
  3. Action configuration:
    • Action Type: Select Command Script
    • Action Params:
    var a=getVar('servo_angle',0);
    a=(a+30)%180;
    servoWrite('servo_01',a);
    setVar('servo_angle',a);
  4. Click Save

Scenario 3: Servo Scan Mode (Timer Trigger + Script) ​

Function: Servo auto sweeps back and forth (0°↔180°)

Web Interface Configuration Steps ​

  1. Create rule, name: Servo Scan
  2. Trigger configuration:
    • Trigger Type: Select Timer Trigger
    • Timer Mode: Select Fixed Interval
    • Interval Time: 50 (50ms refresh)
  3. Action configuration:
    • Action Type: Select Command Script
    • Action Params:
    var a=getVar('scan_a',0);
    var d=getVar('scan_d',1);
    a+=d*2;
    if(a>=180){a=180;d=-1;}
    if(a<=0){a=0;d=1;}
    servoWrite('servo_01',a);
    setVar('scan_a',a);
    setVar('scan_d',d);
  4. Click Save

SG90 Features ​

FeatureParameterDescription
Angle Range0° ~ 180°Standard servo
Supply Voltage4.8V ~ 6VRecommend 5V
PWM Frequency50HzStandard frequency
Pulse Width Range500~2500μsCorresponds to 0-180°
Torque1.8kg·cmLight load

Notes ​

  1. Power: SG90 needs 5V/500mA power, do not power from ESP32 3.3V
  2. Angle Range: SG90 range 0°~180°, exceeding will damage gears
  3. PWM Frequency: Servo standard PWM frequency 50Hz, pulse width 500~2500μs
  4. Jitter: Lower quality servos may jitter at certain angles, fine-tune pulse width range
  5. Torque: SG90 torque 1.8kg·cm, excessive load will cause stall and heating
  6. Multiple Servos: Multiple servos need independent power supply to avoid power fluctuations

FastBee | fastbee.cn