Arduino RTC Time Setting with Pushbutton & LCD Display | Debounce Code Tutorial
Here is an example Arduino code that sets the time in an RTC (such as the DS3231) using a pushbutton with debouncing, and displays the current time on a 16x2 LCD. The code uses the RTClib library for handling the RTC module and the LiquidCrystal library for displaying the time on the LCD. The button allows you to cycle through setting the hours, minutes, and seconds.
Components:
- RTC Module (DS3231): Used for keeping track of time.
- 16x2 LCD: Used to display the current time and time-setting menu.
- Pushbutton: Used to cycle through hours, minutes, and seconds for setting the RTC.
Circuit:
- RTC Module (DS3231):
- VCC → 5V
- GND → GND
- SDA → A4
- SCL → A5
- 16x2 LCD (with I2C interface):
- VCC → 5V
- GND → GND
- SDA → A4
- SCL → A5
- Pushbutton:
- One pin → Pin 2
- Other pin → GND
Arduino Code:
//////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
Key Concepts:
-
RTC Setup:
- The RTC is initialized using the
RTCliblibrary. If the RTC module has lost power, it will automatically set the time to the compile time of the sketch.
- The RTC is initialized using the
-
Debouncing the Push Button:
- The pushbutton is debounced using the
debounceDelayto prevent multiple reads from one press. - The button cycles through setting hours, minutes, and seconds. Every time the button is pressed, it increments the corresponding part of the time.
- The pushbutton is debounced using the
-
LCD Display:
- A 16x2 LCD is used to display the current time. It updates every second to show the current time in the format
HH:MM:SS. - When adjusting the time, the LCD will show the updated time part (hour, minute, or second).
- A 16x2 LCD is used to display the current time. It updates every second to show the current time in the format
-
Time Update:
- The
updateTime()function checks thetimeSetStateto determine which part of the time (hour, minute, or second) should be updated. - Each time you press the button, it increments the hour, minute, or second by one.
- The
Hardware Connections:
- RTC Module (DS3231):
- VCC → 5V
- GND → GND
- SDA → A4
- SCL → A5
- 16x2 LCD (I2C):
- VCC → 5V
- GND → GND
- SDA → A4
- SCL → A5
- Push Button:
- One side → Pin 2
- Other side → GND
This code will allow you to incrementally adjust the hour, minute, or second by pressing the button. The LCD will display the current time and provide feedback when you adjust the time.
Follow Manmohan Pal for more tech tutorials! 1) How to Install Arduino IDE & Drivers + Upload Your First Code | Beginner's Guide by Manmohan Pal video Link: https://youtu.be/HzhfrlgtWr8 Blog: https://arduinobymanmohan.blogspot.com/p/how-to-install-arduino-ide-drivers.html 2) How to Add LCD to Arduino Project: Connections, Libraries, and Code Explained by Manmohan Pal Video link: https://youtu.be/3D1QUHjCdeY Blog: https://arduinobymanmohan.blogspot.com/p/how-to-add-libraries-and-lcd-to-arduino.html 3) How to Add RTC to Arduino Project | Step-by-Step Tutorial by Manmohan Pal Video link: https://youtu.be/2Bf92OhbAL0 Blog: https://arduinobymanmohan.blogspot.com/p/how-to-add-rtc-to-arduino-project-step.html 4) How to Add Pushbuttons to Your Arduino Projects by Manmohan Pal Video Link: https://youtu.be/gqWiHdVMx_o https://arduinobymanmohan.blogspot.com/p/how-to-add-pushbuttons-to-your-arduino.html 5) Arduino RTC Time Setting with Pushbutton & LCD Display | Debounce Code Tutorial Video Link: https://youtu.be/Xh1mgZ3ySUg https://arduinobymanmohan.blogspot.com/p/set-time-in-rtc-using-push-button.html
No comments:
Post a Comment