Here is the wiring diagram for the project:wiring please
hour cannot be use as function error messageHere is the wiring diagram for the project:
![]()
And here is the code:
Code:#include <LiquidCrystal.h> // Pin assignments const int LED_RED_PIN = 2; const int LED_YELLOW_PIN = 3; const int LED_GREEN_PIN = 4; const int BUZZER_PIN = 5; const int BUTTON_PIN = 6; const int LCD_RS_PIN = 7; const int LCD_EN_PIN = 8; const int LCD_D4_PIN = 9; const int LCD_D5_PIN = 10; const int LCD_D6_PIN = 11; const int LCD_D7_PIN = 12; // LCD display constants const int LCD_WIDTH = 16; const int LCD_HEIGHT = 2; // Medicine schedule constants const int TOTAL_MEDICINES = 3; const int MEDICINE_TIMES[TOTAL_MEDICINES][2] = { {8, 30}, {13, 0}, {19, 30} }; // State variables int currentMedicine = 0; bool medicineTaken = false; // Initialize the LCD display LiquidCrystal lcd(LCD_RS_PIN, LCD_EN_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN); void setup() { // Set up the LED pins pinMode(LED_RED_PIN, OUTPUT); pinMode(LED_YELLOW_PIN, OUTPUT); pinMode(LED_GREEN_PIN, OUTPUT); // Set up the buzzer pin pinMode(BUZZER_PIN, OUTPUT); // Set up the button pin pinMode(BUTTON_PIN, INPUT_PULLUP); // Initialize the LCD display lcd.begin(LCD_WIDTH, LCD_HEIGHT); // Print the initial message to the LCD display lcd.print("Medicine Reminder"); lcd.setCursor(0, 1); lcd.print("Press button"); } void loop() { // Check if the button has been pressed if (digitalRead(BUTTON_PIN) == LOW) { // Reset the medicine taken state medicineTaken = false; // Check if it's time to take a medicine int hour = hour(); int minute = minute(); for (int i = 0; i < TOTAL_MEDICINES; i++) { if (hour == MEDICINE_TIMES[i][0] && minute == MEDICINE_TIMES[i][1]) { currentMedicine = i; break; } } // Display the medicine name on the LCD display lcd.clear(); lcd.print("Take medicine:"); lcd.setCursor(0, 1); switch (currentMedicine) { case 0: lcd.print("Medicine A"); break; case 1: lcd.print("Medicine B"); break; case 2: lcd.print("Medicine C"); break; } // Light up the appropriate LED switch (currentMedicine) { case 0: digitalWrite(LED_RED_PIN, HIGH); break; case 1: digitalWrite(LED_YELLOW_PIN, HIGH); break; case 2: digitalWrite(LED_GREEN_PIN, HIGH); break; } // Play a sound to alert the user tone(BUZZER_PIN, 1000, 2000); // Wait for the user to press the button while (digitalRead(BUTTON_PIN) == HIGH) { // Do nothing } // Mark the medicine as taken medicineTaken = true; // Turn off the appropriate LED and clear the LCD display switch (currentMedicine) { case 0: digitalWrite(LED_RED_PIN, LOW); break; case 1: digitalWrite(LED_YELLOW_PIN, LOW); break; case 2: digitalWrite(LED_GREEN_PIN, LOW); break; } lcd.clear(); lcd.print("Medicine taken!"); // Wait for a few seconds before moving on delay(5000); } // If it's not time to take a medicine, just display the time if (!medicineTaken) { lcd.clear(); lcd.print("Current time:"); lcd.setCursor(0, 1); lcd.print(hour()); lcd.print(":"); if (minute() < 10) { lcd.print("0"); } lcd.print(minute()); } }
Please note that the code uses theTimelibrary, so you will need to install this library in your Arduino IDE in order to compile and upload the code to the Arduino Nano.
what is the title of tthe libraryHere is the wiring diagram for the project:
![]()
And here is the code:
Code:#include <LiquidCrystal.h> // Pin assignments const int LED_RED_PIN = 2; const int LED_YELLOW_PIN = 3; const int LED_GREEN_PIN = 4; const int BUZZER_PIN = 5; const int BUTTON_PIN = 6; const int LCD_RS_PIN = 7; const int LCD_EN_PIN = 8; const int LCD_D4_PIN = 9; const int LCD_D5_PIN = 10; const int LCD_D6_PIN = 11; const int LCD_D7_PIN = 12; // LCD display constants const int LCD_WIDTH = 16; const int LCD_HEIGHT = 2; // Medicine schedule constants const int TOTAL_MEDICINES = 3; const int MEDICINE_TIMES[TOTAL_MEDICINES][2] = { {8, 30}, {13, 0}, {19, 30} }; // State variables int currentMedicine = 0; bool medicineTaken = false; // Initialize the LCD display LiquidCrystal lcd(LCD_RS_PIN, LCD_EN_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN); void setup() { // Set up the LED pins pinMode(LED_RED_PIN, OUTPUT); pinMode(LED_YELLOW_PIN, OUTPUT); pinMode(LED_GREEN_PIN, OUTPUT); // Set up the buzzer pin pinMode(BUZZER_PIN, OUTPUT); // Set up the button pin pinMode(BUTTON_PIN, INPUT_PULLUP); // Initialize the LCD display lcd.begin(LCD_WIDTH, LCD_HEIGHT); // Print the initial message to the LCD display lcd.print("Medicine Reminder"); lcd.setCursor(0, 1); lcd.print("Press button"); } void loop() { // Check if the button has been pressed if (digitalRead(BUTTON_PIN) == LOW) { // Reset the medicine taken state medicineTaken = false; // Check if it's time to take a medicine int hour = hour(); int minute = minute(); for (int i = 0; i < TOTAL_MEDICINES; i++) { if (hour == MEDICINE_TIMES[i][0] && minute == MEDICINE_TIMES[i][1]) { currentMedicine = i; break; } } // Display the medicine name on the LCD display lcd.clear(); lcd.print("Take medicine:"); lcd.setCursor(0, 1); switch (currentMedicine) { case 0: lcd.print("Medicine A"); break; case 1: lcd.print("Medicine B"); break; case 2: lcd.print("Medicine C"); break; } // Light up the appropriate LED switch (currentMedicine) { case 0: digitalWrite(LED_RED_PIN, HIGH); break; case 1: digitalWrite(LED_YELLOW_PIN, HIGH); break; case 2: digitalWrite(LED_GREEN_PIN, HIGH); break; } // Play a sound to alert the user tone(BUZZER_PIN, 1000, 2000); // Wait for the user to press the button while (digitalRead(BUTTON_PIN) == HIGH) { // Do nothing } // Mark the medicine as taken medicineTaken = true; // Turn off the appropriate LED and clear the LCD display switch (currentMedicine) { case 0: digitalWrite(LED_RED_PIN, LOW); break; case 1: digitalWrite(LED_YELLOW_PIN, LOW); break; case 2: digitalWrite(LED_GREEN_PIN, LOW); break; } lcd.clear(); lcd.print("Medicine taken!"); // Wait for a few seconds before moving on delay(5000); } // If it's not time to take a medicine, just display the time if (!medicineTaken) { lcd.clear(); lcd.print("Current time:"); lcd.setCursor(0, 1); lcd.print(hour()); lcd.print(":"); if (minute() < 10) { lcd.print("0"); } lcd.print(minute()); } }
Please note that the code uses theTimelibrary, so you will need to install this library in your Arduino IDE in order to compile and upload the code to the Arduino Nano.
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#define LED_PIN 8
#define BUTTON_PIN 9
RTC_DS3231 rtc;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int ledState = LOW;
int buttonState;
int lastButtonState = LOW;
DateTime now;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
lcd.begin(16, 2);
if (! rtc.begin()) {
lcd.print("RTC failed!");
while (1);
}
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
now = rtc.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print("Take Medicine");
buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
}
}
lastButtonState = buttonState;
delay(50);
}
OLED Display:
GND -> GND
VCC -> 5V
SCL -> A5
SDA -> A4
DS3231 RTC:
GND -> GND
VCC -> 5V
SCL -> A5
SDA -> A4
Button:
One leg -> D2
Other leg -> GND
LED:
Anode -> D3
Cathode -> GND
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
RTC_DS3231 rtc;
int buttonPin = 2;
int ledPin = 3;
bool buttonState = false;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("Current Time:");
display.setTextSize(2);
display.setCursor(30, 10);
if (hour < 10) {
display.print("0");
}
display.print(hour);
display.print(":");
if (minute < 10) {
display.print("0");
}
display.print(minute);
if (hour == 8 && minute == 0) {
digitalWrite(ledPin, HIGH);
display.setCursor(30, 30);
display.setTextSize(1);
display.print("Take your medicine!");
} else {
digitalWrite(ledPin, LOW);
}
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Last Taken:");
display.setTextSize(2);
display.setCursor(30, 10);
if (hour < 10) {
display.print("0");
}
display.print(hour);
display.print(":");
if (minute < 10) {
display.print("0");
}
display.print(minute);
}
display.display();
}
how to adjust the time interval of taking medicineSure, here's a wiring diagram and code for your medicine reminder project using an Arduino Nano, 0.9 OLED display, 1 button, 1 LED, and RTC DS3231.
Wiring diagram:
Code:OLED Display: GND -> GND VCC -> 5V SCL -> A5 SDA -> A4 DS3231 RTC: GND -> GND VCC -> 5V SCL -> A5 SDA -> A4 Button: One leg -> D2 Other leg -> GND LED: Anode -> D3 Cathode -> GND
Code:
Code:#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <RTClib.h> #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); RTC_DS3231 rtc; int buttonPin = 2; int ledPin = 3; bool buttonState = false; void setup() { Serial.begin(9600); if (!rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } if (rtc.lostPower()) { Serial.println("RTC lost power, setting the time"); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.display(); } void loop() { DateTime now = rtc.now(); int hour = now.hour(); int minute = now.minute(); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 0); display.println("Current Time:"); display.setTextSize(2); display.setCursor(30, 10); if (hour < 10) { display.print("0"); } display.print(hour); display.print(":"); if (minute < 10) { display.print("0"); } display.print(minute); if (hour == 8 && minute == 0) { digitalWrite(ledPin, HIGH); display.setCursor(30, 30); display.setTextSize(1); display.print("Take your medicine!"); } else { digitalWrite(ledPin, LOW); } buttonState = digitalRead(buttonPin); if (buttonState == LOW) { display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); display.println("Last Taken:"); display.setTextSize(2); display.setCursor(30, 10); if (hour < 10) { display.print("0"); } display.print(hour); display.print(":"); if (minute < 10) { display.print("0"); } display.print(minute); } display.display(); }
This code displays the current time on the OLED display, and turns on the LED and displays a message on the OLED when it's time to take the medicine (in this case, at 8:00 AM). When the button is pressed, it displays the last time the medicine was taken.
Make sure to include the required libraries in your Arduino IDE before uploading the code. I hope this helps!
OLED Display Arduino Nano
GND GND
VCC 5V
SDA A4
SCL A5
Button 1 D2
Button 2 D3
Button 3 D4
LED 1 D5
LED 2 D6
LED 3 D7
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_ADDR 0x3C
#define OLED_SDA A4
#define OLED_SCL A5
#define BUTTON_1 2
#define BUTTON_2 3
#define BUTTON_3 4
#define LED_1 5
#define LED_2 6
#define LED_3 7
RTC_DS1307 rtc;
Adafruit_SSD1306 display(128, 32, &Wire, OLED_ADDR);
void setup() {
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_3, INPUT_PULLUP);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.display();
delay(2000);
display.clearDisplay();
}
void loop() {
DateTime now = rtc.now();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Medicine Reminder");
display.setTextSize(2);
display.setCursor(0,10);
// Check button 1
if (digitalRead(BUTTON_1) == LOW) {
display.println("Take Medicine 1");
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
delay(5000);
digitalWrite(LED_1, LOW);
}
// Check button 2
else if (digitalRead(BUTTON_2) == LOW) {
display.println("Take Medicine 2");
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_3, LOW);
delay(5000);
digitalWrite(LED_2, LOW);
}
// Check button 3
else if (digitalRead(BUTTON_3) == LOW) {
display.println("Take Medicine 3");
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, HIGH);
delay(5000);
digitalWrite(LED_3, LOW);
}
// Display time
else {
display.println(now.toString());
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
}
display.display();
display.clearDisplay();
}
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int button1 = 7; // set button pin
int button2 = 6; // set button pin
int button3 = 8; // set button pin
int button4 = 9; // set button pin
int hour = 0; // set default values
int minute = 0;
int day = 0;
int interval = 0;
void setup() {
lcd.begin(16, 2);
pinMode(button1, INPUT_PULLUP); // set button as input
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
lcd.setCursor(0,0); // set cursor position
lcd.print("MEDICINE REMINDER");
delay(2000);
lcd.clear();
}
void loop() {
lcd.setCursor(0,0);
lcd.print("SET TIME (HH:MM)");
lcd.setCursor(0,1);
lcd.print("Press button 1");
// set hour and minute using buttons
while(digitalRead(button1) == HIGH){}
hour = hour + 1;
delay(200);
if(hour > 23){
hour = 0;
}
lcd.setCursor(0,0);
if(hour < 10){
lcd.print("0");
}
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
while(digitalRead(button2) == HIGH){}
minute = minute + 1;
delay(200);
if(minute > 59){
minute = 0;
}
lcd.setCursor(0,0);
if(hour < 10){
lcd.print("0");
}
lcd.print(hour);
lcd.print(":");
if(minute < 10){
lcd.print("0");
}
lcd.print(minute);
// set day interval using buttons
lcd.setCursor(0,1);
lcd.print("SET DAY INTERVAL");
lcd.setCursor(0,0);
lcd.print("Press button 3");
while(digitalRead(button3) == HIGH){}
day = day + 1;
delay(200);
if(day > 31){
day = 1;
}
lcd.setCursor(0,1);
lcd.print("Interval:");
lcd.print(day);
lcd.print(" day(s)");
// display reminder message
lcd.setCursor(0,0);
lcd.print("MEDICINE TIME");
lcd.setCursor(0,1);
lcd.print("Press button 4");
delay(1000);
// check if it's time for reminder message
while(digitalRead(button4) == HIGH){}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("REMINDER");
lcd.setCursor(0,1);
lcd.print("Take medicine!");
delay(3000);
// update day interval
interval = interval + 1;
if(interval == day){
interval = 0;
lcd.clear();
}
}
reminderTimes array in the code.c++
// include libraries
#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"
// initialize objects
RTC_DS3231 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// set reminder times (24-hour format)
int reminderTimes[] = {8, 12, 18}; // 8am, 12pm, 6pm
void setup() {
// initialize serial communication
Serial.begin(9600);
// initialize LCD
lcd.begin(16, 2);
lcd.print("Medicine Reminder");
// initialize RTC
rtc.begin();
// set RTC to current time if necessary
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// get current time from RTC
DateTime now = rtc.now();
// format current time for display on LCD
String timeString = now.hour() + ":" + now.minute() + ":" + now.second();
// display current time on LCD
lcd.setCursor(0, 1);
lcd.print(timeString);
// check if it's time to remind to take medicine
for (int i = 0; i < sizeof(reminderTimes) / sizeof(int); i++) {
if (now.hour() == reminderTimes[i]) {
// display reminder message on LCD
lcd.clear();
lcd.print("Time to take med!");
delay(5000); // display message for 5 seconds
lcd.clear();
lcd.print("Medicine Reminder");
break; // exit for loop
}
}
delay(1000); // wait 1 second before updating display
}
test code for rtc ds3231Here's a sample code for medicine reminder using Arduino Nano, 16x2 LCD, and RTC DS3231. This code displays the current time on the LCD and reminds you to take medicine at certain intervals throughout the day. You can adjust the reminder times by modifying thereminderTimesarray in the code.
Code:c++ // include libraries #include <Wire.h> #include <LiquidCrystal.h> #include "RTClib.h" // initialize objects RTC_DS3231 rtc; LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // set reminder times (24-hour format) int reminderTimes[] = {8, 12, 18}; // 8am, 12pm, 6pm void setup() { // initialize serial communication Serial.begin(9600); // initialize LCD lcd.begin(16, 2); lcd.print("Medicine Reminder"); // initialize RTC rtc.begin(); // set RTC to current time if necessary if (!rtc.isrunning()) { Serial.println("RTC is NOT running!"); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } } void loop() { // get current time from RTC DateTime now = rtc.now(); // format current time for display on LCD String timeString = now.hour() + ":" + now.minute() + ":" + now.second(); // display current time on LCD lcd.setCursor(0, 1); lcd.print(timeString); // check if it's time to remind to take medicine for (int i = 0; i < sizeof(reminderTimes) / sizeof(int); i++) { if (now.hour() == reminderTimes[i]) { // display reminder message on LCD lcd.clear(); lcd.print("Time to take med!"); delay(5000); // display message for 5 seconds lcd.clear(); lcd.print("Medicine Reminder"); break; // exit for loop } } delay(1000); // wait 1 second before updating display }
Note: You'll need to install the RTClib library (You do not have permission to view the full content of this post. Log in or register now.) for this code to work.
Here's a simple test code for the RTC DS3231 module with Arduino Nano and 16x2 LCD.test code for rtc ds3231
c++
#include <Wire.h>
#include <RTClib.h> // include the RTClib library
#include <LiquidCrystal.h> // include the LiquidCrystal library
RTC_DS3231 rtc; // initialize the RTC object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object
void setup() {
// initialize the serial communication
Serial.begin(9600);
// initialize the LCD
lcd.begin(16, 2);
lcd.print("RTC Test");
// initialize the I2C communication
Wire.begin();
// initialize the RTC
rtc.begin();
// set the RTC time to the current time if not set
if (!rtc.initialized()) {
Serial.println("RTC is NOT initialized, setting time...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// get the current time from the RTC
DateTime now = rtc.now();
// display the time on the LCD
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
// send the time to the serial monitor
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.println(now.second(), DEC);
delay(1000); // wait for 1 second
}
wiring for this pleaseHere's a simple test code for the RTC DS3231 module with Arduino Nano and 16x2 LCD.
Code:c++ #include <Wire.h> #include <RTClib.h> // include the RTClib library #include <LiquidCrystal.h> // include the LiquidCrystal library RTC_DS3231 rtc; // initialize the RTC object LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object void setup() { // initialize the serial communication Serial.begin(9600); // initialize the LCD lcd.begin(16, 2); lcd.print("RTC Test"); // initialize the I2C communication Wire.begin(); // initialize the RTC rtc.begin(); // set the RTC time to the current time if not set if (!rtc.initialized()) { Serial.println("RTC is NOT initialized, setting time..."); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } } void loop() { // get the current time from the RTC DateTime now = rtc.now(); // display the time on the LCD lcd.setCursor(0, 1); lcd.print(now.hour(), DEC); lcd.print(":"); lcd.print(now.minute(), DEC); lcd.print(":"); lcd.print(now.second(), DEC); // send the time to the serial monitor Serial.print(now.hour(), DEC); Serial.print(":"); Serial.print(now.minute(), DEC); Serial.print(":"); Serial.println(now.second(), DEC); delay(1000); // wait for 1 second }
Note that you need to install the RTClib library for this code to work. You can download it from the Arduino Library Manager or from the GitHub repository (You do not have permission to view the full content of this post. Log in or register now.). Also, make sure you have connected the RTC module to the correct pins of the Arduino Nano and the 16x2 LCD.
Sure, here's the wiring diagram for connecting the RTC DS3231 module and 16x2 LCD to an Arduino Nano:wiring for this please
theres an error for this codeHere's a simple test code for the RTC DS3231 module with Arduino Nano and 16x2 LCD.
Code:c++ #include <Wire.h> #include <RTClib.h> // include the RTClib library #include <LiquidCrystal.h> // include the LiquidCrystal library RTC_DS3231 rtc; // initialize the RTC object LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object void setup() { // initialize the serial communication Serial.begin(9600); // initialize the LCD lcd.begin(16, 2); lcd.print("RTC Test"); // initialize the I2C communication Wire.begin(); // initialize the RTC rtc.begin(); // set the RTC time to the current time if not set if (!rtc.initialized()) { Serial.println("RTC is NOT initialized, setting time..."); rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } } void loop() { // get the current time from the RTC DateTime now = rtc.now(); // display the time on the LCD lcd.setCursor(0, 1); lcd.print(now.hour(), DEC); lcd.print(":"); lcd.print(now.minute(), DEC); lcd.print(":"); lcd.print(now.second(), DEC); // send the time to the serial monitor Serial.print(now.hour(), DEC); Serial.print(":"); Serial.print(now.minute(), DEC); Serial.print(":"); Serial.println(now.second(), DEC); delay(1000); // wait for 1 second }
Note that you need to install the RTClib library for this code to work. You can download it from the Arduino Library Manager or from the GitHub repository (You do not have permission to view the full content of this post. Log in or register now.). Also, make sure you have connected the RTC module to the correct pins of the Arduino Nano and the 16x2 LCD.
I apologize for the mistake in my previous response. Here's the corrected code:theres an error for this code
c++
#include <Wire.h>
#include <RTClib.h> // include the RTClib library
#include <LiquidCrystal.h> // include the LiquidCrystal library
RTC_DS3231 rtc; // initialize the RTC object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object
void setup() {
// initialize the serial communication
Serial.begin(9600);
// initialize the LCD
lcd.begin(16, 2);
lcd.print("RTC Test");
// initialize the I2C communication
Wire.begin();
// initialize the RTC
rtc.begin();
// set the RTC time to the current time if not set
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, setting time...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// get the current time from the RTC
DateTime now = rtc.now();
// display the time on the LCD
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
// send the time to the serial monitor
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.println(now.second(), DEC);
delay(1000); // wait for 1 second
}
if (! rtc.isrunning()) instead of if (! rtc.initialized()).