I have figured out how to make a piezo buzzer play two tones at once with analogWrite() and tone() but how would I make an actual song by playing both bass and treble, such as this one. Here is the circuit I had to do it:


and here is the code I had to do it:

int buzzer1 = 9;
int buzzer2 = 10;

void setup() {
  pinMode(buzzer1, OUTPUT);
  pinMode(buzzer2, OUTPUT);

void loop() {
  analogWrite(buzzer1, 100);
  tone(buzzer2, 262);
  • 3
    That circuit looks dangerous to me: you have two outputs shorted together, and each will be driving the other. It will damage the outputs, if it hasn't already. You need some sort of voltage adder I think.
    – Mark Smith
    Commented Jan 3, 2017 at 8:08

2 Answers 2


First of all - You can drain max 20mA via pin, when one of the pins is HIGH and the other LOW, there is 5V between them, so there should be at least 250 Ohm resistor placed there, if you do not want damage your Arduino. I sugest add, to each of the red wires, at least a 200 Ohm resistor (to be safe).

For playing melody you have to save it somewhere, probabely easiest way is in arrays, and then play it note by note at regular intervals.

something like this (but insert your notes and set the length correctly):

int num_tones=6;
int tones1[] = {100,200,400,200,100,50};
int tones2[] = {262,131,131,262,131,524};

void loop() {
  for (int i=0;i<num_tones;i++){
    analogWrite(buzzer1, tones1[i]);
  • @Amorris: thanks for edit, I am not native speaker (not good speaker too)
    – gilhad
    Commented Jan 6, 2017 at 0:17

I completed the following project https://github.com/linhartr22/count

Plays PWM files on Arduino through piezo speaker.

Most of the results came from the following page:


I'm using the https://github.com/damellis/PCM/zipball/master library mentioned there. count.ino is my sample sketch. As the web site mentions the audio comes out on Digital I/O pin 11. I've tested with both a piezo buzzer and a 4 ohm speaker. I'm really impressed. It does mess with some of the PWM timers so this may not be suitable for all applications.

I used Audacity to convert the sample wave files I was provided into PWM. The .h files are included in this repo.

The number of notes you can hold on the Arduino will depend on the size of the wave samples. If you need more notes, it might be possible to extend this with an SD card reader or other form of memory. It wasn't required for my project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.