Example 22: Servo Control (SG90)
About 2 min
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 Label | GPIO Pin | Connected Device |
|---|---|---|
| SIGNAL | GPIO17 | SG90 signal line (orange) |
SG90 wiring: Red→5V, Brown→GND, Orange→GPIO signal line.
JSON Configuration Example
{
"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
- Click left menu Peripheral Configuration → Switch to Peripheral Execution Management tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Servo to 90 Degrees - Report Data: ✅ Enable
- Enabled: ✅ Enable
- Rule Name:
Step 2: Configure Trigger
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Platform Trigger Receive cloud platform commands Operator Select Set Mode Received data as action parameters
Step 3: Configure Action
Click Add Action button
Fill in:
- Action Type: Select Set Servo
- Target Peripheral: Select
servo_01 - Action Params:
90(angle, 0-180)
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
- Create rule, name:
Button Control Servo - Trigger configuration:
- Trigger Type: Select Event Trigger
- Event Type:
100(GPIO interrupt) - Event Source:
key1
- 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); - Click Save
Scenario 3: Servo Scan Mode (Timer Trigger + Script)
Function: Servo auto sweeps back and forth (0°↔180°)
Web Interface Configuration Steps
- Create rule, name:
Servo Scan - Trigger configuration:
- Trigger Type: Select Timer Trigger
- Timer Mode: Select Fixed Interval
- Interval Time:
50(50ms refresh)
- 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); - Click Save
SG90 Features
| Feature | Parameter | Description |
|---|---|---|
| Angle Range | 0° ~ 180° | Standard servo |
| Supply Voltage | 4.8V ~ 6V | Recommend 5V |
| PWM Frequency | 50Hz | Standard frequency |
| Pulse Width Range | 500~2500μs | Corresponds to 0-180° |
| Torque | 1.8kg·cm | Light load |
Notes
- Power: SG90 needs 5V/500mA power, do not power from ESP32 3.3V
- Angle Range: SG90 range 0°~180°, exceeding will damage gears
- PWM Frequency: Servo standard PWM frequency 50Hz, pulse width 500~2500μs
- Jitter: Lower quality servos may jitter at certain angles, fine-tune pulse width range
- Torque: SG90 torque 1.8kg·cm, excessive load will cause stall and heating
- Multiple Servos: Multiple servos need independent power supply to avoid power fluctuations
