The SmartTime Watch is an Arduino-based smartwatch that displays the current date and time on an OLED screen when you bring your hand near an IR sensor. Using a Real-Time Clock (RTC) module, the watch accurately shows the date and time for 3 seconds, after which the display goes into an idle state, turning off the screen for a more energy-efficient design.
Components needed:
- Arduino board (e.g., Arduino Uno, Nano, or any compatible board)
- OLED display (I2C, 128×64 pixels, for time display)
- IR sensor (to detect when you bring your hand near)
- RTC module (like DS3231 or DS1307 to keep track of time)
Libraries required:
- Wire: For communication with the I2C devices (like OLED and RTC)
- Adafruit_SSD1306: For controlling the OLED display
- RTClib: For RTC communication
Wiring:
- OLED Display:
- VCC to 5V
- GND to GND
- SCL to A5 (for Arduino Uno)
- SDA to A4 (for Arduino Uno)
- IR Sensor:
- VCC to 5V
- GND to GND
- OUT to a digital pin (let’s assume pin 2)
- RTC Module (DS3231):
- VCC to 5V
- GND to GND
- SCL to A5 (same as OLED)
- SDA to A4 (same as OLED)
Code:
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
// OLED display settings
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Reset pin not used with I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// IR sensor pin
const int irPin = 2;
// RTC instance
RTC_DS3231 rtc;
unsigned long displayStartTime = 0; // To track when to switch from date and time to idle
const unsigned long displayDuration = 5000; // 5 seconds for date and time display
bool handDetected = false; // To track if hand is detected near the IR sensor
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Use 0x3C for most displays
Serial.println(F("OLED initialization failed"));
while (true);
}
display.display();
delay(2000); // Wait for a while
// Initialize the RTC
if (!rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
while (true);
}
// Set the time automatically from the computer (if needed)
// Uncomment to set time once (use it once and then comment out)
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Set IR sensor pin mode
pinMode(irPin, INPUT);
// Initially clear the display
display.clearDisplay();
}
void loop() {
// Check if hand is near the IR sensor
if (digitalRead(irPin) == HIGH) { // IR sensor detects hand (HIGH)
if (!handDetected) {
handDetected = true; // Mark hand as detected
displayStartTime = millis(); // Start the timer when hand is detected
}
unsigned long elapsedTime = millis() - displayStartTime;
// Show the date and time for 3 seconds
if (elapsedTime < displayDuration) {
DateTime now = rtc.now();
display.clearDisplay();
display.setTextSize(1); // Smaller text for date
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0); // Set starting position for date
display.print(now.day(), DEC);
display.print('/');
display.print(now.month(), DEC);
display.print('/');
display.print(now.year(), DEC);
display.setTextSize(2); // Larger text for time
display.setCursor(0, 20); // Set position for time
display.print(now.hour(), DEC);
display.print(':');
if (now.minute() < 10) display.print('0');
display.print(now.minute(), DEC);
display.print(':');
if (now.second() < 10) display.print('0');
display.print(now.second(), DEC);
display.display();
} else {
// After 3 seconds, go idle (clear the screen)
display.clearDisplay();
display.display();
}
} else {
// When hand is away, reset the hand detection and clear the screen
if (handDetected) {
handDetected = false; // Mark hand as not detected
display.clearDisplay(); // Clear the screen when hand is no longer detected
display.display();
}
}
}
- Libraries:
Wire.h
allows I2C communication between the Arduino and the OLED/RTC.Adafruit_SSD1306.h
is used to interact with the OLED display.RTClib.h
helps with the RTC (Real-Time Clock) module.
- Initialization:
- The
setup()
function initializes the OLED, RTC, and IR sensor pin. - The time is set from the computer’s date and time when
rtc.adjust()
is uncommented and used once to set the initial time.
- The
- Main logic:
- In the
loop()
function, the IR sensor is constantly monitored. When the sensor detects your hand (i.e.,digitalRead(irPin) == HIGH
), the time from the RTC module is fetched and displayed on the OLED screen.
- In the
How to Set Time:
- If you’re uploading the code for the first time and want to set the RTC from your computer’s current date and time, uncomment the line
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
in thesetup()
function, and upload the code. - After the initial setup, comment out this line again to keep the time from the RTC running.