From 706829905cbc94038ced13b35bf8ebc1fe6c74b5 Mon Sep 17 00:00:00 2001 From: Tibor Kiss Date: Tue, 17 Sep 2024 11:35:39 +0200 Subject: [PATCH] Add time module. v: 1.1.0 --- README.md | 34 ++++++++------ djbooth_sensor/djbooth_sensor.ino | 76 ++++++++++++++++++++----------- 2 files changed, 70 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 4de00fb..d652725 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Project Background -This project is designed to monitor temperature using the **DS18B20** sensor and display the readings in both Celsius and Fahrenheit on a **16x2 LCD** display. It aims to create a simple but effective way to visualize real-time temperature in a DJ booth or any other environment where monitoring temperature is essential for comfort and equipment safety. +This project is designed to monitor temperature using the **DS18B20** sensor and get current datetime using the **DS3231+AT24C32 RTCM** real-time module. Then display the current time in EU timeformat and temperature in Celsius and on a **16x2 LCD** display. It aims to create a simple but effective way to visualize real-time temperature in a DJ booth or any other environment where monitoring temperature is essential for comfort and equipment safety. The code leverages the I2C protocol to communicate with the LCD and the **DallasTemperature** library to read from the temperature sensor. The display welcomes the user with a custom message and then continuously updates the temperature values in both Celsius and Fahrenheit. @@ -10,6 +10,7 @@ The code leverages the I2C protocol to communicate with the LCD and the **Dallas - **AR-NANOCH-TYPE-C**: Arduino Nano clone development board. - **DS18B20**: One-wire digital temperature sensor. +- **DS3231+AT24C32 RTCM**: Real-time clock module with EEPROM. - **KC-1602-BB-I2C**: 16x2 I2C LCD display for displaying messages and temperature readings. - **4.7kΩ resistor**: Used to pull up the data line of the DS18B20 sensor. - **Breadboard**: Optional, for wiring connections. @@ -27,6 +28,7 @@ You need to install the following libraries using the Arduino IDE's Library Mana - **OneWire**: Used for communicating with the DS18B20 temperature sensor. - **DallasTemperature**: Simplifies reading from the DS18B20 sensor. - **LiquidCrystal_I2C**: For controlling the 16x2 LCD display via I2C. +- **Wire**: Required for I2C communication. To install these libraries: @@ -42,6 +44,13 @@ To install these libraries: - **GND (Black)**: Connect to Arduino's **GND**. - **Data (Yellow)**: Connect to Arduino **D2**. Add a **4.7kΩ resistor** between the **VCC** and **Data** lines. +- **DS3231+AT24C32 RTCM** connections: + + - **VCC**: Connect to Arduino's **5V**. + - **GND**: Connect to Arduino's **GND**. + - **SDA**: Connect to Arduino's **A4**. + - **SCL**: Connect to Arduino's **A5**. + - **16x2 LCD display** (I2C): - **VCC**: Connect to Arduino's **5V**. - **GND**: Connect to Arduino's **GND**. @@ -59,29 +68,26 @@ Once the hardware is connected and the libraries are installed: ## What This Code Does -This project uses the **DS18B20 temperature sensor** and an **I2C LCD** to display the current temperature in both Celsius and Fahrenheit. Here’s how the code works: +This project uses the **DS18B20 temperature sensor**, **DS3231+AT24C32 RTCM module** and an **I2C LCD** to display the current temperature and current time in Celsius. Here’s how the code works: 1. **Setup Phase**: + - Time is initialized using the **DS3231+AT24C32 RTCM** module. - The temperature sensor is initialized. - The LCD is initialized and the backlight is turned on. + - Version number message is displayed on the LCD, then waits for 2 seconds. + - Clear the LCD display. - A welcome message, **"Welcome SIPP!"**, is displayed on the first line of the LCD. - A message, **"Let's get party!"**, is displayed on the second line. - The setup waits for 5 seconds before moving into the loop. + - The LCD is cleared again. 2. **Main Loop**: + - Time is read from the **DS3231+AT24C32 RTCM** module. + - The time is converted to **EU time format** (24-hour clock, ). Format: HH:MM:SS (mm-dd) - The temperature is read from the **DS18B20** sensor in **Celsius**. - - The Celsius temperature is converted to **Fahrenheit**. - - The first row of the LCD displays the temperature in Celsius with a **degree symbol (ºC)**. - - The second row displays the temperature in Fahrenheit with a **degree symbol (ºF)**. - - The display updates every second. - -### Code Breakdown: - -- **sensors.requestTemperatures()**: Requests the current temperature from the sensor. -- **getTempCByIndex(0)**: Reads the temperature in Celsius from the sensor. -- **lcd.clear()**: Clears the LCD before writing new content. -- **lcd.setCursor(x, y)**: Positions the cursor on the LCD at row `y` and column `x`. -- **delay(1000)**: Delays the loop for 1 second, causing the display to update every second. + - The first row of the LCD displays the time in the format above. + - The second row displays the temperature in Celsius with a **degree symbol (ºC)**. + - The display updates every 250 milliseconds, because time module is updated sharply every second and we need to catch the time update. If we update the display every second, we may miss the time update and the display shows the same time for 2 seconds. The code is designed to be flexible and could be easily extended to handle additional sensors or display features. diff --git a/djbooth_sensor/djbooth_sensor.ino b/djbooth_sensor/djbooth_sensor.ino index ef12b67..020964b 100644 --- a/djbooth_sensor/djbooth_sensor.ino +++ b/djbooth_sensor/djbooth_sensor.ino @@ -2,6 +2,9 @@ #include #include #include +#include + +RTC_DS3231 rtc; // Real-time module #define ONE_WIRE_BUS 2 // Data cable on D2 @@ -13,56 +16,77 @@ LiquidCrystal_I2C lcd(0x27, 16, 2); // 16x2 LCD display, I2C address: 0x27 or 0 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); -// int runCounter = 0; - void setup() { - //randomSeed(analogRead(0)); // Seed the random number generator with noise from an unused analog pin + // Init time module + Serial.begin(9600); + + String statusMessage = ""; + + if (!rtc.begin()) { + statusMessage = "Couldn't find RTC"; + Serial.println(statusMessage); + while (1); + } + + if (rtc.lostPower()) { + // If the RTC lost power and you need to set the time, uncomment the following line and adjust the date and time. + rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); + } + sensors.begin(); // Temperature sensor init - lcd.begin(16, 2); // Display size: 16 column és 2 row + lcd.begin(16, 2); // Display size: 16 column és 2 row lcd.backlight(); // Backlight on + + // *********************** + // Version information + // *********************** + lcd.setCursor(0, 0); // First row, first position + lcd.print(" Version: 1.1.0 "); + + lcd.setCursor(0, 1); // Second row, first position + lcd.print("- Cloud Mentor -"); + delay(2000); // Wait + lcd.clear(); // Clear screen + + // *********************** + // Welcome message + // *********************** lcd.setCursor(0, 0); // First row, first position - lcd.print("Welcome SIPP!"); + lcd.print(" Welcome SIPP!"); lcd.setCursor(0, 1); // Second row, first position lcd.print("Let's get party!"); delay(5000); // Wait + lcd.clear(); // Clear screen } void loop() { - //int randomNumber = random(10, 41); // Generate a random number between 10 and 40 - //runCounter++; - + // Temp sensor sensors.requestTemperatures(); // Get temperature from sensor float temperatureC = sensors.getTempCByIndex(0); // convert it to celsius - // Convert Celsius to Fahrenheit - float temperatureF = temperatureC * 9.0 / 5.0 + 32.0; + // Time module + char currentTime[20]; + DateTime now = rtc.now(); + sprintf(currentTime, "%02d:%02d:%02d (%02d-%02d)", now.hour(), now.minute(), now.second() ,now.month(), now.day()); - String textOne = "Temp: "; - String rowOne = textOne + temperatureC; - String textTwo = "Temp: "; - String rowTwo = textTwo + temperatureF; - lcd.clear(); // *********************** // First Row // *********************** lcd.setCursor(0, 0); // First row, first position - lcd.print(" "); // Clear row - lcd.setCursor(0, 0); // First row, first position - lcd.print(rowOne); - lcd.print(" "); // Print a space - lcd.print((char) 223); // º character - lcd.print("C"); // Celsius + lcd.print(currentTime); + // *********************** // Second Row // *********************** + String tempRow = " Temp: " + String(temperatureC, 1); // Limit to 1 decimal point lcd.setCursor(0, 1); // Second row, first position - lcd.setCursor(0, 1); // Second row, first position - lcd.print(rowTwo); + lcd.print(tempRow); lcd.print(" "); // Print a space lcd.print((char) 223); // º character - lcd.print("F"); // Farenheit - delay(1000); - + lcd.print("C"); // Celsius + + // Wait before restart the sequence (250 ms) + delay(250); }