Ads

Get STM32 tutorial using HAL at $10 for a limited time!

Tuesday, April 7, 2015

Arduino 16x2 LCD Keypad Shield

In this tutorial, I will share how to use 16x2 LCD Keypad Shield with Arduino Uno. This product is compatible with Arduino LCD library (LiquidCrystal.h). There are 5 buttons in this product (up, down, left, right, and select) that can be used for make project such as display menu items and select them with the buttons.
The 5 buttons in this product are connected to only single pin (A0), so this will save the use of pins. This technique usually called multiplexing. Multiplexing technique can be implemented with multiplexer IC such as 74153. But in this product they use a chain of resistor to do multiplexing. The buttons are connected to a chain of resistors using voltage divider technique. 


The output voltage of voltage divider circuit is depending on which button is pressed. For example if button 5 (left button) is pressed then the input voltage to the ADC will be 2.47V. You can calculate this by using voltage divider equation.



In this case (button left is pressed), the value of R1 is 2K and the value of R2 is 1.95K (330R + 620R + 1K). So if we calculate with voltage divider equation, the voltage output will be 2.47V. This voltage value can be read by analogRead() function on A0 pin. If we read analog value when left button is being pressed, then the return value will be about 505 (for 10-bit ADC). 505 is from (2.47V / 5V) * 1024.  

This is example code for read all ADC value when every buttons is pressed. The ADC value will be print to the LCD with along with analog input voltage value and which button is being pressed.
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int adc = 0;
float vin = 0.0;
 
void setup() 
{
    lcd.begin(16, 2);
   
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Arduino 16x2 LCD");
    lcd.setCursor(0, 1);
    lcd.print("& Keypad Shield.");
}
 
void loop() 
{
    // Read button state
    adc = analogRead(0);
    vin = (adc / 1024.0) * 5.0;
    
    // Button right
    if (adc < 100)         
    {
        printInfo("Button Right", adc, vin);    
    } 
    // Button up
    else if (adc < 200)    
    {
        printInfo("Button Up", adc, vin);    
    } 
    // Button down
    else if (adc < 400)
    { 
        printInfo("Button Down", adc, vin);    
    } 
    // Button left
    else if (adc < 600)    
    {
        printInfo("Button Left", adc, vin);    
    } 
    // Button select
    else if (adc < 800)
    {
        printInfo("Button Select", adc, vin);    
    }
    
    delay(100);
}

// Print which button is being pressed, ADC value, and voltage value
void printInfo(char *button, int adc, float vin)
{
    lcd.clear();
    
    lcd.setCursor(0, 0);
    lcd.print(button);
    
    lcd.setCursor(0, 1);
    lcd.print("ADC:");
    lcd.print(adc);  
    
    lcd.setCursor(8, 1);
    lcd.print("Vin:");
    lcd.print(vin, 1);
    lcd.print("V");    
}

No comments :

Post a Comment