You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino LCD计时代码添加Serial.begin(9600)后无法显示求助

Arduino添加Serial.begin后LCD无显示的解决方法

问题原因

你的代码存在两处关键引脚冲突:

  1. Arduino硬件串口(Serial)默认占用数字引脚0(RX)和1(TX),但你把LCD的rs设为0、en设为1,添加Serial.begin(9600)后,串口通信会干扰LCD的控制信号,导致LCD无法正常接收显示指令。
  2. LCD的d7引脚设为5,同时键盘的行引脚也用了5;LCD的对比度引脚cs设为6,键盘行引脚也包含6,这两处冲突会导致键盘和LCD互相干扰,即使没有串口也可能出现异常。

解决步骤

  • 重新分配LCD的控制引脚,避开0、1以及被键盘占用的引脚;
  • 调整键盘的引脚定义,避免和LCD引脚重叠;
  • 确保硬件接线和代码里的引脚定义完全匹配。

修正后的完整代码

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

// 修改LCD引脚,避开串口和键盘占用的引脚
const int rs = 10, en = 11, d4 = 2, d5 = 3, d6 = 4, d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// 修改对比度引脚,避开键盘占用的6
int cs = A0;  // 改用模拟引脚A0做对比度控制
const int contrast = 100;  // default contrast

// Time Set Buttons REMOVE LATER
int button1;
int button2;

// Pins definition for Time Set Buttons
int hs=8;// 修改小时设置引脚为8(原0被串口占用)
int ms=9;// 修改分钟设置引脚为9(原2被键盘列引脚占用)


// initial Time display is 12:59:45 PM
int h = 7;
int m = 6;
int s = 45;
int flag = 1;  //PM

// Keypad
const byte ROWS = 4; // number of rows
const byte COLS = 3; // number of columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

// 修改键盘行引脚,避开LCD和对比度引脚
byte rowPins[ROWS] = {13, 7, A1, A2}; // 替换原8、7、6、5,避免冲突
byte colPins[COLS] = {4, 3, 2};    // column pinouts of the keypad C1 = D4, C2 = D3, C3 = D2
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// Backlight Time Out 
const int Time_light = 150;
int bl_TO = Time_light;  // Backlight Time-Out
int bl = 9;  // Backlight pin
const int backlight = 120;  // no more than 7mA !!!

// For accurate Time reading, use Arduino Real Time Clock and not just delay()
static uint32_t last_time, now = 0;  // RTC

void setup() { 
  lcd.begin(16, 2);
  pinMode(hs,INPUT_PULLUP);// avoid external Pullup resistors for Button 1 REMOVE LATER
  pinMode(ms,INPUT_PULLUP);// and Button 2 REMOvE LATER
  analogWrite(cs, contrast);  // Adjust Contrast VO
  analogWrite(bl, backlight);  // Turn on Backlight
  now = millis();  // read RTC initial value
  Serial.begin(9600);
}

void loop()
{ 
  // 移除重复的lcd.begin,这个只需要在setup里执行一次
  // lcd.begin(16,2);// every second

// Update LCD Display
// Print TIME in Hour, Min, Sec + AM/PM
 lcd.setCursor(0,0);
 lcd.print("Time ");
 if(h<10)lcd.print("0");// always 2 digits
 lcd.print(h);
 lcd.print(":");
 if(m<10)lcd.print("0");
 lcd.print(m);
 lcd.print(":");
 if(s<10)lcd.print("0");
 lcd.print(s);

 if(flag==0) lcd.print(" AM");
 if(flag==1) lcd.print(" PM");
 
 lcd.setCursor(0,1);// for Line 2
 lcd.print("Precision clock");


// improved replacement of delay(1000) 
// Much better accuracy, no more dependant of loop execution time

for ( int i=0 ;i<5 ;i++)// make 5 time 200ms loop, for faster Button response
{

  while ((now-last_time)<200) //delay200ms
  { 
    now=millis();
  }
 // inner 200ms loop
 last_time=now; // prepare for next loop 

 // read Setting Buttons
 button1=digitalRead(hs);// Read Buttons
 button2=digitalRead(ms);

 //Backlight time out 
 bl_TO--;
 if(bl_TO==0)
 {
  analogWrite(bl,0);// Backlight OFF
  bl_TO++;
 }
 
 // Hit any to activate Backlight 
 if(  ((button1==0)|(button2==0)) & (bl_TO==1)  )
 {
  bl_TO=Time_light;
  analogWrite(bl,backlight);
  // wait until Button released
  while ((button1==0)|(button2==0))
  {
   button1=digitalRead(hs);// Read Buttons
   button2=digitalRead(ms);
  }
 }
 else
 // Process Button 1 or Button 2 when hit while Backlight on 
 {
  if(button1==0){
   h=h+1;
   bl_TO=Time_light;
   analogWrite(bl,backlight);
  }

 if(button2==0){
  s=0;
  m=m+1;
  bl_TO=Time_light;
  analogWrite(bl,backlight);
  }

/* ---- manage seconds, minutes, hours am/pm overflow ----*/
 if(s==60){
  s=0;
  m=m+1;
 }
 if(m==60)
 {
  m=0;
  h=h+1;
 }
 if(h==13)
 {
  h=1;
  flag=flag+1;
  if(flag==2)flag=0;
 }


 if((button1==0)|(button2==0))// Update display if time set button pressed
 {
  // Update LCD Display
  // Print TIME in Hour, Min, Sec + AM/PM
  lcd.setCursor(0,0);
  lcd.print("Time ");
  if(h<10)lcd.print("0");// always 2 digits
  lcd.print(h);
  lcd.print(":");
  if(m<10)lcd.print("0");
  lcd.print(m);
  lcd.print(":");
  if(s<10)lcd.print("0");
  lcd.print(s);

  if(flag==0) lcd.print(" AM");
  if(flag==1) lcd.print(" PM");
 
  lcd.setCursor(0,1);// for Line 2
  lcd.print("Precision clock");
 }


 } // end if else
}// end for



// outer 1000ms loop

 s=s+1; //increment sec. counting
    
    
// ---- manage seconds, minutes, hours am/pm overflow ----
 if(s==60){
  s=0;
  m=m+1;
 }
 if(m==60)
 {
  m=0;
  h=h+1;
 }
 if(h==13)
 {
  h=1;
  flag=flag+1;
  if(flag==2)flag=0;
 } 
 

 
// Loop end
}

额外优化说明

  • 移除了loop()里重复的lcd.begin(16,2),这个初始化操作只需要在setup()里执行一次,重复调用会不断重置LCD,影响显示;
  • 调整了时间设置按钮的引脚,避免和键盘、串口引脚冲突。

内容的提问来源于stack exchange,提问作者Akko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 09:04:57