A comprehensive AVR-based weather station and digital clock project featuring real-time clock, environmental sensors, and an intelligent display system with advanced alarm functionality and power management.
- Overview
- Features
- Hardware Requirements
- Pin Configuration
- Installation
- Usage
- Project Structure
- API Documentation
- Configuration
- Troubleshooting
- Contributing
- License
This project implements a sophisticated weather station and digital clock using an AVR microcontroller (ATmega328P). The system combines multiple sensors to provide real-time environmental data, precise timekeeping, and intelligent display management with automatic brightness control, advanced alarm functionality, and power management features.
- Real-time Clock: DS3231 RTC module with battery backup
- Environmental Monitoring:
- Temperature and humidity (DHT22)
- Barometric pressure (BMP180)
- Internal temperature from DS3231
- Triple Display Modes:
- Large digital time display (30 seconds)
- Calendar and date information (15 seconds)
- Weather sensor data (15 seconds)
- Advanced Alarm System: Multi-stage alarm with progressive escalation
- Intelligent Brightness Control: Time-based LCD backlight management
- Interactive Controls: Button-based mode switching
- Power Management: Watchdog timer and sleep capabilities
- Custom LCD Characters: Large digit display using custom character sets
- I2C Communication: Efficient sensor communication protocol
- Interrupt-driven Architecture: Timer-based updates and button handling
- PWM Backlight Control: Smooth brightness transitions
- Musical Alarms: Multiple ringtone patterns for different alarm stages
- Smart Sensor Reading: Optimized sensor polling (every 30 seconds, disabled at night)
- Triple Display System: Enhanced from dual to triple display modes with optimized timing
- Advanced Alarm System: Multi-stage progressive alarm with weekend mode
- Power Management: Watchdog timer implementation for system reliability
- PlatformIO Support: Modern build system integration alongside traditional Make
- Optimized Sensor Reading: Smart polling strategy to reduce power consumption
- Enhanced Brightness Control: More granular time-based brightness management
- Improved Display Content: Better organized weather information display
- Reliability: Watchdog timer prevents system hangs
- Efficiency: Reduced sensor polling during night hours
- User Experience: Better display mode organization and timing
- Maintainability: PlatformIO integration for easier development
- ATmega328P (Arduino Uno compatible)
- Clock Speed: 8MHz (configurable)
- Memory: 32KB Flash, 2KB SRAM
- DS3231 RTC Module: Real-time clock with temperature sensor
- DHT22: Digital temperature and humidity sensor
- BMP180: Barometric pressure sensor
- LCD1602 with I2C Backpack: 16x2 character display
- Buzzer: Piezo buzzer for alarm sounds
- Photoresistor: Ambient light sensing (optional, currently disabled)
- Push Button: Mode switching control
- Operating Voltage: 5V DC
- Current Consumption: ~100-200mA (depending on display brightness)
- Backup Power: CR2032 battery for RTC (included in DS3231 module)
Component | Pin | Function |
---|---|---|
I2C Bus | ||
SDA | A4 (PC4) | I2C Data Line |
SCL | A5 (PC5) | I2C Clock Line |
DHT22 | ||
Data | PD5 | Digital I/O |
Buzzer | ||
Signal | PB0 | PWM Output |
LCD Backlight | ||
PWM | PD3 | PWM Control |
Button | ||
Mode Switch | PD2 | Digital Input (with pull-up) |
Photoresistor | ||
Analog | A2 (PC2) | ADC Input (optional) |
- AVR-GCC Toolchain: Version 3.4 or later
- AVRDUDE: For programming the microcontroller
- Make: Build system
- Git: Version control
-
Clone the repository:
git clone <repository-url> cd simple-clock
-
Install PlatformIO (if not already installed):
pip install platformio
-
Build the project:
pio run
-
Upload to microcontroller:
pio run --target upload
-
Alternative: Using Make (if preferred):
make clean make all make upload
-
Connect I2C devices:
- DS3231: VCCβ5V, GNDβGND, SDAβA4, SCLβA5
- LCD1602: VCCβ5V, GNDβGND, SDAβA4, SCLβA5 (I2C address: 0x27)
-
Connect sensors:
- DHT22: VCCβ5V, GNDβGND, DataβPD5
- BMP180: VCCβ3.3V, GNDβGND, SDAβA4, SCLβA5
-
Connect peripherals:
- Buzzer: PositiveβPB0, NegativeβGND
- Button: One terminalβPD2, Other terminalβGND
- LCD Backlight: PWM controlβPD3
-
Power on the system - The device will display "Hello World" and perform a countdown
-
Set initial time and date (uncomment in main.c):
DS3231_setDate(31, 8, 25); // Day, Month, Year DS3231_setTime(10, 52, 0); // Hour, Minute, Second
-
Configure alarm (optional):
DS3231_setAlarm2(7, 0); // 7:00 AM alarm
The system automatically cycles between three display modes:
- Shows large, easy-to-read time format using custom characters
- Displays current time in HH:MM:SS format
- Optimized for distance viewing
- Only mode active during night hours (0:00-6:00)
- Line 1: Time (HH:MM:SS) and Month abbreviation
- Line 2: Day of month, Full day name, Year
- Shows complete date and time information
- Line 1: BMP180 temperature, DHT22 temperature, DS3231 temperature
- Line 2: DHT22 humidity, BMP180 pressure (mmHg)
- Only displays during daylight hours (6:00-23:00)
- Sensors read every 30 seconds
The alarm system features progressive escalation with multiple stages:
- Initial Alarm (at set time): Beep every 15 seconds
- +1 minute: Beep every 10 seconds
- +5 minutes: Ringtone every 20 seconds
- +10 minutes: Alarm beep every 20 seconds
- +11 minutes: Alarm beep every 15 seconds
Note: Alarm is disabled on Saturdays (weekend mode)
Automatic brightness adjustment based on time:
- Night (23:00-06:00): Display off (0% brightness)
- Evening (21:00-23:00): Dim (64% brightness)
- Morning (06:00-07:00): Dim (64% brightness)
- Day (07:00-21:00): Full brightness (96%)
simple-clock/
βββ src/ # Source code files
β βββ main.c # Main application logic
β βββ ds3231.c # RTC module driver
β βββ lcd1602.c # LCD display driver
β βββ dht22.c # Temperature/humidity sensor
β βββ bmp180.c # Pressure sensor driver
β βββ i2c.c # I2C communication
β βββ pwm.c # PWM control
β βββ adc.c # Analog-to-digital conversion
β βββ buzzer.c # Audio output
β βββ uart.c # Serial communication
βββ include/ # Header files
β βββ ds3231.h
β βββ lcd1602.h
β βββ dht22.h
β βββ bmp180.h
β βββ i2c.h
β βββ pwm.h
β βββ adc.h
β βββ buzzer.h
β βββ uart.h
βββ datasheet/ # Component datasheets
βββ schemes/ # Circuit diagrams
βββ lib/ # External libraries
βββ test/ # Test files
βββ README.md # This file
void DS3231_init(void);
void DS3231_getTime(uint8_t* hour, uint8_t* minute, uint8_t* second);
void DS3231_setTime(uint8_t hour, uint8_t minute, uint8_t second);
void DS3231_getDate(uint8_t* day, uint8_t* month, uint8_t* year);
void DS3231_setDate(uint8_t day, uint8_t month, uint8_t year);
void DS3231_setAlarm2(uint8_t hour, uint8_t minute);
void DS3231_getTemperature(int8_t* temperature);
void lcd_init(void);
void lcd_clear(void);
void lcd_set_cursor(char row, char col);
void lcd_print(const char *str);
void display_large_time2(uint8_t hours, uint8_t minutes, uint8_t seconds);
void create_custom_chars(void);
void DHT22_Read(uint8_t* humidity_int, uint8_t* humidity_dec,
uint8_t* temperature_int, uint8_t* temperature_dec);
void BMP180_init(void);
float BMP180_readTemperature(void);
float BMP180_readPressure(void);
void initTimer2(void);
void playBeep(void);
void playAlarmBeep(void);
void playRingtone2(void);
void playNote(uint16_t frequency, uint16_t duration);
void playMorningScale(void);
void playBugleCall(void);
void playCheerfulWake(void);
void playChimePattern(void);
void init_watchdog(void);
void wdt_reset(void);
void pwm_init(int8_t pin);
void pwm_set_duty_cycle(uint8_t duty_cycle);
#define F_CPU 8000000UL // 8MHz operation
#define PRESCALER 1024
#define TARGET_FREQ 1 // 1Hz interrupt
#define SCL_CLOCK 100000L // 100kHz I2C clock
// Display mode switching (in main loop)
// 0 β for 0β29 seconds (30 seconds)
// 1 β for 30β44 seconds (15 seconds)
// 2 β for 45β59 seconds (15 seconds)
current_display_mode = (time_second % 60 < 30) ? 0 : ((time_second % 60 < 45) ? 1 : 2);
// Watchdog timer configuration
wdt_enable(WDTO_8S); // 8-second timeout
WDTCSR |= (1 << WDIE); // Enable watchdog interrupt
-
LCD Not Displaying:
- Check I2C connections (SDAβA4, SCLβA5)
- Verify I2C address (should be 0x27)
- Ensure proper power supply (5V)
-
Time Not Updating:
- Check DS3231 connections
- Verify battery in RTC module
- Ensure proper I2C communication
-
Sensors Not Reading:
- Check individual sensor connections
- Verify power supply for each sensor
- Test I2C bus with scanner
-
Alarm Not Working:
- Verify buzzer connection (PB0)
- Check alarm time setting
- Ensure Timer2 is properly initialized
-
Display Too Dim/Bright:
- Check PWM backlight control (PD3)
- Verify time-based brightness logic
- Test manual brightness adjustment
Enable UART debugging by uncommenting UART-related code in main.c:
//#include "uart.h" // Uncomment for debugging
//uart_init(UART_BAUD_SELECT(UART_BAUD_RATE, F_CPU));
-
System Hanging:
- Check watchdog timer configuration
- Verify
wdt_reset()
calls in main loop - Ensure proper interrupt handling
-
Display Not Updating:
- Check Timer1 configuration and interrupts
- Verify
update_flag
is being set properly - Ensure global interrupts are enabled (
sei()
)
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow existing code style and naming conventions
- Add appropriate comments for complex functions
- Test changes on actual hardware
- Update documentation for new features
- Ensure backward compatibility
This project is licensed under the MIT License - see the LICENSE file for details.
- Sergey Denisov - Original LCD library implementation
- Andy Gock - UART library foundation
- Bosch Sensortec - BMP180 sensor documentation
- AVR Community - Various sensor driver implementations
- PlatformIO - Modern embedded development platform
For questions, issues, or contributions:
- Create an issue in the repository
- Check existing documentation and troubleshooting guide
- Review component datasheets in the
datasheet/
directory
Note: This project is designed for educational and hobbyist purposes. Always follow proper electrical safety practices when working with electronic components.