搜索代码

Lesson 55-1: Controlling a Single DC Motor Using Push Buttons

Lesson 55-1: Controlling a Single DC Motor Using Push Buttons

In this tutorial, we will learn how to control a single DC motor using an L298N motor controller module, push buttons, and a potentiometer. By pressing the buttons, we can make the motor rotate clockwise or counterclockwise, as well as stop it and control its speed using the potentiometer. This project is excellent for understanding motor control in Arduino projects and provides a hands-on experience with hardware interfacing. For further clarification, be sure to check out the video accompanying this tutorial (in video at 0:45).

L298N Motor Controller Module
L298N Motor Controller MOdule

Hardware Explained

The primary component in this project is the L298N motor controller, which allows you to control the direction and speed of DC motors. It operates using an H-bridge configuration, enabling the motor to rotate in both directions. The module requires a supply voltage between 6 to 35 volts, making it versatile for different motor types. Additionally, it has PWM (Pulse Width Modulation) capabilities, allowing for speed control. We will also use push buttons for controlling the motor's direction and stopping it. The potentiometer will help adjust the speed of the motor. These components work together to provide a simple yet effective way to manage motor operations in your Arduino projects.

Datasheet Details

Manufacturer STMicroelectronics
Part number L298N
Logic/IO voltage 5 V
Supply voltage 6–35 V (VS)
Output current (per channel) 2 A max/channel (abs. max), 1 A typ. continuous
Peak current (per channel) 3 A
PWM frequency guidance 5 kHz
Input logic thresholds 0.8 V (low), 2.0 V (high)
Voltage drop / RDS(on) / saturation 2 V (typ.)
Thermal limits 150 °C
Package Multiwatt15
Notes / variants Can control up to 2 motors
  • Ensure proper heat sinking for continuous operation.
  • Use PWM pins for speed control to avoid overheating.
  • Connect the ground of the motor driver to the Arduino ground.
  • Be cautious of the supply voltage to prevent damage.
  • Use pull-up resistors for push buttons to ensure stable readings.
  • Disconnect the power supply while programming to avoid back-powering issues.
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control single motor

Wiring Instructions

Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control single motor

To wire the L298N motor controller with the Arduino, start by connecting the battery's positive terminal to the VCC terminal on the L298N module and the negative terminal to the GND. Connect the GND terminal of the L298N to the Arduino's GND as well. The 5V output from the L298N can be connected to the Arduino's 5V input, but only after programming to avoid conflicts. Next, connect the motor to the output terminals of the L298N. For motor control, wire the input pins as follows: connect IN1 to pin 2 on the Arduino, IN2 to pin 4, and the enable pin ENA to pin 3. For the push buttons, wire one side of each button to GND, and the other side as follows: connect the direction button to pin 8, the stop button to pin 9. Finally, connect the potentiometer: one side to GND, the other to 5V, and the middle pin to A0 on the Arduino.

Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control 2 motors

Code Examples & Walkthrough

The following code snippet initializes the motor control and setup the pins:


#include 
#define IN1 2
#define IN2 4
#define ENA 3 

const int motor1PushButtonDirection = 8;
const int motor1PushButtonStop = 9;

void setup() {
  Serial.begin(115200);
  motor.begin();
  pinMode(motor1PushButtonDirection, INPUT_PULLUP);
  pinMode(motor1PushButtonStop, INPUT_PULLUP);
}

In this excerpt, we include the necessary library for the L298N motor control and define the pins for the motor and push buttons. The `setup()` function initializes the serial communication and sets the button pins as input with pull-up resistors. Next, we check the state of the buttons and control the motor in the loop:


void loop() {
  updateState1();
  if(motor1StopState == HIGH) {
    motor.brake(motor1);     
  } else {
    motor.rotate(motor1, motor1Speed, motor1Direction);    
  }
  delay(pushButtonDelayTime);
}

Here, the `loop()` function continuously checks the state of the push buttons. If the stop button is pressed, the motor will brake; otherwise, it will rotate at the specified speed and direction. The `updateState1()` function reads the button states and updates the motor direction:


void updateState1() {
  if(digitalRead(motor1PushButtonDirection) == LOW) {
    motor1Direction = (motor1Direction == CW) ? CCW : CW;
    Serial.println("Direction changed");
  }  
  if(digitalRead(motor1PushButtonStop) == LOW) {
    motor1StopState = 1 - motor1StopState; // toggle Start/Stop
  }  
}

This function toggles the motor direction when the direction button is pressed and toggles the stop state when the stop button is pressed. For the full code, please note that it loads below the article.

Demonstration / What to Expect

When you run the program, you should be able to control the motor's direction and speed using the push buttons and potentiometer. Pressing the direction button will toggle between clockwise and counterclockwise rotation, while the stop button will start or stop the motor (in video at 12:30). Be cautious of the potentiometer settings; if set too low, the motor may not start.

Chapters

  • Introduction – 0:00
  • Hardware Overview – 1:30
  • Wiring Instructions – 3:45
  • Code Walkthrough – 6:00
  • Demonstration – 12:00

图像

L298N Motor Controller Module
L298N Motor Controller MOdule
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control single motor
Arduino wiring for L298N with pot 3push button to control 2 motors
Arduino wiring for L298N with pot 3push button to control 2 motors
485-Lesson 55: Controlling a DC motor using push buttons
语言: C++
/*
 * 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: https://youtu.be/diS_WkUdU-8
 * 
 * Written by Ahmad Shamshiri on October 20, 2019 at 14:58
 * in Ajax, Ontario, Canada. www.robojax.com
 * 
 * 
 * 
 * Get this code and other Arduino codes from Robojax.com

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

or make a 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 downloaded 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
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 direction 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() {
  Serial.begin(115200);
  motor.begin();

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

   
}


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

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

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

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

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

  }  

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

|||您可能需要的东西

文件📁

没有可用的文件。