Display output keypad to LCD using arduino UNO

arduino, keypad, lcd, wires (Small)

This tutorial is about how to interface keypad to arduino and display output of keypad to LCD. Sometime in your project you need to display output of keypad to LCD. Such as when you input a password, number or other project like calculator. So, this project is about how to display characters to LCD directly by keystrokes/pressing keypad.

Keypad used in this tutorial has 4 rows and 4 columns, and an LCD 20×4. You can use another type when you practicing by your self.

Parts needed to display keypad to LCD using arduino UNO

Parts needed is :

1. Arduino UNO

2. Keypad

3. LCD

4. Some wire to connect all parts

arduino, keypad, lcd, wires (Small)
arduino, keypad, lcd, wires

Steps to do

Connect keypad to arduino pin 9,8,7,6,5,4,3,2 (from left to right) like picture below :

arduino uno and keypad (Small)arduino uno and keypad

Then connect LCD to arduino to these pin :

LCD RS -> A1

LCD E -> A0

LCD D4 -> 13

LCD D5 -> 12

LCD D6 -> 11

LCD D7-> 10

And completed parts connected will be like this :

display keypad output to LCD (Small)
display keypad output to LCD

The code

Make sure you connect all wires and part correctly. Double check your wiring. If you already sure everything are correct, then now time to code !

First include all libarary you need, in this case you need keypad and liquidCrystal

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

LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);

String pad;

Configure keypad pin and the characters. You can change according to your keypad and pin.

const byte numRows= 4;
const byte numCols= 4;

char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//------------------------------------------------------------
byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols] = {5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad

In setup you need to initialize lcd

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
}

Make function to detect keystrokes

void bacaKeypad() {
  char keypressed = myKeypad.getKey(); //deteksi penekanan keypad
  String konv = String(keypressed);
  pad+=konv;
}

And the last, call all function in loop function

void loop() {
  // put your main code here, to run repeatedly:
  bacaKeypad();
  lcd.setCursor(0,0);
  lcd.print(pad);
  delay(100);
}

If you still unclear about all parts of code, here’s the complete code :

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

LiquidCrystal lcd(A1, A0, 13, 12, 11, 10);

String pad;
const byte numRows= 4;
const byte numCols= 4;

char keymap[numRows][numCols]= 
{
{'1', '2', '3', 'A'}, 
{'4', '5', '6', 'B'}, 
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//------------------------------------------------------------
byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols] = {5,4,3,2};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols); //mapping keypad

void setup() {
  // put your setup code here, to run once:
  lcd.begin(20,4);
}

void loop() {
  // put your main code here, to run repeatedly:
  bacaKeypad();
  lcd.setCursor(0,0);
  lcd.print(pad);
  delay(100);
}

void bacaKeypad() {
  char keypressed = myKeypad.getKey(); //deteksi penekanan keypad
  String konv = String(keypressed);
  pad+=konv;
}

 

Make arduino ADC more accurate to read small voltage

arduino uno AREF pin

Sometime when we make a project, we have to use sensor that has small voltage output. Small voltage make the arduino less accurate to read the signal. We know that arduino UNO has 10 bits adc resolution. This means arduino can read 0-5V with 1023 steps. So, 5000mv/1024 = 4,88mV. So arduino can only measure signal change in ADC if the voltage increase or decrease about 5mv. How if the sensor has output smaller than 5V?. There are some solution for this, first we can use amplifer for sensor, so sensor output will multiplied and become bigger. And we have the second solution, and this is simpler. We don’t need to amplify the sensor, but instead of it we will derease the voltage reference. 5V is voltage reference.

arduino analog input
arduino analog input

Decrease the ADC voltage reference

Arduino UNO or atmega328 has internal reference with voltage value 1.1V. We can use this by set the ADC voltage reference to internal. We can set analog reference in setup.

void setup()
{
analogReference(INTERNAL);
}

How about other arduino? Like arduino mega? Taken from arduino.cc website this is the internal reference provided by some arduino :
• DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
• INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8(not available on the Arduino Mega)
• INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
• INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
• EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.

ADC External Reference

And the next question maybe, can we use another analogReference that not provided by arduino? Yes, we can do that!. As long as the voltage value is in range 0-5V. Greater than 5V is obvously prohibited, because arduino operating voltage is 5V. So how we can do that?

void setup()
{
analogReference(EXTERNAL);
}

Use above code, and put the voltage in AREF pin. Don’t forget to use common ground for your analog Reference. If not, that will not work.

arduino uno AREF pin
arduino uno AREF pin

Example

We use LM35 sensor to read temperature. From datasheet we knew that this sensor will give output 10mV for every °C. For normal analog reference, we can only get half °C reading accuracy (remember what I explain in first paragraph).

For normal analog reference the code will be :

Temperature = (adc*5000/1023)/10;

And if we change the analog reference to 1.1V then the code should change to :

Temperature = (adc*1100/1023)/10;

This will give us more accurate result of temperature read by LM35. The arduino now can read every 0.93mV change in adc input. And we can get about 0.1°C of resolution.

Understanding driver circuit / electronics driver

motor DC with driver to arduino

Driver is something that can control other electronic component, in this case something means electronics components or circuit. For example we need driver to spin motor, power up High Power LED, open or close solenoid, switch on/off AC lamp and many more.

LED driver example
LED driver example

Why use driver?

Driver is also a tool that needed to control other electronics component or circuit. Basicly why we need a tool to do something because it’s much easier when we use the tool or even we can’t do that thing without the tool. For example, why we need a driver to power a high power LED?

Driver Example

Let say we want to make blink LED with high power LED using arduino UNO. The LED has operational voltage 12V and current 500mA. How we do that? We already knew that arduino UNO has voltage output 5V and maximum current is only 40mA. If we force 5V and 40mA to power up the LED not only the LED that will not glow, but the arduino can damaged.

Drive a motor using transistor

Let’s take another example. Say we want to control a DC motor with arduino. The DC motor rating is 5V 1A.  Now, we knew why we need driver. The motor need 1A current while arduino only can provide 40mA. If we connect the motor directly to ardunio, we can damage our arduino. And of course the motor will difficult to spin.

Now take a look at picture below

motor DC directly to arduino
motor DC directly to arduino

A motor connected to arduino directly, this means motor only has supply 5V and 40mA. Beside the motor will very difficult to spin, the arduino also can damage.

motor dc with batteries
motor dc with batteries

At the second picture, motor connected directly to batteries. The motor will spin fast, but we can’t control with arduino of course. 😛

motor DC with driver to arduino
motor DC with driver to arduino

So the solution taken from picture above. We can see that there is a resistor and a transistor used as a driver. The transistor is used to make motor draw current from batteries, not from arduino directly. So arduino still can control the motor, but the supply for motor is taken from the batteries. So the arduino will safe. Transistor here work like switch, and the arduino like our hand. The arduino will give signal to transistor through resistor and when transistor receive the signal, it will turn on the switch.

Arduino button without resistor pull up or pull down

arduino push button without resistor

Buttons are everywhere, many of our projects using button to interact with users. Many tutorials told us to use resistor as pull up or pull down for button. And maybe you have question and that’s why you are here. Does resistor really needed? Can we just skip the resistor and using only button?. Hmmmm fortunately the answer is yes! The arduino has internal pull up resistor that you can use it so you don’t need resistor to use your button.

So, how do we do that? Here’s the schematic example :

arduino push button without resistor
arduino push button without resistor

Or this

arduino push button without pull up
arduino push button without pull up

And this is the code:

int button = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(button, INPUT);
  digitalWrite(button, HIGH); //activate arduino internal pull up
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(button)==LOW){
    Serial.println("Button is pressed");
  }
  delay(500);
}

 

How is it? Do you love it? Comment below.

Make button respond immediately without delay in arduino

arduino button with pull up

Sometimes in our arduino project we need a button that repon immediately. When the button pressed the arduino respon directly without delay even there are delay in the sketch. Yes, we will make the button respon immediately and ignore the delay when the button pressed!

So how we do that?

If you are new in arduino, or microcontroller programming maybe you not yet familiar with interrupt. So our key now is interrupt. Just like the word interrupt. This means that you will interrupt the arduino, make arduino do what you what to do immediately and leave what its job for a while.

You can wire your arduino and button like below:

arduino button with pull up
arduino button pull up

If you don’t want to use resistor. You can read how to make button without resistor here. Make sure you are connect button to pin 2 or pin 3, because another pin in arduino UNO will not work for interrupt. How about another arduino board? You can refer to table below.

 

Board Digital Pins Usable For Interrupts
Uno, Nano, Mini, other 328-based 2, 3
Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based 0, 1, 2, 3, 7
Zero all digital pins, except 4
MKR1000 Rev.1 0, 1, 4, 5, 6, 7, 8, 9, A1, A2
Due all digital pins
101 all digital pins

 

You can read more about interrupt here.

OK, Let’s get our hand dirty!

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), glow, FALLING);  //attach interrupt on pin 2, then call function glow when interrupt happen and interrupt happen when input on pin 2 FALLING (transition from HIGH to LOW);
}

void loop() {
  glow();
  delay(5000);
}

void glow() {
  digitalWrite(ledPin, HIGH);
}

 

You can change FALLING to RISING if you want interrupt to happen when input goes from LOW to HIGH. Or you can use CHANGE instead and make interrupt happen when input is changing, means like you use RISING and FALLING at the same time.

How to use Thermocouple type K (MAX6675) with arduino

max6675 with thermocouple type K

Thermocouple is powerful temperature sensor. In this case, using thermocouple type K and MAX6675 can read temperature in range 0ºC to 1024ºC,  this sensos also waterproof. So it’s okay if you want place this sensor in water for measure water temperature. Beside that, thermocouple has tons of module that you can get easily, not like RTD which usually we have to make signal conditioning by ourselves. Sometimes it is really annoying.

max6675 with thermocouple type K
max6675 with thermocouple type K

And, let’s get dirty!

First wire your max6675 to arduino. In this example I use arduino UNO.

Pin SCK -> 6

Pin CS -> 5

Pin SO -> 4

Pin VCC-> 5V

Pin GND -> GND

 

Gambar koneksi

 

Double check your wiring, make sure everythings is connected correctly.

Now, it’s time to code.

Before we start code, to make our life easier we can use a library. As example we can use a library from adafruit. You can download here.

After you download the library, copy the folder to your arduino directory/library. It is usually in C:\program files (x86)\arduino\library. If your arduino software is running when copying the library. Make sure you restart the software after add the library to let arduino recognized new library.

Hmmm now it’s real time to code. Haha

You can use code below, or you can find this code at library example.

 #include "max6675.h"

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;
 
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;
 
void setup() {
 Serial.begin(9600);
 // use Arduino pins 
 pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
 pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
 
 Serial.println("MAX6675 test");
 // wait for MAX chip to stabilize
 delay(500);
}

void loop() {
 // basic readout test, just print the current temp 
 Serial.print("C = "); 
 Serial.println(thermocouple.readCelsius());
 Serial.print("F = ");
 Serial.println(thermocouple.readFahrenheit());
 delay(1000);
}

 

The result :

Gambar contoh hasil