Touch Keypad Interfacing with ATmega32 Microcontroller

Published  July 13, 2015   0
User Avatar Dilip Raja
Author
Touch Keypad Interfacing with AVR Microcontroller

In this tutorial we are going to interface a 4x2 (8 key) touch keypad with ATMEGA32A microcontroller. We all know keypad is one of the most important input devices used in electronics engineering. This module does not have actual keys, but have specially designed capacitive metal pads, and these pads are very sensitive. So when a person gets in contact with one of the pads, there will a capacitive change in the corresponding loop, and this change will be sensed by the control electronic in the module. As a response to the touch the corresponding pad output pin goes high.

 

For a eight key touch pad we will have eight outputs. Although there are other features with this module, we are not going to discuss them here.

 

Components Required

Hardware: ATMEGA32 microcontroller, power supply (5v), AVR-ISP PROGRAMMER, JHD_162ALCD (16x2LCD), 100uF capacitor, 100nF capacitor, 1KΩ resistor (2 pieces), Touch keypad module.

Software: Atmel studio 6.1 or Atmel studio 6.2, progisp or flash magic.

 

Circuit Diagram and Working Explanation

Touch keypad interfacing circuit diagram

In circuit PORTB of ATMEGA32 is connected to data port LCD. Here one should remember to disable the JTAG communication in PORTC to ATMEGA by changing the fuse bytes, if one wants to use the PORTC as a normal communication port. In 16x2 LCD there are 16 pins over all if there is a back light, if there is no back light there will be 14 pins. One can power or leave the back light pins. Now in the 14 pins there are 8 data pins (7-14 or D0-D7), 2 power supply pins (1&2 or VSS&VDD or gnd&+5v), 3rd pin for contrast control (VEE-controls how thick the characters should be shown), and 3 control pins (RS&RW&E)

 

In the circuit, you can observe that I have only took two control pins, this give the flexibility of better understanding, the contrast bit and READ/WRITE are not often used so they can be shorted to ground. This puts LCD in highest contrast and read mode. We just need to control ENABLE and RS pins to send characters and data accordingly.

 

The connections which are done for LCD are given below:

PIN1 or VSS to ground

PIN2 or VDD or VCC to +5v power

PIN3 or VEE to ground (gives maximum contrast best for a beginner)

PIN4 or RS (Register Selection) to PD6 of uC

PIN5 or RW (Read/Write) to ground (puts LCD in read mode eases the communication for user)

PIN6 or E (Enable) to PD5 of uC

PIN7 or D0 to PB0 of uC

PIN8 or D1 to PB1 of uC

PIN9 or D2 to PB2 of uC

PIN10 or D3 to PB3 of uC

PIN11 or D4 to PB4 of uC

PIN12 or D5 to PB5 of uC

PIN13 or D6 to PB6 of uC

PIN14 or D7 to PB7 of uC

 

In the circuit you can see we have used 8bit communication (D0-D7) however this is not a compulsory, we can use 4bit communication (D4-D7) but with 4 bit communication program becomes a bit complex.

 

So by observing the above table we are connecting 10 pins of LCD to controller in which 8 pins are data pins and 2 pins for control.

 

Before going further, it is important to know that, the capacitive module works for a voltage of 2.5V. And also the current drawn by the touch module is not high. So for getting 2.5V for the module from 5V we are going to use voltage divider circuit.

The voltage divider circuit former by resistors is shown in the figure below.

Voltage divider circuit

Now the voltage divider circuit provides low voltages for modules and other references. As shown in figure, the output voltage at the midpoint is a ratio of resistances. So for getting 2.5v from 5V we are going to use R1 = R2 = 1KΩ, so for a supply voltage of 5V the midpoint voltage will be 2.5V with respect to ground. This voltage from the divider circuit is connected to the module. A capacitor is connected across it for filtering the harmonics, as shown in the circuit diagram.

 

The output port of touch module is connected to atmega controller, so whenever a pad is touched the corresponding pin output goes high. This logic change is sensed by the controller. The controller shows the digit on the LCD based on the pin, which goes high.

As of security, one can pull down all the module output pins to ground through 10K resistors, although its not compulsory.

 

Working of TOUCH KEAYPAD INTERFACE is best explained in step by step of C code given below.

Code

#include <avr/io.h>

//header to enable data flow control over pins

#define F_CPU 1000000      

//telling controller crystal frequency attached

#include <util/delay.h>

//header to enable delay function in program

#define    E   5

//giving name “enable”  to 5th pin of PORTD, since it Is connected to LCD enable pin

#define RS  6

//giving name “registerselection” to 6th pin of PORTD, since is connected to LCD RS pin

void send_a_command(unsigned char command);

void send_a_character(unsigned char character);

void send_a_string(char *string_of_characters);    

int main(void)

{

DDRB = 0xFF;

// putting portB and portD as output pins

DDRD = 0xFF;

DDRA = 0;//porta as input

_delay_ms(50);//giving delay of 50ms

int key=0;//allocating integer to reset the LCD once it reaches its display limit

int keypressed=0;//integer for storing matrix value

send_a_command(0x01); //Clear Screen 0x01 = 00000001

_delay_ms(50);

send_a_command(0x38);//telling lcd we are using 8bit command /data mode

_delay_ms(50);

send_a_command(0b00001111);//LCD SCREEN ON and courser blinking

send_a_string("PRESS A KEY");//displaying a string

send_a_command(0x80 + 0x40 +0);// moving courser to second line of LCD

while(1)

{

if (bit_is_set(PINA,0))

{

send_a_string("1");//if pin0 goes high show” 1”

_delay_ms(220);

key++;

}

if (bit_is_set(PINA,1))

{

send_a_string("2");// if pin1 goes high show” 2”

_delay_ms(220);

key++;

}

if (bit_is_set(PINA,2))

{

send_a_string("3");// if pin2 goes high show” 3”

_delay_ms(220);

key++;

}

if (bit_is_set(PINA,3))

{

send_a_string("4");// if pin3 goes high show” 4”

_delay_ms(220);

key++;

}

if (bit_is_set(PINA,4))

{

send_a_string("5");// if pin4 goes high show” 5”

_delay_ms(220);

key++;

}

if (bit_is_set(PINA,5))

{

send_a_string("6");// if pin5 goes high show” 6”

_delay_ms(220);

key++;

}

if (bit_is_set(PINA,6))

{

send_a_string("7");// if pin6 goes high show” 7”

_delay_ms(220);

key++;

}

if (bit_is_set(PINA,7))

{

send_a_string("8");// if pin7 goes high show” 8”

_delay_ms(220);

key++;

}

if (key==16)//if 16 characters are shown on LCD

{

send_a_command(0x01); //clear lcd

send_a_string("PRESS A KEY");//display string

send_a_command(0x80 + 0x40 +0);//move courser to second line.

key=0;

}

}

}

void send_a_command(unsigned char command)

{

PORTA = command;

PORTD &= ~ (1<<RS); //putting 0 in RS to tell lcd we are sending command

PORTD |= 1<<E; //telling lcd to receive command /data at the port

_delay_ms(50);

PORTD &= ~1<<E;//telling lcd we completed sending data

PORTA= 0;

}

void send_a_character(unsigned char character)

{

PORTA= character;

PORTD |= 1<<RS;//telling LCD we are sending data not commands

PORTD |= 1<<E;//telling LCD to start receiving command/data

_delay_ms(50);

PORTD &= ~1<<E;//telling lcd we completed sending data/command

PORTA = 0;

}

void send_a_string(char *string_of_characters)

{

while(*string_of_characters > 0)

{

send_a_character(*string_of_characters++);

}

}

Video

Have any question realated to this Article?

Ask Our Community Members