Back to Step by Step Course by Robojax

Lesson 55: Controlling DC motor using push buttons

Please select other codes for this lecture from the links below.

Related or required files and link to this lesson

Part 5: DC Motors

In this lesson we learn how to start and stop one or two DC motors useing L298N motor driver module with Arduino. We use 2 push buttons. One push button to Start or Stop the motor and the second push button switch to change the direction of rotation from clocowise CW to counterclockwise CCW. Please watch the video for full details. There are two codes to control either single motor or two motores. This is to control One DC motor.

  • 00:00 Introduction
  • 01:52 Wiring Explained
  • 05:47 Code Explained (one motor)
  • 13:49 Demonstration (one motor)
  • 15:12 Demonstration (two motors) Product - 1pcs L298N MOTOR DRIVER module New Dual H Bridge DC Stepper Motor Drive Controller Board Module

 * Lesson 55: Controlling DC Motors with 2 push buttons with L298N 
 * Library Example for L298N Module to control with 2 push buttons
 * One Push button is used for CW/CCW  
 * and One push button is used to stop the motor
 Watch video for full details of this code:
 * Written by Ahmad Shamshiri on October 20, 2019   at 14:58
 * in Ajax, Ontario, Canada.
 * Get this code and other Arduino codes from

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon

or make donation using PayPal

 *  * 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
    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
    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 <>.

#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
const int pushButtonDelayTime =200;

//*** Push buttons for motor 1 started 
int motor1Speed =60;// speed of motor 1 in % (60 means 60%)
const int motor1PushButtonDirection = 8;// push button for direcion change
const int motor1PushButtonStop = 9;// push button for START/STOP
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
//*** Push buttons for motor 1 started  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() {

  //L298N DC Motor by
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);


void loop() {
  updateState1();//read all push 1 buttons

  if(motor1StopState ==HIGH)
    motor.rotate(motor1, motor1Speed, motor1Direction);    

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

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

  if(digitalRead(motor1PushButtonDirection) ==LOW){
      if(motor1Direction ==CW)
        motor1Direction =CCW;// 
        Serial.println("*****Now CCW"); 
        motor1Direction =CW;//
        Serial.println("*****Now CC");  


  if(digitalRead(motor1PushButtonStop) ==LOW)
   motor1StopState =1-motor1StopState;// toggle Star/stop
}//updateState end


The least I expect from you is to give the video a thumbs up and subscribe to my channel. I appreciate it. I have spent hundreds of hours making these lectures and writing code. You don't lose anything by subscribing to my channel. Your subscription is a stamp of approval for my videos, helping more people find them and, in turn, helping me. Thank you!

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

Right Side