Robojax

Using L298N To control 2 DC Motors with Potentiometer and 3 Push buttons

دروس آردوینو به فارسی

Using L298N To contorl DC Motors with Potentiometer and 3 Push buttons

This this video shows how to use L298N Module to control 2 DC Motor fully with Robojax Library. We are using 3 push buttons to STOPClockwise CW, or Counter Clockwise CCW.Please watch video for instruction. Download library.

In this page you will codes to control 1 motor and 2 motors using L298N Motor Controller module

L298N Motor Controller MOdule L298N Motor Controller module
L298N with pot and push buttons Single motor control with Push buttons and Potentiometer
L298N with pot and push buttons Signal IN1, IN2 and ENA wiring
L298N with pot and push buttons 3 push buttons wiring
L298N with pot and push buttons
L298N with pot and push buttons

Resources for this sketch

Code to control 1 DC motor using L298N Module with push button and pot


 /*
 * Library Example for L298N Module to control DC 1 motor with 3 push buttons (see the link for controlling 2 motors)
 * 
 * This code is to control DC motors using two potentiometer (variable resistors)
 * 
 * Written by Ahmad Shamshiri on October 11, 2019  
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * Watch video instruction for this code:https://youtu.be/qo7eJ6PUx2M
 * 
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin



const int CCW = 2; // do not change
const int CW  = 1; // do not change

#define motor1 1 // do not change
#define motor2 2 // do not change

//*** Potentiometer and push button for motor 1 started 

const int motor1PushButtonCW = 8;
const int motor1PushButtonStop = 9;
const int motor1PushButtonCCW = 10;
const int pot1Input = A0;
const int motor1Minimum =15;//30% is minimum for motor 1

// do not change below this line
int motor1Direction =CCW;//
int motor1StopState=HIGH;//Stope state of motor 1
int motor1Speed =0;// speed of motor 1
//*** Potentiometer and push button for motor 1 ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



// use the line below for single motor
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, true);

// use the line below for two motors
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN2, IN3, ENB, true);

void setup() {
  Serial.begin(115200);
  motor.begin();

  //L298N DC Motor by Robojax.com
  pinMode(motor1PushButtonCW, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);
  pinMode(motor1PushButtonCCW, INPUT_PULLUP); 
   
}


void loop() {
  updateState1();//read all push buttons
  int pot1Value= analogRead(pot1Input);// read potentiometer value

  motor1Speed = map(pot1Value, 0,1023, motor1Minimum, 100);// convert 0-5V to 0-100%


  if(motor1StopState ==HIGH)
  {
    motor.brake(1);  
    
  }else{
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }
  //motor.demo(1);

  //motor.rotate(motor1, motor1Speed, CW);//run motor1 at 60% speed in CW direction
 delay(100);



  
}//

/*
 * updateState1()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Oct 11, 2019 in Ajax, Ontario, Canada
 */
void updateState1()
{

  if(digitalRead(motor1PushButtonCW) ==LOW && motor1StopState !=LOW){

   motor1Direction =CW;// CW button is pressed  
   motor1StopState =LOW;
  }  

  if(digitalRead(motor1PushButtonCCW) ==LOW && motor1StopState !=LOW)
  {
   motor1Direction =CCW;
   motor1StopState =LOW;   

  }  
  if(digitalRead(motor1PushButtonStop) ==LOW)
  {
   motor1StopState =HIGH;// STOP button is pressed 
  }  
}//updateState end

   

Code to control 2 DC motors using L298N Module with push button and pot


 /*
 * Library Example for L298N Module to control DC 2 motors with 3 push buttons
 * 
 * This code is to control DC motors using two potentiometer (variable resistors)
 * 
 * Written by Ahmad Shamshiri on October 11, 2019 
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * Watch video instruction for this code:https://youtu.be/qo7eJ6PUx2M
 * 
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <Robojax_L298N_DC_motor.h>
// motor 1 settings
#define IN1 2
#define IN2 4
#define ENA 3 // this pin must be PWM enabled pin

// motor 2 settings
#define IN3 6
#define IN4 7
#define ENB 5 // this pin must be PWM enabled pin

const int CCW = 2; // do not change
const int CW  = 1; // do not change

#define motor1 1 // do not change
#define motor2 2 // do not change

//*** Potentiometer and push button for motor 1 started 

const int motor1PushButtonCW = 8;
const int motor1PushButtonStop = 9;
const int motor1PushButtonCCW = 10;
const int pot1Input = A0;
const int motor1Minimum =15;//30% is minimum for motor 1

// do not change below this line
int motor1Direction =CCW;//
int motor1StopState=HIGH;//Stope state of motor 1
int motor1Speed =0;// speed of motor 1
//*** Potentiometer and push button for motor 1 ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

//*** Potentiometer and push button for motor 2 started 

const int motor2PushButtonCW = 11;
const int motor2PushButtonStop = 12;
const int motor2PushButtonCCW = 13;
const int pot2Input = A1;
const int motor2Minimum =15;//30% is minimum for motor 2

// do not change below this line
int motor2Direction =CCW;//
int motor2StopState=HIGH;//Stope state of motor 2
int motor2Speed =0;// speed of motor 2
//*** Potentiometer and push button for motor 2 ended
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


// use the line below for single motor
//Robojax_L298N_DC_motor motor(IN1, IN2, ENA, true);

// use the line below for two motors
Robojax_L298N_DC_motor motor(IN1, IN2, ENA, IN3, IN4, ENB, true);

void setup() {
  Serial.begin(115200);
  motor.begin();

  //L298N DC Motor by Robojax.com
  //push buttons for motor 1
  pinMode(motor1PushButtonCW, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);
  pinMode(motor1PushButtonCCW, INPUT_PULLUP); 

  //push buttons for motor 2
  pinMode(motor2PushButtonCW, INPUT_PULLUP);
  pinMode(motor2PushButtonStop, INPUT_PULLUP);
  pinMode(motor2PushButtonCCW, INPUT_PULLUP);   
}


void loop() {
  updateState1();//read all push buttons for motor1
  int pot1Value= analogRead(pot1Input);// read potentiometer value
  motor1Speed = map(pot1Value, 0,1023, motor1Minimum, 100);// convert 0-5V to 0-100%

  if(motor1StopState ==HIGH)
  {
    motor.brake(1);  
    
  }else{
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }

// codes to control motor 2
  updateState2();//read all push buttons for motor2
  int pot2Value= analogRead(pot2Input);// read potentiometer value for motor 2
  motor2Speed = map(pot2Value, 0,1023, motor2Minimum, 100);// convert 0-5V to 0-100%

  if(motor2StopState ==HIGH)
  {
    motor.brake(2);  
    
  }else{
    motor.rotate(motor2, motor2Speed, motor2Direction);    
  }
  delay(100);
  
}//

/*
 * updateState1()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Oct 11, 2019 in Ajax, Ontario, Canada
 */
void updateState1()
{

  if(digitalRead(motor1PushButtonCW) ==LOW && motor1StopState !=LOW){

   motor1Direction =CW;// CW button is pressed  
   motor1StopState =LOW;
  }  

  if(digitalRead(motor1PushButtonCCW) ==LOW && motor1StopState !=LOW)
  {
   motor1Direction =CCW;
   motor1StopState =LOW;   

  }  
  if(digitalRead(motor1PushButtonStop) ==LOW)
  {
   motor1StopState =HIGH;// STOP button is pressed 
  }  
}//updateState1 end

/*
 * updateState2()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Oct 11, 2019 in Ajax, Ontario, Canada
 */
void updateState2()
{

  if(digitalRead(motor2PushButtonCW) ==LOW && motor2StopState !=LOW){

   motor2Direction =CW;// CW button is pressed  
   motor2StopState =LOW;
  }  

  if(digitalRead(motor2PushButtonCCW) ==LOW && motor2StopState !=LOW)
  {
   motor2Direction =CCW;
   motor2StopState =LOW;   

  }  
  if(digitalRead(motor2PushButtonStop) ==LOW)
  {
   motor2StopState =HIGH;// STOP button is pressed 
  }  
}//updateState2 end