Mirko Pavleski
Published © GPL3+

DIY STM32 Alarm Clock with 7-Segment Display (Using Arduino

With its simple design, low cost, and easy setup, this STM32 alarm clock is a perfect DIY project for beginners and electronics enthusiasts

BeginnerFull instructions provided2 hours916
DIY STM32 Alarm Clock with 7-Segment Display (Using Arduino

Things used in this project

Hardware components

STM32F103C8 -Blue Pill
×1
TM1637 - 4 Digit 7 Segment Display
×1
DS3231 Realtime clock midule
×1
Pushbutton Switch, Momentary
Pushbutton Switch, Momentary
×4
Buzzer
Buzzer
×1
Resistor 10k ohm
Resistor 10k ohm
×4

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

Schematic

...

Code

Code

C/C++
...
#include <Wire.h> 
#include <STM32_TM1637.h> // http://rcl-radio.ru/wp-content/uploads/2020/02/STM32_TM1637_V1_3.zip 
#include <uRTCLib.h> // https://github.com/Naguissa/uRTCLib.git 
#include <EEPROM.h> // http://rcl-radio.ru/wp-content/uploads/2019/12/Arduino_STM32-master.zip 
   STM32_TM1637 tm ( PB0,PB1 ) ; // CLK, DIO 
   uRTCLib rtc ( 0x68 ) ;       
 
   // PB7 = SDA DS1307 (DS3231) 
   // PB6 = SCL DS1307 (DS3231) 
   // PB0 = CLK TM1637 
   // PB1 = DIO TM1637 
   // PB5 = time correction 
   // PB10 = hours++, brightness++ 
   // PB11 = minutes++, brightness-- 
   // PA7 = alarm correction 
   // PA1 = piezo buzzer
 
float h;
int i,hh,mm,brig,al_hh,al_mm;
byte w,alarm,w1;
 
void setup ( ) {   
   Wire. begin ( ) ;
  EEPROM.init(0x801F000, 0x801F800, 0x400); // 1024 byte
   brig = EEPROM. read ( 10 ) ;al_hh = EEPROM. read ( 11 ) ;al_mm = EEPROM. read ( 12 ) ;
   // rtc.set(30, 37, 23, 2, 17, 12, 19); 
  // RTCLib::set(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) 
  pinMode ( PB5,INPUT ) ; pinMode ( PB10,INPUT ) ; pinMode ( PB11,INPUT ) ; pinMode ( PA7,INPUT ) ;
  pinMode ( PA1, PWM ) ;
   if ( brig > 4 ) { brig= 4 ; } tm. brig ( brig ) ;
 }
 
void loop ( ) { alarm= 0 ;
 //////////////////////////////////// time output   
  if ( digitalRead ( PB5 ) ==LOW && alarm== 0 && digitalRead ( PA7 ) ==LOW ) { 
   hh=rtc. hour ( ) ;mm=rtc. minute ( ) ;
   rtc. refresh ( ) ; // time poll 
   h = rtc. hour ( ) * 100 + rtc. minute ( ) ;tm. print_time ( h, 0 ) ;delay ( 500 ) ;
   tm. print_time ( h, 1 ) ;delay ( 500 ) ;i++;
 //if(i==10){i=0;tm.print_float(rtc.temp()/100,0 ,0b1111000,0,0,0);delay(2000);}// DS3231 temperature output (t 27) 
if ( i== 10 ) { i= 0 ;tm. print_float ( rtc. temp ( ) , 0 , 0 , 0 , 0b01100011 , 0b00111001 ) ; delay ( 2000 ) ; } // output temperature DS3231 (27*C) 
  } 
///////////////////////////////////////// time correction - hours and minutes 
  if ( digitalRead ( PB5 ) ==HIGH && digitalRead ( PB10 ) ==HIGH ) { w= 1 ;hh++; if ( hh > 23 ) { hh= 0 ; } delay ( 300 ) ;tm. print_time ( hh * 100 +mm, 1 ) ; } 
  if ( digitalRead ( PB5 ) ==HIGH && digitalRead ( PB11 ) ==HIGH ) { w= 1 ;mm++; if ( mm > 59 ) { mm= 0 ; } delay ( 300 ) ;tm. print_time ( hh * 100 + mm, 1 ) ; } 
  if ( digitalRead ( PB5 ) ==HIGH && digitalRead ( PB10 ) ==LOW && digitalRead ( PB11 ) ==LOW ) { rtc. set ( 0 , mm , hh , - 1 , - 1 , - 1 , - 1 ) ; } 
  if ( w== 1 ) { w= 0;rtc. set ( 0 , mm , hh , - 1 , - 1 , - 1 , - 1 ) ; }   
////////////////////////////////// indicator brightness   
  if ( digitalRead ( PB10 ) ==HIGH && digitalRead ( PA7 ) ==LOW && digitalRead ( PB5 ) ==LOW ) { brig++; if ( brig > 4 ) { brig= 4 ; } tm. brig ( brig ) ;EEPROM. update ( 10 , brig ) ; } 
  if ( digitalRead ( PB11 ) ==HIGH && digitalRead ( PA7 ) ==LOW && digitalRead ( PB5 ) ==LOW ) { brig--; if ( brig < 0 ) { brig= 0 ; } tm. brig ( brig ) ;EEPROM. update ( 10 , brig ) ; } 
/////////////////////////////////// alarm clock 
  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 ) ; } 
  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 ) ; } 
  if ( digitalRead ( PA7 ) ==HIGH ) { tm. print_time ( al_hh * 100 + al_mm, 1 ) ; }
 
  if ( hh * 100 +mm==al_hh * 100 +al_mm && w1== 0 ) { alarm= 1 ; } else { alarm= 0 ; } 
  if ( alarm== 1 && ( digitalRead ( PA7 ) ==HIGH || digitalRead ( PB5 ) ==HIGH || digitalRead ( PB10 ) ==HIGH || digitalRead ( PB11 ) ==HIGH ) ) { alarm= 0 ;w1= 1 ; } 
  if ( alarm== 1 ) { pwmWrite ( PA1, 35000 ) ;delay ( 200 ) ; pwmWrite ( PA1, 0 ) ;delay ( 100 ) ; } else { pwmWrite ( PA1, 0 ) ; } 
  } // loop                                   

Libraries

C/C++
...
No preview (download only).

Credits

Mirko Pavleski
188 projects • 1450 followers

Comments