I am trying to convert a sketch that uses the dual L298 dual motor driver shield and replace the shield with two L298N motor drivers. I have figured out (I think) the PWM signals and the motor functions, but I don't understand where the 74HC595N 8-Bit Shift Register DIR_CLK, DIR_EN, DATA, and DIR_LATCH are set up. I am using four gear motors and mecanum wheels.
const int PWM2A = 11; //M1 motor PWM signal pin
const int PWM2B = 3; //M2 motor PWM signal pin
const int PWM0A = 6; //M3 motor PWM signal pin
const int PWM0B = 5; //M4 motor PWM signal pin
const int DIR_CLK = 4; // Data input clock line
const int DIR_EN = 7; //Equip the L293D enabling pins
const int DATA = 8; // USB cable
const int DIR_LATCH = 12; // Output memory latch clock
const int Move_Forward = 39; //Move Forward
const int Move_Backward = 216; //Move Backward
const int Left_Move = 116; //Left translation
const int Right_Move = 139; //Right translation
const int Right_Rotate = 149; //Rotate Right
const int Left_Rotate = 106; //Rotate Left
const int Stop = 0; //Stop
const int Upper_Left_Move = 36; //Upper Left Move
const int Upper_Right_Move = 3; //Upper Right Move
const int Lower_Left_Move = 80; //Lower Left Move
const int Lower_Right_Move = 136; //Lower Right Move
const int Drift_Left = 20; //Drift on Left
const int Drift_Right = 10; //Drift on Right
int Speed1 = 255;//Set the default speed between 1 and 255
int Speed2 = 255;//Set the default speed between 1 and 255
int Speed3 = 255;//Set the default speed between 1 and 255
int Speed4 = 255;//Set the default speed between 1 and 255
void Motor(int Dir, int Speed1, int Speed2, int Speed3, int Speed4) {
analogWrite(PWM2A, Speed1); //Motor PWM speed regulation
analogWrite(PWM2B, Speed2); //Motor PWM speed regulation
analogWrite(PWM0A, Speed3); //Motor PWM speed regulation
analogWrite(PWM0B, Speed4); //Motor PWM speed regulation
digitalWrite(DIR_LATCH, LOW); //DIR_LATCH sets the low level and writes the direction of motion in preparation
shiftOut(DATA, DIR_CLK, MSBFIRST, Dir); //Write Dir motion direction value
digitalWrite(DIR_LATCH, HIGH); //DIR_LATCH sets the high level and outputs the direction of motion
}
void setup()
{
pinMode(DIR_CLK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(DIR_EN, OUTPUT);
pinMode(DIR_LATCH, OUTPUT);
pinMode(PWM0B, OUTPUT);
pinMode(PWM0A, OUTPUT);
pinMode(PWM2A, OUTPUT);
pinMode(PWM2B, OUTPUT);
}
void loop()
{
Motor(Right_Rotate, Speed1, Speed2, Speed3, Speed4);
delay(2000);
Motor(Stop, 0, 0, 0, 0);
delay(1000);
Motor(Left_Rotate, Speed1, Speed2, Speed3, Speed4);
delay(2000);
Motor(Stop, 0, 0, 0, 0);
delay(1000);
// Motor(Move_Forward, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
// Motor(Move_Backward, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
// Motor(Left_Move, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
// Motor(Right_Move, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
// Motor(Upper_Left_Move, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
// Motor(Upper_Right_Move, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
// Motor(Lower_Left_Move, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
// Motor(Lower_Right_Move, Speed1, Speed2, Speed3, Speed4);
// delay(1000);
// Motor(Stop, 0, 0, 0, 0);
// delay(1000);
}
I am trying to convert a sketch that uses the dual L298 dual motor driver shield and replace the shield with two L298N motor drivers. I have figured out (I think) the PWM signals and the motor functions, but I don't understand where the 74HC595N 8-Bit Shift Register DIR_CLK, DIR_EN, DATA, and DIR_LATCH are set up. I am using four gear motors and mecanum wheels.
const int PWM2A = 11; //M1 motor PWM signal pin const int PWM2B = 3; //M2 motor PWM signal pin const int PWM0A = 6; //M3 motor PWM signal pin const int PWM0B = 5; //M4 motor PWM signal pin const int DIR_CLK = 4; // Data input clock line const int DIR_EN = 7; //Equip the L293D enabling pins const int DATA = 8; // USB cable const int DIR_LATCH = 12; // Output memory latch clock const int Move_Forward = 39; //Move Forward const int Move_Backward = 216; //Move Backward const int Left_Move = 116; //Left translation const int Right_Move = 139; //Right translation const int Right_Rotate = 149; //Rotate Right const int Left_Rotate = 106; //Rotate Left const int Stop = 0; //Stop const int Upper_Left_Move = 36; //Upper Left Move const int Upper_Right_Move = 3; //Upper Right Move const int Lower_Left_Move = 80; //Lower Left Move const int Lower_Right_Move = 136; //Lower Right Move const int Drift_Left = 20; //Drift on Left const int Drift_Right = 10; //Drift on Right int Speed1 = 255;//Set the default speed between 1 and 255 int Speed2 = 255;//Set the default speed between 1 and 255 int Speed3 = 255;//Set the default speed between 1 and 255 int Speed4 = 255;//Set the default speed between 1 and 255 void Motor(int Dir, int Speed1, int Speed2, int Speed3, int Speed4) { analogWrite(PWM2A, Speed1); //Motor PWM speed regulation analogWrite(PWM2B, Speed2); //Motor PWM speed regulation analogWrite(PWM0A, Speed3); //Motor PWM speed regulation analogWrite(PWM0B, Speed4); //Motor PWM speed regulation digitalWrite(DIR_LATCH, LOW); //DIR_LATCH sets the low level and writes the direction of motion in preparation shiftOut(DATA, DIR_CLK, MSBFIRST, Dir); //Write Dir motion direction value digitalWrite(DIR_LATCH, HIGH); //DIR_LATCH sets the high level and outputs the direction of motion } void setup() { pinMode(DIR_CLK, OUTPUT); pinMode(DATA, OUTPUT); pinMode(DIR_EN, OUTPUT); pinMode(DIR_LATCH, OUTPUT); pinMode(PWM0B, OUTPUT); pinMode(PWM0A, OUTPUT); pinMode(PWM2A, OUTPUT); pinMode(PWM2B, OUTPUT); } void loop() { Motor(Right_Rotate, Speed1, Speed2, Speed3, Speed4); delay(2000); Motor(Stop, 0, 0, 0, 0); delay(1000); Motor(Left_Rotate, Speed1, Speed2, Speed3, Speed4); delay(2000); Motor(Stop, 0, 0, 0, 0); delay(1000); // Motor(Move_Forward, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); // Motor(Move_Backward, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); // Motor(Left_Move, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); // Motor(Right_Move, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); // Motor(Upper_Left_Move, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); // Motor(Upper_Right_Move, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); // Motor(Lower_Left_Move, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); // Motor(Lower_Right_Move, Speed1, Speed2, Speed3, Speed4); // delay(1000); // Motor(Stop, 0, 0, 0, 0); // delay(1000); }
The L298N modules require two direction pins (IN1/IN2) per motor. So for 4 motors, you’ll need 8 digital pins for direction plus 4 PWM pins.
The current sketch uses the 74HC595N shift register to serialize motor direction control bits (like IN1/IN2 for each motor), using:
-
DATA(pin 8): serial data line -
DIR_CLK(pin 4): clock line -
DIR_LATCH(pin 12): latch to output buffer -
DIR_EN(pin 7): enable signal for the motor driver
This approach shifts one byte (Dir) to the shift register to set all 4 motor directions (IN1/IN2 for each motor) using only 3 control lines, thanks to the shift register.
@aliarifat thank you, my original idea was to control all motors with dedicated pins, even though I will be using a Mega 2560 (needing the four interrupts for motor encoders). You got me thinking, maybe using a 74HC595N 8-Bit Shift Register might make things easier and cleaner. This would leave more pins for sensors.
@aliarifat thank you, my original idea was to control all motors with dedicated pins, even though I will be using a Mega 2560 (needing the four interrupts for motor encoders). You got me thinking, maybe using a 74HC595N 8-Bit Shift Register might make things easier and cleaner. This would leave more pins for sensors.
If you need to drive tens or hundreds of outputs / read tens or hundreds of inputs from a single prototype board, the daisy-chain shift I/O board can be useful for your purpose. You can use a board like this:
https://www.pcbway.com/project/shareproject/Daisy_Chain_Shift_IO_Board.html
This is an easily daisy-chainable shift I/O board for your multi-input / output projects using standard SN74HC595N and CD4021BE chips!


