8Servos Unit
This is the driver library for the 8Servos Unit. It is a 8-channel servo controller that can control up to 8 servos. It can be used to control the angles of the servos, set the pulse width, and set the mode of the servos.
Support the following products:
UiFlow2 Example
servo control
Open the cores3_servo_example.m5f2 project in UiFlow2.
This example controls the servo angle of the 8Servos Unit.
UiFlow2 Code Block:
Example output:
None
rgb control
Open the cores3_rgb_example.m5f2 project in UiFlow2.
This example controls the RGB LED of the 8Servos Unit.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
servo control
This example controls the servo angle of the 8Servos Unit.
MicroPython Code Block:
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")
Example output:
None
rgb control
This example controls the RGB LED of the 8Servos Unit.
MicroPython Code Block:
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")
Example output:
None
API
Servos8Unit
- class unit.servos8.Servos8Unit(i2c, address=37)
基类:
object
Create a servos 8 unit object.
- 参数:
i2c (machine.I2C | PAHUBUnit) – The I2C bus the servos 8 unit is connected to.
address (int) – The I2C address of the device. Default is 0x25.
- 抛出:
UnitError – If the servos 8 unit is not connected.
- UiFlow2 Code Block:
MicroPython Code Block:
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_12bit_adc_raw(channel)
Get the 12-bit ADC value of a specific channel.
- 参数:
channel (int) – The channel number (0 to 7) to get the 12-bit ADC value for.
- 返回:
The 12-bit ADC value (0 to 4095) of the specified channel.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_12bit_adc_raw(0)
- get_8bit_adc_raw(channel)
Get the 8-bit ADC value of a specific channel.
- 参数:
channel (int) – The channel number (0 to 7) to get the 8-bit ADC value for.
- 返回:
The 8-bit ADC value (0 to 255) of the specified channel.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_8bit_adc_raw(0)
- get_device_spec(mode)
Get the device specification. :param int mode: The mode to get the specification for. :return: The device specification value. :rtype: int
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_device_spec(0xFE)
- get_digital_input(channel)
Get the digital input value of a specific channel.
- 参数:
channel (int) – The channel number (0 to 7) to get the digital input for.
- 返回:
The digital input value (True or False) of the specified channel.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_digital_input(0)
- get_input_current()
Get the input current of the servos 8 unit.
- 返回:
The input current in Amperes.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_input_current()
- get_mode(channel)
Get the current mode of a specific channel.
- 参数:
channel (int) – The channel number (0 to 7) to get the mode for.
- 返回:
The mode of the specified channel.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_mode(0)
- get_rgb_led(channel)
Get the RGB LED color of a specific channel.
- 参数:
channel (int) – The channel number (0 to 7) to get the RGB LED color for.
- 返回:
The RGB color value (0x000000 to 0xffffff) of the specified channel.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_rgb_led(0)
- get_servo_angle(channel)
Get the servo angle of a specific channel.
- 参数:
channel (int) – The channel number (0 to 7) to get the servo angle for.
- 返回:
The servo angle (0 to 180) of the specified channel.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_servo_angle(0)
- get_servo_pulse(channel)
Get the servo pulse of a specific channel.
- 参数:
channel (int) – The channel number (0 to 7) to get the servo pulse for.
- 返回:
The servo pulse (500 to 2500) of the specified channel.
- 返回类型:
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.get_servo_pulse(0)
- set_i2c_address(addr)
Set the I2C address of the servos 8 unit.
- 参数:
addr (int) – The new I2C address (1 to 127) to set for the servos 8 unit.
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.set_i2c_address(0x25)
- set_mode(mode, channel)
Set the mode of a specific channel.
- 参数:
- 返回类型:
None
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.set_mode(0, 0)
- set_output_value(value, channel)
Set the digital output value of a specific channel.
- 参数:
- 返回类型:
None
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.set_output_value(1, 0)
- set_pwm_dutycycle(duty, channel)
Set the PWM duty cycle of a specific channel.
- 参数:
- 返回类型:
None
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.set_pwm_dutycycle(50, 0)
- set_rgb_led(rgb, channel)
Set the RGB LED color of a specific channel.
- 参数:
- 返回类型:
None
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.set_rgb_led(0xFF0000, 0)
- set_servo_angle(angle, channel)
Set the servo angle of a specific channel.
- 参数:
- 返回类型:
None
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.set_servo_angle(90, 0)
- set_servo_pulse(pulse, channel)
Set the servo pulse of a specific channel.
- 参数:
- 返回类型:
None
UiFlow2 Code Block:
MicroPython Code Block:
servos8_0.set_servo_pulse(1500, 0)