Interfacing Arduino With Keypad, DC Motor-1
Interfacing Arduino With Keypad, DC Motor-1
Components to be Used:
4×4 keypad
Arduino UNO
Connecting Wires
If you bought an original keypad used for this purpose then you don’t need to
figure out pins but if you got it from some other electronic device then you need
to identify pins which will be used for this purpose.
First you need to get a piece of paper and draw the right hand diagram as you see
it below. I’ve already written my pin numbers (1, 2, 3 across the bottom and 7, 6,
5, 4 down the right side) which you can just leave off of your drawing. Next, you
are going to use your Ohm meter to find out which pins are connected to which
keys. The first thing to do is count how many pins are on your keypad (as seen in
the photo below.) The photo is showing 14 pins though not all of the pins are
used. Don’t worry, once you’ve completed this procedure you will know which
pins are unused and can be ignored.
Procedure:
Connect your Ohm meter leads to pins 1 and 2.
Press all the buttons until the meter indicates a closure.
Write down the pin numbers next to the column and row for the key you
just found. Example: Your meter is connected to pins 1 and 5. When you
pressed the number 7 your meter reacted. Write 1 under COL0 and 5 next
to ROW2.
If the meter didn’t react then move the meter lead from pin 2 to pin 3 and
repeat steps 2 and 3 above.
Now, keep moving the lead to the next pin and repeat steps 2 and 3 for
each pin.
Once you have reached the end move the first meter lead from pin 1 to pin
2 and repeat steps 2 and 3 while connecting the second meter lead to pins 3
through the highest pin.
Once you have completely identified all the pins on the diagram then you
can safely ignore any unused keypad pins. You are now ready to wire the
keypad to your Arduino.
If you bought keypad look one below. You don’t need to perform above steps.
Below diagram is enough for knowing pin configuration.
Wiring the Circuit:
Follow the given pin order for wiring the circuit. As shown in left diagram. Start
from left to right.
official website.
Code for Arduino:
#include <Keypad.h>
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B' },
{'7','8','9','C'},
{'*','0','#','D'}
};
void setup(){
Serial.begin(9600);
void loop(){
if (key != NO_KEY){
Serial.println(key);
}
Introduction
DC Motor
DC motor converts electrical energy in the form of Direct Current into mechanical energy in
the form of rotational motion of the motor shaft.
The DC motor speed can be controlled by applying varying DC voltage; whereas the
direction of rotation of the motor can be changed by reversing the direction of current
through it.
For reversing the current, we can make use of H-Bridge circuit or motor driver ICs that
employ the H-Bridge technique.
For more information about DC motors and how to use them, H-Bridge circuit
configurations, and PWM technique, refer the topic DC Motors in the sensors and modules
section.
Interfacing Diagram
Interfacing DC Motor with Arduino UNO
Example
Here, we are going to control the speed and rotational direction of DC motor using Arduino
Uno.
Here, a potentiometer is used as a means for speed control and an input from a tactile
switch is used to change the direction of the motor.
L293D motor driver IC is used for controlling the direction of the motor.
PWM wave generated on the Arduino UNO is used to provide a variable voltage to the motor
through L293D. In Arduino, analogWrite function is used to generate PWM wave.
Sketch for Direction and Speed Control of DC Motor
void setup() {
pinMode(4, OUTPUT); /* Motor control pin 1 */
pinMode(7, OUTPUT); /* Motor control pin 2 */
pinMode(3, OUTPUT); /* PWM pin for Speed Control */
pinMode(2, INPUT_PULLUP); /* Interrupt pin for direction control */
attachInterrupt(digitalPinToInterrupt( 2), motor, FALLING); /* Interrupt on
falling edge on pin 2 */
}
void loop() {
int pwm_adc;
pwm_adc = analogRead(POT_input); /* Input from Potentiometer for speed control */
digitalWrite(4,d1);
digitalWrite(7,d2);
void motor(){
d1 = !d1;
d2 = !d2;
_delay_ms(200);
}
Functions Used
1. digitalPinToInterrupt(pin)
This function is used to configure the mode of interrupt event and declare
the ISR for that interrupt. The interrupt event and ISR is for the interrupt pin
declared by the function digitalPinToInterrupt(pin).
ISR in this function is the name of the ISR that will be used for this
interrupt.
mode defines when the interrupt will be triggered. There are four modes
available to choose from :
- LOW : trigger the interrupt whenever the pin is low.
- CHANGE : trigger the interrupt whenever the pin changes value.
- RISING : trigger when the pin goes from low to high.
- FALLING : trigger when the pin goes from high to low.
3. analogWrite(pin,value)
This function is used for generating PWM on PWM digital pins(pins
3,5,6,9,10,11 for Arduino UNO).
value can be any number between 0 to 255. 0 being 0% duty cycle and 255
being 100% duty cycle.