搜索代码

1602 LCD Keypad Shield: Basic Code

1602 LCD Keypad Shield: Basic Code

In this tutorial, we will explore how to program and utilize a 1602 LCD Keypad Shield with an Arduino. This shield features a 16-character by 2-line display and a keypad that allows for various input functionalities. By the end of this guide, you will be able to display messages and respond to key presses effectively.

The focus will be on creating a simple program that detects key presses and displays corresponding messages on the LCD. You can follow along with the video for a visual demonstration (in video at 03:00).

Hardware Explained

The primary component in this build is the 1602 LCD Keypad Shield. This shield connects directly to the Arduino board and provides both a display and a keypad for user interaction. The LCD can show text messages, while the keypad allows users to select options through various buttons.

The keypad operates using an analog input, which the Arduino reads to determine which button is pressed. Each button has a unique resistance, allowing the microcontroller to identify the button based on the analog value received from the sensor.

Datasheet Details

ManufacturerGeneric
Part number1602 LCD Keypad Shield
Logic/IO voltage5 V
Supply voltage5 V
Output current (per channel)2 mA
Peak current (per channel)20 mA
PWM frequency guidanceN/A
Input logic thresholds0.3 Vcc to 0.7 Vcc
Voltage drop / RDS(on) / saturationN/A
Thermal limits70 °C
PackageShield format
Notes / variantsCompatible with Arduino Uno

  • Ensure the LCD is powered at 5 V to avoid damage.
  • Use proper pull-up resistors for the keypad if needed.
  • Debounce key presses in the code to avoid multiple triggers.
  • Keep an eye on the current limits to avoid overheating.
  • Test connections before powering the circuit.

Wiring Instructions

To wire the 1602 LCD Keypad Shield, simply plug it directly into the Arduino board. The shield is designed to align with the pin configuration of an Arduino Uno. Ensure the pins are securely connected.

The keypad buttons connect to the analog input pin A0. The LCD data lines are handled internally by the library used, so you won't need to wire them separately. The shield also uses a few digital pins for controlling the backlight and other functionalities, which are pre-defined in the library. If you follow the library documentation, you'll find that it typically handles these connections automatically.

Code Examples & Walkthrough

In the code, we first include the necessary library for controlling the LCD:

#include <LCD4Bit_mod.h>

This library facilitates communication with the 1602 LCD. We then create an instance of the LCD object with the number of lines specified.

Next, we initialize the LCD in the setup() function:

lcd.init();

This line prepares the LCD for use. After initialization, we can display a test message using the lcd.printIn() function.

In the main loop, we read the analog input to detect key presses:

adc_key_in = analogRead(0);

This line reads the value from the keypad. The value helps determine which key was pressed using the get_key() function that converts the ADC value into a key number.

Demonstration / What to Expect

When you run the code, pressing the buttons will display messages corresponding to the key pressed on the LCD. For example, pressing the right key will show "Right Key OK" on the display (in video at 05:30). If the key detection is not functioning correctly, check the wiring and ensure the correct libraries are included in your Arduino IDE.

Video Timestamps

  • 00:00 - Introduction
  • 03:00 - Code Overview
  • 05:30 - Key Press Demonstration
18-Source code for a 602 LCD keypad shield for Arduino
语言: C++
/*
 * // LCD4Bit_mod库的示例用法
 * // 下载自https://github.com/douggilliland/lb-Arduino-Code/tree/master/libraries/LCD4Bit_mod
 * // 2017年10月1日。此示例适用于1602 LCD键盘扩展板。
 * // 此代码用于Robojax频道的YouTube视频
 * // 观看视频https://youtu.be/naSSiS_9rEw
 */
#include <LCD4Bit_mod.h>
 // 创建一个对象来控制LCD。
 // 显示中的行数 = 1
LCD4Bit_mod lcd = LCD4Bit_mod(2);

 // 关键信息
char msgs[5][15] = {"Right Key OK ",
                    "Up Key OK    ",
                    "Down Key OK  ",
                    "Left Key OK  ",
                    "Select Key OK" };
int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

void setup() {
  pinMode(13, OUTPUT); // 我们将使用调试LED输出心跳信号。

  lcd.init();
 // 可选地,现在设置我们的应用程序特定显示设置,覆盖lcd.init()中lcd所做的任何操作。
 // lcd.commandWrite(0x0F);// 光标开启,显示开启,闪烁开启。(真糟糕!)
   lcd.clear();
  lcd.printIn("Robojax.com Testing");

}

void loop() {

	adc_key_in = analogRead(0); // 从传感器读取值
  digitalWrite(13, HIGH);
  key = get_key(adc_key_in); // 转换为按键操作

	if (key != oldkey) // 如果检测到按键输入
	{
    delay(50); // 等待去抖时间
		adc_key_in = analogRead(0); // 从传感器读取值
    key = get_key(adc_key_in); // 转换为按键操作
    if (key != oldkey)
    {
      oldkey = key;
      if (key >=0){
      lcd.cursorTo(2, 0); // 线 = 2,x = 0
  			lcd.printIn(msgs[key]);
      }
    }
  }

 // 延迟(1000);
  digitalWrite(13, LOW);





}

 // 将ADC值转换为键编号
int get_key(unsigned int input)
{
	int k;

	for (k = 0; k < NUM_KEYS; k++)
	{
		if (input < adc_key_val[k])
		{

    return k;
        }
	}

    if (k >= NUM_KEYS)
        k = -1; // 未按下有效的键

    return k;
}

资源与参考

文件📁

没有可用的文件。