1

I set the fuses of my attiny85 so it uses the internal 128khz clock, prescaled so it is 16kHz. I set the CKOUT fuse too so I can check the clock using an oscilloscope on pin 3 and yes it is 16kHz.

Now my problem is I have no option in the Arduino IDE to compile my code for 16kHz clock speed. The slowest I have is 128khz, which causes the "blink" sketch to run for 8 seconds up, 8 seconds low on my attiny instead of 1 second.

I tried with "attiny" from David A. Mellis using https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json (Slowest : 1Mhz) and "ATTinyCore" from Spence Konde using http://drazzy.com/package_drazzy.com_index.json (Slowest : 128khz).

Does someone know how I can set my Arduino IDE to compile the code for an attiny85 using the 16khz internal clock ?

(Please, no question "why use a 16khz clock ?". The point is, this chip may be set to 16khz clock, so it should be possible to program it to use such clock speed)

Thank you.

6
  • I suggest you pick whichever core is your favourite and then open a github (or wherever it's hosted) issue to add the relevent clock speed settings.
    – Majenko
    Aug 8, 2021 at 14:44
  • @Majenko Thank you for the suggestion. I hope there is already one providing this clock speed and someone will point it out here, but if not I will do as you say.
    – Squall124
    Aug 8, 2021 at 14:48
  • For the ATTinyCore, you can change in the boards.txt setting for the variant you're using (github.com/SpenceKonde/ATTinyCore/blob/master/avr/…). Modify f_cpu foremost. I don't know about clocksource = 3 though, look that up in the code. The boards.txt should locally be in C:\Users\<user>\AppData\Local\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2 Aug 8, 2021 at 14:55
  • It should just be a matter of adjusting the boards.txt file for the core, but there may be some technical reason (calculations that fail) at that clock speed for why it's not implemented already.
    – Majenko
    Aug 8, 2021 at 14:55
  • 1
    @MaximilianGerhardt I modified the f_cpu and the low_fuses values to my values (16000L and 0x24) and it works ! Thank you ! I didn't modify the clocksource=3 as I don't know what it means.
    – Squall124
    Aug 8, 2021 at 15:18

1 Answer 1

1

The Arduino core you've referenced (and pretty much all other AVR Arduino cores too) specify the clock frequency that a particular selected board is running at in the boards.txt file. The platform.txt file then retrieves that value and creates the F_CPU macro with the clock frequency in hertz as a value.

For the ATTinyCore by Spence Konde, the ATTiny85 board definition is the attinyx5 section. There, you can either add a new clock selection menu item or modify the settings of an existing one, e.g.

https://github.com/SpenceKonde/ATTinyCore/blob/f5eabc8fb97e8fd72f1782a5e97345729e42ec47/avr/boards.txt#L409-L412

attinyx5.menu.clock.128internal=128 kHz (internal WDT)
attinyx5.menu.clock.128internal.bootloader.low_fuses=0xC4
attinyx5.menu.clock.128internal.build.f_cpu=128000L
attinyx5.menu.clock.128internal.build.clocksource=3

The file is locally under C:\Users\<user>\AppData\Local\Arduino15\packages\ATTinyCore\hardware\avr\1.5.2.

There, f_cpu should be modified to 16000L for 16KHz, the low_fuses (lfuse) setting should be set to 0x24 and the text "128 kHz" should be adapted too. This is working per the comment on the question.

On a final note, you should create an issue at the core's repo to have a 16kHz added by default.

Also, you might find it easier to work with PlatformIO (+VSCode) instead of the Arduino IDE. A getting started page is here. In PlatformIO, instead of having to modify the internal boards.txt file, you can create a project configuration file (platformio.ini) in which you specify the same thing setting.

[env:attiny85]
platform = atmelavr
board = attiny85
framework = arduino
board_build.f_cpu = 16000L
; for upload settings, see 
; https://docs.platformio.org/en/latest/platforms/atmelavr.html#upload-using-programmer
1
  • 1
    Thank you for all the informations ! A lot to read, a lot to learn, a lot to try ! :p
    – Squall124
    Aug 8, 2021 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.