ATtiny and tone()

Hi all

I've been trying to get the tone() function to work on an ATtiny45. It won't happen. When I search for info I find all these threads about how well it works, and even though I copy their sketches they won't produce sound on mine.

I use a small piezo connected to pin 1. The code is really simple:

#define PIEZO 1

void setup(){
pinMode(PIEZO, OUTPUT);
}

void loop(){
tone(PIEZO,500);
delay(1000);
noTone(PIEZO);
delay(1000);
}

Like that. Not a sound. Naturally I tried the piezo on an UNO, and that works fine. I also tried the piezo with another tone library (http://w8bh.net/avr/TrinketTone.pdf) and that worked. But it uses delays, so I can't have it. It would interfere with the other code I need to run on the chip.

I use an UNO as ISP, and the board settings are downloaded from board manager. I get no complaints from the compiler when I call tone(), so it seems to be included. I've tried both 1 and 8 MHz.

Any ideas? I'm lost.

Ok, not a lot of answers. But I found the error myself and thought I'd share it so noone else needs to spend several days on this.

The ATtiny core provided in the board manager is faulty. The tone() function is included but doesn't work. When I installed an older ATtiny core it works just fine!

Installing that was a bit of a hassle for me though, since the install instructions in the "ReadMe" file are obsolete. I'm on mac, and had to find the library folder/Arduino15 etc to manually insert the board profiles into the existing boards.txt. Probably a piece of cake for someone more at ease under the hood than me.

Does anyone know who you contact about a found error in the cores provided by the boards manager?

Mixe:
The ATtiny core provided in the board manager is faulty.

Which one? Before you were able to install the core via Boards Manager you had to add a URL to File > Preferences > Additional Boards Manager URLs. Don’t you think it would have been helpful to specify which URL you used?

Mixe:
Does anyone know who you contact about a found error in the cores provided by the boards manager?

The author of the core. I can’t tell you who that is because you didn’t specify which Boards Manager URL you used. I can tell you who you should not contact about the issue, the Arduino IDE developers who have nothing to do with that 3rd party core other than being kind enough to allow 3rd party hardware packages to be installed via Boards Manager. This despite the fact that 3rd party hardware cuts into the sales of Arduino’s products which support the development of the Arduino IDE. Now they have to deal with support requests for these cores. For this reason I would recommend you to close your issue report:

unless you have determined that this is absolutely a bug with the Arduino IDE or Arduino AVR Boards core, which I really doubt.

Which core? On my core, I haven't touched tone() since I got it working off the fancy timer1, so I'm pretty sure it works. If it's my core, and it doesn't work, that's something i should be made aware of so I can fix it

On my core, use pin 4 fit best results with tone, btw - though I remember testing it on the scope with both pin 4 and someven other pin

Ok, I downloaded version 1.02 of attiny by David A. Mellis. Is that yours?

And I will naturally close my issue report. I started that for lack of better advice- and, I might add, in an attempt to be helpful.

Mixe:
Ok, I downloaded version 1.02 of attiny by David A. Mellis. Is that yours?

It is GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8

Huh, I thought he had gotten that working... I thought i remember that coming up a while back

Mine is this one GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8

Here's the damellis/attiny tone() issue report:

The problem is that damellis/attiny references the arduino:arduino core, it doesn't include its own implementation of tone(). The arduino:arduino core's tone() doesn't work on ATtiny85. That would have been understandable before the ATtiny85 based Gemma was added as an official Arduino board but now it does constitute a bug, which has been reported:

So everyone involved is aware of this issue but nobody seems to be motivated to do anything about it. Luckily we have DrAzzy's excellent ATTinyCore to save the day! I recommend you to use that core instead of the much less capable damellis/attiny.

Thanks for the info! Beeing a relative newbie, I followed a tutorial on how to get the ATtiny:s from board manager. In a perfect world that core should have a warning when included by the Board Manager until issues are solved. Or just remove the reference to the Arduino tone(), so you'll at least get an error when compiling. But then again, I'm not capable of doing that myself, and in a world of open source you probably shouldn't ask people to do stuff you're not doing yourself.

Anyway, thanks for the info and advice. I have now found a fully working core. Unfortunately, this costed me precious evenings when trying to get a prototype working for a crucial demonstration.

Mixe:
maybe that core should be removed from Board Manager until issues are solved?

You're misunderstanding how Boards Manager works. There are several official Arduino cores(AVR. SAM, SAMD) and also some Arduino certified packages(e.g. Edison, Galileo, Curie) always available for installation via Boards Manager. Those companies pay a licensing fee to be "Arduino Certified". Originally official and certified cores were all that was available via Boards Manager. Next, Adafruit figured out how to hack the Arduino IDE to make it possible to install the esp8266 core via Boards Manager. Soon after that Arduino opened Boards Manager up to make it super easy for users of the Arduino IDE to install and update 3rd party hardware packages via Boards Manager, which previously had required a slightly more complex manual installation process and didn't have update notifications. So now anyone who wants to make their hardware package installable via Boards Manager can easily do so. All the user needs to do is add the URL to their JSON file to File > Preferences > Additional Boards Manager URLs. When you followed the tutorial that's what you did, maybe you forgot about it. Without that URL damellis/attiny would not be available via Boards Manager. So if you want to remove the "attiny by David Mellis" entry from Boards Manager, just remove that URL(https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json) from your preferences. Arduino has no control over what URLs the user adds to their preferences. It's worth spending a bit of time researching the available options and auditing your choice before deciding on a 3rd party hardware package because there is definitely a lot of variation in the quality of them. If you had clicked the "More info" link in the "attiny by David Mellis" Boards Manager entry and then done a search for tone in the issue tracker you would have quickly seen that tone() is not currently supported by that package.

damellis/attiny is a very simple project because it references the Arduino AVR Boards core. This made it easy to write and less effort to maintain than a project like ATTinyCore, which includes its own core. damellis/attiny still works well for many uses but not having its own core does impose limitations in that any necessary core modifications must be pushed to the Arduino AVR Boards core. I think the Arduino developers would be happy to merge a pull request that adds ATtiny85, etc. support to tone(), someone just needs to make the effort to submit the pull request. damellis/attiny was good in its day and many people are still using it but now we have very actively maintained superior core provided by the hard work of DrAzzy.

I'm actually the person who added Boards Manager installation support to damellis/attiny. It was the first project I did this for right after that capability was added to the Arduino IDE because at that time I was using damellis/attiny and was unaware of ATTinyCore so I thought it would be a useful contribution to the Arduino community. Since then David Mellis has made it the official installation method for the project and it's actually a little tricky to do a manual installation of it now.

DrAzzy, do you have plans to update your core to allow use of the tone() function?

I tried manually adding the core the OP linked to, but I ran into a slew of compiler errors.

The totality of the code is:

void setup() {
 pinMode(4, OUTPUT);
}

void loop() {
  tone(4, 2000);
}

So I'm pretty sure it has to do with broken pathways. I've already removed the old core, so I don't have the errors handy, but does anyone know if there are detailed instructions for manually installing new cores for IDE 1.6.9?

My core supports tone() - if it’s not working, that’s news to me.
Last I checked on the tiny85 with a simple test sketch (something like what you have), the code compiled and produced output at the specified frequency (verified with the scope, with both the hardware timer pin, and a different pin using the interrupt).

Since you’re using 1.6.9, make sure that LTO is disabled in the tools menu - the new compiler version that supports it came with 1.6.11 (specifically, AVR boards 1.6.13 is what brings the new compiler version - It comes with IDE version 1.6.11) - if you have that enabled on a version that doesn’t support it, nothing will compile.

Assuming that doesn’t fix it, please post the full text of any and all error messages.

Hi DrAzzy, I'm still not having any luck with the piezo, unfortunately. There are no compiler errors or anything that pop up. The errors I mentioned above were from using the Tiny Core and I'm pretty sure it had to do with broken directory pathways for accessing the compiler.

I'm using your core (Attiny Core by Spence Konde from the link you gave above) version 1.1.2, and I've tried several combinations of clock speeds still to no avail. Currently, I have Timer1 set to CPU, Clock is 8 MGHz internal, BOD and LTO both disabled. I've tried using pins 0, 1, 2, and 4. And I've tried several attiny85 chips as well.

The piezo I'm using tests fine when plugged into a nano. But all I get from the attiny is a single small click right when its powered on. When I plug an LED into the pins and send it a PWM signal, the LED lights up like it should. The only way I'm able to get any sound of the piezo at all is to send it a PWM signal of ~250 or less. Then it buzzes, but its not a very clean sound.

That sounds exactly like the errors I got when I was using David A Mellis' ATtiny core. Are you sure you are using the right core? The one I got to work was this one.
Not without some hiccups though- I too get error messages when compiling, but that's just phrase confusement and the compiler seems to correct it automatically. They look like this:

Board arduino:avr:attiny85at1 doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT1

It should be easy enough to change the faulty preference phrase, but it works and I haven't bothered.

I uninstalled the Damellis core both from the preferences and the board manager. It no longer appears anywhere in the program. The Spence Konde ATTiny Core is the only one that shows up.

I'll go back and try the other Tiny Core from here and see if I can get past those error codes (they do look the same as what I was getting). What did you do to correct them? And I apologize if you already covered this in a previous post.

No worries, I haven’t. Like I wrote: the errors are automatically corrected by the compiler, like it says in the error message, and the code compiles anyway. So I haven’t done anything to correct them.

Weird, mine would not compile using that core; it gave a "Problem compiling for ...." message. I'll go back and give it another go, then report back.

Mixe:
I too get error messages when compiling...

That is a warning. The IDE produces it. It can be ignored.

here is the complete compiler output:

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware "C:\Program Files (x86)\Arduino\hardware" -hardware "C:\Users\Alan\AppData\Local\Arduino15\packages" -hardware "C:\Users\Alan\Documents\Arduino\hardware" -tools "C:\Program Files (x86)\Arduino\tools-builder" -tools "C:\Program Files (x86)\Arduino\hardware\tools\avr" -tools "C:\Users\Alan\AppData\Local\Arduino15\packages" -built-in-libraries "C:\Program Files (x86)\Arduino\libraries" -libraries "C:\Users\Alan\Documents\Arduino\libraries" -fqbn=tiny:avr:attiny85at16p -ide-version=10609 -build-path "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp" -warnings=none -prefs=build.warn_data_percentage=75 -verbose "C:\Users\Alan\AppData\Local\Temp\arduino_modified_sketch_850444\sketch_feb07a.ino"
C:\Program Files (x86)\Arduino\arduino-builder -compile -logger=machine -hardware "C:\Program Files (x86)\Arduino\hardware" -hardware "C:\Users\Alan\AppData\Local\Arduino15\packages" -hardware "C:\Users\Alan\Documents\Arduino\hardware" -tools "C:\Program Files (x86)\Arduino\tools-builder" -tools "C:\Program Files (x86)\Arduino\hardware\tools\avr" -tools "C:\Users\Alan\AppData\Local\Arduino15\packages" -built-in-libraries "C:\Program Files (x86)\Arduino\libraries" -libraries "C:\Users\Alan\Documents\Arduino\libraries" -fqbn=tiny:avr:attiny85at16p -ide-version=10609 -build-path "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp" -warnings=none -prefs=build.warn_data_percentage=75 -verbose "C:\Users\Alan\AppData\Local\Temp\arduino_modified_sketch_850444\sketch_feb07a.ino"
Warning: Board tiny:avr:attiny85at16p doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT16P
Warning: Board tiny:avr:attiny85at8 doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT8
Warning: Board tiny:avr:attiny85at1 doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT1
Warning: Board tiny:avr:attiny85at16e doesn't define a 'build.board' preference. Auto-set to: AVR_ATTINY85AT16E
WARNING: Spurious .github folder in 'Adafruit MMA8451 Library' library
Warning: platform.txt from core 'Arduino Tiny' contains deprecated tools.avrdude.cmd.path={runtime.ide.path}/hardware/tools/avr/bin/avrdude, automatically converted to tools.avrdude.cmd.path={path}/bin/avrdude. Consider upgrading this core.
Warning: platform.txt from core 'Arduino Tiny' contains deprecated tools.avrdude.config.path={runtime.ide.path}/hardware/tools/avr/etc/avrdude.conf, automatically converted to tools.avrdude.config.path={path}/etc/avrdude.conf. Consider upgrading this core.
Warning: platform.txt from core 'Arduino Tiny' contains deprecated recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{build.path}/{archive_file}" "{object_file}", automatically converted to recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} "{archive_file_path}" "{object_file}". Consider upgrading this core.
Warning: platform.txt from core 'Arduino Tiny' contains deprecated recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} "{build.path}/{archive_file}" "-L{build.path}" -lm, automatically converted to recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} -o "{build.path}/{build.project_name}.elf" {object_files} "{archive_file_path}" "-L{build.path}" -lm. Consider upgrading this core.
"avr-g++" -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections  -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=16000000L -DARDUINO=10609 -DARDUINO_AVR_ATTINY85AT16P -DARDUINO_ARCH_AVR  "-IC:\Users\Alan\Documents\Arduino\hardware\tiny\avr\cores\tiny" "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\sketch\sketch_feb07a.ino.cpp" -o "nul"
"avr-g++" -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections  -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=16000000L -DARDUINO=10609 -DARDUINO_AVR_ATTINY85AT16P -DARDUINO_ARCH_AVR  "-IC:\Users\Alan\Documents\Arduino\hardware\tiny\avr\cores\tiny" "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\sketch\sketch_feb07a.ino.cpp" -o "nul"
"avr-g++" -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections  -w -x c++ -E -CC -mmcu=attiny85 -DF_CPU=16000000L -DARDUINO=10609 -DARDUINO_AVR_ATTINY85AT16P -DARDUINO_ARCH_AVR  "-IC:\Users\Alan\Documents\Arduino\hardware\tiny\avr\cores\tiny" "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\sketch\sketch_feb07a.ino.cpp" -o "C:\Users\Alan\AppData\Local\Temp\build6b670ed53e9cccc2da9fe5b6ba847dfa.tmp\preproc\ctags_target_for_gcc_minus_e.cpp"
exec: "avr-g++": executable file not found in %PATH%
Error compiling for board ATtiny85 @ 16 MHz  (internal PLL; 4.3 V BOD).

Okay, I found instructions here for getting the compiler to work. It now compiles without any errors, but I am still getting no tone.

The closest I can get is to just use PWM for the rough sounding tone. If I set the clock speed to 1 MGHz, and the frequency to 2,000 then I get an unsteady output of clicks, but if I change the clock speed and keep everything else stable, I don't even get that. If I keep the clockspeed at 1MGHz and set the output frequency to 10,000, then I get a buzzing, but not a clean tone. Raising the frequency above 10,000, even as high as 50,000 makes no further change in the sound.