DIY STM32 Alarm Clock with 7-Segment Display (Using Arduino IDE)
With its simple design, low cost, and easy setup, this STM32 alarm clock is a perfect DIY project for beginners and electronics enthusiasts alike.
Devices & Components
1
bluepill, stm32f103
4
Push Button
4
Metalfilm resistor 10k tolerance 0,1%
1
Grove - Buzzer - Piezo
1
DS3231 Real Time Clock Module
1
TM1637 Display
Hardware & Tools
1
Soldering kit
Software & Tools
Arduino IDE
Project description
Code
Code
cpp
...
1#include <Wire.h> 2#include <STM32_TM1637.h> // http://rcl-radio.ru/wp-content/uploads/2020/02/STM32_TM1637_V1_3.zip 3#include <uRTCLib.h> // https://github.com/Naguissa/uRTCLib.git 4#include <EEPROM.h> // http://rcl-radio.ru/wp-content/uploads/2019/12/Arduino_STM32-master.zip 5 STM32_TM1637 tm ( PB0,PB1 ) ; // CLK, DIO 6 uRTCLib rtc ( 0x68 ) ; 7 8 // PB7 = SDA DS1307 (DS3231) 9 // PB6 = SCL DS1307 (DS3231) 10 // PB0 = CLK TM1637 11 // PB1 = DIO TM1637 12 // PB5 = time correction 13 // PB10 = hours++, brightness++ 14 // PB11 = minutes++, brightness-- 15 // PA7 = alarm correction 16 // PA1 = piezo buzzer 17 18float h; 19int i,hh,mm,brig,al_hh,al_mm; 20byte w,alarm,w1; 21 22void setup ( ) { 23 Wire. begin ( ) ; 24 EEPROM.init(0x801F000, 0x801F800, 0x400); // 1024 byte 25 brig = EEPROM. read ( 10 ) ;al_hh = EEPROM. read ( 11 ) ;al_mm = EEPROM. read ( 12 ) ; 26 // rtc.set(30, 37, 23, 2, 17, 12, 19); 27 // RTCLib::set(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) 28 pinMode ( PB5,INPUT ) ; pinMode ( PB10,INPUT ) ; pinMode ( PB11,INPUT ) ; pinMode ( PA7,INPUT ) ; 29 pinMode ( PA1, PWM ) ; 30 if ( brig > 4 ) { brig= 4 ; } tm. brig ( brig ) ; 31 } 32 33void loop ( ) { alarm= 0 ; 34 //////////////////////////////////// time output 35 if ( digitalRead ( PB5 ) ==LOW && alarm== 0 && digitalRead ( PA7 ) ==LOW ) { 36 hh=rtc. hour ( ) ;mm=rtc. minute ( ) ; 37 rtc. refresh ( ) ; // time poll 38 h = rtc. hour ( ) * 100 + rtc. minute ( ) ;tm. print_time ( h, 0 ) ;delay ( 500 ) ; 39 tm. print_time ( h, 1 ) ;delay ( 500 ) ;i++; 40 //if(i==10){i=0;tm.print_float(rtc.temp()/100,0 ,0b1111000,0,0,0);delay(2000);}// DS3231 temperature output (t 27) 41if ( i== 10 ) { i= 0 ;tm. print_float ( rtc. temp ( ) , 0 , 0 , 0 , 0b01100011 , 0b00111001 ) ; delay ( 2000 ) ; } // output temperature DS3231 (27*C) 42 } 43///////////////////////////////////////// time correction - hours and minutes 44 if ( digitalRead ( PB5 ) ==HIGH && digitalRead ( PB10 ) ==HIGH ) { w= 1 ;hh++; if ( hh > 23 ) { hh= 0 ; } delay ( 300 ) ;tm. print_time ( hh * 100 +mm, 1 ) ; } 45 if ( digitalRead ( PB5 ) ==HIGH && digitalRead ( PB11 ) ==HIGH ) { w= 1 ;mm++; if ( mm > 59 ) { mm= 0 ; } delay ( 300 ) ;tm. print_time ( hh * 100 + mm, 1 ) ; } 46 if ( digitalRead ( PB5 ) ==HIGH && digitalRead ( PB10 ) ==LOW && digitalRead ( PB11 ) ==LOW ) { rtc. set ( 0 , mm , hh , - 1 , - 1 , - 1 , - 1 ) ; } 47 if ( w== 1 ) { w= 0;rtc. set ( 0 , mm , hh , - 1 , - 1 , - 1 , - 1 ) ; } 48////////////////////////////////// indicator brightness 49 if ( digitalRead ( PB10 ) ==HIGH && digitalRead ( PA7 ) ==LOW && digitalRead ( PB5 ) ==LOW ) { brig++; if ( brig > 4 ) { brig= 4 ; } tm. brig ( brig ) ;EEPROM. update ( 10 , brig ) ; } 50 if ( digitalRead ( PB11 ) ==HIGH && digitalRead ( PA7 ) ==LOW && digitalRead ( PB5 ) ==LOW ) { brig--; if ( brig < 0 ) { brig= 0 ; } tm. brig ( brig ) ;EEPROM. update ( 10 , brig ) ; } 51/////////////////////////////////// alarm clock 52 if ( digitalRead ( PA7 ) ==HIGH && digitalRead ( PB10 ) ==HIGH ) { w1= 0 ;al_hh++; if ( al_hh > 23 ) { al_hh= 0 ; } delay ( 300 ) ;EEPROM. update ( 11 , al_hh ) ; } 53 if ( digitalRead ( PA7 ) ==HIGH && digitalRead ( PB11 ) ==HIGH ) { w1= 0 ;al_mm++; if ( al_mm > 59 ) { al_mm= 0 ; } delay (300 ) ;EEPROM. update ( 12 , al_mm ) ; } 54 if ( digitalRead ( PA7 ) ==HIGH ) { tm. print_time ( al_hh * 100 + al_mm, 1 ) ; } 55 56 if ( hh * 100 +mm==al_hh * 100 +al_mm && w1== 0 ) { alarm= 1 ; } else { alarm= 0 ; } 57 if ( alarm== 1 && ( digitalRead ( PA7 ) ==HIGH || digitalRead ( PB5 ) ==HIGH || digitalRead ( PB10 ) ==HIGH || digitalRead ( PB11 ) ==HIGH ) ) { alarm= 0 ;w1= 1 ; } 58 if ( alarm== 1 ) { pwmWrite ( PA1, 35000 ) ;delay ( 200 ) ; pwmWrite ( PA1, 0 ) ;delay ( 100 ) ; } else { pwmWrite ( PA1, 0 ) ; } 59 } // loop
Documentation
Schematic
...
SchematicJPG.jpg

Comments
Only logged in users can leave comments