Part 4: Interfacing of Keypad with programming (Keypad Interfacing)
A matrix keypad is nothing but a systematic arrangement of buttons in horizontal and vertical
fashion. For example, a 4×4 Keypad consists of 16 keys or buttons that are arranged in 4 Rows and 4
Columns. The following image shows a typical Button Type 4×4 Matrix Keypad.
There are two types of matrix keypads available today. One is the traditional Button type Keypad as
shown in the image above while the other is a Membrane type Keypad, which doesn’t contain any
buttons but works due to electrical contact between surface of the key and the underlying circuit.
What does a 4x4 membrane keypad do?
The specific use of a 4 x 4 membrane keypad can vary depending on the application. For example, it
could be used for numeric or alphanumeric data entry, menu navigation, or as a simple control
interface for a device. Its compact size and low cost make it a popular choice for many applications
where a small and simple input device is needed.
How does a Membrane Keypad work?
When a button is pressed on a membrane keypad, it completes an electrical circuit and sends a signal
to the microcontroller. The microcontroller is a computer chip that processes the input from the
buttons and determines which key has been pressed.
All rows and columns in the keypad are wired together in order to reduce the number of i/o pins.
Normally connecting 16 push buttons like that would require 17 I/O pins. 16 for push buttons and 1
for ground.
By connecting rows and columns together only 8 pins are required. this specific technique is called
as multiplexing.
The 4x4 keypad has 4 rows and 4 columns so it has total of 4+4 =8 output pins
Assume all the rows of the Keypad are made LOW and all the columns of the Keypad are made HIGH
by the Microcontroller. When no button or key is pressed, this will be the default status of the rows
and columns.
Now, when a key is pressed, the corresponding column will become LOW as the current flows from
HIGH Column Pin to LOW Row Pin. The Microcontroller (or Arduino) can easily identify the Column of
the Key just by scanning for LOW on Columns.
The trick part comes in identifying the Row of the Key. For this, the Microcontroller should make the
Rows of the keypad HIGH, one-by-one and check for the Column Pins to become HIGH. This
procedure is continued till the earlier detected Column becomes HIGH.
This way, the microcontroller can determine both the Column and the Row of the Key and hence the
Key pressed is identified.
Circuit Diagram
In this circuit, I’ve connected the Rows of the keypad to the Digital Pins 0 through 3 of the Arduino
i.e. ROW1 to Digital Pin 0, ROW2 to Digital Pin 1, ROW3 to Digital Pin 2 and ROW4 to Digital Pin 3.
Similarly, the Columns of the Keypad are connected to Digital Pins 4 through 7 of Arduino.
Components Required
    •   Arduino UNO
    •   16×2 LCD Display
    •   4×4 Matrix Keypad
    •   10KΩ Potentiometer
    •   1KΩ Resistor (1/4 Watt)
    •   Breadboard
    •   Connecting Wires
    •   Power Supply
Circuit Design
The circuit design of the project is very similar to the Arduino Keypad interface shown above. The
additional component is the 16×2 LCD Display. The data pins of the LCD are connected to Digital Pins
11 through 8. The E and RS Pins of the 16×2 LCD Display are connected to Pins 12 and 13 of Arduino.
Code
Part 6: Interfacing of DC motor with programming
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.
A DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just
two leads, one positive and one negative. If you connect these two leads directly to a battery, the
motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.
Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver
Circuit or an IC.
Components Required −
    •   1x Arduino UNO board
    •   1x PN2222 Transistor
    •   1x Small 6V DC Motor (L293D)
    •   1x 1N4001 diode
    •   1x 270 Ω Resistor
    (A) Spinning of Motor
                                                Circuit Diagram
        Spin Control Arduino Code
   Code to Note
   The transistor acts like a switch, controlling the power to the motor. Arduino pin 3 is used to
   turn the transistor on and off and is given the name 'motorPin' in the sketch.
   Result
   Motor will spin in full speed when the Arduino pin number 3 goes high.
(B) Controlling Motor Speed (L293D)
                                         Schematic diagram of a DC motor
   Main Arduino Code
    Code to Note
    The transistor acts like a switch, controlling the power of the motor. Arduino pin 3 is used to
    turn the transistor on and off and is given the name 'motorPin' in the sketch.
    When the program starts, it prompts you to give the values to control the speed of the
    motor. You need to enter a value between 0 and 255 in the Serial Monitor.
    In the 'loop' function, the command 'Serial.parseInt' is used to read the number entered as
    text in the Serial Monitor and convert it into an 'int'. You can type any number here. The 'if'
    statement in the next line simply does an analog write with this number, if the number is
    between 0 and 255.
    Result
    The DC motor will spin with different speeds according to the value (0 to 250) received via
    the serial port.
(C) Controlling the direction of spinning motor
    To control the direction of the spin of DC motor, without interchanging the leads, you can use
    a circuit called an H-Bridge. An H-bridge is an electronic circuit that can drive the motor in
    both directions. H-bridges are used in many different applications. One of the most common
    application is to control motors in robots. It is called an H-bridge because it uses four
    transistors connected in such a way that the schematic diagram looks like an "H."
    The L298 bridge IC can control the speed and direction of DC motors and stepper motors,
    and can control two motors simultaneously. Its current rating is 2A for each motor.
    Pin IN1 of the IC L298 is connected to pin 8 of Arduino while IN2 is connected to pin 9. These
    two digital pins of Arduino control the direction of the motor. The EN A pin of IC is connected
    to the PWM pin 2 of Arduino. This will control the speed of the motor.
         Complete Arduino Code
Result
The motor will run first in the clockwise (CW) direction for 3 seconds and then counter-clockwise
(CCW) for 3 seconds.
Part 7: Interfacing of 16x2 LCD with programming
LCDs (Liquid Crystal Displays) are used in embedded system applications for displaying various
parameters and status of the system.
LCD 16x2 is a 16-pin device that has 2 rows that can accommodate 16 characters each.
LCD 16x2 can be used in 4-bit mode or 8-bit mode.
It is also possible to create custom characters.
It has 8 data lines and 3 control lines that can be used for control purposes.
For more information about LCD 16x2 and how to use it, refer the topic LCD 16x2 module in the
sensors and modules section.
The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several
interface pins at once to control the display. The interface consists of the following pins:
    •   A register select (RS) pin that controls where in the LCD's memory you're writing data to. You
        can select either the data register, which holds what goes on the screen, or an instruction
        register, which is where the LCD's controller looks for instructions on what to do next.
    •   A Read/Write (R/W) pin that selects reading mode or writing mode
    •   An Enable pin that enables writing to the registers
    •   8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you're writing to
        a register when you write, or the values you're reading when you read.
There's also a display contrast pin (Vo), power supply pins (+5V and GND) and LED Backlight (Bklt+
and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off
the LED backlight, respectively.
Components Required
    •   Arduino Board
    •   LCD Screen (compatible with Hitachi HD44780 driver)
    •   pin headers to solder to the LCD display pins
    •   10k ohm potentiometer
    •   220 ohm resistor
   •   hook-up wires
   •   breadboard
Schematic diagram of the circuit
                              The schematic (made using Fritzing).
CODE for Hello World Example
This example sketch prints Hello World! to the LCD and shows the time in seconds since the Arduino
was reset.
/*
 LiquidCrystal Library - Hello World
 Demonstrates the use a 16x2 LCD display. The LiquidCrystal library works with all LCD
displays that are compatible with the Hitachi HD44780 driver. There are many of them out
there, and you can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD and shows the time.
The circuit is as follows:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
https://docs.arduino.cc/learn/electronics/lcd-displays
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin with the arduino pin
number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
  lcd.init();
  lcd.clear();
  lcd.backlight(); // make sure backlight is on
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message of both the line of the LCD.
  lcd.setCursor(3,0); //Set cursor to character 2 on line 0
  lcd.print("Hello World!");
  lcd.setCursor(5,1); //Move cursor to character 2 on line 1
  lcd.print("LCD_I2C");
}
void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis() / 1000);
}