Arduino Power Saving Tutorial: Clock Pre-Scale and Sleep

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • Experiments to reduce current consumption of the Arduino-compatible Pro Mini.
  • Věda a technologie

Komentáře • 91

  • @itisonlyadream
    @itisonlyadream Před 6 lety +1

    Before watching your excellent video, I attempted to cut the clock frequency in half by selecting the 3.3V , 8 mHz option in the Arduino IDE before loading my program. The code I loaded was originally written to run at the standard 16 mHz, and it generates various tones separated by delays. I'm using a 5V, 16mHz Arduino Pro Mini, but (except when programming) I'm bringing in battery power on Vcc (bypassing the 5 Volt regulator), on the assumption that the 328P won't care about the voltage as long as I stay within the voltage limits of the 328P. To my surprise, this actually made the delays shorter and the tones were a higher pitch, as though the clock was running at a higher frequency than 16 mHz. Then, I added your two lines of code to Setup, to change the clock frequency to 8 mHz, and I reloaded the program using the 5V, 16mHz option in the IDE. Now the delays are twice as long and the tones are lower in pitch than they are at 16 mHz, which is what I expected. I'm still trying to figure out why using the IDE to change the clock frequency to 8 mHz seemed to cause the 328P to act as if the clock frequency had been doubled, instead of cut in half. What am I overlooking?

  • @MrGrowslikeatree
    @MrGrowslikeatree Před 7 lety

    Great video, man...exactly what I've been searching for going on days now! Thanks!

  • @solento5555
    @solento5555 Před 6 lety +2

    You've inspired some more confidence in me as I have the same voltmeter!

  • @DaleLadDaz
    @DaleLadDaz Před 9 lety +20

    You are one clever dude...how you can look at a data sheet and make sense of it is beyond me lol

  • @SineHacker
    @SineHacker Před 7 lety

    This was super useful thank you! Need to read up on waking from sleep mode with interrupts next

  • @AgentAileron
    @AgentAileron Před 8 lety +1

    Awesome sauce, I never knew there was a way to input assembly to Arduino - cheers mate!

    • @semplar2007
      @semplar2007 Před 4 lety

      Here is some info on that: www.nongnu.org/avr-libc/user-manual/inline_asm.html

  • @JeremyCook
    @JeremyCook Před 2 lety

    Thanks so much. Great idea for lower power consumption in less demanding applications without having to deal with waking up etc.
    Of course, I suppose you'd want to actually use sleep modes if you've gone that far, but definitely a good programming time/power savings ratio!

  • @tywalch
    @tywalch Před 8 lety +7

    This was so helpful! Quick question I can't seem to find the answer two: How does the prescaler affect the baud rate for the serial log? Thanks again for this video, I have enjoyed watching the others as well

  • @depressedrobot2491
    @depressedrobot2491 Před 6 lety +1

    Firstly: Thanks for the vid.
    Secondly: Did you notice a current reduction when in deep sleep and reducing the clock? Because I was testing with an arduino nano, and without sleep mode the current goes down quite a bit with clock reduction (from about 15 mA at 16mHz to about 5.8 mA at 1 mHz). However, in deep sleep (using the low power library, ADC_OFF, BOD_OFF) the clock frecuency doesn't seem to have any influence (1.90 mA for ALL frecuencies). I suspect that those 1.9 mA are due to the USB port (arduino gets powered throught the 5V pin)

  • @bitluni
    @bitluni Před 10 lety +2

    Like this video! Does the OLED have a built in memory or does it need constant input from the MCU?
    There are even more possibilities to reduce the consumption. You can deactivate unused components like the the ADC and some timers by setting the PRR register.
    The best results can be achieved using the watchdog timer if you have idle phases. I was able to go down to 5µA during idle using the ATiny. You can check the video
    Mini project: Bluetooth keys finder (aka "No worries keyfob")
    around minute 5:40, source links are available in the description.
    Another observation was that reduced voltage to minimum also reduced the leakage current. But since you are using a battery the regulation would eat up the difference.

    • @JulianIlett
      @JulianIlett  Před 10 lety +1

      The OLED has its own memory. In fact that scrolling is also done by the OLED chip.
      I did play with the ADC and PRR - shutting off the ADC helped a bit, the PRR did surprisingly little given its name.

    • @bitluni
      @bitluni Před 10 lety +1

      Julian Ilett So it even could be possible running the display while the mcu is shut down... nice. The PRR is truly disappointing but easy to apply so worth to be mentioned.

  • @TheDutyPaid
    @TheDutyPaid Před 7 lety

    Could you do the same for a battery powered arduino 7 segment based clock using a RTC module. So a button to reset, display the time and then go back to sleep?

  • @suave319
    @suave319 Před 9 lety

    Great video. Really helpful.

  • @Baranecosable
    @Baranecosable Před 9 lety

    thank you for this it's really useful for me!

  • @DanTup
    @DanTup Před 5 lety

    Great video - is there a way to sleep for a duration (eg. if you wanted to wake up and do something every 5 minutes)?

  • @peaps
    @peaps Před 10 lety

    If you turn off the ADC, Analog Comparator and Brown Out Detect circuits, you will get it down to 1uA. However changing the BOD level requires modifying FUSE bits.

  • @sri198904
    @sri198904 Před 3 lety

    Great video!! Have you tried waking up the mini with an interrupt using wd ? I am trying similar but having issues with configuration of wd with nano the nano just crashes

  • @bernt6500
    @bernt6500 Před 6 lety +1

    Does this work for the arduino Nano the same way it did for the mini?

  • @iantcroft
    @iantcroft Před 2 lety

    Julian, have you produced a video on how to lower the brown out detector to 1.8 volts? Can you use another arduino to reset the bod or do you have to use programmer?
    Thanks.

  • @bun_zees3245
    @bun_zees3245 Před 4 lety

    Now that's a vedio worth subscribing

  • @omsingharjit
    @omsingharjit Před 6 lety

    What's the voltage at 5 ma and 8mhz ??????

  • @DiyintheGhetto
    @DiyintheGhetto Před 5 lety

    hello Julian i have tried this many times and one problem i have is that when in sleep mode i can not get below 2.06 ma no matter what i do.

  • @omsingharjit
    @omsingharjit Před 6 lety

    Your are converting 5v pro mini into 3.3 volt 8mhz that is already available in market . Two versions 5v and 3.3 volt pro mini !!????

  • @hbcchefmailde
    @hbcchefmailde Před 6 lety

    Hi Julian, thanks for the detailed info. I did the same as you described with the clock dividing. code is running well but serial monitor on pc always has only special chars and not the ones it should. Is there a trick to let the serial monitor work correct after clock speed changing?

    • @MathiasBacher
      @MathiasBacher Před 3 lety

      You have to multiply the baud speed in your sketch by the amount you divided the clock speed by.

  • @lipwoon
    @lipwoon Před 4 lety

    Does it makes any sense to put a (NodeMCU-8266 + MAX7219) project into Sleep mode if the display time is only updating every 1 minute?

  • @tonysicily2687
    @tonysicily2687 Před 4 lety

    Can the clock frequency be changed dynamically? Or can it only be changed at start up?
    Ie, can I use this to reduce power consumption during ‘down time’ but speed it up during other times ?

  • @programmingtips404
    @programmingtips404 Před 3 lety

    Could you also disable the ADC to lower the power requirement?

  • @shafnet
    @shafnet Před 6 lety

    4:24 Oh my a pro like you using an old maplin knock off ? Mind you I have the same one - it must be around 15 years old now and is still working

  • @phinok.m.628
    @phinok.m.628 Před 4 lety

    You can also use inline assembly for the sleep instruction. After setting up the registers just write the following line:
    __asm__ __volatile__("sleep");
    Ok, unfortunately youtube makes the "asm" italic and removes two underscores. It's two underscores before and after asm.
    Also, if you wanna save power you should probably start with the simple stuff, like setting all your pins high or low, so they are not floating (as that causes the ATmega and many other microcontrollers to draw a little more power) and disabling peripherals like the ADC, UART and/or timers that aren't needed etc. (keep in mind that timer0 is used by millis, delay etc.) then if you wanna save even more power you can put the ATmega to sleep and get the current under 1 microamp if needed.

  • @kenansenagic384
    @kenansenagic384 Před 5 lety

    Great video ;)

  • @Anima420
    @Anima420 Před 5 lety

    Could you clockdown the processor on a wemos d1 mini pro although it has not an atmel chip?

    • @carlh2007
      @carlh2007 Před 5 lety

      no but the sleep modes on a wemos d1 mini use about 1/10th of the power as this arduino. I use a wemos d1 mini in a weather station and can get over a week out of a 400mah lithium battery if i disconnect the solar panel.

  • @teamdictionary2412
    @teamdictionary2412 Před 9 lety

    for some reason, when I try to upload the code, nothing happens. Ive tried various versions of the code but nothing happens when I am monitoring power consumption. When I use the sleep modes, it works, but when I do the prescaler code, nothing happens. Can anyone let me know what I might be doing wrong?

  • @dantosrock43
    @dantosrock43 Před 6 lety

    goodjob very useful thank u sir

  • @half4team
    @half4team Před 10 lety

    How about dynamically adjusting the speed in the code?
    Instead of adjusting all delays, just give it the prescale command again when waking up so it can run at full speed until sleep.

    • @JulianIlett
      @JulianIlett  Před 10 lety +1

      In the deepest sleep modes, the external resonator/crystal is stopped altogether, so there's no need to prescale the clock in deep sleep. Dynamically adjusting the clock prescale in code is perfectly viable as a power saving measure.

  • @Buciasda33
    @Buciasda33 Před 5 lety

    I also disabled the ADC and Brown-Out and got the board down to 22uA.
    I had trouble with the onboard Linear Regulator. Mine was a SOT23 L05 (3 Legs), yours is a SOT23-5 LG50. (5 Legs)
    It was drawing 2.2mA at start, but current draw kept increasing as time went by.
    I tried powering the board from 12V initially, but then I powered everything from 5V and I got the same 2.2mA current draw.
    So I started removing components from the board (Crystal, Linear Regulator, Resistors, Capacitors)
    I started looking into 7805 datasheets and I saw a Typical Quiescent Current of 3.2mA.
    So be aware of cheap Chinese boards if your are looking into Low Power stuff.

    • @fabirodriguez3677
      @fabirodriguez3677 Před 3 lety +1

      Hello, I did not understand if it is Chinese the arduino consumes more? can't remove the regulator?

    • @Buciasda33
      @Buciasda33 Před 3 lety

      @@fabirodriguez3677 the chinese Arduino consumes more.
      The voltage regulator was the problem, I removed it.
      But I also needed to find a replacement with low quiescent current and a fair price, preferably in a SOT23 package size.

  • @haz939
    @haz939 Před 10 lety

    Now I know why my project with an Attiny programmed with Arduino code runs much slower then on the 16 MHz Arduino. The Attiny only runs at 8 MHz / 4 MHz so any timers that runs on it will need dividing by 2 for the 8 MHz clock and 3 for the 4 MHz clock. I thought it was something to do with the system clock . Thanks !!

  • @gaza2322
    @gaza2322 Před 2 lety

    I always thought it was mad to include a deep sleep function that requires a physical interrupt to bring it back to life. Why not just deepsleep(1Hour)? Anyway, best way to do this is by removing the power completely to the Arduino with a TPL5110 power timer breakout chip - really easy to use. The only downside is it can only delay by 2 hours max. Data sheet for TPL5110 says 35nA when timing. In reality it is 20uA, which is pretty good!

  • @deqdanandi6927
    @deqdanandi6927 Před 6 lety +1

    can u explain to me..why we used the coding
    CLKPR = 0 x 80;.....the 80 is stand for what?

    • @genericandroid5611
      @genericandroid5611 Před 5 lety

      0x80 is hexidecimal for the decimal value of 128.
      Hope this helps!

    • @steipingenieria322
      @steipingenieria322 Před 5 lety

      You need to read datasheet section CLKPR register. This section explains that CLKPR register setup clock prescale system and it's has 7 bits for read/write register. So the is"CLKPCE" sub-register on CLKPR and allow you enable a change over clock system before you setup clock prescale value with the bit 3 to 0 on CLKPR. The BIT7 CLKPCE register is only "OK "setup, if you simultaneosly written to zero the bits6 to bits0 on register general CLKPR. So this is bit7[1]-bit6[0]-bit5[0]-bit4[0]-bit3[0]-bit2[0]-bit1[0]-bit0[0] = 1 0 0 0 0 0 0 0 and if you take bits in two group of 4 bits you obtain [1000]-[0000] that is [8]-[0] in hexadecimal value. So you can use CLKPR = 0b10000000 or CLKPR=0x80 when index "b" is binary value and "x" is binary value register setup.

    • @neosapien247
      @neosapien247 Před 5 lety

      I suggest getting a good understanding of binary and hexadecimal notations before jumping on to projects.

  • @MikePoirier
    @MikePoirier Před 10 lety

    Nice !

  • @chenglin1440
    @chenglin1440 Před 8 lety +1

    Could anyone tell me why u can estimate the current consumption by measuring between the GND of the FTDI and the GND of the arduino mini ? Thanks a lot !

    • @nrdesign1991
      @nrdesign1991 Před 7 lety +2

      It's a little late, but here's my answer: You can place a current meter anywhere in series with the circuit. It does not matter if it is placed on VCC or GND. Take into account the internal shunt resistance of the meter.

  • @abyssunderground
    @abyssunderground Před 10 lety

    I didn't know about the pre-scalar, I'll have to look into this one for my current project. Thanks =) Also, do you have a link to that massive PDF?

    • @JulianIlett
      @JulianIlett  Před 10 lety +3

      www.atmel.com/Images/doc8161.pdf‎

    • @abyssunderground
      @abyssunderground Před 10 lety

      Julian Ilett Thank you very much =)

    • @abyssunderground
      @abyssunderground Před 10 lety

      Julian Ilett Thanks again for this video! I have just spent a few hours playing with this on my Uno and it works a treat. I did figure out that if you use OneWire or anything relying on precise timing you do have to set the speed to 16MHz again while it does it's stuff, then you can drop down again. Thankfully that's easy as pie with those two simple lines. I made two functions, clock_high() and clock_low() which I can call when needed in my sketches. Specifically I've been using a DS18B20 temperature probe which prompted me to find a solution otherwise it just didn't show a value when probed.

    • @JulianIlett
      @JulianIlett  Před 10 lety

      abyssunderground I don't think there's much inside the microcontroller which is unaffected by changes in the clock prescaler. The block diagrams in the data sheet will help identify those that are. Very pleased that you've found a use for it.

  • @supercables251
    @supercables251 Před 5 lety +2

    you are setting CLKPR to two different values. Why?

    • @waqarmehdi4394
      @waqarmehdi4394 Před 4 lety

      That is what the datasheet instructs you to do in order to follow the correct procedure.
      Watch here @4:30

  • @user-ov8zx7bv2k
    @user-ov8zx7bv2k Před 4 lety

    IQ +10 Thank you.

  • @beehive808
    @beehive808 Před 10 lety

    Julian, I am trying to follow along. The part a don't understand is "have to set bit 7 and all other to zero", I don't know how bit 7 equal CLKPR = 0x80. I do understand the second CLKPR = 0x01 for / by 2 , 0x02 is / by 4. Keep the videos comming

    • @JulianIlett
      @JulianIlett  Před 10 lety

      0x80 (hexadecimal 80) is 10000000 in binary - so bit7 is set (leftmost bit) and all the others are reset.

    • @beehive808
      @beehive808 Před 10 lety

      I understand 1000000 is 0x80 but you were looking at the 7 bit. I am still missing something

    • @Duisnu
      @Duisnu Před 10 lety

      bee Hive 10000000 sets the 8th bit, but since the bits are numbered from 0 that is the 7th bit

    • @beehive808
      @beehive808 Před 10 lety

      Thanks

    • @ColinRichardson
      @ColinRichardson Před 8 lety

      +Julian Ilett it might be worth next time writing stuff as 0b1000000 rather than 0x80, I know hex is so much shorter, but might help newcomers.

  • @joohop
    @joohop Před 10 lety

    clever

  • @supercables251
    @supercables251 Před 5 lety

    try OBS screen capture or uploading you source code

  • @encodersofia
    @encodersofia Před 9 lety

    There is sleep library called Narcoleptic - code.google.com/p/narcoleptic/. I also did the same prescale experiments quite a time ago and the Narcoleptic library is on my HDD from back then. Unfortunately i don't remember the results using it. May be worth a try again. Cheers

  • @frankpariscom
    @frankpariscom Před 5 lety +1

    Not for nothing but you do not show the power source hookup for us newbies so I give you one star

  • @tobortine
    @tobortine Před 10 lety +1

    Given that it's doing absolutely nothing 230 micro amps is quite a lot ;-) But nevertheless an interesting video.

    • @abyssunderground
      @abyssunderground Před 10 lety +4

      Most batteries have a self discharge higher than this so it is pretty low compared to that =)

    • @tobortine
      @tobortine Před 10 lety +2

      abyssunderground ...and it's not strictly true that it isn't doing anything because it does have interrupt detecting routines still running in order to wake up.

    • @Ziplock9000
      @Ziplock9000 Před 6 lety

      Thats not how interrupts work. By thier very nature they are not polled

  • @miksu103
    @miksu103 Před 10 lety

    I've read you could remove regulator from this board and connect it straight to li-po battery. Voltage should stay in safe limits for this chip.

    • @JulianIlett
      @JulianIlett  Před 10 lety

      You may be better off with the 3.3v Pro Mini. There's a graph in that 440 page datasheet which shows the maximum clock frequency recommended for different Vcc voltages. It's difficult to tell from the graph, but running at 16MHz at 3.7v or less looks a bit borderline. I think that's why the 3.3v Pro Mini has an 8MHz resonator.

    • @miksu103
      @miksu103 Před 10 lety

      Julian Ilett It's out of spec but what I've read it runs just fine. And I remember they sell those where you can change the voltage from 5v to 3.3v (16mhz) on ebay.

    • @superdau
      @superdau Před 10 lety

      *****
      The problem with running out of spec isn't it stopping to work. I've run AVRs at around 30Mhz. The problem is, that it SOMETIMES and irreproduceable craps out. It could depend on temperature or on input voltage or the "weather of the day".
      Some things just get unreliable as soon as you move out of spec. The same AVR (speced for 16MHz max), that I could run at 30MHz with normal code, would corrupt the EEPROM for example when just running at 20MHz.
      There is a reason the manufacturers give you specs for a device.

  • @BjornErikLarsen
    @BjornErikLarsen Před 10 lety +3

    Hi Julian,
    The sparkfun tutorial www.sparkfun.com/tutorials/309 "Adventures in Low Power Land" is a great resource regarding lowering the power consumption as musch as possible.
    -Bjorn Erik

    • @BjornErikLarsen
      @BjornErikLarsen Před 10 lety +1

      Also look at the KickStarter project called µCurrent GOLD. Dave Jones has shed some light into the Low Current Land and limitations when measuring very low currents. Ref: www.kickstarter.com/projects/eevblog/current-gold-precision-multimeter-current-adapter
      I have ordered this adapter and I am looking forward to use it.
      From the video you can see the MCU current consumption duing startup from sleep using a DSO. Very informative :-)

    • @JulianIlett
      @JulianIlett  Před 10 lety

      That is a good tutorial - thanks Bjorn

  • @lezbriddon
    @lezbriddon Před 8 lety

    if you buy the cheap nano withbuilt in usb they tend to ship with a 5v regulator, you can swap these to a 3 but then the poor thing is running at 3v/16mhz, which is essentially overclocking it, so you could use the clkpr command to bring it back down to 8mz, and i'm sure if you tell the arduino IDE software its an 8mhz device, all the timings for delay and serial comms etc will be fine.
    please note, if you change the 5v reg to a 3v, when you connect the usb cable then by the way these are designed you put 5v into the output of the 3v regulator , it does not like this!, so remove the diode on the usb 5v line and power the chip with the 5v from usb connected to vin/vraw with a diode or a jumper

    • @Diamonddrake
      @Diamonddrake Před 8 lety

      +lez briddon Nanos run on a atmega328p, they are rated for 20mhz, so no. 16mhz is not overclocking it.

    • @pavelp80
      @pavelp80 Před 8 lety +1

      Running atmega328p on 20mhz/5v, 16mhz/5v, 8mhz/5v, 8mhz/3.3v is okay, but running at 16mhz/3.3v is running above specification - maximum frequency depends on voltage.

  • @Anvilshock
    @Anvilshock Před 7 lety +1

    "16 times slower" God please no, please stop using this idiotic incomputable phrasing of multiplying a relative modifier!! It makes NO sense, it is completely arbitrary, and people who actually want to learn about the stuff you're talking can't reliably tell whether it's now 1/16th the speed, 1/17th, 1/15, or anything else. "A sixteenth of a thousand" - See? Was that so hard? Perfectly logical, computable, and reliable.

    • @goomba5331
      @goomba5331 Před 5 lety

      To be honest dude the way he put it was alot easier to understand than "thousands" that's just more shit you gotta make your head compute, stop being a smartass you internet troll