2. Push button Switch
Switches are really simple components. When you press a
button or flip a lever, they connect two contacts together so
that electricity can flow through them.
The little tactile switches that are used in our example have
four connections, which can be a little confusing.
2
3. Push button Switch
Actually, there are only really two electrical connections, as
inside the switch package pins B and C are connected
together, as are A and D.
3
4. Push button Switch
To build such application, you will need the
following Components:
• Arduino Uno
• USB cable
• Push Switch
• Jumper Wires
• LED
• Resistor
4
6. Push button –With Code
int d=2; // to store on or off value
void setup()
{pinMode(2,INPUT);
pinMode(13,OUTPUT);
}
void loop()
{
d=digitalRead(2);
if(d==0)
{digitalWrite(13,HIGH);}
else
{digitalWrite(13,LOW);}
}
6
8. Keypad
Keypads are used in all types of devices, including
cell phones, fax machines, microwaves, ovens,
door locks, etc. They're practically everywhere.
electronic devices use them for user input. So
knowing how to connect a keypad to a
microcontroller such as an Arduino is very
valuable for building many different types of
commercial products.
when a key is pressed, it show up at the Serial
Monitor on your computer. For this project, the
type of keypad we will use is a matrix keypad. The
matrix keypad we are using has 16 keys (0-9, A-D,
*, #), yet only 8 output pins.
8
9. Keypad
How the rows and column are
arranged inside the keypad is
shown in the figure.
The Arduino can determine
which button was pressed.
For example, when key 1 is
pressed, column 1 and row 1
are shorted. The Arduino will
detect that and input a 1 to the
program.
9
R1
R2
R3
R4
C1 C2 C3 C4
R1 R2 R3R4C1C2 C3C4
10. Keypad
To build such application, you need the following
components:
• Arduino Uno
• USB cable
• 4x4 Matrix Keypad
• Jumper wires
10
12. Keypad
#include <Keypad.h>
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns
//just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
12
13. Keypad
//Code that shows the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins,
colPins, numRows, numCols);
void setup()
{
Serial.begin(9600);
}
13
14. Keypad
//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is
//pressed during the whole keypad scan process
void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
Serial.print(keypressed);
}
}
}
14