Many of us have run a Blink program on a microcontroller before. It’s effectively the “Hello, World!” of the embedded space. However, few of us have ever thought about optimizing our Blink code to be as miniscule as possible. But that’s precisely what [Rudra Lad] did for this entry into the 2025 One Hertz Challenge!
This example of Blink, delay_blinky_13, is built specifically for the STM32F4 Discovery microcontroller development board. [Rudra] notes the code is “highly optimized” and compiles down to a binary size of under 50 bytes. The code doesn’t even use RAM, and it aims to get the blink as close to 1 Hz as possible. Many optimizations were used to crunch it down as small as possible. For example, the standard startup code isn’t used, with the entire program instead written in the Reset_Handler to save space. Bit-band is also used to write to peripheral registers to blink the LED, since this uses less instructions than the typical methods. Meanwhile, with many tweaks to the delay counting routine, [Rudra] was eventually able to get the blink frequency to 1.00019 Hz, as measured on a logic analyzer. That’s pretty darn close!
While it’s rare that you have only 50 bytes of binary space to blink an LED, work like this is a great way to flex your coding muscles. Code is on Github for the curious, and if you’ve worked up your own impressive tiny binaries, don’t hesitate to let us know!
Finally a worthwile contestant. I’m surprised though that this wasn’t done by FreddieChopin. He’s the definitive authority when it comes to STM32 code.
Thanks !
I used to download openOCD binaries from FreddieChopin’s website. But the website doesn’t show anything if accessed now.
I’d be interested to see the (dis)assembly. Feels like a few bytes could be shaved.
Yeah, I have uploaded all binaries, assembly and disassembly files in GitHub repo.
Also, there is an example delay_blinky_11 in github repo, which is just 36 bytes in size, but results in 0.76Hz blinky, hence not submitted for 1Hz challenge.
Anyways, please let me know if you find anything new to optimize.
I’m surprised it wasn’t written in assembly.
Yup, the goal from the very beginning was to avoid assembly (and that includes avoiding the inline assembly too !)
Feeling the alternative 1Hz challenge:
How many bytes do you need to flash a LED at 1Hz?
We have a strong contender in the WCH ch5xx family, which @monte-monte made to blink in 24 bytes: https://github.com/cnlohr/ch32fun/blob/master/misc/attic/ch5xx_blobs_for_minichlink/ch5xx_blink.asm
Impressive!
I also found 28 bytes blinky for LPC17xx here : https://community.nxp.com/t5/LPCXpresso-IDE/Minimal-Blinky-in-about-200-bytes/m-p/540452/highlight/true#M6994
Looking at both these codes (ch5xx and lpc17xx), it seems like they do not require enabling clock for gpio unlike stm32f4. That helps save couple of bytes more.
May be 8-bit MCUs can help lower the size further, if load/store operations are possible in single 8-bit instructions, but need to give it a try.