Go Down

Topic: toneAC v1.2 - Twice the volume, higher quality, higher frequency, etc. (Read 72524 times) previous topic - next topic

teckel

Use the tone function and it works perfectly fine! It's a great library, I use only one of the pins in ATmega328P and works well. I would like to release one of the pins for me to wear with something else in my code?
toneAC needs to use 2 pins or it's really worthless using ToneAC at all.  If you're using toneAC because it uses timer 1, it's better to use my Arduino New Tone library instead, as it only uses one pin.  But, maybe I don't understand the question.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

teckel

Hey, I was wondering, is there a way to make ToneAC work with a sensor and one LED?
Fx. you activate the sensor, the LED will turn on and a sound from ToneAC will play. I already did this with the normal Arduino settings and a speaker but to be honest am I not quiet so skilled at Arduino.
I want to use ToneAC because of the improved volume.
Thanks a lot :-)
Yes, you can do that with toneAC.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

teckel

Hey, not sure if i'm doing something wrong or what ...

i'm using a Micro (ATMega32U4) and have a buzzer connected on pins 9&10. Positive pin on the speaker has a 100 Ohm resistor on it ...

first of all volume (at 10) is equal if not even lower than when using default tone. If i switch pins the volume gets even lower.

As i said ... not sure what i'm doing wrong.

PS: i also have a switch on D12(pullup) and LCD 16x2 on D2-D8

any ideas.
It's probably the buzzer being used.  Some buzzers are a fixed frequency and you just supply voltage to them to buzz.  Those would not be compatible with toneAC (nor with any other sound library) as they don't need a library, they just buzz when voltage goes to them.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

sid1202

Hi there,

I'm using a Adafruit Feather32U4 with a ATMega32U4 and I get compiling errors when I try to use toneAC2 Pin 9 floats at around 2v so we can measure battery voltage, so I can't use toneAC. If I change the board to the Uno it compiles fine. Any suggestions ?

Sid

teckel

Hi there,

I'm using a Adafruit Feather32U4 with a ATMega32U4 and I get compiling errors when I try to use toneAC2 Pin 9 floats at around 2v so we can measure battery voltage, so I can't use toneAC. If I change the board to the Uno it compiles fine. Any suggestions ?

Sid
First off, it's impossible to use toneAC2 on a ATMega32U4 microcontroller as the ATMega32U4 doesn't have timer 2 and that's exactly what the "2" in toneAC2 stands for.  Basically, you're trying to use a feature that doesn't exist on the ATMega32U4 so it's impossible.

However, there's still hope for using toneAC on your platform.  It's very possible that toneAC doesn't use pins 9 and 10 on your platform.  9 & 10 is just what Arduino uses.  For example, on the Teensy 2.0 (which is  ATMega32U4 based) it has pins 14 & 15 mapped to the timer 1 pins.  I suggest you investigate this first as to which pins are mapped to what timers on your platform as they could be totally different.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

sid1202

Thank you. I think I miss understood the Timers on the Atmega processors. It's starting to make sense but it is still foggy.

Trying to learn everything at once is hard lol...

Regards,

Sidney

teckel

Thank you. I think I miss understood the Timers on the Atmega processors. It's starting to make sense but it is still foggy.

Trying to learn everything at once is hard lol...

Regards,

Sidney
I looked at the Feather pinouts and it appears that toneAC would use pins 9 & 10.  Not sure why there would be a problem using pin 9 on the Feather, it shouldn't have anything on it unless you have a sketch that's doing something else with the pin.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

q2dg

One stupid question...Are ToneAC, ToneAC2, New-Tone... libraries compatible with Arduino Zero/MKR1000? And with Genuino 101? I've searched in bitbucket's wiki but I haven't found anything. Thanks a lot.

teckel

One stupid question...Are ToneAC, ToneAC2, New-Tone... libraries compatible with Arduino Zero/MKR1000? And with Genuino 101? I've searched in bitbucket's wiki but I haven't found anything. Thanks a lot.
ToneAC, ToneAC2 and NewTone are absolutely NOT compatible as all libraries use ATmega AVR timers which Arduino hasn't implemented compatibility for their ARM based processors.  As there's no standard with Arduino ARM based processors, a custom library would need to be developed for each ARM platform (which is out of scope for the ToneAC project).

Further, ARM based platforms may times have better methods to generate sound (like sine waves).  So in reality, ToneAC/ToneAC2/NewTone is probably not the best thing to use with ARM anyway as there's probably a better solution.

The only "tone" library of mine that would work with an ARM processor is TimerFreeTone as it doesn't use timers and therefore can easily implement compatibility for non-AVR processors.  But again, TimerFreeTone probably isn't the best library to use for an ARM processor as the advantages of TimerFreeTone are really not important to an ARM based project.

Basically, EVERY Arduino library that uses times won't work with ANY non-AVR (for example ARM) based microcontroller.  There's no need to look for a note about this, consider no libraries to work on your platform as most libraries use timers.  This isn't specific to ToneAC or my libraries, it's how it works.  Also, most of my libraries were started before there was ARM based platforms, so concern with non-AVR compatibility was never part of the scope of the project.  Nor do I have these Arduino non-AVR platforms to care about making a custom version for each.

In a nutshell, don't count on ANY libraries working with non-AVR microcontrollers.  That's why I stay away from non-AVR microcontrollers (the Teensy 3.x is the exception because Paul Stoffregen gets it).

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

franck102

Hi,

Great library, unfortunately it uses pin 10 which is one of the SPI pins I need to communicate with an RFM69 module.

If I go inside the source code can I patch the code to use another PWM pin (seems like 1 & 9 would work for me for example)?

Thanks!

TryBit

I am getting the below errors when compiling a simple the simple code.



Here's the code

#include <toneAC.h>


void setup() {
 pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
 pinMode(5, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
 pinMode(0, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
 digitalWrite(LED_BUILTIN, LOW);
 digitalWrite(5, LOW);
 digitalWrite(0, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                   // but actually the LED is on; this is because
                                   // it is acive low on the ESP-01)
 delay(1000);                      // Wait for a second

 digitalWrite(LED_BUILTIN, HIGH);
 digitalWrite(5, HIGH);
 digitalWrite(0, HIGH);  // Turn the LED off by making the voltage HIGH
 delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}




franck102

Hi,

Great library, unfortunately it uses pin 10 which is one of the SPI pins I need to communicate with an RFM69 module.

If I go inside the source code can I patch the code to use another PWM pin (seems like 1 & 9 would work for me for example)?

Thanks!
I just found the toneAC2 which addresses this exact problem... sorry :)
Franck

teckel

Hi,

Great library, unfortunately it uses pin 10 which is one of the SPI pins I need to communicate with an RFM69 module.

If I go inside the source code can I patch the code to use another PWM pin (seems like 1 & 9 would work for me for example)?

Thanks!
No, toneAC uses certain pins because that's the pins the timer uses. If you need different pins, that's what toneAC2 is for.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

teckel

I am getting the below errors when compiling a simple the simple code.



Here's the code

#include <toneAC.h>


void setup() {
 pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
 pinMode(5, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
 pinMode(0, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
 digitalWrite(LED_BUILTIN, LOW);
 digitalWrite(5, LOW);
 digitalWrite(0, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                   // but actually the LED is on; this is because
                                   // it is acive low on the ESP-01)
 delay(1000);                      // Wait for a second

 digitalWrite(LED_BUILTIN, HIGH);
 digitalWrite(5, HIGH);
 digitalWrite(0, HIGH);  // Turn the LED off by making the voltage HIGH
 delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}




It would probably be beneficial if you included in your message that you're running a non-Arduino non-AVR platform "Adafruit HUZZAH ESP8266".  You can expect many compatibility issues trying to use Arduino libraries with non-AVR microcontrollers. 

Anyway, see the toneAC compatibility section for a list of what microcontrollers work with toneAC:

toneAC compatibility

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

mureytasroc

Also, making an amp for an alternating signal like with toneAC would be a challenge.  You can't just use two 2N2222 transistors as you also need to alternate the ground as well, which flips between the two.  Not that it's impossible, just much more complex than a typical simple transistor circuit.  Keep in mind that toneAC is designed to be like a voltage amplifier without needing an actual circuit. In essence, it doubles the voltage, which increases the volume.

Tim
If you are using toneAC for the sound quality and still want an amplifier for it a simple solution will be two NPN transistors - on both connect the emitter to ground, the collectors to the two poles of the speaker (choose a "positive rail" transistor and a "negative" one and connect each's base to its respective pole of the speaker), and the base through a current limiting resistor (see below for value) to D9 (for the transistor on the "negative" side of the speaker) and through a current limiting resistor (see below for value) to D10 (for the "positive" side transistor).  





The final touch is to add two pullup resistors on the collectors of both the transistors (the resistance values of these will limit the current sufficiently by the way so you don't need that 100ohm recommended resistor on the positive speaker pole).   To determine these pullup values take 1/2 the vcc (the "+" voltage) of the circuit (should be the same as the high voltage of the digital pins - 3.3 on some arduinos, 5v on most) (5*1/2=2.5 volts).   The current through each resistor can be determined with:

v=ir (ohm's law)
(v/2)=i*r
i=v/(2r)

r is resistance of the pullup (ohms), i is current through the pullup (amps), v is vcc (volts)

So the current through each resistor is v/(2r) r being the resistance of the pullup.   Multiply this by two because you have 2 resistors (one on each collector) supplying current to the speaker - now we get v/r for the current passing through the speaker (r being the resistance of each individual pullup - they should be the same (the math gets more complicated if they aren't)

We can then plug this watt's law to determine the power output of this amp:

p=iv (watt's law)
p=v*v/r

p being power (watts)

now if you want to pass 1.5 watts through a speaker (say if its rated for 2 watts) you plug 1.5 in (and lets use 5v as vcc - we'll pretend we are using an arduino uno)

p=v*v/r
1.5=5*5/r
r=25/1.5
r=16.67 ohms

So you should have each resistor be 16.67 ohms - remember the resistors should have an adequate power rating ( at least 1/2 the speaker wattage (>(v*v)/(2*r) each so >0.75 watts each if the speaker wattage is 1.5 watts - you should have some buffer room though)

**IF YOU COULDN'T FOLLOW THAT, USE THIS EQUATION: resistance=positive voltage*positive voltage/desired power through the speaker (for an uno: r=25/desired power)




Now for the value of the resistor between the digital pins on the bases of the transistors (they should both be the same as you might have guessed) referred to as base res in the schematic below.   We will first start with the desired "collector current" flowing through the transistor (the current that the transistor is giving to our speaker in plain english); we want:

("collector current" rating of the transistor (Ic max))>i>v/(2r)
i being the most "collector current" that the transistor will let through (Ic) and r being the PULLUP RESISTOR ON THE COLLECTOR (we want the current flowing through the transistors to be able to match that of the pullup resistors (v/(2r); calculated above) so it should be designed to handle more current than the resistors will draw - this current must also be below the specified collector current rating for the transistor - choose your transistor wisely)

when choosing a transistor consider: Ic max (see above) and Max power rating (above v*v/(2r), r  being the pullup resistor value)

So now look at your chosen transistor datasheet and find a chart or a graph that specifies "current gain" for a given collector current (current going through the transistor) - the collector current will be the "i" value you chose above (greater than v/(2r) and less than the Ic max rating) and find the "current gain" at that given value.   Now plug the current gain and  the i value (collector current; Ic) into this equation:

Base current= Ic/current gain

Now find the Emitter-base voltage (Vebo) specified in the datasheet and the required voltage drop across the resistor will be: vcc ("+" voltage of the system; 5v if using an uno) - Vebo

So take that voltage drop and plug it into ohms law to find the resistor value:
v=i*r
(vcc-Vebo)=(Ic/(current gain))*(r)
r=Ic/((vcc-Vebo)*(current gain))

Ic was chosen by you above (the "i" value), the current gain was found in the datasheet with Icm vcc was determined at the beginning of this tutorial, and Vebo was found in your transistor dtasheet as well.   Now you have an r value for teh resistors on the bases of your transistors.   Power rating of these resistors are equal to:

power = Ic*(vcc-Vebo)/(current gain) - this will usually be negligible though so a 1/4 watt resistor should be fine in general

**IF YOU COULDN'T FOLLOW THAT, A 1/4 watt resistor with a VALUE OF 2.2k ohms on each transistor base should be fine***




Enjoy unlimited volume - REMEMBER SPEAKER POWER RATING, RESISTOR POWER RATING (On bases and on pullups), AND TRANSISTOR POWER RATING AND CURRENT RATING to not break stuff

Here is a schematic of the circuit:

                                    v *NPN bjt transistors (EBC = emitter, base, collector)*

                                  <E>----GND
D9--<base res>--<B>
                                  <C>---|------<Resistance value chosen with above math>----VCC
                                             |
                                             ------ negative pole of the speaker (-)
                             GND                                                                   >>>> LOUD SOUND
                              ^--<E>   |------positive pole of the speaker (+)
D10--<base res>--<B>     |
                                  <C>---|------<Resistance value chosen with above math>----VCC
                                           

Please correct me if I have made any mistakes in my explanation but I have tested this circuit and it works.

Go Up