Arduino Forum

Using Arduino => Microcontrollers => Topic started by: nickelbake95 on Jan 22, 2013, 03:02 am

Title: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:02 am
This
http://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0022-0008.zip&can=2&q= (http://code.google.com/p/arduino-tiny/downloads/detail?name=arduino-tiny-0022-0008.zip&can=2&q=)
doesn't seem to work. Nor does this: http://code.google.com/p/arduino-tiny/
Are they just outdated, and if so can I just revert back to an older version of arduino IDE?
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 03:06 am
I'm using the latter with v1.0.3, what is the specific problem? ... "doesn't seem to work" is a bit broad.
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:09 am
The entire ATTiny works fine. I can blink an LED. But when I use tone() with a speaker, nothing happens. I'll try it with 1.0.3 now and see if that makes a difference. (I've been using 1.0.1 up until now)
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:20 am
So I re-installed arduino as 1.0.3. Also re-installed the core you used. Built the circuit correctly. Made sure that a normal LED sketch works. (It does.) The tone() still won't work.
Title: Re: ATTiny85 tone() core?
Post by: Coding Badly on Jan 22, 2013, 03:21 am
The tone() still won't work.


Wanna bet?
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 03:22 am
Post your code, I'll have a look and/or give it a try.
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:33 am
I doubt it's actually the code, seeing as how it's just a simple test:
Code: [Select]
void setup() {
}
void loop() {
  tone(3, 2000, 500);
  delay(500);
  tone(3, 3500, 500);
  delay(500);
}

I'm thinking something is different about the core, but I followed the readme.txt exactly... :/
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 03:37 am
Works fine here.
Title: Re: ATTiny85 tone() core?
Post by: Coding Badly on Jan 22, 2013, 03:40 am

Pin 3 is on the left side of the processor one down from the top.  What's connected to it?
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:40 am
The speaker (which I know works) connected to ground.
Title: Re: ATTiny85 tone() core?
Post by: Coding Badly on Jan 22, 2013, 03:41 am
Quote
...with a speaker...


An 8 ohm speaker?
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:46 am
No, it's a buzzer. I think it's called a piezzo buzzer. And I know it works with the tone() function on the arduino.
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 03:48 am
Hmm. And an LED will blink on the same pin?
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:48 am
Yes.
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 03:52 am
Load the blink sketch, blink an LED on pin 3, say 100ms on and 100ms off. Once that is working, add the piezo to the circuit, from pin 3 to ground (through a small resistor, 100 ohms or so is best if you have one.)

What do you hear from the piezo?
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 03:53 am
A quiet click every 200ms.
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 04:04 am
The core I'm downloading is called "arduino-tiny-0100-0015" if that makes a difference. (Found it from the second link at the beginning of this post)
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 04:05 am
Well I thought it should be a click every 100ms but no matter ;)

I think we've ruled out all the hardware. You've re-installed the Arduino IDE v1.0.3 and the current Arduino-tiny core. I'm out of ideas at the moment...
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 04:07 am

The core I'm downloading is called "arduino-tiny-0100-0015" if that makes a difference. (Found it from the second link at the beginning of this post)


Yep, actually, I had arduino-tiny-0100-0014, so I installed 0100-0015, but no difference.
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 04:09 am
Well, I'm going to go to bed. Maybe tomorrow I'll think of another variable we can test. If so, I'll post here about it. In the mean time, thanks for all the help.
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 04:12 am
Sounds like a plan. I'll track this thread, will be interested what you might find and if any light bulbs (or LEDs) go on here I'll let you know too.
Title: Re: ATTiny85 tone() core?
Post by: strykeroz on Jan 22, 2013, 04:30 am
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:
Code: [Select]
analogWrite(3, 100);
Geoff
Title: Re: ATTiny85 tone() core?
Post by: creatrope on Jan 22, 2013, 11:17 am
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!
Title: Re: ATTiny85 tone() core?
Post by: nickelbake95 on Jan 22, 2013, 12:29 pm
Quote
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.
Quote
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!
Title: Re: ATTiny85 tone() core?
Post by: JChristensen on Jan 22, 2013, 01:27 pm
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.
Title: Re: ATTiny85 tone() core?
Post by: Dr_Ugi on Jan 23, 2013, 01:16 pm
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
Title: Re: ATTiny85 tone() core?
Post by: strykeroz on Jan 23, 2013, 02:27 pm
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 !)
Code: [Select]
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
Title: Re: ATTiny85 tone() core?
Post by: Dr_Ugi on Jan 23, 2013, 02:42 pm
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
Title: Re: ATTiny85 tone() core?
Post by: Erni on Jan 23, 2013, 04:01 pm
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
Title: Re: ATTiny85 tone() core?
Post by: Dr_Ugi on Jan 23, 2013, 05:27 pm
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
Title: Re: ATTiny85 tone() core?
Post by: strykeroz on Jan 23, 2013, 10:19 pm

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
Title: Re: ATTiny85 tone() core?
Post by: Coding Badly on Jan 23, 2013, 10:28 pm
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.
Title: Re: ATTiny85 tone() core?
Post by: strykeroz on Jan 23, 2013, 10:34 pm

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.
Thanks for the clarification, and that's a nice detail touch too.
Title: Re: ATTiny85 tone() core?
Post by: Dr_Ugi on Jan 24, 2013, 12:40 am

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.


That's the class way to do it!

Thanks all.

Ugi
Title: Re: ATTiny85 tone() core?
Post by: Dr_Ugi on Jan 24, 2013, 01:42 pm

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.

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
Title: Re: ATTiny85 tone() core?
Post by: Coding Badly on Jan 24, 2013, 07:59 pm

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...
http://www.atmel.com/devices/attiny85.aspx?tab=documents

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".
Title: Re: ATTiny85 tone() core?
Post by: Dr_Ugi on Jan 25, 2013, 02:48 pm
Thank you - that's extremely helpful.
Title: Re: ATTiny85 tone() core?
Post by: strykeroz on Jan 25, 2013, 03:32 pm

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
Title: Re: ATTiny85 tone() core?
Post by: Coding Badly on Jan 25, 2013, 09:46 pm
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".

Quote
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.
Title: Re: ATTiny85 tone() core?
Post by: strykeroz on Jan 25, 2013, 11:11 pm
That clears it up perfectly - thanks !

Geoff