Arduino Audio Output
Arduino Audio Output
adv ertisement
adv ertisement
531 favorites
amandaghassaei
uh-man-duh-guss-eye-dot-com
Follow 3526
M ore by amandaghassaei:
Generate sound or output analog voltages with an Arduino. This Instructable will
show you how to set up a really basic digital to analog converter so you can start
adv ertisement
generating analog waves of all shapes and sizes from a few digital pins on an
Arduino. (This article is a companion to another Instructable I've written about
sending audio into an Arduino, find that here)
Feel free to use any of the info here to put together an amazing project for
the DIY Audio Contest! We're giving away an HDTV, some DSLR cameras,
and tons of other great stuff! The contest closes Nov 26.
PartsList:
Additional Materials:
This is called addressing the port directly. On the Arduino, digital pins 0-7 are all
on port d of the Atmel328 chip. The PORTD command lets us tells pins 0-7 to go
HIGH or LOW in one line (instead of having to use digitalWrite() eight times). Not
only is this easier to code, it's much faster for the Arduino to process and it causes
the pins to all change simultaneously instead of one by one (you can only talk to
one pin at a time with digitalWrite()). Since port d has eight pins on it (digital pins
0-7) we can send it one of 2^8 = 256 possible values (0-255) to control the pins.
For example, if we wrote the following line:
PORTD = 0;
it would set pins 0-7 LOW. With the DAC set up on pins 0-7 this will output 0V. if
we sent the following:
PDF generated automatically by the HTML to PDF API of PDFmyURL
PORTD = 255;
it would set pins 0-7 HIGH. This will cause the DAC to output 5V. We can also
send combinations of LOW and HIGH states to output a voltage between 0 and 5V
from the DAC. For example:
PORTD = 125;
125 = 01111101 in binary. This sets pin 7 low (the msb is 0), pins 6-2 high (the
next five bits are 1), pin 1 low (the next bit is 0), and pin 0 high (the lsb is 1). You
can read more about how this works here. To calculate the voltage that this will
output from the DAC, we use the following equation:
The code below sends out several voltages between 0 and 5V and holds each for a
short time to demonstrate the concepts I've described above. In the main loop()
function I've written:
//Analog out
//by Amanda Ghassaei
//https://www.instructables.com/id/Arduino-Audio-Output/
//Sept 2012
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
void setup(){
//set digital pins 0-7 as outputs
for (int i=0;i<8;i++){
pinMode(i,OUTPUT);
}
}
The code below outputs a ramp from 0 to 5V. In the loop() function, the variable
"a"void
is incremented
loop(){ from 0 to 255. Each time it is incremented, the value of "a" is
sentPORTD = 0;//send
to PORTD. This(0/255)*5
value is= held
0V out
forDAC
50us before a new value of "a" is sent. Once
delay(1);//wait 1ms
"a" reaches 255, it gets reset back to 0. The time for each cycle of this ramp (also
PORTD = 127;//send (127/255)*5 = 2.5V out DAC
called the period) takes:
delay(2);//wait 2ms
PORTD = 51;//send (51/255)*5 = 1V out DAC
period = (duration
delay(1);//wait 1ms of each step) * (number of steps)
PORTD
period = 255;//send
= 50us * 256 =(255/255)*5
12800us == 5V out DAC
0.0128s
delay(3);//wait 3ms
}
so the frequency is:
PDF generated automatically by the HTML to PDF API of PDFmyURL
frequency of ramp = 1/0.0128s = 78Hz
//Ramp out
//by Amanda Ghassaei
//https://www.instructables.com/id/Arduino-Audio-Output/
//Sept 2012
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
void setup(){
//set digital pins 0-7 as outputs
for (int i=0;i<8;i++){
pinMode(i,OUTPUT);
}
The
} code below outputs a sine wave centered around 2.5V, oscillating up to a max
of 5V and a min of 0V. In the loop() function, the variable "t" is incremented from 0
to void
100.loop(){
Each time it is incremented, the expression:
for (int a=0;a<256;a++){
127+127*sin(2*3.14*t/100)
PORTD = a;//send out ramp to digital pins 0-7
is sent to PORTD. This value is held for 50us before "t" is incremented again and a
delayMicroseconds(50);//wait 50us
new} value is sent out to PORTD. Once "t" reaches 100, it gets reset back to 0.
The} period of this sine wave should be:
//Sine out
//by Amanda Ghassaei
//https://www.instructables.com/id/Arduino-Audio-Output/
//Sept 2012
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
void setup(){
//set digital pins 0-7 as outputs
for (int i=0;i<8;i++){
pinMode(i,OUTPUT);
}
}
But this is not the case, the output from the DAC is shown in fig 6. As indicated in
thevoid
image notes, it does not have a frequency of 200hz, its frequency is more like
loop(){
45hz. This is because the line: "t"
for (int t=0;t<100;t++){//increment
PORTD = 127+127*sin(2*3.14*t/100);//send sine wave to DAC, centered around (127/255
)*5 = 2.5V
PORTD = 127+127*sin(2*3.14*t/100);
delayMicroseconds(50);//wait 50us
takes} a very long time to calculate. In general multiplication/division with decimal
numbers
} and the sin() function take the Arduino a lot of time to perform.
One solution is to calculate the values of sine ahead of time and store them in the
Arduino's memory. Then when the Arduino sketch is running all the Arduino will
have to do is recall these values from memory (a very easy and quick task for the
Arduino). I ran a simple Python script (below) to generate 100 values of
PDF generated automatically by the HTML to PDF API of PDFmyURL
127+127*sin(2*3.14*t/100):
import math
for x in range(0, 100):
print str(int(127+127*math.sin(2*math.pi*x*0.01)),)+str(","),
I stored these values in an array called "sine" in the Arduino sketch below. Then in
my loop, for each value of "t" I sent an element of sine[] to PORTD:
PORTD = sine[t];
The output from this DAC for this sketch is shown in fig 7. You can see that it
outputs a sine wave of 200hz, as expected.
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
byte sine[] = {127, 134, 142, 150, 158, 166, 173, 181, 188, 195, 201, 207, 213, 219, 224, 22
9, 234, 238, 241, 245, 247, 250, 251, 252, 253, 254, 253, 252, 251, 250, 247, 245, 241, 238
, 234, 229, 224, 219, 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119,
111, 103, 95, 87, 80, 72, 65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6, 3, 2, 1, 0, 0, 0, 1, 2,
3, 6, 8, 12, 15, 19, 24, 29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119,};
Stepvoid3:setup(){
DAC Buffer
//set digital pins 0-7 as outputs
PDF generated automatically by the HTML to PDF API of PDFmyURL
PDF generated automatically by the HTML to PDF API of PDFmyURL
Now that we have a good signal coming out Arduino, we need to protect it. The
R2R DAC is very sensitive to any loads put on it, so trying to drive speakers
directly from the DAC will distort the signal heavily. Before doing anything with the
signal you need to set up some kind of buffer circuit. I set up one of the op amps
in the TS922 dual op amp package as a voltage follower to buffer my DAC from the
rest of my circuit (see schematic in fig 6, be sure to power the op amp with 5V and
ground).
Once this was set up I wired an LED and 220ohm resistor in series between the
output of the op amp and ground. The sketch below outputs a slow ramp out the
DAC so you can actually see the LED get brighter as the ramp increases in
voltage. The period of the ramp is:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
void setup(){
//set digital pins 0-7 as outputs
for (int i=0;i<8;i++){
pinMode(i,OUTPUT);
}
}
Step 4: Low Pass Filter
void loop(){
for (int a=0;a<256;a++){
PORTD = a;//send out ramp to digital pins 0-7
delay(5);//wait 5ms
}
}
You can calculate the values of the capacitor and resistor you need for a low pass
filter according to the following equation:
Nyquist's Theroum states that for a signal with a sampling rate of x Hz, the highest
frequency that can be produced is x/2 Hz. You should set your cutoff frequency to
x/2Hz (or maybe slightly lower depending on what you like). So if you have a
since 8nF capacitors are hard to come by I rounded up to 0.01uF. This gives a
cutoff frequency of about 16kHz. You can mess around with different values and
see what you like best, I tend to like heavier filtering because it removes more
unwanted noise.
Step 6: Amplifier
Step 7: DC Offset
Step 8: Output
To set up the interrupt you need to copy the following lines into your setup()
PDF generated automatically by the HTML to PDF API of PDFmyURL
function:
cli();//disable interrupts
//set timer0 interrupt at 40kHz
TCCR0A = 0;// set entire TCCR0A register to 0
TCCR0B = 0;// same for TCCR0B
TCNT0 = 0;//initialize counter value to 0
// set compare match register for 40khz increments
OCR0A = 49;// = (16*10^6) / (40000*8) - 1 (must be <256)
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS11 bit for 8 prescaler
TCCR0B |= (1 << CS11);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
sei();//enable interrupts
the contents of the interrupt routine are encapsulated in the following function:
You want to keep the interrupt routine as short as possible, only the necessities.
You can do all of your other tasks (checking on buttons, turning on leds, etc) in the
loop(). Also keep in mind that setting up interrupts may affect other Arduino
functions such as analogWrite and delay.
In the code below, I use the interrupt function to send a new value of sine[] to
PORTD at a rate of 40kHz and increment the variable "t." Figs 1 and 2 show the
(unfiltered) output of the code on an oscilloscope. We can calculate the expected
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
byte sine[] = {127, 134, 142, 150, 158, 166, 173, 181, 188, 195, 201, 207, 213, 219, 224, 22
9, 234, 238, 241, 245, 247, 250, 251, 252, 253, 254, 253, 252, 251, 250, 247, 245, 241, 238
, 234, 229, 224, 219, 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119,
111, 103, 95, 87, 80, 72, 65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6, 3, 2, 1, 0, 0, 0, 1, 2,
3, 6, 8, 12, 15, 19, 24, 29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119,};
int t = 0;//time
Step 10: Extra Tips
void setup(){
//set digital pins 0-7 as outputs
for (int i=0;i<8;i++){
pinMode(i,OUTPUT);
}
PDF generated automatically by the HTML to PDF API of PDFmyURL
This DAC uses quite a bit of the Arduino's available digital pins, including some that
are normally used for serial communications and PWM, so here are a few tips that
will help you deal with pin conflicts.
If you need to use the PWM pins, or otherwise need to use different pins as
the DAC: If you must use the PWM pins you can use bit manipulation to free up
pins 3, 5, and 6 and replace them with pins 8, 12, and 13. Say you want to send
the number 36 to PORTD. You can use the following lines:
//define variables:
boolean bit3state;
boolean bit5state;
boolean bit6state;
be sure to keep these PORTD and PORTB lines right next to each other in your
code, you want the pins on port d and port b to switch at as close to the same time
as possible.
Here is the code from the previous step, edited so that it does not use any PWM
pins. As you see in fig 1, the unfiltered output from the DAC has many
discontinuities caused by the lag between sending data to port d and port b, as well
as splitting up the commands for setting pins high and low. You can get rid of most
of these discontinuities with the low pass filter (fig 2). If you wanted to use this
technique you might consider increasing the cutoff frequency of your low pass
filter. If you wanted to make this really good, you could send your 5 most
significant bits to port d and your 3 least significant bits to port b. This would
decrease the amplitude of some of the discontinuities, reducing the magnitude of
the noise. I'll let you figure that one out on your own.
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
byte sine[] = {127, 134, 142, 150, 158, 166, 173, 181, 188, 195, 201, 207, 213, 219, 224, 22
9, 234, 238, 241, 245, 247, 250, 251, 252, 253, 254, 253, 252, 251, 250, 247, 245, 241, 238
, 234, 229, 224, 219, 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119,
111, 103, 95, 87, 80, 72, 65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6, 3, 2, 1, 0, 0, 0, 1, 2,
3, 6, 8, 12, 15, 19, 24, 29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119,};
Ifyou
int t =run out of digital pins and need more: Remember you can always use
0;//time
your analog pins as Digital I/O. Try out the following functions, they work just like
you are dealing
boolean with a regular digital pin.
bit3state;
boolean bit5state;
boolean bit6state;
digitalWrite(A0,HIGH);//set pin A0 high
digitalWrite(A0,LOW);//set
void setup(){ pin A0 low
digitalRead(A0);//read digital
//set digital pins 0-13 as data from pin A0
outputs
for (int i=0;i<14;i++){
pinMode(i,OUTPUT);
Otherwise, try using a multiplexer. If you need more digital outputs, the 74HC595
}
allows you to turn three of the Arduino's digital pins into 8 outputs. You can even
daisy chain multiple
cli();//disable 595's together to create many more outputs pins. You could
interrupts
set //set
up yourtimer0whole DAC
interrupt on one of these chips if you wanted (though it would take a
at 40kHz
fewTCCR0A
lines of =code to entire
0;// set address it andregister
TCCR0A might to
slow
0 you down too much for higher
TCCR0B
sampling = 0;// same
rates). for TCCR0B
The Arduino website is a good place to start learning about how to
TCNT0 = 0;//initialize counter value to 0
use the 595.
// set compare match register for 40khz increments
PDF generated automatically by the HTML to PDF API of PDFmyURL
If you need more digital inputs, the 74HC165 or CD4021B let you turn three of the
Arduino's digital pins into 8 inputs. Again, the Arduino website is a good place to
start learning how to use these chips.
If you want to use the info in this Instructable with the Mega or other
boards: In this Instructable I talked exclusively about the Arduino Uno with
Atmel328. The same code will run fine on any board with an Atmel328 or Atmel168
chip on it. You can also use the same ideas with a Mega. You should try to attach
your DAC to any port that has 8 available pins, that way you can address your
DAC with one line of code ("PORTD =" ) On the Uno, the only port that has 8
available pins is port d. This picture indicates that the Mega has several ports with
8 pins: ports a, b, c, and l are the obvious choices. If you don't care about wasting
analog pins you could also use ports f or k.
adv ertisement
Comments
Would you please hint to how this project or your waveform generator project (that I
got from Jameco) can output a suitable Dc/Ac voltage(considering the pot max
amplitude ) to be hooked up to smartphone Microphone jack , without damaging
the internal circuit of the smartphone.
Do I need to do a change to the software ,hardware or the power source?
Thanks.
I am doing a blind stick with ultrasonic sensor and voice guide where it tells if you
should go left or right. Is it possible if i use this audio output?
Hello! Nice tutorial. I want to make a low-pass filter like you for Arduino toneAC().
With toneAC, we're sending out of phase signals on two pins. How will I connect
the resistor and capacitor? I need two resistors and two capacitors connected
between every pin and ground? Or is sufficient one resistor and one capacitor
between one pin and ground?
I did a test on the timings of direct port write. I used the following pins: PB1,PB0,
PD7,PD6,PD5,PD4,PD3,PD2 ()leaving PD1 and PD0 for rx/tx).
PORTD = (PORTD & B00000011)|((input<<2)&B11111100);
PORTB = (PORTB & B11111100)|((input>>6)&B00000011);
These two lines set the input on the aforementioned pins with direct bit-banging
write method described. The result is astonishing- It only takes ~1.6 usec to
execute these two lines. So for interrupt service routine you get ample time to do
other processing.
Here is the code:
#include "Arduino.h"
//The setup function is called once at startup of the sketch
uint8_t input=100;
String inputString="";
void setup()
{
// Add your initialization code here
Serial.begin(115200);
}
void testSerialEvent(){
while(Serial.available()){
char c=(char)Serial.read();
inputString += c;
if (c == '\n') {
input=inputString.toInt();
inputString="";
Serial.print("Input->");Serial.println(input);
break;
PDF generated automatically by the HTML to PDF API of PDFmyURL
}
}
}
Hello, I think this tutorial is wonderful, thank you so much for posting it, however I've
run into a problem now. When I hook the Arduino up to an oscilloscope, it shows a
perfect sine wave. However, when I plug my Arduino into my audio interface, so I
can record the output on my computer, the signal becomes truncated and only has
a peak to peak voltage of 80 mV. Could you please help me understand why this
is happening, and what I can do to fix it?
Thank you.
Hi Amanda,
thankyou very much, all this is great.
I would use this project to generate a wave and split the signal from one output to
multiple guitar amps through something like a plugboard(I believe it will be
connected in paralel), do i need to change something in the scheme to send a
good signal to all the speakers?
Thank you :)
Hi Amanda,
thx for the brilliant project. I sent the 6 most significant bits to PORTD and 2 to
PDF generated automatically by the HTML to PDF API of PDFmyURL
PORTB. I used the following commands to decrease the discontinuities as much
as possible and also speed up the output by using just one instruction per port:
PORTD = x[i] & B11111100 | PORTD & (x[i] | B00000011);
PORTB = x[i] & B00000011 | PORTB & (x[i] | B11111100);
Now I go on to the analog part.
Thx, rgds,
Gabor
Hey..
First off, thank you for the great tutorials.
I'm trying to incorporate your instructable into my "Arduino short range walkie
talkie" project.
I'm using a mems mic (https://www.sparkfun.com/products/9868) to record my
voice. Can you help me out with how to reduce lag during tranmission?
I've tried decreasing delay during input but that rally doesnt seem to do the job.
HI all,
at first i would like to THNX for cool tutorial.
I tried to use as output converter MCP4921 (with AH_MCP4921.h) but i am not
able to get any "audible" sounds. Have anybody tried this D/A converter with this
solution ?
Best regards
Jan
Yeah that chip looks good, but (as you mentioned) the code will have to
change a bit. If your speakers have their own amp, you can probably get
away with connecting the output of the buffer to the input of your speaker's
amp. It's possible you may need a little preamp in there, in that case you can
use the amp in my schematic w a resistor in place of the potentiometer.
It also seems I'm going to need to run off 3V instead of 5V. I know I'll need a
different DAC, but other than that: I assume I'll need a different DC offset
capacitor? Are there any other components I'll need to change?
Could the bit manipulation be used to free up Digital pins 0 and 1 for serial
communication? I have only one serial communication used so I can't use software
serial for that. I have this code so far:
// bit manipulation, sending number 36 to 0 and 1. PORTB: digitalPin 8-13
//define variables:
boolean bit0state;
boolean bit1state;
//in your main loop():
bit0state = (36 & B00000001)>>0;//get the zero bit of 36
bit1state = (36 & B00000010)>>1;//get the first bit of 36
//send data to portb w/o disrupting pins 9, 10, and 11, affect 8, 12 and 13 ???
PORTB |= 0 | (bit3state) | (bit5state<<4) | (bit6state<<5);//set high pins
PORTB &= 255 & ~(1-bit3state) & ~((1-bit5state)<<4) & ~((1-bit6state)<<5);//set
low pins
i'm stuck at the last part where i send data to portb, which i only want to affect pins
11 and 12, in replacement of pins 0 and 1. Am I on the right track to do this?
Thanks so much!
thanks for your reply! yup 36 is just an example. following what you did for the
instructable :) what if I want to "shift" pins 0 and 1 back to pins 11 and 12? do
I have to include this part?
//send data to portb w/o disrupting pins 9, 10, and 11, affect 8, 12 and 13
PORTB |= 0 | (bit3state) | (bit5state<<4) | (bit6state<<5);//set high pins
PORTB &= 255 & ~(1-bit3state) & ~((1-bit5state)<<4) & ~((1-bit6state)
<<5);//set low pins
Thanks for this instructable, very helpful. Can i use an AD712KNZ since they dont
make the 922 anymore.
yes, you might also check out the lm386 bc it doesn't require a dual power
supply.
Hello,
Thanks for your instructable! I am now managed to use this trick on one of my
8051 microcontroller!
nice!
http://arduino.cc/en/Reference/tone
Hey Amanda! Great job on this instructable, I'm using it to embase my work on a
eletronic drum sound generator with Arduino (hopefully, one day I'll post here how
to do it).
I'm writing to ask abuot that 0.01 uF and 10ohm resistor in parallel with the
speaker and the DC offset capacitor. What are they used for?
Thanks in advance!
thanks! they're just threre to reduce noise, not a big deal if you don't have
them
what's the upper limit on the sampling frequency for the arudino? can i get it up to
80kHz?
Just wanted to confirm this: since the TS922IN is now obsolete, would this be a
sufficient replacement? Thank you!
that's going to be really hard to work w bc it's surface mount. Just get the
lm386 chip and a couple of resistors and capacitors and wire it up like this:
http://www.hobby-hour.com/electronics/lm386-20.gif
it may need a 9v supply instead of 5v, I can't remember.
I'm working my way through this trying to substitute the lm386 at Step 3. The
actually, a resistor and capacitor only act as a low pass filter when the output
signal is connected to the junction between them, here is a pic. You can see
that switching the order of the components will turn it into a high pass filter.
The lm386 circuit is not wired up the same way, so it won't act as a low pass
filter. So here's what I would do:
arduino - dac - lm386 - low pass filter - output
you could also use a tl1072 or tl082 to replace both ts922's, but these
require a +/- 9v supply, which is annoying.
Can you explain the DC offset more? I understand how a +2.5VDC offset works
but am confused about this one. Thanks!
I always think of it like this: the signal going into one side of the capacitor
causes an alternating excess of positive or negative charge on one side of
the cap. The other side of the cap reacts by accumulating opposite charge -
this causes an alternating voltage on the opposite side of the cap. Since no
current (or a negligible amount) actually gets passed across the cap, the DC
voltage on one side does not transfer over to the other side, so the
alternating voltage is centered around 0.
thanks!!
sounds like you're shorting out one of the arduino's power pins (the strip of
pins near the analog inputs). Double check those.
How did you write your code in those squares that you scroll?
I More Comments
© 2017 Autodesk, Inc. Terms of Service | Privacy Statement | Legal Notices & Trademarks | Mobile Site