sunrays

Just another WordPress.com site

A Keypad for the Arduino!

After the LCD Display was connected and working, I decided to connect a keypad to the Arduino.

Mark Stanley and Alexander Brevig have developed an entire library for keypads with functions and examples neatly listed out. All you have to do is download it. The tutorial on “How to Connect the Keypad to the Arduino” looks very simple and straightforward too. So, well armed with the theory and a 3X4 matrix keypad from Element14, I got down to work.

3x4 Matrix Keypad from Element14

3x4 Matrix Keypad from Element14

That’ how my keypad looks. It has 12 keys arranged in a 3X4 matrix. What that basically means is that we need only 7 pins to access the 12 keys. For example, from the connection diagram (below), one can see that if the key ‘1’ is pressed, column 1 and row 1 are connected. Similarly, pressing ‘5’ connects column 2 and row 2. So by looking for the combination of row and column connected, you know which key is pressed. The code does all this by itself, so that is not our worry.

Connection Matrix

Connection Matrix

In my case, I hooked up the keypad output pins from 1 -7 to Arduino digital pins 15 – 21 so the connections are like this:

Pin Connections

Pin Connections

The code for declaring the keypad looks like this:

/**********************************************************************/
// start of creating the keypad//
const byte ROWS = 4; //Four rows//
const byte COLS = 3; //Three columns//
char keys[ROWS][COLS] =
{
{‘1′,’2′,’3’},
{‘4′,’5′,’6’},
{‘7′,’8′,’9’},
{‘*’,’0′,’#’}
}; // Define the Keymap//
byte rowPins[ROWS] = { 20, 15, 16, 18 };// Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins//
byte colPins[COLS] = { 19, 21, 17 }; // Connect keypad COL1, COL2 and COL3 to these Arduino pins//
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//end of creating the Keypad//
/**********************************************************************/

(The entire code for printing a punched key onto the LCD Diplay is at the end of this post.)

But then, things never work at the first go.

Instead of getting numbers printed on my LCD, I got two vertcical lines ‘||’.

Irrespective of which key i pressed, I got this – ‘||’ displayed on the LCD. Also, I noticed, it was not able to differentiate which key was being pressed – only the key punches were being detected. I changed the char key in the code to int key and started getting ‘0’ for everything.

After searching and hunting for hours, I realised I had missed an important step – resistors. The Arduino mentioned “…no external diodes or resistors are needed…” so I had given this no thought (I am not saying I understood what they said. I just skipped over it after these words. Oops!). Turns out, resistors need to be connected to either on the row pins or the column pins.

Instead of attaching external resistors, I used the internal pull-up resistors of the Arduino itself.

/**********************************************************************/

pinMode (21, INPUT);//Column 1
pinMode (19, INPUT);//Column 2
pinMode (17, INPUT);//Column 3
digitalWrite(21, HIGH);//Pull up resistor of Arduino
digitalWrite(19, HIGH);// Pull up resistor of Arduino
digitalWrite(17, HIGH);// Pull up resistor of Arduino

/**********************************************************************/

Just this simple step and yay! I had numbers on my LCD!

Numbers on the LCD!

Numbers on the LCD!

Complete code for printing keys on LCD:

/**********************************************************************/

#include <LiquidCrystal.h>
#include <Keypad.h>

// start of creating the keypad//
const byte ROWS = 4; //Four rows//
const byte COLS = 3; //Three columns//
char keys[ROWS][COLS] =
{
{‘1′,’2′,’3’},
{‘4′,’5′,’6’},
{‘7′,’8′,’9’},
{‘#’,’0′,’*’}
}; // Define the Keymap//
byte rowPins[ROWS] = { 20, 15, 16, 18 };// Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins//
byte colPins[COLS] = { 19, 21, 17 }; // Connect keypad COL1, COL2 and COL3 to these Arduino pins//
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//end of creating the Keypad//

// LCD Display//
LiquidCrystal lcd(30, 31, 32, 33, 34, 35); //LiquidCrystal lcd(RS, E, D4, D5, D6, D7), initialize the library with the numbers of the interface pins//
void setup()
{
lcd.begin(20,4); //20 columns and 4 rows in the lcd display//
pinMode (21, INPUT);//Column 1
pinMode (19, INPUT);//Column 2
pinMode (17, INPUT);//Column 3
digitalWrite(21, HIGH);//Pull up resistor of Arduino
digitalWrite(19, HIGH);// Pull up resistor of Arduino
digitalWrite(17, HIGH);// Pull up resistor of Arduino
lcd.setCursor(0,0);
lcd.print(“The Keypad Test”);
delay(1000);
lcd.setCursor(0, 1);
}

void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
lcd.print(key);
delay(500);
}
}
/**********************************************************************/

8 Comments »

Connecting the LCD Display to the Arduino.

HARDWARE:

The Arduino has an entire library dedicated to the task of interfacing a LCD Display with the Arduino board. To take advantage of that, the LCD Display has to have a Hitachi HD44780 compatible driver. There are many of them out there, and you can usually spot them by the 16-pin interface. Most common ones have a display of 16×2 or 20×4. I picked a 20×4 for my use.

Hitachi HD44780 20x4 Display

20x4 LCD Display

Any of the many pins of the Arduino can be used to connect to the LCD Display. Further, it can be done using 4 or 8 data lines. In case the LCD is being used with 4 data lines, D0-D3 are usually left unconnected.  The thing to remember is that when the variable LiquidCrystal is created in the code using the function LiquidCrystal(rs, rw, enable, d4, d5, d6, d7), the pins are initialised in the right order.  I used the 4 data line connection and connected the LCD Dsiplay to pins 30-35. The initialization looks like this:

// LCD Display
LiquidCrystal lcd(30, 31, 32, 33, 34, 35); //LiquidCrystal lcd(RS, E, D4, D5, D6, D7), initialize the library with the numbers of the interface pins

This was my first time soldering something like a LCD Display and boy did it hurt my eyes. The only thing I have soldered before is a few hundred solar cells but well, there is room for error there (Shorted a cell? Ok, ok. We have seven spare cells. Let’s use them.) Happy to say, I did a good job with the soldering! Oh well, so the soldering is done, let’s talk about the connections from the Display to the Arduino:

  • LCD RS pin to digital pin 30
  • LCD Enable pin to digital pin 31
  • LCD D4 pin to digital pin 32
  • LCD D5 pin to digital pin 33
  • LCD D6 pin to digital pin 34
  • LCD D7 pin to digital pin 35
  • A pot (but a resistor works just fine) to +5V and GND, with it’s wiper (output) to LCD screen VO pin (pin3).
LCD Connections

LCD Connections

The coding is rather simple and straightforward. The LiquidCrystal library outlines each function with examples.SOFTWARE:

My setup looks like this:

LCD Connection Set Up

LCD Connection Set Up


The final outcome was this:

LCD Display

It Works!

The code I used is here:

#include <LiquidCrystal.h>
// LCD Display
LiquidCrystal lcd(30, 31, 32, 33, 34, 35); //LiquidCrystal lcd(RS, E, D4, D5, D6, D7), initialize the library with the numbers of the interface pins
void setup()
{
lcd.begin(20,4); //20 columns and 4 rows in the lcd display
}
void loop()
{
lcd.setCursor(0, 0); //set the location at which subsequent text written to the LCD will be displayed
lcd.clear();
lcd.print(” It Works! 🙂 “);
delay(5000);
}

Leave a comment »

Blink Blink!

So, I got my first Arduino Mega and was all excited to upload my first sketch.

The first task was to connect the Arduino board to the computer so that sketches can be simply ‘uploaded’ to the board from the computer.

The Arduino site has a separate section called Getting Started w/ Arduino on Windows  (since I am using Windows) which states in 9 simple steps the way to get the Arduino up and running.

It’s just a matter of finding a USB cable, downloading the environment, connecting the board to the computer, installing the drivers and lanching the application. Nothing really looks like it can go wrong but then Windows kept telling me it could not locate drivers. I found out that quite a few people have faced this issue and most forums suggest fixing the issue by changing some computer registers. I am not a fan of tampering with registers and well after some trial and error, I got rid of it.

The Arduino site states “browse to the drivers/FTDI USB Drivers directory of the Arduino distribution” but I left the destination at drivers and that seems to have fixed the issue. (Do not ask me why or how. I have NO clue.)

Now, the Arduino environment comes loaded with some examples of simple and basic sketches that are pretty useful in checking the basic functioning of the entire set up at any point of time. So well, the next thing to do was to run the most basic sketch found in those examples – Blink. It makes the LED go on and off every second.

You can use either the on-board LED (connected to Digital Pin13) or use your own LED on another pin. Connecting the LED is straight forward enough. You just have to make sure the LED is connected in the right direction. The order look something like this:

output pin of Arduino-> resistor -> anode of LED (longer leg) -> LED  itself -> cathode of LED (shorter leg) -> GND pin of Arduino

Connecting LED to Arduino

Connecting LED to Arduino

The rest of the steps flowed smoothly enough and I had a nice blinking LED!!

The Blinking LED

The Blinking LED

Leave a comment »

My First Arduino

I am undergoing a geeky transformation. I never thought this thing could excite me but OMG! it’s AMAZING!

I got my first Arduino!!!!

For the uninitiated, Arduino is an open source hardware. It’s simply a microprocessor board that comes with all the basics things to get the microprocessor running. So all you have to do, is download the Arduino environment, plug the board to your laptop, add in any external hardware that you need and write amazing programs!

The Arduino language itself is very simple. It is merely a set of C/C++ functions which undergo a few change as they are compiled. The ‘sketch’, as it is known has a setup() that I compare to the main() in C language and a loop(). The setup() is run only once at the start or at reset. It is use to initialize variables, pin modes and start using libraries. The loop() usually contains the major section of the code and runs over and over again. It responds to changes and is used to control the board. Then, there are libraries that have been developed, by Arduino and other users, which further simplify things like writing to LCDs and getting input form a keypad. All this is done in the Arduino environment which looks like this.

Arduino Environment

Arduino Environment

There are a whole lot of Arduino boards to choose from. I picked a Arduino Mega– an awesome Arduino board based on the 8 bit ATmega2560. It has 54 digital input/output pins (of which 14 can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It plugs into a computer with a standard USB cable.

Arduino Mega 2560 R3

Arduino Mega 2560 R3

The board came in a rather pretty package (oh yes, presentation matters!) and for my life, I didn’t imagine it to be this small! I know it says “4 and2.1 inches” (approx. 10cm by 5 cm) on the site but how small that is hit me only once i saw the board. But well, the board is pretty big when compared to the other Arduinos.

Arduino 2560 Box / Package

Arduino 2560 Box

So, I have unwrapped my first Arduino and just started tinkering around with the board. Let’s see how it goes!

1 Comment »