Here it is shown how to output recorded audio using Arduino Mega 2560, R-2R DAC and using electronic amplifier using transistors. The audio is put into the Arduino Mega ROM memory. The R--2R is a simple resistors based digital to analog converter. Two transistors BC547 and BD135 are used as simple Arduino audio amplifier.
Components required
- Arduino Mega 2560
- 2.2KOhm and 4.7Kohm resistor for R-2R DAC
- 0.1uF capacitor and 82Ohm resistor for Low Pass Filter
- BC547 transistor and BD135 transistor for amplifier
- 8 Ohm speaker
- Connecting Wires & Arduino programmer USB cable
Hardware & Circuit Diagram
The breadboard realization of this circuit is shown below.
The circuit schematic diagram is shown below.
Here we have used Arduino Mega 2560 port C to output 8bits/sample audio data to R-2R ladder DAC. The R-2R DAC is made up of 2.2KOhm and 4.7KOhm resistors. The DAC output is first filtered using a single pole RC low pass filter with cutoff frequency of 20KHz which results in using 82Ohm resistor and 0.1uF capacitor. Then the filtered audio is passed through kind of darlington amplifier made up of BC537(general purpose NPN transistor) and BD135(medium power transistor). This darlington pair acts as Arduino audio amplifier. Following the electronic amplifier stage we have used simple 8Ohm rated speaker.
Audio Sample & Coding
The audio was recorded using audacity software with a sampling rate of 16khz and 8bits/sample. After recording, it was exported as a .raw file with 8bits/sample. Here Arduino mega was used instead of Arduino uno because of memory space required to store the audio sampled data.The Arduino Mega 2560 has 256kb of flash memory out of which 8kb is used for boot-loader so there is space of 248kb left but other codes are also used in that space, so much lesser space is available for storing data samples.
The steps for making the audio samples for use with Arduino is as follows.
First open the audacity recording software and record something. The time length should be small enough to make sampled audio data size to fit into the program space. The time length is determined by the sampling rate. That is sampling rate times the time length gives us sampled audio data size. For example, if the sampling rate is 16bits/sec and time length is 1.4 sec then sampled data size is 16bits/sec*1.4sec=22400bits or 22.4kb.
The following picture shows example of audio record saying "sound test" in the audacity record.
While recording you can use project rate or sampling rate of 44.1KHz. But when exporting it should be at 16KHz for the reason mentioned above.
The next step is export the audio sample in .raw file. First set the Project Rate to 16KHz and then go to File > Export > Export Audio as shown below.
In the window that pops up, give the file name some like soundtest in this example. Save as type as other uncompressed files and use RAW(header-less) as header and Unsigned 8 bit PCM as encoding format and then save the file. This is as shown in the picture below.
Another window will open which you can ignore and just click OK.
Now once you have saved the audio file as soundtest.raw in our example, we have to convert it as C header file. To convert .raw file to .h header file you can use the bin2h.exe tool which you can freely from the link below.
https://code.google.com/archive/p/bin2h/downloads
Once you have downloaded the file, copy the .raw file(soundtest.raw) into the same folder as the bin2h.exe file. Open up the bin2h.exe application from the command prompt and type in bin2h soundtest.raw soundtest.h and hit enter. This will create the header file soundtest.h in the same folder. This file will be used in the arduino sketch. This process is shown below.
Now open Arduino IDE and create a new sketch. Save the sketch with some suitable name and then put the previously generated header file(soundtest.h) into the same folder as your sketch.
Then type in the following code.
#include "soundtest.h"
short i = 0;
void setup() {
// put your setup code here, to run once:
DDRC = 255;
}
void loop() {
// put your main code here, to run repeatedly:
PORTC = pgm_read_byte(&(data[i++]));
if(i >= sizeof(data))
i = 0;
delayMicroseconds(62);
}
The same thing is shown in the picture below.
Now open the soundtest.h file. Then add const and PROGMEM as shown in picture below.Then compile and upload the code into Arduino Mega. Once you have uploaded the code, you should be able to hear the recorded audio sound from the speaker.
The following video shows the audio output.
Next, see how audio can be produced using LM358 operational amplifier or using LM368 audio amplifier IC instead of using the transistor based arduino audio amplifier.
- Generate Audio using Arduino using R-2R DAC and LM358 Operational Amplifier
- Audio generate using Arduino, R-2R DAC and LM368