环境配置: 参考 Arduino IDE上手教程完成 IDE 安装,并根据实际使用的开发板安装对应的板管理,与需要的驱动库。
使用到的驱动库:
使用到的硬件产品:




G2 (SDA),G1 (SCL)。
#include "M5Unified.h"
#include "M5UnitPbHub.h"
M5UnitPbHub pbhub;
void setup() {
M5.begin();
Serial.begin(115200);
M5.Display.fillScreen(WHITE);
M5.Display.setTextColor(BLACK);
M5.Display.setFont(&fonts::FreeMonoBold9pt7b);
M5.Display.setCursor(0, 0);
if (!pbhub.begin(&Wire, UNIT_PBHUB_I2C_ADDR, 2, 1, 400000U)) {
Serial.println("Couldn't find Pbhub");
while (1) delay(1);
}
Serial.println("Pbhub v1.1 analog_read test");
}
void loop() {
// ch: 0-5
// index: 0-1
// status: 0/1
for (uint8_t ch = 0; ch < 6; ch++) {
// only one pin supports analog reading each channel
Serial.printf("ch:%d adc:%d\r\n", ch, pbhub.analogRead(ch));
M5.Display.printf("ch:%d adc:%d\r\n", ch, pbhub.analogRead(ch));
delay(500);
}
M5.Display.fillScreen(WHITE);
M5.Display.setCursor(0, 0);
}
#include "M5Unified.h"
#include "M5UnitPbHub.h"
M5UnitPbHub pbhub;
void setup() {
M5.begin();
Serial.begin(115200);
M5.Display.fillScreen(WHITE);
M5.Display.setTextColor(BLACK);
M5.Display.setFont(&fonts::FreeMonoBold9pt7b);
M5.Display.setCursor(0, 0);
if (!pbhub.begin(&Wire, UNIT_PBHUB_I2C_ADDR, 2, 1, 400000U)) {
Serial.println("Couldn't find Pbhub");
while (1) delay(1);
}
Serial.println("Pbhub v1.1 digital write read test");
}
void loop() {
// ch: 0-5
// index: 0-1
// status: 0/1
for (uint8_t ch = 0; ch < 6; ch++) {
for (uint8_t index = 0; index < 2; index++) {
pbhub.digitalWrite(ch, index, 1);
delay(300);
Serial.printf("ch:%d index:%d\r\n", pbhub.digitalRead(ch, index),
index);
M5.Display.printf("ch:%d index:%d\r\n", pbhub.digitalRead(ch, index),
index);
delay(300);
pbhub.digitalWrite(ch, index, 0);
Serial.printf("ch:%d index:%d\r\n", pbhub.digitalRead(ch, index),
index);
M5.Display.printf("ch:%d index:%d\r\n", pbhub.digitalRead(ch, index),
index);
delay(300);
}
}
M5.Display.fillScreen(WHITE);
M5.Display.setCursor(0, 0);
}
#include "M5Unified.h"
#include "M5UnitPbHub.h"
M5UnitPbHub pbhub;
void setup() {
M5.begin();
M5.Display.fillScreen(WHITE);
M5.Display.setTextColor(BLACK);
M5.Display.setFont(&fonts::FreeMonoBold9pt7b);
M5.Display.setCursor(0, 0);
Serial.begin(115200);
if (!pbhub.begin(&Wire, UNIT_PBHUB_I2C_ADDR, 2, 1, 400000U)) {
Serial.println("Couldn't find Pbhub");
while (1) delay(1);
}
Serial.println("Pbhub v1.1 PWM test");
}
void loop() {
// ch: 0-5
// index: 0-1
// status: 0/1
for (uint8_t ch = 0; ch < 6; ch++) {
for (uint8_t index = 0; index < 2; index++) {
for (uint8_t h = 0; h < 255; h++) {
pbhub.setPWM(ch, index, h);
Serial.printf("ch:%d index:%d PWM: %.1f%%\r\n", ch, index,
h / 255.0 * 100.0);
M5.Display.printf("ch:%d index:%d PWM: %.1f%%\r\n", ch, index,
h / 255.0 * 100.0);
delay(1000);
}
}
M5.Display.fillScreen(WHITE);
M5.Display.setCursor(0, 0);
}
delay(1000);
}

#include "M5Unified.h"
#include "M5UnitPbHub.h"
M5UnitPbHub pbhub;
void setup()
{
M5.begin();
Serial.begin(115200);
M5.Display.fillScreen(WHITE);
M5.Display.setTextColor(BLACK);
M5.Display.setFont(&fonts::FreeMonoBold9pt7b);
M5.Display.setCursor(0, 0);
if (!pbhub.begin(&Wire, UNIT_PBHUB_I2C_ADDR, 2, 1, 100000U)) {
Serial.println("Couldn't find Pbhub");
while (1) delay(1);
}
for (uint8_t ch = 0; ch < 6; ch++) {
pbhub.setLEDNum(ch, 74);
pbhub.setLEDBrightness(ch, 40);
}
}
void loop()
{
// ch: 0-5
// index: 0-1
// status: 0/1
for (uint8_t ch = 0; ch < 6; ch++) {
pbhub.fillLEDColor(ch, 0, 73, 0xff0000); // Red
Serial.printf("ch:%d color:red\r\n", ch);
M5.Display.printf("ch:%d color:red\r\n", ch);
}
delay(1000);
M5.Display.fillScreen(WHITE);
M5.Display.setCursor(0, 0);
for (uint8_t ch = 0; ch < 6; ch++) {
pbhub.fillLEDColor(ch, 0, 73, 0x00ff00); // Green
Serial.printf("ch:%d color:green\r\n", ch);
M5.Display.printf("ch:%d color:green\r\n", ch);
}
delay(1000);
M5.Display.fillScreen(WHITE);
M5.Display.setCursor(0, 0);
for (uint8_t ch = 0; ch < 6; ch++) {
pbhub.fillLEDColor(ch, 0, 73, 0x0000ff); // Blue
Serial.printf("ch:%d color:blue\r\n", ch);
M5.Display.printf("ch:%d color:blue\r\n", ch);
}
delay(1000);
M5.Display.fillScreen(WHITE);
M5.Display.setCursor(0, 0);
}

#include "M5Unified.h"
#include "M5UnitPbHub.h"
M5UnitPbHub pbhub;
void setup() {
M5.begin();
M5.Display.fillScreen(WHITE);
M5.Display.setTextColor(BLACK);
M5.Display.setFont(&fonts::FreeMonoBold9pt7b);
M5.Display.setCursor(0, 0);
Serial.begin(115200);
if (!pbhub.begin(&Wire, UNIT_PBHUB_I2C_ADDR, 2, 1, 400000U)) {
Serial.println("Couldn't find Pbhub");
while (1) delay(1);
}
Serial.println("Pbhub v1.1 Servo test");
}
void loop() {
// ch: 0-5
// index: 0-1
// status: 0/1
for (uint8_t ch = 0; ch < 3; ch++) {
for (uint8_t index = 0; index < 2; index++) {
pbhub.setServoAngle(ch, index, 90);
Serial.printf("ch:%d index:%d Servo Angle: 90\r\n", ch, index);
M5.Display.printf("ch:%d index:%d Servo Angle: 90\r\n", ch, index);
}
}
delay(2000);
for (uint8_t ch = 0; ch < 3; ch++) {
for (uint8_t index = 0; index < 2; index++) {
pbhub.setServoAngle(ch, index, 0);
Serial.printf("ch:%d index:%d Servo Angle: 0\r\n", ch, index);
M5.Display.printf("ch:%d index:%d Servo Angle: 0\r\n", ch, index);
}
}
delay(2000);
M5.Display.fillScreen(WHITE);
M5.Display.setCursor(0, 0);
}下载模式:不同设备进行程序烧录前需要进入下载模式,不同的主控设备该步骤可能有所不同。详情可参考Arduino IDE上手教程页面底部的设备程序下载教程列表,查看具体的操作方式。
CoreS3 长按复位按键 (大约 2 秒) 直到内部绿色 LED 灯亮起,便可松开,此时设备已进入下载模式,等待烧录。
