ATTiny85 tone() core?

Just to rule out the piezo itself, since you have it already connected on one of the ATTiny PWM pins, can you get a sound from it using analogWrite, as an example:

analogWrite(3, 100);

Geoff

If you've verified you're running sketches ok, and verified you can blink an LED on this pin, it's time to check whether the buzzer polarity might be a factor, whether the buzzer is damaged, and to try a real 8 ohm speaker (with an additional small resistor). Another reasonable step would be to try your buzzer with another Arduino.

good luck!

Just to rule out the piezo itself, since you have it already connected on one of the ATTiny PWM pins, can you get a sound from it using analogWrite

AnalogWrite does not produce a sound.

it's time to check whether the buzzer polarity might be a factor

It's a non polar buzzer, which works no problem with the arduino itself.

Despite these not being the problem, I found the solution: there was an older ATTiny core that I never removed. It was overriding the new core with the tone() function. Once I removed the older core, the new one took effect and now the project works fine.
Thanks to everyone for their help!

Good morning, glad to hear you found it. Just having coffee here, but I was about to suggest we start comparing sketch sizes, linker maps and such.

I'm having a similar issue - at least it seems so.

I can't get tone or any PWM to work on PB3 but PWM works fine on PB0 (ATTiny85).

I was using Arduino 0022 with v 0012 of the tiny cores but I just installed the newest versions of each (1.03 & v0015) and that's made no difference. I also wondered whether it was a fuse setting but I have used "burn bootloader", which I understand sets the fuses for the 'Tiny. I've also tried two different chips - just in case I had managed to fry something - and that doesn't seem to make a difference either.

Is there some initialisation of the ATTiny that you should do on first use, or something, that I might have missed?

Any ideas appreciated.

Ugi

PS I'm running the ATTiny at 1MHz off a 3v lithium CR2032 button cell, if that could concievably make a difference

Hi Ugi

I just ran the test sketch below using tone on D3 then D4 and both results were equivalent. tone() works on PB3 and PB4 based on my tests. I used an 8Ohm speaker and two different piezos for the tests (they sounded terrible in contrast - I'll stick to speakers for this kind of thing I think !)

const int testPin = 3;

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

void loop() {
  for(int val = 30; val < 3000; val++) {
  tone(testPin, val);
  delay(5);
  }
    for(int val = 3000; val >30; val--) {
  tone(testPin, val);
  delay(5);
  }
  noTone(testPin);
}

So while that worked, analogWrite() on PB3/D3 actually didn't which agrees with your findings. It was a hard flash rather than the pulse I'd intended so acted as a digitalWrite. I'm certain analogWrite() has worked for me in the past on PB3 but when I went back through past sketches I've not kept a sketch that used it. Not at all certain on that one now.

Anyway I can confirm tone() works with the above sketch, so if it doesn't for you I'd be looking elsewhere. I tested on an ATTiny85 running at 8MHz off a CR2032 3V cell - the only other component on my test rig is a 0.1uF decoupling cap.

Cheers ! Geoff

Thanks Geoff - I was also finding the "hard flash" effect - analogWrite(3,128) and analogWrite(3,0) are both equivalent to digitalWrite(3,LOW) but analogWrite(3,255) is equivalent to a digital HIGH.

I've got to work for a few hours now but will try out your test sketch this evening.

Cheers

Ugi

I just ran strykeroz sketch, and tone is working on all 5 pins
Also I checked PWM and ADC pins

Working PWM pins: PB4,PB0 and PB1

Working analogRead() on pins A1,A2 and A3

Thanks Guys - I tried swapping PB1 & PB3 (only one needs to be PWM) and it works fine on PB1. Odd that - I'm sure I checked the datasheet when I was planning this project.

Anyhow, tone works on PB4 (&PB3) - I think that may have been down to my old version before - and PWM works on PB1, which will do me fine.

I don't understand how tone works on PB3 if PWM doesn't, but maybe that's a cunning interrupt-based version.

Thanks for your help.

Ugi

Dr_Ugi:
I don't understand how tone works on PB3 if PWM doesn't, but maybe that's a cunning interrupt-based version.

That's right Ugi, tone() doesn't utilise hardware PWM so it can be used on your other pins.

Cheers, Geoff

strykeroz:
That's right Ugi, tone() doesn't utilise hardware PWM so it can be used on your other pins.

The version in Tiny Core sort of does. If the tone pin is an output pin for the tone timer, direct hardware output is used. Otherwise, an interrupt service routine is used. You get the best of both worlds.

[quote author=Coding Badly link=topic=144160.msg1085697#msg1085697 date=1358976502]
If the tone pin is an output pin for the tone timer, direct hardware output is used. Otherwise, an interrupt service routine is used. You get the best of both worlds.
[/quote]Thanks for the clarification, and that's a nice detail touch too.

That's the class way to do it!

Thanks all.

Ugi

Come to think of it, which pins are the hardware outputs for for the tone timer? While I'm switching pins around, I may as well use hardware PWM for this if I can, 'cos there's plenty to do under SW already & I don't really want to go up to 8MHz if I can avoid it.

Thanks

Ugi

By default, for the ATtiny85 processor, timer 1 is used for millis. Timer 0 is used for everything else (tone). That can be reversed with a compile-time switch.

Open the datasheet...

Navigate to the 1. Pin Configurations section. Pins marked with OC (oh-see) are Output Compare (PWM) pins. The number after OC is the timer. The letter after is the channel.

Find all pins marked with OC0 (oh-see-zero): PB1 and PB0. PB1 and PB0 are potentially "tone output pins".

The problem with PB1 (OC0B) is that it overlaps with an output from the other timer (OC1A). Which output is used is determined by another compile-time switch. By default, "phase correct PWM" is favored so OC0B is used.

So, by default, PB1 and PB0 are the "tone output pins".

Thank you - that's extremely helpful.

Dr_Ugi:
Thank you - that's extremely helpful.

It sure is. Thankyou.

Can you just clear up for me the role of PB3 with PWM? It's marked as OC1B with a line over it, in the same way that PB0 is OC1A with a line above it. Is it that the hardware is capable but we can't drive it with hardware PWM using the Arduino core resulting in what we saw in earlier tests when we tried to analogWrite to it?

Thanks, Geoff

strykeroz:
Can you just clear up for me the role of PB3 with PWM? It's marked as OC1B with a line over it, in the same way that PB0 is OC1A with a line above it.

The line over means "inverted". I have no idea when an inverted output would be used or how exactly it works.

From a practical perspective, it means "not Arduino compatbile".

Is it that the hardware is capable but we can't drive it with hardware PWM using the Arduino core resulting in what we saw in earlier tests when we tried to analogWrite to it?

I'm not certain about "earlier tests" but the Tiny Core does not make any use of inverted output pins.

That clears it up perfectly - thanks !

Geoff