Arduino Mega - DC Motor Shield

by DIYables in Circuits > Arduino

20 Views, 0 Favorites, 0 Comments

Arduino Mega - DC Motor Shield

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/arduino-mega/arduino-mega-dc-motor-shield

This build shows how to spin, reverse, and brake DC motors on an Arduino Mega using the Motor Shield Rev3 and the DIYables_DC_Motor library. The shield's L298P driver handles two independent motor channels, and the library gives you simple begin(), run(), and brake() calls plus current-sense readings, so you can get a motor turning in a few minutes.

Supplies

Wiring

Align the Motor Shield with the Arduino Mega's headers and press down until every pin is seated. Connect your DC motor leads to the Channel A screw terminals labeled on the PCB. Wire a 6-12V battery pack to the shield's power screw terminals - DC motors draw more current than USB can supply, so the motor runs from the battery while the Mega keeps running from USB independently.

How It Works

The shield carries an L298P dual full-bridge driver, a single chip that powers two independent motor channels. Each channel uses four control signals, and on the Mega these land on the same physical pins as on an Uno, so all other Mega pins stay free for sensors or displays. Pins A0 (Channel A) and A1 (Channel B) output a voltage proportional to motor current; on the Mega's 10-bit ADC that reads 0-1023, and multiplying by the shield's mV-per-amp factor gives you real current.

Code

Install DIYables_DC_Motor from the Libraries panel in the Arduino IDE, with the Mega selected as the board. In code, motor.begin() sets pin modes and engages the brake, motor.run(direction, speed) releases the brake and applies PWM in a chosen direction, and motor.brake() locks the shaft and zeros the PWM - that's the whole control flow.

arduino-mega-dc-motor-shield.ino - the sketch from the tutorial, unchanged.

#include <DIYables_DC_Motor.h>

DIYables_DC_Motor motor(MOTOR_CH_A);

void setup() {
motor.begin();
}

void loop() {
motor.run(MOTOR_FORWARD, 100);
delay(2000);

motor.brake();
delay(1000);
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/arduino-mega/arduino-mega-dc-motor-shield