Environment Setup: Refer to Arduino IDE Getting Started Guide to complete IDE installation, and install corresponding board manager and required driver libraries based on the development board used.
Required Driver Libraries:
Required Hardware Products:




G2 (SDA) and 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);
}Download Mode: Different devices need to enter download mode before programming. This step may vary depending on the main controller. For details, refer to the device-specific programming tutorial list at the bottom of the Arduino IDE Getting Started Guide page.
For CoreS3: Press and hold the reset button (about 2 seconds) until the internal green LED lights up, then release. The device is now in download mode and ready for programming. The green LED turns off after the button is released, confirming that the device is in download mode.
