Go Down

Topic: Arduino Due and tone() (Read 23656 times) previous topic - next topic


Hello Guys,

I have to get a tone out of my headphones. Just like a metronome. In the beginning I thought that I can use tone(), turns out like you all know, this function is not available for arduino due.

So I wanted to try the code above from ninja2, but it won't compile because something does not work with #include <Streaming.h>. Could someone give me a shore explanation for that? Or do you have a better idea to create the metronome with an arduino due and headphones?



Feb 03, 2018, 06:20 am Last Edit: Feb 03, 2018, 06:27 am by ninja2
yes my code is suitable for your project

to get it to compile you just need to either:

1) download the streaming library (from http://arduiniana.org/libraries/streaming/) and put it in the .../Sketches/libraries folder


2) update all the lines containing << with conventional Serial.print commands


Feb 03, 2018, 06:38 am Last Edit: Feb 03, 2018, 06:40 am by ninja2
I just had a quick look at my example at post #12 and the commands using streaming.h are only in 2 places in setup(), so for option 2 above is easy: just comment the lib out and edit setup() to be like this:

Code: [Select]
//#include <Streaming.h>

void setup() {
  pinMode(TONEpin, OUTPUT);
  Serial.println("\n==== Simple Due tone example ====");
  //Serial << F("\n==== Simple Due tone example ====\n");
  beep();  delay(1000);                             // 1 beep
  beep2(); delay(1000);                             // 2
  beep3(); delay(1000);                             // 3
  beep4();                                          // 4
  //Serial << F("done...\n");


just out of interest, why don't the Arduino folks provide a tone lib for the Due, too?


Ok I will try that on Monday at work! Thank you for that.

Do you know whether I have to be careful about frying my earphones? Or will this code definitely work for headphones?


Feb 03, 2018, 10:20 pm Last Edit: Feb 03, 2018, 10:25 pm by ninja2
The code is OK but there is a limit on how much current the Arduino can deliver per pin (40mA) and total board (200mA). So you won't be able to drive headphones directly as their impedance may be too low.


V = IR
R = V/I = 5V/40mA = 125ohms
So trying to drive any impedance less than 125 ohms would damage an arduino pin.

Ideally you would feed the arduino tone to a "line in" input on an amplifier, with the headphones connected to the amp.

As a minimum you need to add a resistor into a power transistor between the TONEpin and the headphones.


I think at the arduino due it is even 3.3 V, so 3.3/40 = 82.5ohms. So to not destroy the pins, I have to put a resistor between the arduino pin and the headphones that is let's say about 100ohms?


Feb 04, 2018, 01:02 am Last Edit: Feb 04, 2018, 01:05 am by ninja2
Oops yes 3.3V for Due. Looking at the Due Tech specs the max total current (all pins) is 130mA. It doesn't mention single pin limit, so I'd stick with 40mA/pin.

Interfacing to the headphones is not as easy as simply adding a 100ohm resistor because you also need to get enough power (I*I*R) into the headphones. If the headphones are say 10 ohms then less than 10% of the power will get to the headphones, most being lost in the resistor. You can try it, but you'll probably hear nothng.

So you need a (power) amplifier ..  and that can be a simple transistor fed with through the 100ohm resistor to the transistor base, the emitter to earth and headphones driven off a power supply and the transistor's collector.

The solution also depends on the specs of your headphones.

There may be easier or better solutions and I may be leading you up the garden path. So I suggest you Google "how to drive headphones" ...

good luck!


there ars some really cheap mini-amplifiers on eBay based on the LM386 chip that may be useful ...


Apparently, I tried to take a random resistor (7.5k ohms), and it works. No idea why. Anyways, I ordered the amplifier so that I can easily handle the volume of the headphones...

Thank you very much!


Feb 06, 2018, 02:37 am Last Edit: Feb 06, 2018, 02:44 am by ninja2
nice [random] work ! :)

keep in mind you always have the option to award 'Karma' points in this forum... (but not randomly)

Go Up