Does a microcontroller need a break?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Kleriger
Posts: 6
Joined: Mon Dec 30, 2019 9:16 am
Location: Germany

Does a microcontroller need a break?

Post by Kleriger » Fri Jan 17, 2020 10:46 am

Hello,
I have two quick questions.
  • Is it necessary to use a sleep in "main.py" when I run a loop? I know this from programming on the computer so that the CPU is not 100% used.
    For example:

    Code: Select all

    while True:
    	do_quick_stuff()
    	time.sleep(0.1)
    
  • For my program development I use a lot of serial outputs to see what my program does. Is that rather bad for the finished program for the esp32 because it also has to output the data on the serial interface or can I ignore this?
    For example:

    Code: Select all

    while True:
    	do_quick_stuff()
    	do_print_result
    
Thanks for your help.

User avatar
jimmo
Posts: 1835
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Does a microcontroller need a break?

Post by jimmo » Sat Jan 18, 2020 6:18 am

Kleriger wrote:
Fri Jan 17, 2020 10:46 am
Is it necessary to use a sleep in "main.py" when I run a loop? I know this from programming on the computer so that the CPU is not 100% used.
This is not necessary on a computer either. Only sleep if your program needs to actually slow down or to save power (i.e. if you only need to do something once per second or whatever). There's nothing wrong with 100% CPU utilization if your goal is to get something done as fast as possible.

(This seems to be a common misconception that it's bad to be at 100% CPU utilization when the truth is actually the opposite).
Kleriger wrote:
Fri Jan 17, 2020 10:46 am
For my program development I use a lot of serial outputs to see what my program does. Is that rather bad for the finished program for the esp32 because it also has to output the data on the serial interface or can I ignore this?
This might slow down your program (and make your program take up more space) but there's no harm in doing so.

Generally you'd do something like

Code: Select all

_DEBUG = const(True)

if _DEBUG:
  log('message')
then when you change it to False, the bytecode emitter won't even generate code for the log lines.

User avatar
pythoncoder
Posts: 4466
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Does a microcontroller need a break?

Post by pythoncoder » Sat Jan 18, 2020 6:45 am

I should perhaps add that the reason sleep is not required on a normal computer is that the OS uses pre-emptive scheduling. It grabs time slices from your program to enable other things (such as display updates) to take place concurrently. Your code can be written as if it had sole access to the CPU even though, in fact, it doesn't.

There are environments where there is no underlying pre-emptive scheduling, notably on a bare metal MicroPython target. Then it is possible to use uasyncio to provide concurrency. The model here is cooperative scheduling. In this case a task completely monopolises the CPU and other tasks only get to run when it sleeps or waits for an event - so loops commonly include a special sleep statement. The cooperative model has a number of advantages and is widely used in firmware development.

I think this is how the notion of a mandatory sleep has arisen.

Another application of sleep is in power saving applications: MicroPython supports sleep modes with greatly reduced power draw.
Peter Hinch

Post Reply