Replacing Arduino 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!

The car is one of these, plus the Arduino board, batteries, etc. It has a tailwheel, so I guess it's a three-wheel car, not two.

For reference, here's the old code for the Arduino UNO board, it worked perfectly on the 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()

LilAbner:
The car is one of these, plus the Arduino board, batteries, etc. It has a tailwheel, so I guess it's a three-wheel car, not two.

http://ep.yimg.com/ay/yhst-14197398381157/remote-control-ultrasonic-ranging-smart-car-kit-for-arduino-23.gif

For reference, here's the old code for the Arduino UNO board, it worked perfectly on the 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()

Why are you asking here? What does the documentation say?

(a) Where should I ask?

(b) What's "documentation"?

:slight_smile:

OK, I put this thread in the Controllers forum. My apologies. I'll try to delete it here.

There is no need to move the Thread anywhere else - the same people will see it.

"Documentation" is the instructions that go with most things.

You need to read the documentation to check if there is some difference between the Mega and the Uno which you have not allowed for. Perhaps something needs to be connected to a different I/O pin ?

Reading your Original Post it seems that you have made some changes without understanding what you were doing.

Get it working again with the Uno and then try it unchanged on the Mega and if it does not work study carefully to see where the problem may be before making any changes. And then only make changes one at a time so you can see if the change is useful.

...R

Try not to use 5 and 6.

Notes and Known Issues

The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.

Other than that it should work the same. Check your ground.

Robin2:
There is no need to move the Thread anywhere else - the same people will see it.

I got snapped at when I put it here. I don't want to violate board protocols.

Get it working again with the Uno and then try it unchanged on the Mega and if it does not work study carefully to see where the problem may be before making any changes. And then only make changes one at a time so you can see if the change is useful.

Excellent advice. I went back to the Arduino UNO board and the old sketch, and got that working as it had. Then unplugged the UNO and substituted the MEGA 2560, and plugged the wires into the same ports as on the UNO. Changed the IDE to a MEGA board, downloaded the old sketch to the MEGA.

Success!

Still don't know why the other MEGA configuration didn't work. But now I have a working base to start from. Thank you!!!

LilAbner:
I got snapped at when I put it here. I don't want to violate board protocols.

Cross posting is a violation of board protocol. The reason is you have people on this post trying to help you and people on the other post(Replaced Arduion UNO with Arduino MEGA 2560 - Uh-oh. - Microcontrollers - Arduino Forum) trying to help you so it wastes time due to duplicate efforts. If you do need to move a post then you should "report to moderator" and ask them to to move it.

LilAbner:
I got snapped at when I put it here. I don't want to violate board protocols.

It seems that you are referring to

Why are you asking here? What does the documentation say?

in Reply #2.

I don't think that was a complaint about this section of the Forum. I read it as "why are you asking here instead of reading the documentation"

Glad you have it fixed.

...R