Sie sind auf Seite 1von 11

4×4 keypad interfacing with Arduino

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.

Keypad Pin 1 (R4) –> Arduino Pin 5

Keypad Pin 2 (R3) –> Arduino Pin 4

Keypad Pin 3 (R2) –> Arduino Pin 3

Keypad Pin 4 (R1) –> Arduino Pin 2

Keypad Pin 5 (C4) –> Arduino Pin 9

Keypad Pin 6 (C3) –> Arduino Pin 8

Keypad Pin 7 (C2) –> Arduino Pin 7

Keypad Pin 8 (C1) –> Arduino Pin 6

Connect the circuit according to given diagram.


Add Keypad Library:
Before going to programming part first download keypad library from Arduino

official website.
Code for Arduino:

#include <Keypad.h>

const byte ROWS = 4; //four rows

const byte COLS = 4; //four columns

char keys[ROWS][COLS] = {

{'1','2','3','A'},

{'4','5','6','B' },

{'7','8','9','C'},

{'*','0','#','D'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row


pinouts of the keypad

byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column


pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins,


ROWS, COLS );

void setup(){

Serial.begin(9600);

void loop(){

char key = keypad.getKey();

if (key != NO_KEY){

Serial.println(key);
}

Testing the Circuit:


1. After hardware connection, insert the sample sketch into the Arduino IDE.
2. Using a USB cable, connect the ports from the Arduino to the computer.
3. Upload the program.
4. See the results in the serial monitor

DC Motor Interfacing with Arduino UNO

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 applying varying voltage, we can make use of PWM technique.

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

const int POT_input = A1; /* assign ADC Channel */


bool d1 = HIGH;
bool d2 = LOW;

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);

analogWrite(3, pwm_adc / 4);


}

void motor(){
d1 = !d1;
d2 = !d2;

_delay_ms(200);
}
Functions Used

1. digitalPinToInterrupt(pin)

 This function is used to declare the digital pin as an interrupt pin.


 Example,digitalPinToInterrupt(2) is used to declare digital pin 2 as an
interrupt pin.

 On UNO board, only pins 2 and 3 can be configured as interrupt pins.


Hence, argument to this function can only be pin 2 or pin 3.

2. attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

 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.

 Example, attachInterrupt(digitalPinToInterrupt(2), motor, FALLING)


configures digital pin 2 as an interrupt pin with ISR named motor and which
generates interrupt for every falling edge event on pin 2.

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.

Das könnte Ihnen auch gefallen