pdf-icon

Arduino 上手教程

Unit HBridge Arduino 使用教程

1.准备工作

2.案例程序

案例说明
Unit HBridge 是一款直流电机驱动模块,支持配置电机速度,方向,电压等。Unit HBridge v1.1版本还支持读取当前电流值。

电源开关

电机电源选择
Unit HBridge 内部集成DC/DC降压电路,可以将外部3.96端子输入的 6 ~ 12V 降低至 5V 用于适配不同电机的电源需求。同时提供了一个电源切换开关,可用于选择电机电源使用外部输入的 6 ~ 12V 或 DC/DC 降压后的5V。开关切至 HPWR 表示使用外部输入电压,切至5V表示使用DC/DC降压后的5V电压。 实际使用时,请根据电机的规格选择适合的驱动电压。

电机接线

完整程序

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#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);
}

3.编译上传

  • 1.下载模式: 不同设备进行程序烧录前需要下载模式, 不同的主控设备该步骤可能有所不同。详情可参考Arduino IDE上手教程页面底部的设备程序下载教程列表, 查看具体的操作方式。

  • CoreS3长按复位按键(大约2秒)直到内部绿色LED灯亮起,便可松开,此时设备已进入下载模式,等待烧录。

  • 2.选中设备端口, 点击Arduino IDE左上角编译上传按钮, 等待程序完成编译并上传至设备。

4.电机控制

使用 Unit HBridge 控制电机旋转与停止。

On This Page