Replaced Arduion UNO with Arduino MEGA 2560 - Uh-oh.

I have a two-wheel "smart car" from a few years ago, that had two motors, six AA batteries, and an L298N motor driver board. It had an Arduino UNO board, and everything worked beautifully.

  • Write a sketch to go forward, it went forward.
  • Write a sketch to go backward, it went backward.
  • Program one motor forward and the other backward, and it pivoted on its axis, exactly as expected.

I wanted more I/O, so I took the Arduino UNO off and put on an Arduino MEGA 2560. And now I can't get it to go forward, backward, or anything else. The lights light up, but it just sits there.

On the UNO I used the two PWM outputs on ports to run the ENABLE lines for Motor A and Motor B.

But I noticed that on the MEGA, all the ports from 2 thru 13 are marked PWM on the board (are they really PWM?). So I changed the wires to the board, and port numbers in the sketch, to ports 2 and 3 respectively.

And the ports from 23 to 53 on the MEGA, are marked as DIGITAL ports on the board. (Are they?) So I used four of them, ports 23, 24, 25, and 26, to control Motor Direction 1 and 2 for Motors A and B. Again I changed the wires going from the motor driver card to the Arduino board, and also changed the port assignments in the sketch.

I figured this would run perfectly. Of course, it didn't.

Did I change the ports correctly when I went from the UNO to the MEGA?

Here's the code:

/* Modified 28Mar2016 for Arduino MEGA 2560 */

/*  Dual Motor Driver  */

/* List of **OLD** H-Bridge Servo Board Connections for the old Arduino UNO board
 
   Name        Color   Description
   ENA           BLU   PWM  for MOTORA to Arduino Port 5 (output PWM)
   MOTORA_DIR1   GRN   DIR1 for MOTORA to Arduino Port 2
   MOTORA_DIR2   YEL   DIR2 for MOTORA to Arduino Port 3
   MOTORB_DIR1   ORG   DIR1 for MOTORB to Arduino Port 4
   MOTORB_DIR2   RED   DIR2 for MOTORB to Arduino Port 7
   ENB           BRN   PWM  for MOTORB to Arduino Port 6 (output PWM)
*/

// Give names to computer ports for new Arduino MEGA 2560 board, showing 
// former and new connections

int EN_MOTOR_A=2;      // BLU connected to arduino's port X5X (output PWM) -- 2
int MOTOR_A_DIR1=23;   // GRN connected to arduino's port X2X -- 23             
int MOTOR_A_DIR2=24;   // YEL connected to arduino's port X3X -- 24
int EN_MOTOR_B=3;      // BRN connected to arduino's port X6X (output PWM) -- 3
int MOTOR_B_DIR1=25;   // ORG connected to arduino's port X4X -- 25
int MOTOR_B_DIR2=26;   // RED connected to arduino's port X7X -- 26

int led13=13;          // LED on Arduino UNO board, port 13

void setup()
{
  // Program motor-control pins to be inputs or outputs
  pinMode(EN_MOTOR_A,OUTPUT);      // Motor A (right motor) power - PWM
  pinMode(MOTOR_A_DIR1,OUTPUT);      // Motor A (right motor) Wire 1 - DIRECTION
  pinMode(MOTOR_A_DIR2,OUTPUT);      // Motor A (right motor) Wire 2 - DIRECTION
  
  pinMode(EN_MOTOR_B,OUTPUT);      // Motor B (left motor) power - PWM
  pinMode(MOTOR_B_DIR1,OUTPUT);      // Motor B (left motor) Wire 1 - DIRECTION
  pinMode(MOTOR_B_DIR2,OUTPUT);      // Motor B (left motor) Wire 2 - DIRECTION
  
  // Make sure motors are stopped at beginning of program
  digitalWrite(EN_MOTOR_A,LOW);    // stop Motor A (right motor)
  digitalWrite(EN_MOTOR_B,LOW);    // stop Motor B (left motor)
  
} // End of void setup()

void loop()
{
      // GO FORWARD
        
  digitalWrite(MOTOR_A_DIR1,LOW);    // Set forward direction for motorA (right motor)
  digitalWrite(MOTOR_A_DIR2,HIGH);
  digitalWrite(MOTOR_B_DIR1,LOW);    // Set forward direction for motorB (left motor)
  digitalWrite(MOTOR_B_DIR2,HIGH);
  
  analogWrite(EN_MOTOR_A,200);     // Turn on motor A (right motor)
  analogWrite(EN_MOTOR_B,200);     // Turn on motor B (left motor)
  delay(1000);            // Let motors run forward for 1 sec
  
  analogWrite(EN_MOTOR_A,0);       // stop motor A
  analogWrite(EN_MOTOR_B,0);       // stop motor B
  delay(1000);            // Let motors stay stopped for 1 sec
  
        // BACK UP
  
  digitalWrite(MOTOR_A_DIR1,HIGH);   // Set backward direction for motorA (right motor)
  digitalWrite(MOTOR_A_DIR2,LOW);
  digitalWrite(MOTOR_B_DIR1,HIGH);   // Set backward direction for motorB (left motor)
  digitalWrite(MOTOR_B_DIR2,LOW);
  
  analogWrite(EN_MOTOR_A,200);     // Turn on motor A
  analogWrite(EN_MOTOR_B,200);     // Turn on motor B
  delay(1000);            // Let motors run for 1 sec
  
  analogWrite(EN_MOTOR_A,0);     // stop motor A
  analogWrite(EN_MOTOR_B,0);     // stop motor B
  delay(1000);            // Let motors stay stopped for 1 sec
  
} // End of loop()

Are ports 2 and 3 really PWM ports? Am I programming them correctly here?

And are ports 23, 24, 25, and 26 really Digital I/O ports? Am I programming them correctly?

I very much appreciate any help!

Thanx all!

For reference, here's the old code. This worked perfectly on an Arduino UNO board.

/*  Dual Motor Driver  */

/* List of H-Bridge Servo Board Connections
   ENA           BLU   PWM  for MOTORA to Arduino Port 5 (output PWM)
   MOTORA_DIR1   GRN   DIR1 for MOTORA to Arduino Port 2
   MOTORA_DIR2   YEL   DIR2 for MOTORA to Arduino Port 3
   MOTORB_DIR1   ORG   DIR1 for MOTORB to Arduino Port 4
   MOTORB_DIR2   RED   DIR2 for MOTORB to Arduino Port 7
   ENB           BRN   PWM  for MOTORB to Arduino Port 6 (output PWM)
*/

// Give names to computer pins

int ENA=5;           // BLU connected to arduino's port 5 (output pwm)
int MOTORA_DIR1=2;   // GRN connected to arduino's port 2               
int MOTORA_DIR2=3;   // YEL connected to arduino's port 3
int ENB=6;           // BRN connected to arduino's port 6 (output pwm)
int MOTORB_DIR1=4;   // ORG connected to arduino's port 4
int MOTORB_DIR2=7;   // RED connected to arduino's port 7

int led13=13;        // LED on Arduino UNO board, port 13

void setup()
{
  // Program motor-control pins to be inputs or outputs
  pinMode(5,OUTPUT);      // Motor A (right motor) power - PWM
  pinMode(2,OUTPUT);      // Motor A (right motor) Wire 1 - DIRECTION
  pinMode(6,OUTPUT);      // Motor A (right motor) Wire 2 - DIRECTION
  
  pinMode(3,OUTPUT);      // Motor B (left motor) power - PWM
  pinMode(4,OUTPUT);      // Motor B (left motor) Wire 1 - DIRECTION
  pinMode(7,OUTPUT);      // Motor B (left motor) Wire 2 - DIRECTION
  
  // Make sure motors are stopped at beginning of program
  digitalWrite(5,LOW);    // stop Motor A (right motor)
  digitalWrite(3,LOW);    // stop Motor B (left motor)
  
} // End of void setup()

void loop()
{
      // GO FORWARD
        
  digitalWrite(2,LOW);    // Set forward direction for motorA (right motor)
  digitalWrite(6,HIGH);
  digitalWrite(4,LOW);    // Set forward direction for motorB (left motor)
  digitalWrite(7,HIGH);
  
  analogWrite(5,200);     // Turn on motor A (right motor)
  analogWrite(3,200);     // Turn on motor B (left motor)
  delay(1000);            // Let motors run forward for 1 sec
  
  analogWrite(5,0);       // stop motor A
  analogWrite(3,0);       // stop motor B
  delay(1000);            // Let motors stay stopped for 1 sec
  
        // BACK UP
  
  digitalWrite(2,HIGH);   // Set backward direction for motorA (right motor)
  digitalWrite(6,LOW);
  digitalWrite(4,HIGH);   // Set backward direction for motorB (left motor)
  digitalWrite(7,LOW);
  
  analogWrite(5,200);     // Turn on motor A
  analogWrite(3,200);     // Turn on motor B
  delay(1000);            // Let motors run for 1 sec
  
  analogWrite(ENA,0);     // stop motor A
  analogWrite(ENB,0);     // stop motor B
  delay(1000);            // Let motors stay stopped for 1 sec
  
} // End of loop()

All pins can be used as digital pins. Even analog pins - pinMode(A0,OUTPUT); digitalWrite(A0,LOW); works. Analog pins just have the added function that they can read analog voltages.

Code looks okay at first glance. Suspect wiring problem.

Hmm, I just looked again at the Arduino MEGA 2560 on this smart car.

As I said, on the board itself, ports 2 through 13 are marked as PWM. And I plugged the Enable lines into ports 2 and 3, both enables need to be PWM.

But on the outside of the connector, facing away from the chip, those ports are marked as "Digital" (PWM~). And only a few of the actual pins have the little ~ that means the pin is PWM. Just like on the Arduino Uno.

Only ports 3, 5, 6, 9, 10, and 11 have the little ~. But I've got the two ENABLE lines for the motors, plugged into 2 and 3.

Well, I just swapped the enable lines, and changed the code in the sketch, so that the two ENABLE lines are on ports 3 and 5 on the Arduino MEGA 2560.

And it still doesn't work. Same result (nothing) as before.

Are ports 3, 5, 6, 9, 10, and 11 the only PWM outputs?

Or are all of ports 2 thru 13, all PWMs?

LilAbner:
Hmm, I just looked again at the Arduino MEGA 2560 on this smart car.

As I said, on the board itself, ports 2 through 13 are marked as PWM. And I plugged the Enable lines into ports 2 and 3, both enables need to be PWM.

But on the outside of the connector, facing away from the chip, those ports are marked as "Digital" (PWM~). And only a few of the actual pins have the little ~ that means the pin is PWM. Just like on the Arduino Uno.

Only ports 3, 5, 6, 9, 10, and 11 have the little ~. But I've got the two ENABLE lines for the motors, plugged into 2 and 3.

Well, I just swapped the enable lines, and changed the code in the sketch, so that the two ENABLE lines are on ports 3 and 5 on the Arduino MEGA 2560.

And it still doesn't work. Same result (nothing) as before.

Are ports 3, 5, 6, 9, 10, and 11 the only PWM outputs?

Or are all of ports 2 thru 13, all PWMs?

2~13 are capable of PWM on the mega 2560. On the Uno, only 3/5/6/9/10/11.

I continue to suspect that something is wired wrong, or there's some other hardware issue. Are the grounds on arduino + motor controller connected? They need to be. Get your multimeter out and verify continuity of all connections from the Arduino board to the motor controller, then turn it on and verify all the voltages are what you expect.