
Arduino Quick Start
The DC Motor is a brushed DC motor module with feedback, built around the LV8548MC motor driver. The M+ and M- inputs control the rotation direction. When M+ is HIGH and M- is LOW, the motor rotates forward. When M+ is LOW and M- is HIGH, it rotates in reverse. When both inputs are LOW, the motor brakes.
Connect control pins PIN21 and PIN22 to the module's M+ and M- inputs, respectively, and turn on the independent power supply.
This example uses buttons A, B, and C to control forward rotation, stop, and reverse rotation, while displaying the button history on the screen.
#include "M5Unified.h"
#include <M5GFX.h>
constexpr int kTextX = 10;
constexpr int kTextY = 50;
constexpr int kCharWidth = 12;
constexpr int kCharHeight = 16;
int cursorX = kTextX;
int cursorY = kTextY;
void appendChar(char c) {
M5.Lcd.setCursor(cursorX, cursorY);
M5.Lcd.print(c);
cursorX += kCharWidth;
if (cursorX > M5.Lcd.width() - kCharWidth) {
cursorX = kTextX;
cursorY += kCharHeight;
if (cursorY > 200) {
M5.Lcd.fillRect(0, kTextY, M5.Lcd.width(), 200 - kTextY, BLACK);
cursorX = kTextX;
cursorY = kTextY;
}
M5.Lcd.setCursor(cursorX, cursorY);
}
}
// The setup() function runs once each time the microcontroller starts
void setup() {
// init lcd, serial, but don't init sd card
M5.begin();
M5.Lcd.clear(BLACK);
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(65, 10);
M5.Lcd.println("DC motor example");
M5.Lcd.setCursor(30, 220);
M5.Lcd.println("Forward");
M5.Lcd.setCursor(140, 220);
M5.Lcd.println("stop");
M5.Lcd.setCursor(220, 220);
M5.Lcd.println("Reverse");
pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
M5.Lcd.setCursor(cursorX, cursorY);
}
// Add the main program code into the continuous loop() function
void loop() {
M5.update();
if (M5.BtnA.wasReleased()) {
appendChar('A');
digitalWrite(22, LOW);
digitalWrite(21, HIGH);
} else if (M5.BtnB.wasReleased()) {
appendChar('B');
digitalWrite(21, LOW);
digitalWrite(22, LOW);
} else if (M5.BtnC.wasReleased()) {
appendChar('C');
digitalWrite(21, LOW);
digitalWrite(22, HIGH);
}
}After uploading the program, you should see the following result.