{"id":3376,"date":"2025-04-10T13:31:20","date_gmt":"2025-04-10T13:31:20","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3376"},"modified":"2025-04-10T13:40:51","modified_gmt":"2025-04-10T13:40:51","slug":"using-micropython-for-stm32f411-blink-without-delay","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3376","title":{"rendered":"Using Micropython for STM32F411: Blink Without Delay"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh.webp\" alt=\"\" class=\"wp-image-3377\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh.webp 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh-768x768.webp 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh-750x750.webp 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh-400x400.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/DALL\u00b7E-2025-02-13-09.56.17-A-detailed-high-quality-digital-illustration-depicting-the-process-of-compiling-and-flashing-MicroPython-onto-an-STM32F411-Nucleo-board.-The-image-sh-250x250.webp 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall use MicroPython on the STM32F411 Nucleo board to blink two LEDs connected to PA0 and PA1. The LEDs will toggle at different time intervals using\u00a0<code>utime<\/code>\u00a0for non-blocking periodic execution inside the main loop.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Introduction.<\/li>\n\n\n\n<li>Hardware setup.<\/li>\n\n\n\n<li>Firmware development.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction:<\/h2>\n\n\n\n<p>Periodic task execution is a fundamental concept in embedded systems development. It allows certain actions to be performed at regular time intervals, such as blinking LEDs, updating sensor readings, communicating over serial interfaces, or controlling motors. In traditional embedded programming, this is often achieved using hardware timers or interrupt-based mechanisms. However, in many applications, especially simple ones, it is easier and more flexible to implement periodic tasks using software timing within the main loop.<\/p>\n\n\n\n<p>MicroPython is a lightweight and efficient implementation of Python designed for microcontrollers like the STM32F411 Nucleo board. It provides useful modules such as&nbsp;<code>utime<\/code>&nbsp;that allow developers to perform non-blocking timing operations without relying on hardware interrupts. This makes the code simpler, easier to understand, and safer when dealing with functions that should not be called from interrupt context (such as print statements or communication functions).<\/p>\n\n\n\n<p>In this guide, we shall use MicroPython on the STM32F411 Nucleo board to demonstrate how to blink two LEDs connected to GPIO pins PA0 and PA1. Each LED will toggle independently at a different time interval. The first LED will blink every 1 second, while the second LED will blink every 500 milliseconds. This will be achieved using the&nbsp;<code>utime<\/code>module and the&nbsp;<code>ticks_ms()<\/code>&nbsp;function to track elapsed time in a non-blocking way within the main loop. This method is efficient, easy to extend for multiple tasks, and suitable for many real-world embedded projects.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Hardware Setup:<\/h2>\n\n\n\n<p>The connection of the two LEDs as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"596\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-1024x596.jpg\" alt=\"\" class=\"wp-image-3378\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-1024x596.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-300x175.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-768x447.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-1536x894.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-2048x1192.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-1150x669.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-750x436.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-400x233.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-10_16-16-36-250x145.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Note: It is recommended to use 220 Ohm current limiting resistors. However, in this application, they have been omitted for simplicity and short running time for LEDs and MCU.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Firmware Development:<\/h2>\n\n\n\n<p>First, we need to configure Thonny to work with STM32F411 Nucleo-64. Please refer to&nbsp;<a href=\"https:\/\/blog.embeddedexpert.io\/?p=3237\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a>&nbsp;for how to install and configure Thonny.<\/p>\n\n\n\n<p>After the setup has been completed, we start a new project.<\/p>\n\n\n\n<p>Start by importing the following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">from machine import Pin\nimport utime<\/pre><\/div>\n\n\n\n<p>Declare the LED pins as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\"># Define LEDs\nled1 = Pin('PA0', Pin.OUT)  # LED 1 on PA0\nled2 = Pin('PA1', Pin.OUT)  # LED 2 on PA1<\/pre><\/div>\n\n\n\n<p>Next, variable to hold the blinking intervals as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\"># Define blink intervals (ms)\ninterval1 = 1000  # 1 second\ninterval2 = 500   # 0.5 second<\/pre><\/div>\n\n\n\n<p>Next variable to hold the timer since the last LED has been toggled:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\"># Store last toggle times\nlast_time1 = utime.ticks_ms()\nlast_time2 = utime.ticks_ms()<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, declare variables to store the toggle state:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#Toggle State\ntoggle1=0\ntoggle2=0<\/pre><\/div>\n\n\n\n<p>Declare while loop as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">while True:\n    \n    #get the current time in milliseconds\n    now = utime.ticks_ms()\n\n    # Handle LED1 (PA0)\n    if utime.ticks_diff(now, last_time1) &gt;= interval1:\n        toggle1 = not toggle1\n        last_time1 = now\n        led1.value(toggle1)\n\n    # Handle LED2 (PA1)\n    if utime.ticks_diff(now, last_time2) &gt;= interval2:\n        toggle2 = not toggle2\n        last_time2 = now\n        led2.value(toggle2)\n<\/pre><\/div>\n\n\n\n<p>The concept as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Get the current time in milliseconds.<\/li>\n\n\n\n<li>Calculate the time difference between the current time and the last time the led has been toggled, if the time is larger.<\/li>\n\n\n\n<li>Store the current time into the last time the led blink.<\/li>\n\n\n\n<li>Invert the toggle state.<\/li>\n\n\n\n<li>Write the new value to the pin.<\/li>\n<\/ul>\n\n\n\n<p>Thats all for the firmware.<\/p>\n\n\n\n<p>Click on run as following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"706\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-1024x706.jpg\" alt=\"\" class=\"wp-image-3246\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-1024x706.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-300x207.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-768x530.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-1536x1059.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-2048x1412.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-1150x793.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-750x517.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-400x276.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/02\/2025-02-15_15-36-02-250x172.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Blinking Without Delay using Micropython on STM32F411\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/DCz-PSuw0YE?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Notice the two LEDs. They are blinking as expected.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall use MicroPython on the STM32F411 Nucleo board to blink two LEDs connected to PA0 and PA1. The LEDs will toggle at different time intervals using\u00a0utime\u00a0for non-blocking periodic execution inside the main loop. In this guide, we shall cover the following: 1. Introduction: Periodic task execution is a fundamental concept in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,9,12],"tags":[],"class_list":["post-3376","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-state-machines","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3376"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3376"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3376\/revisions"}],"predecessor-version":[{"id":3382,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3376\/revisions\/3382"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}