Building an Alarm Clock Step by Step
by danielgass in Circuits > Microcontrollers
574 Views, 7 Favorites, 0 Comments
Building an Alarm Clock Step by Step
Build an alarm clock with the following specifications:
- Use of a 2.8i ST7789 TFT display in SPI mode
- Use of the Rapsberry pico W controller.
- Display of indoor, outdoor temperature and weather data.
- Few components required.
- Programming in micropython language.
- Easy application access/modification.
- Think outside the box with lesser-known solutions in this context (ntp, thermistor, font size, pwm, file handling, https request, weather).
All files (programs and documentation) can be downloaded HERE.
This project uses various components and techniques.
To achieve the result, we'll progress step by step. Experienced users can proceed directly to step 5.
To start with, the hardware base consists of the Raspberry pico W controller and the LCD1602 display.
Downloads
Components and Basic Assembly
Very few components are required. I'm using a prototyping board to demonstrate feasibility.
A - The temperature function is provided by the DHT11 component. (it won't be the final choice)
B - A switch and two push-buttons for the alarm function.
C - The LCD1602 display is connected via I2C.
D - A buzzer completes the assembly.
Testing Software Components
Several programs will be used to test the software functions separately.
A - The ntp function, which connects to the Internet to retrieve GMT time. This function is the basis of our alarm clock. You can modify the software to familiarize ourselves with this function ("Test ntp" folder).
B - The scan function to detect hardware on the I2C bus, in particular the LCD1602 display ("Test I2scan" folder).
C - The http request function to obtain weather information. I'm using the services of "api.openweathermap.org" to retrieve weather data for my home. ("Test get request https" folder)
Subscription and use are free below 1400 request/day. All you must do is register, define your home address and you'll get an api key to use in the request.
https://api.openweathermap.org/data/2.5/weather?lat=xx.xx&lon=yy.yy&appid=kkk...k&units=metric
xx.xx, yy.yy, kkk…k for latitude, longitude and api code respectively.
The &units parameter can be adapted according to the country. We retrieve a character string from which we extract the temperature.
{"coord":{"lon":xx.xx,"lat":yy.yy},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":10.89,"feels_like":9.76,"temp_min":9.93,"temp_max":11.29,"pressure":996,"humidity":66},"visibility":10000,"wind":{"speed":5.66,"deg":220},"clouds":{"all":100},"dt":1707663493,"sys":{"type":1,"id":6595,"country":"CC","sunrise":1707633879,"sunset":1707669718},"timezone":3600,"id":297783,"name":"S","cod":200}
Other data can also be extracted (atmospheric pressure, wind speed, cloud cover, etc.).
This program is designed to familiarize you with this function.
Alarm Clock Based on Pico + LCD1602
As usual, the program is made up of several modules, the main ones being "main.py" and "functions.py". The "functions.py" file groups together the functions that control the components (Wifi connection, alarm, temperature, display). This makes the "main.py" module easier to read.
I'll let the reader discover this application ("pico LCD1602" folder).
Alarm Clock Based on Esp8266 + LCD1602
Same programs codes (with few differences) as for the previous step.
I leave it to the reader to discover this application ("esp8266 LCD1602" folder).
The micropython version for esp8266 is included.
Alarm Clock Based on Pico + 2.8i TFT Diplay
With the experience we've gained now behind us, let's move on to assembling the alarm clock with a 2.8" TFT display.
Various hardware and software improvements were made during assembly.
The temperature sensor was replaced by a more compact MF52 thermistor.
This application is stored in the "pico ST7789" folder.
Components, Schematics, Assembly, Programming Features
Implementing the display was difficult due to lack of documentation, or even documents that didn't correspond to my goal.
The display is driven by an SPI interface. The port names differ from one display to another. The various denominations are shown in the diagram.
For the fonts, I chose two fonts with free licenses ("digital" and "louis"). As the default size of these fonts is small, I had to find a way of enlarging them to fill the screen.
You can download a document explaining how to find and change the size of the fonts, as well as their conversion to micropython format ("Fonts" folder).
Use of the PWM function to control screen brightness.
File handling to memorize display brightness (folder “JSON” for tests purpose).
The Programs
The application envolves several modules (folder "pico ST7789"). "main.py" focuses on the display part. The other functions are grouped together in the "functions.py" module. The "st7789.py" module manages the display primitives. The other modules contain the fonts.
I won't go into detail about the py files, as they are well annotated.
The screen definition is 240x320 and applies to different screen sizes.
Some details:
For display control, above screenshots explain some functions For other functions, simply explore the "st7789.py" file.
The alarm function and the " cloud cover" function share the same zone.
In the event of a major problem (wifi not available, https request in error), the program performs a "reset". This prevents the alarm clock from being freezed.
On power-up, the display may not start up immediately. This may be due to the wifi connection not working. The program then restarts after 80 seconds, and another attempt is usually successful!
The alarm clock is powered by a USB cable connected directly to the Pico. Connecting to a PC allows you to fine-tune the program and make improvements.
The alarm clock operates in day/night mode, alternating the color of the information to be more discreet at night. You can find out how to change the brightness in the program and memorize the associated value, which will be retrieved when the alarm clock is restarted.
The weather function is called every 5 minutes to keep within the range of free from charge requests (<1400/day).
A progress bar shows the seconds.
The micropython program "RPI-PICO-xxx-.uf2" is also supplied to ensure consistency with the application.
If you have any problem, please come back to me.
Conclusion
This project doesn't involve many components. It's easy to assemble. The step-by-step approach makes it easy to master the various technologies used. By the end, you get a nice little alarm clock that tells you not only the time, but also the outside temperature and cloud cover. As the alarm clock can be connected directly to a PC, you can adapt the program to your needs. The cost of this project was around 15€. Make the most of it!
References
Call https
Control ST7789
https://github.com/russhughes/st7789py_mpy
Thermistor temperature calculations
https://www.luisllamas.es/en/measure-temperature-arduino-mf52-thermistor/ and download file
Micropython format function
https://www.w3schools.com/python/ref_string_format.asp
https://learnpython.com/blog/python-string-formatting/
PWM used for brightness
https://how2electronics.com/pwm-usage-in-raspberry-pi-pico-breathing-led-example/
Read and write to file
https://electrocredible.com/rpi-pico-save-data-permanently-flash-micropython/