1.环境配置: 参考 Arduino IDE上手教程 完成IDE安装, 并根据实际使用的开发板安装对应的板管理, 与需要的驱动库。
2.使用到的驱动库:
3.使用到的硬件产品:
Unit HBridge v1.1
版本还支持读取当前电流值。
#include "M5Unified.h"
#include "Wire.h"
#include "M5UnitHbridge.h"
M5UnitHbridge driver;
uint8_t fw_version = 0;
bool motor_run = false;
void get_current_voltage()
{
// getMotorCurrent() function only support in Hbridge V1.1 version
if (fw_version >= 2) {
Serial.printf("%.2fA\r\n", driver.getMotorCurrent());
}
Serial.printf("%.2fV\r\n", driver.getAnalogInput(_12bit) / 4095.0f * 3.3f / 0.09f);
}
void setup()
{
M5.begin();
Serial.begin(115200);
M5.Display.setTextDatum(middle_center);
M5.Display.setFont(&fonts::lgfxJapanMinchoP_24);
while (!driver.begin(&Wire, HBRIDGE_I2C_ADDR, 2, 1, 100000L)) {
M5.Display.drawString("Unit HBridge init Fail!", M5.Display.width() / 2, M5.Display.height() / 2);
delay(1000);
}
fw_version = driver.getFirmwareVersion();
Serial.printf("Hbridge Firmware Version: %d\r\n", fw_version);
M5.Display.clear();
M5.Display.drawString("Unit HBridge init OK", M5.Display.width() / 2, M5.Display.height() / 2 - 20);
M5.Display.drawString("Touch to Start/Stop Motor", M5.Display.width() / 2, M5.Display.height() / 2 + 20);
}
void loop()
{
M5.update();
auto t = M5.Touch.getDetail();
if (t.wasClicked() || M5.BtnA.wasClicked()) {
motor_run = !motor_run;
M5.Display.clear();
if (motor_run) {
driver.setDriverDirection(HBRIDGE_FORWARD);
// driver.setDriverDirection(HBRIDGE_BACKWARD);
driver.setDriverSpeed8Bits(127);
M5.Display.drawString("Motor Running", M5.Display.width() / 2, M5.Display.height() / 2);
} else {
driver.setDriverDirection(HBRIDGE_STOP);
driver.setDriverSpeed8Bits(127);
M5.Display.drawString("Motor Stop", M5.Display.width() / 2, M5.Display.height() / 2);
}
}
get_current_voltage();
delay(10);
}
1.下载模式: 不同设备进行程序烧录前需要下载模式, 不同的主控设备该步骤可能有所不同。详情可参考 Arduino IDE上手教程 页面底部的设备程序下载教程列表, 查看具体的操作方式。
CoreS3长按复位按键(大约2秒)直到内部绿色LED灯亮起,便可松开,此时设备已进入下载模式,等待烧录。
使用 Unit HBridge 控制电机旋转与停止。