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!
