ATmega328PB low power / sleep

There has been a(since closed) thread about adapting the LowPower library to work with ATmega328PB(here ). Since I did not find any solution already posted, I would like to share mine.

The library does check on procesor type, and throws error if you try to use it for unknown procesor. Since ATmega328PB is binary compatible with ATmega328P types, all you need to do is add it to the list of known procesors.

Find your LowPower.h file and locate line

#if defined (__AVR_ATmega328P__) || defined (__AVR_ATmega168__)

For me, it was on line 123, but this might change in future versions.
Now change it to

#if defined (__AVR_ATmega328PB__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega168__)

and save the file.
Now, you should be able to use the LowPower library on ATmega328PB, provided you installed board definition that supports ATmega328PB, such as MiniCore.

Hope this helps someone.

Where does the library come from?

If it's in github somewhere, I would also add the 328, 168P, 168PB, 88, 88P, 88PB to that line (same for other obvious lines where they cater to specific processors and the whole family is obvious), and then submit your changed file via a PR to improve the library.

That said, what does that library do that justifies using a separate library for it? I usually do power management using the avr/sleep.h library (which is extremely lightweight and supplied by manufacturer)... I think a lot of arduino people use additional libraries for things that really don't justify it, adding additional steps like, for example, having to modify libraries to handle new parts.

Could I ask for clarification regarding the library, which one and what file should I change, the .cpp file?

I’m using a PB and I just can’t get it to work as it should in sleep mode, when compared to a P for the same project.

I used minicore for a 328PB, 8MHz external and used the Canique fork of the Rocketscream lib (is that the one you refer to?) and the current consumption is 5mA. Whereas for a P, using Rocketscream, the current consumption was 150uA.

I’m hoping what you’ve stated will help. I’m not trying to high jack the thread, just stating for clarification. Thanks

EDIT
Sorry you said the .h file. My bad

exiledyorkie:
Could I ask for clarification regarding the library, which one and what file should I change, the .cpp file?

I’m using a PB and I just can’t get it to work as it should in sleep mode, when compared to a P for the same project.

I used minicore for a 328PB, 8MHz external and used the Canique fork of the Rocketscream lib (is that the one you refer to?) and the current consumption is 5mA. Whereas for a P, using Rocketscream, the current consumption was 150uA.

I’m hoping what you’ve stated will help. I’m not trying to high jack the thread, just stating for clarification. Thanks

EDIT
Sorry you said the .h file. My bad

Yes, I am refering to the Rocketscream's library. You can find the header file in your library folder, usually in Documents/Arduino/libraries

Unfortunately that didn't work for myself. I changed what you mentioned in the .h file but whenever I compile the sketch, I get a 'Error compiling for 328 board'.

My board is the settings of the Minicore bootloader, which is what I normally do, rather than selecting a board, such as an Uno etc.

Any suggestions? Should I edit the .cpp file?

EDIT;

I did change the .cpp file but it didn't seem to like that, so I reverted back to your advice in the OP. It turns out my code, which worked perfectly for the 328P, does not work on the PB.

    LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,
                 SPI_OFF, USART0_OFF, TWI_OFF);

Just thought I'd add for others looking, as I'm trying to figure the same issue out which other may be also.

tada9 - that's where the library is after you install it. But where did you get it? Is it in library manager by default? Did you download it and install normally? If so, from where?

Unfortunately that didn't work for myself. I changed what you mentioned in the .h file but whenever I compile the sketch, I get a 'Error compiling for 328 board'.

Post the complete error message - there will be content in console window further up that shows the actual error. If you share that we could tell you what's wrong.

that is the GitHub library

The link below is a fork of that lib, but the same problem rises when using the code i posted previously.

Here is the error, I've had to remove some as it was too large to post but I believe that is the relevant stuff

Arduino: 1.8.8 (Windows 10), Board: "ATmega328, Yes (UART0), 328PB, BOD 1.8V, LTO disabled, 8 MHz external"


Linking everything together...
"C:\\Users\\MAGUIRES\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino5/bin/avr-gcc" -w -Os -Wl,--gc-sections -mmcu=atmega328pb -o "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214/PBtest.ino.elf" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214\\sketch\\PBtest.ino.cpp.o" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214\\libraries\\SPI\\SPI.cpp.o" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214\\libraries\\RF24\\RF24.cpp.o" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214\\libraries\\Low-Power-master\\LowPower.cpp.o" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214\\libraries\\Adafruit_BME280_Library\\Adafruit_BME280.cpp.o" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214\\libraries\\Wire\\Wire.cpp.o" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214\\libraries\\Wire\\utility\\twi.c.o" "C:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214/..\\arduino_cache_122153\\core\\core_051c112297c9a9a80fa9b975190c0789.a" "-LC:\\Users\\MAGUIRES\\AppData\\Local\\Temp\\arduino_build_273214" -lm
C:\Users\MAGUIRES\AppData\Local\Temp\arduino_build_273214\sketch\PBtest.ino.cpp.o: In function `send()':

C:\Users\MAGUIRES\Desktop\PBtest/PBtest.ino:113: undefined reference to `LowPowerClass::idle(period_t, adc_t, timer2_t, timer1_t, timer0_t, spi_t, usart0_t, twi_t)'

collect2.exe: error: ld returned 1 exit status

Using library SPI at version 1.0 in folder: C:\Users\MAGUIRES\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.0.3\libraries\SPI 
Using library RF24 at version 1.3.3 in folder: C:\Users\MAGUIRES\Documents\Arduino\libraries\RF24 
Using library Low-Power-master at version 1.6 in folder: C:\Users\MAGUIRES\Documents\Arduino\libraries\Low-Power-master 
Using library Adafruit_Sensor-master at version 1.0.3 in folder: C:\Users\MAGUIRES\Documents\Arduino\libraries\Adafruit_Sensor-master 
Using library Adafruit_BME280_Library at version 1.0.10 in folder: C:\Users\MAGUIRES\Documents\Arduino\libraries\Adafruit_BME280_Library 
Using library Wire at version 1.0 in folder: C:\Users\MAGUIRES\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\2.0.3\libraries\Wire 
exit status 1
Error compiling for board ATmega328.

exiledyorkie:
Unfortunately that didn't work for myself. I changed what you mentioned in the .h file but whenever I compile the sketch, I get a 'Error compiling for 328 board'.

This error does not come from the LowPower library itself. At least I cant find this error message in its files. You seem to be using a lot of different libraries in your sketch. Try testing them one at a time to see where the error comes from.

Ah okay. I’ll look into that. And get rid of things step by step. Thanks for the advice.

Although, it’s the same code, exactly, which works with the 328P. Reading the docs, the code should be interchangeable for the P and PB.