Hacker News new | past | comments | ask | show | jobs | submit login

C does that too unless you sit in a tight loop polling something all the time.



Most embedded applications consist of a tight loop of constant polling. People slow things down by using delays... But if you use the wrong sort of delay (read: the most popular one https://www.arduino.cc/en/Reference/Delay), you'll end up executing nops instead of powering down the processor


What would then be the right way to sleep? I've always wanted to do something like 'sleep until event <X> and do absolutely nothing till them', but end up using the delay every time.


From what I searched, it's dependant on your microcontroller, so there is no generic way to do this (since different Arduinos have different microcontrollers).

For a very low power project I had, I basically found a lib that did deep sleep correctly for my microcontroller (an ATTiny), then you sleep for a significative period of time each time (for example, 500ms), and check if the event happened when sleep ends. That results in a consumption under 10uA.

It is obviously very dependant of what you want to do, some applications can't afford to check only every X ms.

And be careful too, you need to put everything possible behind your microcontroller, to be able to disable things during sleep (otherwise, you are just powering the rest of your circuit for nothing).


Most microcontrollers let you configure a timer that runs while the processor is in sleep, then triggers an interrupt to wake you up.

In this style of coding you end up with

  main()
  {
  configure_interrupts();
  while(1)
  {
     do_irq_bottom_halves();
     sleep(); // "wfi" in ARM
  }
  }


Unfortunately there's no easy answer to the correct solution. One method is to setup interrupt service routines and put the mcu to sleep the rest of the time. There are a couple libraries that provide convenient interfaces to AVR's sleep lib e.g http://playground.arduino.cc/Code/Enerlib

The basic idea is that it is possible to put the chip into lower-power mode... but you need a way to wake it up after that. That could be an interrupt, watchdog timer... etc

Another thing to note is that for most applications the mcu's power consumption will be negligible with respect to the peripherals', so the average person shouldn't care much.


As others said, it depends.

I have one application where the CPU literally does nothing other than process switch actions (it turns a double click into a single click), so it goes into deep sleep and pressing a pushbutton is the only thing that wakes it up.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: