L293D motor driver with arduino

DC motor

Introducing to L293D motor driver

If you want to control an DC motor that can run forward or reverse you can do that in many ways. But I want to tell you the simplest way to drive DC motor. In this project we will control the DC motor using single IC called L293D. This IC is powerful enough to control DC motor with low current. Before we start the wiring. We will introduce you to L293D IC first. This is the pinout :

L293D Pinout
L293D Pinout




L293D can drive up to 2 motors with single IC. Use input 1 and Input 2 to control first motor. So first motor should be connected to Output 1 and Output 2. And if you want to use second motor, connect input control to Input 3 and Input 4. And motor to Output 3 and Output 4. VCC should be connected to 5V to power up the IC. VSS is input power for motors. So if you want to use 12V motor, this pin should connect to 12V power supply. Connect 12V and 5V ground to gnd, wire all gnd. And Enable pin is used to control speed of motors using PWM. Enable 1 used to control speed of first motor, and Enable 2 for second motor.

L293D schematic with motor example

So how we use this IC? check our example schematic below :

L293D with motors schematic
L293D with motors schematic

To Control first motor (left), you can give input from microncontroller like arduino to make motor run forward or reverse. Give Pin 2 HIGH/5V and Pin 3 LOW/0V to run motor forward. If you want to run motor reverse then give Pin 2 LOW/0V and Pin 3 HIGH/5V.  Give Pin 1 (Enable 1) PWM input to control the speed. If you use arduino UNO you can give PWM from 0-255. Mean that 255 is full speed and 0 is stopped. And if you don’t want to control the speed you can just connect Pin 1 to 5V.



Control the second motor (right) use pin 10 and 15 to give the direction, and Pin 9 to vary the speed.

Example in bread board

Here’s example of using L293D with an arduino UNO and a DC motor

L293D single motor bread board

L293D single motor bread board



In picture above, motor power supply using a 4x AAA batteries. And 5V supply for IC given from arduino 5V.

Code example :

const int motorPin1  = 2;  // Pin 14 of L293
const int motorPin2  = 3;  // Pin 10 of L293

void setup() {
  //Set pins as outputs
  pinMode(motorPin1, OUTPUT);
}


void loop() {
  //This code  will turn Motor forward for 2 sec.
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  delay(3000);
  //This code will turn Motor reverse for 2 sec.
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  delay(3000);
}

If you want to drive High current DC motor, you can visit here.

Display output keypad to LCD using arduino UNO

arduino, keypad, lcd, wires (Small)

This tutorial is about how to interface keypad to arduino and display output of keypad to LCD. Sometime in your project you need to display output of keypad to LCD. Such as when you input a password, number or other project like calculator. So, this project is about how to display characters to LCD directly by keystrokes/pressing keypad.

Keypad used in this tutorial has 4 rows and 4 columns, and an LCD 20×4. You can use another type when you practicing by your self.

Parts needed to display keypad to LCD using arduino UNO

Parts needed is :

1. Arduino UNO

2. Keypad

3. LCD

4. Some wire to connect all parts

arduino, keypad, lcd, wires (Small)
arduino, keypad, lcd, wires

Steps to do

Connect keypad to arduino pin 9,8,7,6,5,4,3,2 (from left to right) like picture below :

arduino uno and keypad (Small)arduino uno and keypad

Then connect LCD to arduino to these pin :

LCD RS -> A1

LCD E -> A0

LCD D4 -> 13

LCD D5 -> 12

LCD D6 -> 11

LCD D7-> 10

And completed parts connected will be like this :

display keypad output to LCD (Small)
display keypad output to LCD

The code

Make sure you connect all wires and part correctly. Double check your wiring. If you already sure everything are correct, then now time to code !

First include all libarary you need, in this case you need keypad and liquidCrystal

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);

String pad;

Configure keypad pin and the characters. You can change according to your keypad and pin.

const byte numRows= 4;
const byte numCols= 4;

char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//------------------------------------------------------------
byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols] = {5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad

In setup you need to initialize lcd

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
}

Make function to detect keystrokes

void bacaKeypad() {
  char keypressed = myKeypad.getKey(); //deteksi penekanan keypad
  String konv = String(keypressed);
  pad+=konv;
}

And the last, call all function in loop function

void loop() {
  // put your main code here, to run repeatedly:
  bacaKeypad();
  lcd.setCursor(0,0);
  lcd.print(pad);
  delay(100);
}

If you still unclear about all parts of code, here’s the complete code :

#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);

String pad;
const byte numRows= 4;
const byte numCols= 4;

char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//------------------------------------------------------------
byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols] = {5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
}

void loop() {
  // put your main code here, to run repeatedly:
  bacaKeypad();
  lcd.setCursor(0,0);
  lcd.print(pad);
  delay(100);
}

void bacaKeypad() {
  char keypressed = myKeypad.getKey(); //deteksi penekanan keypad
  String konv = String(keypressed);
  pad+=konv;
}