Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

ELEGOO MEGA · LESSON 29

DC Motors

Run a small DC motor through direction, coast, brake and PWM sequences.

Control an H-bridge without putting motor current through GPIO pins.

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
  • L293D is a dual H-bridge driver that lets logic control the direction and enable state of two small DC motors or one bipolar stepper. 16-pin DIP marked L293D, with notch/dot indicating orientation. Distinguish the D suffix from L293, whose protection/current details differ.
  • A small brushed DC motor spins a fan blade or light mechanical load under external drive. Small motor with a detachable plastic propeller; keep fingers and loose leads clear of the spinning blade.
  • 100 nF nonpolar ceramic capacitor stores electric-field energy. Local IC decoupling and short high-frequency supply disturbances are appropriate uses when voltage and circuit requirements are checked. Small non-polar disc, often marked 104; 104 means 100 nF, not 104 pF. Read the value and rating rather than estimating from body size.
  • Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.
  • The AstroAI meter measures electrical quantities through selectable probe circuits; its exact model, fuse arrangement and safety category remain unverified. AstroAI-branded handheld display with rotary selector and COM/voltage/current sockets. Read the actual model number and socket legends before choosing its manual.
  • ?
    1 ×Regulated 5.0 V current-limited DC supply with output leads, limit set to 0.35 A
    Unconfirmed · Availability and exact identity unconfirmed.

Assembly

  1. Remove the fan blade and disconnect all power; wire the L293D with its notch/pin numbers confirmed.
  2. Replace the pictured breadboard supply with regulated 5.0 V at a 0.35 A current limit; join negatives to Mega GND and keep supply + off Mega 5 V.
  3. Verify both L293D supply pins, all four ground pins and the two bypass capacitors; connect USB, then motor power.
Illustration © ELEGOO. All rights reserved.
Illustration © ELEGOO. All rights reserved. Open full-size ↗

Reviewed connections

FromToNote
External 5.0 V, limit 0.35 AL293D pins 16 VCC1 and 8 VCC2Separate from Mega 5 V; common negative/GND
Mega D5,D3,D4L293D pins 1 EN,2 1A,7 2ADo not interchange this lesson with the old relay sketch’s direction pins
L293D pins 3 and 6Motor terminalsLeave second bridge outputs 11/14 open
L293D 4,5,12,13 plus unused controls 9,10,15Common GNDAll four heat-sink ground pins must connect
100 nF from each of 16 and 8 to GNDLocal bypassSupply’s included leads do not count as jumper wires

Source-reviewed wiring. Hardware not bench-tested; firmware uploads have not been performed.

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600

Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.

main.cpp
#include <Arduino.h>
const uint8_t ENABLE_PIN = 5;  // PWM enable controls the L293D, never a motor winding directly.
const uint8_t DIRECTION_A = 3;
const uint8_t DIRECTION_B = 4;
void drive(bool forward, uint8_t duty) {
  digitalWrite(ENABLE_PIN, LOW);  // Disable before changing direction; do not reverse under drive.
  delay(100);  // Brief coast interval added to the original; it is not measured zero-speed feedback.
  digitalWrite(DIRECTION_A, forward ? HIGH : LOW);
  digitalWrite(DIRECTION_B, forward ? LOW : HIGH);
  analogWrite(ENABLE_PIN, duty);  // Duty is 0..255; it is not a measured shaft speed.
}
void setup() {
  pinMode(ENABLE_PIN, OUTPUT); pinMode(DIRECTION_A, OUTPUT); pinMode(DIRECTION_B, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  Serial.println("One way, then reverse");
  for (uint8_t i = 0; i < 5; ++i) {
    drive(true, 255); delay(500); drive(false, 255); delay(500);
  }
  digitalWrite(ENABLE_PIN, LOW); delay(2000);  // Disabled outputs let the motor coast.
  Serial.println("fast Slow example");
  drive(true, 255); delay(3000);
  digitalWrite(ENABLE_PIN, LOW); delay(1000);
  drive(false, 255); delay(3000);
  // Both inputs LOW with enable HIGH brakes the motor. The source forgot to lower DIRB.
  digitalWrite(DIRECTION_A, LOW); digitalWrite(DIRECTION_B, LOW); delay(2000);
  Serial.println("PWM full then slow");
  drive(true, 255);
  const uint8_t duties[] = {255, 180, 128, 50, 128, 180, 255};
  for (uint8_t duty : duties) { analogWrite(ENABLE_PIN, duty); delay(2000); }
  digitalWrite(ENABLE_PIN, LOW); delay(10000);
}

Source: SOURCE_RECONCILED. Compile: COMPILE_VERIFIED. Hardware: NOT_TESTED. Upload: NOT_PERFORMED.

Expected behavior

At 9600 baud the program announces direction, coast/brake and PWM phases, then pauses 10 s. Five forward/reverse pairs use 500 ms holds plus added 100 ms disabled intervals. Current limiting or driver voltage drop may prevent a particular motor from starting; disconnect promptly if it stalls.

Experiments & Challenges

  • Compare the 255,180,128,50 duty commands with unloaded motion; do not interpret PWM duty as measured RPM.

Troubleshooting

  • Motor does not start: check EN=5, inputs 3/4, supply current limit and L293D voltage drop; identify the motor before changing supply/current limits.