ATtiny and tone()

Sorry for the late reply here - I was sick in bed all week.

The compiler output indicates that you're not using my core - you may have it installed, but you also have at least one other core for the ATTiny, and you've selected a board definition from that core, not from mine.

The name of the board def supplied by my core for the Tiny85 series is "ATtiny25/45/85", and it will be under the ATTinyCore heading - you can then select the clock speed and which specific chip you're using from the tools menu.

If you use my core, Tone() should work.

Hi DrAzzy, I've switched a lot of things around a few times, so to save you from having to read everything in detail, I'll sum it up for you:

I tried using the latest version of your core at multiple clock speeds. Everything compiled with no issues, but there was simply no tone. I tried using several pins, and several attiny's, all with the same effect. I even attempted multiple piezos of different types. The piezos work when hooked up to a nano, but the attiny's won't work. To verify that the pins were working, I tested them with PWM on an LED, which worked fine. I'm pretty positive that your core and no others were at play because I uninstalled them from the boards manager and preferences before loading yours, then restarted arduino before compiling.

Then I switched to a different core that I linked to in an above post, which also did not work. That was the core that produced the compiler errors that, you are correct, indicate that your core was not being used.

after many frustrating hours of repeated failure, I've decided the only way this is going to work is to use the TinyTone() function that can be found here.

I'm beyond clueless why neither DrAzzy's core nor the Tiny Core linked to by the OP work in my setup, but they don't. They fail at every clock speed, every frequency, on every pin, every chip, and every buzzer.

I am too. I haven't had a chance to look into this, I haven't been feeling well the past week or so.

My limited tests with attiny85 using a $.95 development board and ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager.

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

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

Had to hook up buzzer to vcc and other lead to pin 4. If no tone, reverse leads.

Next sketch.

#define PIEZO 1

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

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

On this sketch it works for me with leads on gnd and pin1. Again, you may have to reverse leads to get tone.

SONNOFABITCH!!!!!! It NEVER occurred to me to reverse the leads! I've always used piezos with the the 2nd lead connected to GND, not VCC. Even with this same buzzer and same function on a nano, the leads don't need reversed. But for the attiny for some reason they do!?

I can't believe I've been stumped for like a week over backwards leads!!!!!

Thank you profusely for making this as easy as it should've been all along!!!!!!!! :blush:

adds a piezo buzzer to his ebay shopping cart

That's bizzaro - I've never worked with those things before (obviously - and sort of strangely, considering that I have literally hundreds of drawers of parts, plus a 2 cubic foot bin of parts in static bags because I ran out of drawers) - I know most are polarized. But if it works on the nano in either polarity, it should work on a tiny in either polarity.... Unless you just got lucky when you hooked it up to the nano?

Also kprims, thanks loads, this is one thing I don't have to do.

okay, so polarity was NOT the problem. I did some more testing and narrowed it down to the need for a minimum delay of 1 millisecond for the tone function to work.

This code produces a tone:

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

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

this code produces nothing except tears and anger:

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

void loop(){
    tone(4,2000);
    //delay(1);
}

I didn't think the polarity issue made sense considering that the buzzer worked fine on the nano regardless of polarity, but who was I to question success? Then I made some code changes and got nothing, so I knew it had to be something different. Still not sure why the delay(1) is needed on the attiny85 and not the nano, but it works, so I'm happy.

Here is the specific buzzer I'm using, FYI: CPT-9019S-SMT

This is getting beyond my pay grade. :slight_smile:

But just to tag along. Both types of piezo's I have will work with the original sketch. I do have a small 8 ohm 2w speaker that will not work with the original sketch, but will work with the delay.

Aaaaha!

Thanks for that. I think I know what the problem is, it's gotta be resetting the timer count when tone() gets called, so you get considerably different output, it's not going to be fun to fix, since it's #ifdef hell and timer1 on the tiny85 is weird. I mean, I can't imagine what else it could be, but that explanation totally makes sense.

I suspect your tone will sound subtly different with delay(1) vs delay(10) there!

I'll make an issue for it. tone() doesn't give the expected output if repeatedly called with the same argument in a tight loop · Issue #111 · SpenceKonde/ATTinyCore · GitHub

well glad I could be of assistance. :slight_smile:

I'm glad there's a rational explanation; I was starting to feel l like I was going a little crazy. :stuck_out_tongue:

Alright, back to this again...

Anyone have any insight into why analogWrite would stop working on PB4 when tone() function is used in the sketch?

analogWrite still works on PB0 and PB1, but if I use the tone() command anywhere, even if its ended by a call to noTone(), analogWrite on PB4 is still dead.

Which tone? You have used at least three variations.

silly_cone:
Alright, back to this again...

Anyone have any insight into why analogWrite would stop working on PB4 when tone() function is used in the sketch?

analogWrite still works on PB0 and PB1, but if I use the tone() command anywhere, even if its ended by a call to noTone(), analogWrite on PB4 is still dead.

Which core is it? Is it my core?

If it's my core, please make an issue here: Issues · SpenceKonde/ATTinyCore · GitHub - my guess from the problem description is that it's an x5 series, but please confirm that. I also love code that reproduces problems, even really simple ones.

That's right, its an ATTiny85.

This code results in proper output signal on PB4:

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

void loop() {

  //tone(2, 5000);
  delay(1);
  //noTone(2);
  delay(1);
  
  for (int i=150; i>85; i--){
    analogWrite(4, i);
  }
  delay(2000);
  digitalWrite(4, LOW);
  delay(2000);
}

This code results in nothing from PB4. but interestingly, it produces a ~25 ohm connection to ground that isn't present otherwise.

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

void loop() {

  tone(2, 5000);
  delay(1);
  noTone(2);
  delay(1);
  
  for (int i=150; i>85; i--){
    analogWrite(4, i);
  }
  delay(2000);
  digitalWrite(4, LOW);
  delay(2000);
}

And yes, I'm using your core.

Update: Fixed; I was able to get around the problem using the TimerFreeTone library.

This is a bug in my core. noTone() does not properly restore the state of TCCR1* registers.

Issue with calling tone() repeatedly with the same parameters in a tight loop fixed for ATTinyCore 1.2.0

check this webpage out = TinyTone() = Technoblogy - Simple Tones for ATtiny
It helped my finish my ATTiny85 project!