Go Down

Topic: Please Help a Newbie: ATTiny85 + Piezo Speaker (Read 9035 times) previous topic - next topic


Jun 06, 2012, 07:13 pm Last Edit: Jun 06, 2012, 09:18 pm by mikemalburg Reason: 1
Are there anything extra I need to do when trying to miniaturize an arduino project to play music through a piezo speaker from an ATTiny85?

The project I have created works on the Arduino just fine and plays the tune, but does not work through the ATTiny85 microcontroller when I load the code onto it.

What I have successfully done with the ATTiny85 is I have loaded the blink example sketch onto it, which works just fine through the ATTiny85.  I used this method to do this: http://hlt.media.mit.edu/?p=1695.

I have also loaded my "piezo tune" code onto the ATTiny85 with no errors, but it produces no music or even clicking sounds when it is loaded onto it.  The only difference between the code I loaded onto the Arduino to test the "piezo tune" code and the code I'm loading onto the ATTiny85 is the pin I'm using, which is set to digital pin 0 for the ATTiny85 (pin 5), which is the same pin the MIT high-low tech group has you use for the blink example sketch in their instructions.

I can post the exact code I'm using for this later if needed, but I am at work and do not have it with me right now.  It seems to be very similar to this, though: http://www.arduino.cc/en/Tutorial/PlayMelody.

I am a professional software developer, but I am just getting started on electronics projects like this as a hobby right now.  I have spent some time Googling around for an answer, looking through posts on this forum, and looking though posts on electronics.stackexchange.com, but to no avail.

I would really appreciate any advice you guys can give me to point me in the right direction on this.  Thank-you!


Perhaps post your exact sketch when you are able.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics



Thanks for the responses, guys.  OK, I tried to simplify everything down as much as possible to isolate the problem, and tried using the tone library to simplify it even further.

This sketch works on my Arduino Uno (with DPIN_PIEZO set to 3) just fine.  When I program it onto the ATTiny85, though, it does not produce any sound.  Can anyone tell me what I am doing wrong?

Thank-you in advance for any help you can send my way on this!

Code: [Select]

int DPIN_PIEZO = 0;

void setup() {

void loop() {
 tone(DPIN_PIEZO, 506);


What's your hardware setup? I tried that sketch on my Attiny85 and on the logic analyzer I got an output of around 509 Hz.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


I have an Arduino Uno and I'm running v1.0 of the IDE with a modified ArduinoISP sketch (just changed delay(40); in heartbeat() to delay(20);, as per http://arduino.cc/en/Tutorial/ArduinoISP).  All of my hardware for this project except for the capacitor is from the Sparkfun Inventor's Kit (http://www.sparkfun.com/products/10173).

Below is my hardware setup while programming the ATTiny85:

ATTiny 1 => Arduino 10
ATTiny 4 => GND
ATTiny 5 => Arduino 11
ATTiny 6 => Arduino 12
ATTiny 7 => Arduino 13
ATTiny 8 => 5V

10uF 25V Capacitor- => GND
10uF 25V Capacitor+ => RESET

--- Heartbeat LED on breadboard ---
LED+ => Arduino 9

The options I choose from the IDE to program it are:
  • Tools > Board > ATTiny85 (internal 8MHz clock)

  • Tools > Programmer > Arduino as ISP

I open the blink sketch, change pin to 0, and click "upload".  It gives me some output I expect to see:
Binary sketch size: 774 bytes (of a 8192 byte maximum)
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85

I hook the positive end of the other LED on my breadboard to pin 5 (5 as numbered around the horn, which should be digital pin 0) on the ATtiny, and the negative end to ground, and- it blinks!  I then remove the wires from this LED for my next step.

I then open my piezo sketch, click "upload", and I get the output I expect again:
Binary sketch size: 1750 bytes (of a 8192 byte maximum)
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85

I then hook the positive end of the piezo pin 5 on the ATtiny and negative to ground, and- nothing.

It's encouraging that you got my example sketch to work, but of course I was really hoping it was a software problem, which would be much easier to fix! 

I've double-checked all my connections and retried the blink and piezo sketches as listed above a couple times this morning, but to no avail.  I know that the piezo works, because I have also tried to load the sketch just onto the arduino and it works just fine.  I have one other ATtiny85 lying around that I could try, but the one I have hooked up the board works for the blink sketch, so I did not think that this would be the problem.

Any ideas?


Well I haven't tried anything new yet beyond what I posted the other day due to time constraints, but I'll be trying again tonight or tomorrow.

Do you see anything that sticks out to you with my setup, Nick (or anyone else for that matter)?

Jack Christensen

Are you both (Nick and Mike) using the same core? I don't see tone() listed as supported by the HLT core (http://hlt.media.mit.edu/?p=1695, bottom of page). It does look like Arduino-Tiny supports tone().


Hey Jack, thanks for the reply!  That could be it- I'll try a different core later tonight!


Well I tried that core out, and the good news is that it makes noise, now.  The bad news is that it is only "clicking" noise, not an actual audible "note".  I've tried several different values with my original code I posted, but they all seem to just be clicking.

Well, I'm still counting it as progress!  Any other thoughts of what I could try out?

I really appreciate your patience with me- this is all very new to me!

Jack Christensen

Jun 09, 2012, 05:21 am Last Edit: Jun 09, 2012, 05:24 am by Jack Christensen Reason: 1
Please post your exact code. The following works for me. An 8MHz clock gives a nicer tone than 1MHz.

Code: [Select]
#define PIEZO 4

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

void loop(void)
   tone(PIEZO, 2000);


OK, it seems to be working now!

OK, the first problem of "why was it not making any noise at all" was solved by using a core that actually supported tone(), as Jack suggested:

The next problem of "why is it clicking instead of producing a tone", was solved by taking the other jumper off of pin 5 on the ATtiny...

The ATtiny85 pin 5 (digital 0) was still hooked into pin 11 on the Arduino.  This doesn't seem to affect the blink sketch, so I didn't really think much of it.  I did try removing all wires except ground and power when I was using the HLT core, but I for whatever reason didn't try it for awhile with the arduino-tiny core tonight.  I finally yanked out the wire, and voila- a tone!  I was elated!

Anyway, I know it's a noobie mistake, but like I said- I'm still a noobie at this, so I figure I have an excuse for now  :).  My hope is that my public display of ignorance here will help some other poor soul out there on the Internet who may be having the same problem.

I really appreciate all of the help and patience from both of you guys.  Thanks again!

Jack Christensen

Hey, good job! Glad you got it working. Pretty sure this now makes you an Official Expert :D


Jun 11, 2012, 12:52 am Last Edit: Jun 11, 2012, 03:07 am by mikemalburg Reason: 1
Well thank-you very much again for your help, both of you.

Below are some videos of what my little project looks like.  I programmed 2 different ATtiny85's with different songs, and used a DIP socket so I could switch them out as needed.  Hope you like it!

Mini Mario Overworld Theme Song Player (w/ ATtiny85 & piezo speaker)

Captain Planet Annoy-a-tron (w/ ATtiny85 & piezo speaker)

Jack Christensen

Bahaha! That is very cool, and just a tiny bit evil ]:)  Be sure to let us know how the Annoy-a-tron works out!

Go Up