ATmega328P targeted at low-power battery use: help needed with baby steps!

Hi,

I'm working on the OpenTRV project (I expect I can't post links initially and so I won't even try, but you can find it with your favourite search engine if you wish) for saving energy in heating systems with a smart retrofit Thermostatic Radiator Valve and some stuff to call for heat from a boiler centrally.

I already have a PICAXE prototype up and running at my house across multiple nodes each controlling wireless valves via RFM22/RFM23, etc.

I want to build a parallel ATmega/Arduino stack which I expect to be able to do more sophisticated things, and more power-efficiently.

In some configurations our hardware will be powered by a couple of AA rechargeables so operation down to around 2V is needed, but operation at 3.3V (or 5V) may also happen, eg at mains-powered nodes.

I have so far succeeded in using my UNO as ISP to get a (8MHzRC/8, see below) bootloader into a couple of blank ATmega328P chips (so far as I can tell), but moving on to program them a TTL-232R-3V3 has me foxed so far.

I am running IDE 1.0.3 on a Mac.

I have added this to the end of the boards.txt file:

atmega328bb2AA.name=ATmega328 on a breadboard (8 MHz internal clock / 8), 2xAA supply 1.8V BOD

atmega328bb2AA.upload.protocol=stk500
atmega328bb2AA.upload.maximum_size=30720
atmega328bb2AA.upload.speed=57600

atmega328bb2AA.bootloader.low_fuses=0x62
atmega328bb2AA.bootloader.high_fuses=0xDA
atmega328bb2AA.bootloader.extended_fuses=0x06
atmega328bb2AA.bootloader.path=arduino:atmega
atmega328bb2AA.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
atmega328bb2AA.bootloader.unlock_bits=0x3F
atmega328bb2AA.bootloader.lock_bits=0x0F

atmega328bb2AA.build.mcu=atmega328p
atmega328bb2AA.build.f_cpu=1000000L
atmega328bb2AA.build.core=arduino
atmega328bb2AA.build.variant=standard

Note in particular the fuses (so the CPU clock is 1MHz, and BOD is set at 1.8V) and the f_cpu (set to 1MHz correctly) I hope.

Attempting to load the blink sketch spits out, after a pause:

avrdude: stk500_recv(): programmer is not responding

My best guess was that I probably also had to reduce "upload.speed" to 7200, but no luck so far.

I have automatic reset apparently working (reset tied high with 10K, via 100nF to RTS, so a LED on digital pin 13 (PB5) blinks when the IDE opens the connection and slowly thereafter).

I'm pretty familiar with electronics and computing, but have no AVR-specific test gear. I have a couple of these 328Ps, an UNO and a Leonardo.

Any other clues?

Rgds

Damon

Would need to review your fuse settings with a fuse calculator, make sure they make sense. http://www.engbedded.com/fusecalc

I have not tried to download a sketch via serial into a slow running part; serial speed lkely needs to be reduced as you noted (to 57600/8, or 115200/8). Might also just have to download via ICSP (no bootloader) using File:Upload Using Programmer option.

Should change your descriptions to say 1 MHz also, not 8.

DamonHD: ...but moving on to program them a TTL-232R-3V3 has me foxed so far.

Why bother? You have a method to program the target. Why do you also need a bootloader?

Because if possible I want for me, and others playing with OpenTRV, to be able to be able to reprogram in situ (frequently during development) without levering chips in and out, as I do with the PICAXE for example, and without buying expensive or non-main-line hardware. IE I want the development cycle to be cheap and fast and easy.

And I didn't find out how to get anything other than the bootloader into my ATmega32Ps via my UNO, but if someone could point me at the generic way to do that I'd be interested.

But note that it may simply be impractical given that 5V from the UNO would be toxic to some of the main components on my target system, and other in-situ users of the SPI bus in particular.

Fuse settings: I have run them back and forth through a calculator, and the brain of someone fairly experienced in AVR stuff, so I may have got them wrong still, but not for want of cross-checking!

Yes, probably I should change the name!

Rgds

Damon

PS. But I likely need to be educated about what the best way to do things is in this AVR/Arduino environment: every dev ecosystem is different.

The new optiboot build says:

 make atmega328 AVR_FREQ=1000000 BAUD_RATE=57600
BAUD RATE CHECK: Desired: 57600, Real: 62500, UBRRL = 1, Error=-8.5%
avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=1000000  -DBAUD_RATE=57600 -DLED_START_FLASHES=3       -c -o optiboot.o optiboot.c
optiboot.c:252:2: error: #error BAUD_RATE error greater than -5%
optiboot.c:277:2: error: #error Unachievable baud rate (too fast) BAUD_RATE
make: *** [optiboot.o] Error 1

A bit of research suggests that 9600 should work OK. Also 31250 (MIDI bitrate; it might be widely implemented.)

Does the bootloader on the AVR auto-detect the baud of the incoming data, eg can I just set 9600 in the stuff in boards.txt now?

Or does that value get baked into the boatloader itself from the boards.txt file so I'll have to change it and reload the ATmegas?

Rgds

Damon

OK, used the settings from the tutorial (with the core/variant adjustment) in the IDE's boards.txt:

atmega328bb.name=ATmega328 on a breadboard (8 MHz internal clock)

atmega328bb.upload.protocol=stk500
atmega328bb.upload.maximum_size=30720
atmega328bb.upload.speed=57600

atmega328bb.bootloader.low_fuses=0xE2
atmega328bb.bootloader.high_fuses=0xDA
atmega328bb.bootloader.extended_fuses=0x05
atmega328bb.bootloader.path=arduino:atmega
atmega328bb.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
atmega328bb.bootloader.unlock_bits=0x3F
atmega328bb.bootloader.lock_bits=0x0F

atmega328bb.build.mcu=atmega328p
atmega328bb.build.f_cpu=8000000L
atmega328bb.build.core=arduino
atmega328bb.build.variant=standard

to burn a new bootloader, and that all works fine. I can upload the blink sketch via my FDTI cable, and adjust it and re-upload, just fine.

So my USB and auto-reset and so on is fine by the looks of it.

So I think that nominally, using a baud of 7200 with the 1MHz clock should work. Maybe I have to adjust some of the delays in the ArduinoISP, or get myself a new bootloader .hex file with the F_CPU set for 1MHz and baud set for 9600 at that clock speed? (If the latter, I don't yet know how to do that.)

Rgds

Damon

as I do with the PICAXE for example

Why? If you jut put in a minijack and down load circuit, you can program a PicAxe in circuit.

I don't understand your point, sorry!

That is what I do with my PICAXE, eg here, with jack plugged in:

http://gallery.hd.org/_c/energy-matters/_more2013/_more04/OpenTRV-prototype-20130410-V0p09-PCB-populated-and-prepared-for-simple-deployment-5-DHD.jpg.html

I would like a load/test of new code to be as easy for the AVR version, even when the chip cannot be removed and has <5V peripherals as on the board above.

Rgds

Damon

Just put this circuit on your board with a mini-jack - though that is optional - and you can program in circuit: http://www.picaxe.com/Site_Resources/Media/Site_1/pinout/serial.jpg

And use the serial monitor for debugging.

I'm really not following.

That is how I use the PICAXE already.

Rgds

Damon

Then why can't you program and debug in circuit? Why do you pull the chip out to reprogram?

I don't.

What I'm trying to achieve with the ATmega is the same ease of reprogramming as I already have with the PICAXE.

Rgds

Damon

Oops, when you mentioned the PicAxe it was an example of what you want to do with the AT chips. Sorry I misunderstood.

Anyone got a precompiled 1MHz-friendly ATmega328P bootloader.hex file?

I've tried a few others without success.

I do fine at 8MHz, and have been working on the low power 'scheduler' core for my code instead, with rather more progress!

Rgds

Damon

DamonHD: Anyone got a precompiled 1MHz-friendly ATmega328P bootloader.hex file?

Here you go. They're untested I'm afraid, but I'm using Optiboot on other boards at non-standard frequencies and baud-rates with no problems.

atmega328_1a.hex (Optiboot for 9600baud at 1MHz)

:107E0000112484B714BE81FFE6D085E08093810001
:107E100082E08093C00088E18093C10086E0809377
:107E2000C2008CE08093C4008EE0BFD0259A86E02B
:107E300023EC3FEF91E0309385002093840096BBC4
:107E4000B09BFECF1D9AA8958150A9F7EE24FF2480
:107E5000AA24A394B5E0CB2EA1E1BA2EF3E0DF2E45
:107E600098D0813461F495D0082FA5D0023829F13B
:107E7000013811F485E001C083E083D07FC08234F3
:107E800011F484E103C0853419F485E09CD076C0F8
:107E9000853579F47ED0E82EFF247BD0082F10E0C2
:107EA000102F00270E291F29000F111F84D07801E1
:107EB00065C0863521F484E086D080E0DECF84364C
:107EC00009F040C066D065D0082F63D080E0E81686
:107ED00080E7F80618F4F701D7BEE895C0E0D1E0D6
:107EE00058D089930C17E1F7F0E0EF16F0E7FF06A2
:107EF00018F0F701D7BEE8955ED007B600FCFDCFBD
:107F0000A701A0E0B1E02C9130E011968C9111977F
:107F100090E0982F8827822B932B1296FA010C0160
:107F2000A7BEE89511244E5F5F4FF1E0A038BF0770
:107F300051F7F701C7BEE89507B600FCFDCFB7BE05
:107F4000E8951CC0843761F424D023D0082F21D0B9
:107F500032D0F70185917F0114D00150D1F70EC0C6
:107F6000853739F428D08EE10CD085E90AD08FE02E
:107F700084CF813511F488E018D01DD080E101D084
:107F80006FCF982F8091C00085FFFCCF9093C600E3
:107F900008958091C00087FFFCCF8091C00084FDD0
:107FA00001C0A8958091C6000895E0E6F0E098E150
:107FB000908380830895EDDF803219F088E0F5DF4B
:107FC000FFCF84E1DECF1F93182FE3DF1150E9F7D5
:107FD000F2DF1F910895282E80E0E7DFEE27FF27CC
:027FE000099402
:027FFE0000057C
:0400000300007E007B
:00000001FF

atmega328_1b.hex (Optiboot for 4800baud at 1MHz)

:107E0000112484B714BE81FFE6D085E08093810001
:107E100082E08093C00088E18093C10086E0809377
:107E2000C20089E18093C4008EE0BFD0259A86E02D
:107E300023EC3FEF91E0309385002093840096BBC4
:107E4000B09BFECF1D9AA8958150A9F7EE24FF2480
:107E5000AA24A394B5E0CB2EA1E1BA2EF3E0DF2E45
:107E600098D0813461F495D0082FA5D0023829F13B
:107E7000013811F485E001C083E083D07FC08234F3
:107E800011F484E103C0853419F485E09CD076C0F8
:107E9000853579F47ED0E82EFF247BD0082F10E0C2
:107EA000102F00270E291F29000F111F84D07801E1
:107EB00065C0863521F484E086D080E0DECF84364C
:107EC00009F040C066D065D0082F63D080E0E81686
:107ED00080E7F80618F4F701D7BEE895C0E0D1E0D6
:107EE00058D089930C17E1F7F0E0EF16F0E7FF06A2
:107EF00018F0F701D7BEE8955ED007B600FCFDCFBD
:107F0000A701A0E0B1E02C9130E011968C9111977F
:107F100090E0982F8827822B932B1296FA010C0160
:107F2000A7BEE89511244E5F5F4FF1E0A038BF0770
:107F300051F7F701C7BEE89507B600FCFDCFB7BE05
:107F4000E8951CC0843761F424D023D0082F21D0B9
:107F500032D0F70185917F0114D00150D1F70EC0C6
:107F6000853739F428D08EE10CD085E90AD08FE02E
:107F700084CF813511F488E018D01DD080E101D084
:107F80006FCF982F8091C00085FFFCCF9093C600E3
:107F900008958091C00087FFFCCF8091C00084FDD0
:107FA00001C0A8958091C6000895E0E6F0E098E150
:107FB000908380830895EDDF803219F088E0F5DF4B
:107FC000FFCF84E1DECF1F93182FE3DF1150E9F7D5
:107FD000F2DF1F910895282E80E0E7DFEE27FF27CC
:027FE000099402
:027FFE0000057C
:0400000300007E007B
:00000001FF

boards.txt entries:

atmega328_1mhz_9600baud.name=Optiboot 1MHz 9600baud
atmega328_1mhz_9600baud.upload.protocol=arduino
atmega328_1mhz_9600baud.upload.maximum_size=32256
atmega328_1mhz_9600baud.upload.speed=9600
atmega328_1mhz_9600baud.bootloader.low_fuses=0xff
atmega328_1mhz_9600baud.bootloader.high_fuses=0xde
atmega328_1mhz_9600baud.bootloader.extended_fuses=0x05
atmega328_1mhz_9600baud.bootloader.path=optiboot_v50
atmega328_1mhz_9600baud.bootloader.file=atmega328_1a.hex
atmega328_1mhz_9600baud.bootloader.unlock_bits=0x3F
atmega328_1mhz_9600baud.bootloader.lock_bits=0x2F
atmega328_1mhz_9600baud.build.mcu=atmega328p
atmega328_1mhz_9600baud.build.f_cpu=1000000L
atmega328_1mhz_9600baud.build.core=arduino
atmega328_1mhz_9600baud.build.variant=standard

atmega328_1mhz_4800baud.name=Optiboot 1MHz 4800baud
atmega328_1mhz_4800baud.upload.protocol=arduino
atmega328_1mhz_4800baud.upload.maximum_size=32256
atmega328_1mhz_4800baud.upload.speed=4800
atmega328_1mhz_4800baud.bootloader.low_fuses=0xff
atmega328_1mhz_4800baud.bootloader.high_fuses=0xde
atmega328_1mhz_4800baud.bootloader.extended_fuses=0x05
atmega328_1mhz_4800baud.bootloader.path=optiboot_v50
atmega328_1mhz_4800baud.bootloader.file=atmega328_1b.hex
atmega328_1mhz_4800baud.bootloader.unlock_bits=0x3F
atmega328_1mhz_4800baud.bootloader.lock_bits=0x2F
atmega328_1mhz_4800baud.build.mcu=atmega328p
atmega328_1mhz_4800baud.build.f_cpu=1000000L
atmega328_1mhz_4800baud.build.core=arduino
atmega328_1mhz_4800baud.build.variant=standard

Ooooo! Lovely, thanks!

I'll give that a whirl in the morning when I can get at my ISP!

Rgds

Damon

PS. Down to ~20uA for my 1Hz control loop with 8MHz clock, so at 1MHz... B^>

Thank You tim7 for the nice job... I can use them myself for several low powered battery powered applications.. and Thank You @ DamonHD for asking the questions...

Doc

Hi Tim,

Should the extended fuse be 0x06 to allow for 1.8V BOD?

Should the low fuse be 0x42 at least to have the /8 clock prescaler selected for the 8MHz RC clock?

Rgds

Damon

Hi,

I have just successfully used your bootloader, thank you, with fuse modifications and other tweaks for my IDE:

atmega328_1mhz_4800baud.name=Optiboot 5 for bare 328P, 1MHz internal clock, fast start, 1.8V BOD, 4800baud upload, c/o tim7 20130416
atmega328_1mhz_4800baud.upload.protocol=arduino
atmega328_1mhz_4800baud.upload.maximum_size=32256
atmega328_1mhz_4800baud.upload.speed=4800
#atmega328_1mhz_4800baud.bootloader.low_fuses=0xff
atmega328_1mhz_4800baud.bootloader.low_fuses=0x42
atmega328_1mhz_4800baud.bootloader.high_fuses=0xde
#atmega328_1mhz_4800baud.bootloader.extended_fuses=0x05
atmega328_1mhz_4800baud.bootloader.extended_fuses=0x06
#atmega328_1mhz_4800baud.bootloader.path=optiboot_v50
atmega328_1mhz_4800baud.bootloader.path=atmega
atmega328_1mhz_4800baud.bootloader.file=atmega328_1b.hex
atmega328_1mhz_4800baud.bootloader.unlock_bits=0x3F
atmega328_1mhz_4800baud.bootloader.lock_bits=0x2F
atmega328_1mhz_4800baud.build.mcu=atmega328p
atmega328_1mhz_4800baud.build.f_cpu=1000000L
atmega328_1mhz_4800baud.build.core=arduino
atmega328_1mhz_4800baud.build.variant=standard

Rgds

Damon