How to Get Started with L298N Motor Driver module using Arduino
Introduction
The L298N motor driver module is a cheap solution when you need to drive a 2 DC motors or 1 stepper motor. It is a popular motor driver module to the DIYers and hobbyist due to ease of use and availability.
Features
- It has a dual H-bridge motor driver.
- Onboard 5V linear voltage regulator so it means you can supply the voltage requirements of the other circuit.
Below is the pin assignments and its configuration.
Power Terminal Block: +12V: - is the positive motor supply voltage
- if motor supply voltage is more than 12V, make sure the jumper pin in CON5 is removed. This is to protect the onboard 5V linear voltage regulator from damage. GND: - is the common ground for the L298N and the interface microcontroller +5V: - it is an input voltage supply for the L298N IC when the onboard 5V linear voltage regulator is disabled (jumper pin in CON5 is removed).
- it is an output voltage supply for the interface microcontroller when the linear voltage regulator is enabled (jumper pin in CON5 is in placed).
Bidirectional Motor Driver Output: MotorA: motor driver output for MotorA MotorB: motor driver output for MotorB
Enable and Speed Controller: enA: enable pin A
- Pull down (connect to GND) to disable motor driver output for MotorA
- Pull up (connect to VCC) to enable motor driver output for MotorB (similar results is achieve using jumper pin)
- PWM to control speed of the motor
enB: enable pin B
- Pull down (connect to GND) to disable motor driver output for MotorA
- Pull up (connect to VCC) to enable motor driver output for MotorB (similar results is achieve using jumper pin)
- PWM to control speed of the motor
Direction Logic Controller: IN1 and IN2: - direction logic controller for MotorA
IN3 and IN4: - direction logic controller for MotorB
PWR jumper pin: - this is to enable onboard voltage regulator
Circuit Diagram
NOTES:
- Referring to the circuit diagram, I am supplying the L298N with 6V battery. +6V is connected to the +12V pin motor supply voltage.
- Battery GND pin is connected to the GND pin of the L298N.
- Arduino GND is also tied to the L298N GND pin, this is to create a common reference ground.
- 5V pin of Arduino is tied to the L298N +5V pin to supply voltage for the L298N IC. Please notice also that the jumper pin in the CON5 is removed to disable the onboard 5V voltage regulator of the L298N.
- DC motor is connected to the terminal block MotorB in the right side.
- IN3 and IN4 is tied to Arduino Uno digital pin 13 and 12 to control the direction of motor rotation.
- EnB is tied to Arduino Uno digital pin 11. This pin has a capability of outputting a Pulse Width Modulation (PWM) signal. This is to control the speed of rotation of the motor.
Video Demonstration
Source Code
1#define IN3B 13
2#define IN4B 12
3#define PWMB 11
4
5String cmd="";
6bool isBEnabled = false;
7byte PWMB_val = 100;
8
9void setup() {
10 Serial.begin(9600);
11 pinMode(IN3B, OUTPUT);
12 pinMode(IN4B, OUTPUT);
13 pinMode(PWMB, OUTPUT);
14 digitalWrite(IN3B, LOW);
15 digitalWrite(IN4B, LOW);
16 digitalWrite(PWMB, LOW);
17}
18
19void loop() {
20 checkSerial();
21}
22
23
24void checkSerial() {
25 //Read data from Serial
26 while(Serial.available()>0){
27 cmd+=(char)Serial.read();
28 }
29
30 //Select function with cmd
31 if(cmd!=""){
32 cmd.trim(); // Remove added LF in transmit
33 // We expect Command from bluetooth
34 if (cmd.equals("E")) {
35 Serial.println("Enabling motor");
36 enableB();
37 } else if(cmd.equals("R")) {
38 Serial.println("CW Rotation");
39 forward();
40 }else if (cmd.equals("L")){
41 Serial.println("CCW Rotation");
42 backward();
43 }else if(cmd.equals("U")){
44 Serial.println("Speed up");
45 speedUp();
46 }else if(cmd.equals("D")){
47 Serial.println("Speed down");
48 speedDown();
49 }
50 cmd=""; //reset cmd
51 }
52
53void enableB() {
54 if (isBEnabled) {
55 // motor is running, now disable it
56 isBEnabled = false;
57 digitalWrite(PWMB, 0);
58 } else {
59 // motor is disabled, now run it
60 isBEnabled = true;
61 analogWrite(PWMB, PWMB_val);
62 }
63}
64
65void forward() {
66 digitalWrite(IN3B, LOW);
67 digitalWrite(IN4B, HIGH);
68}
69
70void backward() {
71 digitalWrite(IN3B, HIGH);
72 digitalWrite(IN4B, LOW);
73}
74
75void speedUp() {
76 if (PWMB_val < 245) {
77 PWMB_val = PWMB_val + 10;
78 analogWrite(PWMB, PWMB_val);
79 }
80}
81
82void speedDown () {
83 if (PWMB_val > 9) {
84 PWMB_val = PWMB_val - 10;
85 analogWrite(PWMB, PWMB_val);
86 }
87}
Call To Action
That’s all for now. If you have any questions or clarification, please feel free to leave your comments and suggestions in the comment box. Thank you and have a good day. Happy tinkering.
Posts in this series
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- How to Use Arduino Interrupts
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!