8Servos Unit
这是 8Servos Unit 的驱动库。它是一款 8 通道舵机控制器,最多可控制 8 个舵机。可用于控制舵机角度、设置脉宽,以及设置舵机的模式。
支持以下产品:
UiFlow2 应用示例
伺服电机控制
在 UiFlow2 中打开 cores3_servo_example.m5f2 项目。
该示例控制 8Servos Unit 的舵机角度。
UiFlow2 代码块:
示例输出:
None
rgb 控制
在 UiFlow2 中打开 cores3_rgb_example.m5f2 项目。
该示例控制 8Servos Unit 的 RGB LED。
UiFlow2 代码块:
示例输出:
None
MicroPython 应用示例
伺服电机控制
该示例控制 8Servos Unit 的舵机角度。
MicroPython 代码块:
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from hardware import I2C 9from hardware import Pin 10from unit import Servos8Unit 11import time 12 13 14label0 = None 15i2c0 = None 16servos8_0 = None 17 18 19def setup(): 20 global label0, i2c0, servos8_0 21 22 M5.begin() 23 Widgets.fillScreen(0x222222) 24 label0 = Widgets.Label( 25 "8Servos Unit", 92, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18 26 ) 27 28 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000) 29 servos8_0 = Servos8Unit(i2c0, 0x25) 30 servos8_0.set_mode(3, 3) 31 servos8_0.set_mode(3, 7) 32 33 34def loop(): 35 global label0, i2c0, servos8_0 36 M5.update() 37 servos8_0.set_servo_angle(45, 3) 38 servos8_0.set_servo_angle(45, 7) 39 time.sleep(1) 40 servos8_0.set_servo_angle(150, 3) 41 servos8_0.set_servo_angle(150, 7) 42 time.sleep(1) 43 44 45if __name__ == "__main__": 46 try: 47 setup() 48 while True: 49 loop() 50 except (Exception, KeyboardInterrupt) as e: 51 try: 52 from utility import print_error_msg 53 54 print_error_msg(e) 55 except ImportError: 56 print("please update to latest firmware")
示例输出:
None
rgb 控制
该示例控制 8Servos Unit 的 RGB LED。
MicroPython 代码块:
1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD 2# 3# SPDX-License-Identifier: MIT 4 5import os, sys, io 6import M5 7from M5 import * 8from hardware import I2C 9from hardware import Pin 10from unit import Servos8Unit 11import time 12 13 14label0 = None 15i2c0 = None 16servos8_0 = None 17 18 19def setup(): 20 global label0, i2c0, servos8_0 21 22 M5.begin() 23 Widgets.fillScreen(0x222222) 24 label0 = Widgets.Label( 25 "8Servos Unit", 92, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18 26 ) 27 28 i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000) 29 servos8_0 = Servos8Unit(i2c0, 0x25) 30 servos8_0.set_mode(4, 0) 31 32 33def loop(): 34 global label0, i2c0, servos8_0 35 M5.update() 36 servos8_0.set_rgb_led(0xFF0000, 0) 37 time.sleep(1) 38 servos8_0.set_rgb_led(0x33FF33, 0) 39 time.sleep(1) 40 servos8_0.set_rgb_led(0x3366FF, 0) 41 time.sleep(1) 42 43 44if __name__ == "__main__": 45 try: 46 setup() 47 while True: 48 loop() 49 except (Exception, KeyboardInterrupt) as e: 50 try: 51 from utility import print_error_msg 52 53 print_error_msg(e) 54 except ImportError: 55 print("please update to latest firmware")
示例输出:
None
API参考
Servos8Unit
- class unit.servos8.Servos8Unit(i2c, address=37)
基类:
object创建一个 servos 8 unit 对象。
- 参数:
i2c (machine.I2C | PAHUBUnit) – servos 8 unit 所连接的 I2C 总线。
address (int) – 设备的 I2C 地址。默认为 0x25。
- 抛出:
UnitError – 如果未连接 servos 8 unit。
- UiFlow2 代码块:

MicroPython 代码块:
from hardware import I2C from unit import Servos8Unit i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000) servos8_0 = Servos8Unit(i2c0, 0x25)
- get_mode(channel)
获取指定通道的当前模式。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_mode(0)
- set_mode(mode, channel)
设置指定通道的模式。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.set_mode(0, 0)
- get_digital_input(channel)
获取指定通道的数字输入值。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_digital_input(0)
- set_output_value(value, channel)
设置指定通道的数字输出值。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.set_output_value(1, 0)
- get_8bit_adc_raw(channel)
获取指定通道的 8 位 ADC 值。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_8bit_adc_raw(0)
- get_12bit_adc_raw(channel)
获取指定通道的 12-bit ADC 值。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_12bit_adc_raw(0)
- set_servo_angle(angle, channel)
设置指定通道的舵机角度。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.set_servo_angle(90, 0)
- get_servo_angle(channel)
获取指定通道的舵机角度。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_servo_angle(0)
- set_servo_pulse(pulse, channel)
设置指定通道的舵机脉冲。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.set_servo_pulse(1500, 0)
- get_servo_pulse(channel)
获取指定通道的舵机脉冲。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_servo_pulse(0)
- set_rgb_led(rgb, channel)
设置指定通道的 RGB LED 颜色。
- 参数:
- 返回类型:
None
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.set_rgb_led(0xFF0000, 0)
- get_rgb_led(channel)
获取指定通道的 RGB LED 颜色。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_rgb_led(0)
- set_pwm_dutycycle(duty, channel)
设置指定通道的 PWM 占空比。
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.set_pwm_dutycycle(50, 0)
- get_input_current()
获取 servos 8 unit 的输入电流。
- 返回:
输入电流,单位为安培(A)。
- 返回类型:
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_input_current()
- get_device_spec(mode)
获取设备规格。:param int mode: 用于获取规格的模式。:return: 设备规格值。:rtype: int
UiFlow2 代码块:

MicroPython 代码块:
servos8_0.get_device_spec(0xFE)



