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

Images

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
Language: C++
Copied!

Things you might need

Files📁

No files available.