Thursday, 28 May 2015

Interfacing Keypad with Arduino

Keypads Play Vital role in Embedded systems as Human Machine Interface devices.
Interfacing Keypad with arduino very easy.. We can find many tutorials on the web explaining how to do it and many of them are based on the Arduino Keypad library and we can find the sketch for our task....

like many arduino People the only language i know is Arduino Language and I don't want to use that library for simple Keypad interface which involves 12 switches (4 rows and 3 cols) cause i don't understand a bit in it. So i try to write a little code without library.

Basically Keypads as consist of Buttons which we can read the state of button with Arduino Digital Pin. when there are lot of inputs to read, it is not feasible too allocate one pin to each one of them. In this situations matrix keypad is useful.

if you consider reading a 25 inputs we can make 5*5 matrix which can implemented using 10 digital pins.

Internal Arrangement of Matrix Keypad is shown in fig.
Initially all switches are assumed to be released and there is no connection between Rows and Columns. when anyone of the switches are pressed the corresponding row and column is short circuited.... using this logic the button press can be detected.

How to write a Program to find the key pressed ?

The One of the technique to Identify the pressed keys are the method called Column Searching.
In This method particular Row is kept low and other rows are held High and logic state of each column line is scanned.

If a Particular is found to be having a state Low then that means that the key coming from in between that column and row.

steps to implement the program :

1.Declare the Row pins and column pins
2.initialize the key strokes with reference to the rows and cols as multidimensional array
3.set pin mode of Row pins to OUTPUTS
4.set pin mode of Column pins as inputs (remember this method is called column searching)
5.using for loop make one row pin Low each time and read the all column pins
6.if digitalRead returns low on col pins find the key using respective row and col numebrs.

The schematic of connections in proteus is

Arduino Code is:
/*
Keypad Interfacing with Arduino Without Keypad Library
 Author : Kunchala Anil
 It is implemented using a method called Column Searching
 */

const int row_pin[] = {
  5,4,3,2}; 
const int col_pin[] = {
  6,7,8}; // defining row and column pins as integer arrays

const int rows = 4, cols = 3; //defining the multi dimensional array size constants 

const char key[rows][cols] = {               // defining characters //for keystrokes in Multidimensional Array
  {
    '1','2','3'  }
  ,   
  {
    '4','5','6'  }
  ,
  {
    '7','8','9'  }
  ,
  {
    '*','0','#'  }  
};

void setup(){

  Serial.begin(9600); //begin the serial communication

  for(int i = 0; i<4; i++)
  {
    pinMode(row_pin[i],OUTPUT); //Configuring row_pins as Output Pins
    digitalWrite(row_pin[i],HIGH);//write HIGH to all row pins

    if(i<3)//we only have 3 columns
    {
      pinMode(col_pin[i],INPUT_PULLUP);//configure column pin as Input and activate internal //Pullup resistor
    }//end of if

  }//end of for loop

}//end of setup


void loop(){
  char key = read_key();
  if(key !='\n'){
    Serial.println(key);
    delay(100);
  }
}//end of loop

char read_key(){
  for(int row = 0;row < 4;row++)
  {
    digitalWrite(row_pin[0],HIGH);
    digitalWrite(row_pin[1],HIGH);
    digitalWrite(row_pin[2],HIGH);
    digitalWrite(row_pin[3],HIGH);
    digitalWrite(row_pin[row],LOW);
    //Serial.println(row_pin[row]);

    for(int col = 0;col<3;col++)
    {
      int col_state = digitalRead(col_pin[col]);
      if(col_state == LOW)
      {
        return key[row][col];
      }//end of if 
    }//end of col for loop
  }//end of row for loop
  return '\n';
}//end of read_key

Note : delay(100) is used to eliminate debounce 




you can see rows are changing the HIGH to LOW and and Low at column side when key is pressed.


Two Arduino's Communication with USART

In following schematic shows the connections to establish communication between two arduino's using Hardware Usart Arduino Uno connected to the keyboard scans the human inputs using keyboard and send the keystrokes as it is to the second arduino which shows the entered data.



Code for arduino connected to the Keypad (Tx) is :
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {
    '1','2','3'  }
  ,
  {
    '4','5','6'  }
  ,
  {
    '7','8','9'  }
  ,
  {
    '*','0','#'  }
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 
  5,4,3,2 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 
  6,7,8 }; 

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {

    Serial.print(key);
  }

}

we need arduino Keyboard library to compile the above code, can be found http://playground.arduino.cc/code/keypad

Arduino Connected to the Virtual Terminal(Rx) is:

#define password 123
#include<SoftwareSerial.h>
SoftwareSerial mySerial(10,11);//10-Rx  11-Tx
char data_buffer[15];
boolean read_data = false;
byte index;
void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  mySerial.println("welcome");
  mySerial.println("please enter password with starting letter * and ending letter # like *123#");
}

void loop(){

  if(Serial.available())
  {

    char input = Serial.read();
    if (input == '*')
    {
      read_data = true;
      index = 0;
    }
    else if(input == '#')
    {
      data_buffer[index] = 0;
      read_data  = false;
      mySerial.print("the value value you entered is");
      mySerial.println(atoi(data_buffer));
      if (atoi(data_buffer) == password){
        mySerial.println("password Matched");
      }
      else{
        mySerial.println("wrong Password");
      }
    }
    else
    {
      if(read_data)
      {
        data_buffer[index] = input;
        index = index + 1;
      }
    }
  }
}//end of loop

please see the video below to see how the schematic is done




Wednesday, 27 May 2015

Proteus Virtual Terminal using Arduino Hardware and Software Serial

Proteus schematic :

Arduino Code:

#include<SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // 10 - Rx    11 -Tx

void setup()
{
mySerial.begin(9600);

Serial.begin(9600);

}

void loop()
{
 Serial.println("enter  char");
 while(!Serial.available())
 {
 //wait until user enters the data
 }

 if(Serial.available())
{
 mySerial.println(Serial.read());
 }

}

Video :

Friday, 22 May 2015

Arduino Serial communication in proteus using Virtual Terminal

we can simulate the arduino serial communication using virtual terminal in the proteus

virtual terminal in proteus is located at instruments logo which highlighted as Instruments and can be located
virtual termina;
in the instruments we can choose the virtual instrument

we can select the serial transmission characteristics such as baud rate, parity... etc

virtual terminal

code which is dumped into Arduino is

const int pin = 13;
void setup()
{
pinMode(pin,OUTPUT);
Serial.begin(9600);
Serial.println("enter y to ON and n to OFF");
}

void loop()
{
while(!Serial.available())
{
//wait until user enters the data
}

if(Serial.available())
{
char input = Serial.read();
switch(input)
{
case 'y':
Serial.println("you entered y led ON");
digitalWrite(pin,HIGH);
break;
case 'n':
Serial.println("you entered n led is OFF");
digitalWrite(pin,LOW);
break;
default:
Serial.println("you entered wrong character");
}//end of switch
}//end of If

}//end of loop

Thursday, 21 May 2015

Arduino Relay driver using optocoupler in Proteus

In relay On condition
arduino_relay_ON

In relay Off condition
arduino_relay_off

Arduino Code is


const int pin =13;

void setup(){
pinMode(pin,OUTPUT);
Serial.begin(9600);
Serial.println("Please enter a char");
}

void loop(){
Serial.println("Please enter y to ON and n to OFF led");
while(!Serial.available())
{
//wait until user enters the data
}

char input;
if(Serial.available())
{
Serial.println(input = Serial.read());
if(input == 'y'){
Serial.println("you entered y led is ON");
digitalWrite(pin,HIGH);
}
else if(input == 'n'){
Serial.println("you entered n led is OFF");
digitalWrite(pin,LOW);
}
else{
Serial.println("you entered a wrong character");
}
}
}

Wednesday, 20 May 2015

Adding Arduino Library to proteus 8

when i googled to add arduino library to proteus8 version there are many examples came But I still faced the problem while installing arduino library in proteus version 8.

the usual procedure to add library is
1.download the library
2. paste it in your LIBRARY folder

the problem is I don't find the library folder in program files
so i clicked on the L in the P L device menu

library

and by clicking create library we can know the path where the LIBRARY folder is

Arduino Proteus8 turorial

simulation of arduino controlling relay.. step 1: when you double click on the Proteus8 icon the window open like this

home

step 2: go to FILE and click on NEW PROJECT a new project wizard is open like this new_project_wizard select a name and path for it and click next step 3: create the default schematic schematic click on the create schematic from the selected template and choose DEFAULT and select option for no pcb layout and and no firmware as shown in figures below pcb layoutfirmware and click finish finish after clicking finish we get a empty schematic like this empty schematic I assume that you installed Arduino Proteus library -- if not google it you can find many tutorials of how to do it. at the left panel we have some control tools to add components to the schematic control In above picture the highlighted one having names P L DEVICES when we click on P we have option to pick components pick devices IF you type Arduino in search bar you get the window like this arduino_pick select arduino UNO and click ok to add it to the devices for this we are gonna need following components 1.ULN2003 IC - relay driver 2.relay 3.dc source 4.ac source 5.lamp ULN2003 IC-- we can get this using search bar uln2003 and relay,lamp and ac source using keywords relay,lamp and alternator respectively but for dc source it took me some time to figure out what to do... we can get dc source from generator mode generator by clicking the symbol generator_sign and selecting DC dc_select and after aligning all these in the schematic get to this. arduino_relay to dump the code follow this procedure Using Arduino IDE type the following code void setup() { pinMode(13,OUTPUT); } void loop() { digitalWrite(13,HIGH); delay(1000); digitalWrite(13,LOW); delay(1000); } before verifying the code goto File -- Preferences in Arduino file menu select compilation as shown in fig preferences and press the verify button code_file copy the highlighted section and paste it in paste the above window comes by double clicking the arduino uno.. and run the simulation we can see the lamp is ON and OFF sequentially.