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.

This time I will present you a classic 7 segment display alarm clock which will represent another one from my series of DIY unusual and ordinary clocks which you can watch on my playlist. In this case, it is characteristic that is used the STM32 microcontroller board known as "Blue Pill".

It is a popular and inexpensive development board based on the STM32F103C8T6 microcontroller from STMicroelectronics. The ARM Cortex-M3 core offers much more processing power and peripherals compared to typical 8-bit microcontrollers like those found in the Arduino.

Its price is also significantly lower than many other development boards with similar capabilities. The code is taken from the RCL-Radio blog where you can also find detailed instructions.

The device is extremely simple to build and the total cost does not exceed 5-6 dollars. It consists of several components:
- STM32 Microcontroller board
- TM1637 4-digit 7-segment display module
- DS3231 Realtime clock midule
- 4 Buttons
- and small Buzzer

First, let me explain how to install the code on the Blue Pill board.
To start programming in the Arduino IDE, you need to add the STM32 board
- File > Settings > Additional links for board manager
Add line: http://dan.drown.org/stm32duino/package_STM32duino_index.json

- Then, Open the board manager
Tools > Boards > Board Manager and type STM32 -> install

- Now we select "GenericSTM32 F103C series", and in the Upload method section, select Serial

- The simplest way to upload the code is to use a small USB to COM adapter and connect it as shown in the diagram.

- First we need to set the jumper on the STM32 to "Program mode" as shown in the image.

- Then, We select the COM port that corresponds to the USB to COM adapter and press upload.

When the upload is complete, we return the jumper to its original position, and remove the USB to COM adapter from the PC.
Now the code has been successfully uploaded and we can mount all the components.
In the style of a classic alarm clock, use a real-time clock module with a built-in battery to store the set time when not connected to power.

Thanks to the four buttons, setting is extremely simple. To adjust the time and alarm time, you need to press and hold the time set or alarm button and use the hours++ and minutes++ buttons to set the desired time. Тo set the seconds accurately, set the minutes 1 minute more; when changing the minutes, press the "Time adjustment" button to reset the seconds.

You can also adjust the display brightness by simply pressing the h+ and m+ buttons. The DS3231 real time clock contains a temperature sensor, and the temperature sensor readings are displayed on the display every 10 seconds.

When the alarm is activated, a sound signal is emitted from the piezo emitter for one minute, and if we press any of the buttons in the meantime, the sound stops.
Finally, a short conclusion. With its simple design, low cost, and easy setup, this STM32 alarm clock is a perfect DIY project for beginners and electronics enthusiasts alike.

CODE
#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                                   
License
All Rights
Reserved
licensBg
0