MICROPYTHON WITH THE ESP32 GUIDE SERIES
How To Interrupt A Running Program
In this lesson, I'll show you how to interrupt a running program and how to restart your ESP32 using soft reset.
To demonstrate how to interrupt a running program and rest your ESP32, I'll use our familiar "blinking LED script".
Here is the script:
from machine import Pin
from utime import sleep
led = Pin(21, Pin.OUT)
while True:
print(".")
led.on()
sleep(0.5)
led.off()
sleep(0.5)
Notice that inside the "while" block, I have added a line that prints a dot character in the shell. This will help us recognise that the program is running without having to look at the LED on the breadboard.
In this lesson, we'll take a closer look at the boards that can use MicroPython.
Ctrl-C to interrupt a program
Click on the play button to start the program. Notice that the LED is blinking, and a new dot character appears in the shell every 0.5 seconds.
While the program is running, you cannot use the shell. To re-gain access to the shell, you must interrupt the program. This, essentially, will end the program execution.
To interrupt the program you can use the Interrupt execution command, under the Run menu.
Or, you can type Crtl-C on your keyboard.
Ctrl-D to soft-reboot a program
As opposed to a keyboard interrupt via Ctrl-C, you can do a soft-reboot by typing Ctrl-D, or selecting "Send EOF / Soft Reboot" from the Run menu.
When you do a soft reboot, your ESP32 restarts your program from "scratch". As opposed to Ctrl-C, you do not get access to the shell, since the program is still running.
Try Ctrl-D while the blinking LED program is running. Notice that after you do a soft-reboot, the shell shows a horizontal line, and then new dots appear, generated by the running program.
After a soft reboot, the RAM of your ESP32 will not be cleared. Any variable values remain in the RAM.
Learn more about the reset and boot modes in MicroPython here.
Stop/Restart backend
Another option available to us is to stop and restart the backend. The result of this is that your ESP32 does a "hard reset". This is similar to pressing the reset button on the board itself.
Of course, a hard reset will stop the program and give you a fresh shell prompt. All RAM contents are lost, but the flash memory remains so any files stored in the ESP32 file system will remain intact.
You can also do a hard-reset by clicking on the Stop button in Thonny.
Disconnect
Finally, you can simulate the physical disconnection and connection of the USB cable by selecting Disconnect from the Run menu.
Often, this is an effective way to deal with communications problems between your computer and the target device. If a software disconnect and connect does not help, the next step would be to physically disconnect and connect the USB cable.
Learn MicroPython for the ESP32
With this video course, you will learn how to use the
MicroPython programming language with the ESP32 micro-controller.
MicroPython is the perfect language for anyone looking for the easiest (yet still powerful) way to program a micro-controller.
Jump to another article
1. What is MicroPython?
2. MicroPython vs Python
3. MicroPython resources
4. MicroPython compatible boards
5. Getting started with Thonny IDE
6. How to install the MicroPython firmware
7. Setup an interpreter in Thonny IDE
8. How to write and execute a MicroPython program
9. Thonny IDE with the Raspberry Pi Pico
10. Thonny IDE with the BBC Micro:bit
11. Thonny IDE advanced configuration
12. How to find MicroPython packages at PyPi
13. The MicroPython shell
14. MicroPython Programming with files
15. How to interrupt a running program
16. How to run a program at boot
17. How to debug MicroPython program
Last Updated 8 months ago.
We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time