ad space

Arduino based audio player using a Digital-to-Analog Converter (DAC)

Arduino is a popular open-source electronics platform that allows hobbyists, makers, and professionals to easily create a wide range of projects. With its ease of use, flexibility, and wide range of available hardware, Arduino has become a popular choice for a variety of projects, including audio players. In this blog post, we'll take a look at how to build an Arduino-based audio player using a DAC0832 DAC (Digital-to-Analog Converter).

What is a DAC?

A DAC is a type of integrated circuit that converts digital signals into analog signals. In the context of an audio player, a DAC is used to convert digital audio data into an analog audio signal that can be played through a speaker. The analog audio signal produced by the DAC is proportional to the digital audio data, allowing it to produce high-quality audio.

The DAC0832 is a popular 8-bit DAC that is commonly used with Arduino projects. It has a simple 3-wire interface and is easy to use with the Arduino platform. It is an 8-bit Digital-to-Analog Converter (DAC) integrated circuit. It converts digital signals into analog signals, allowing the user to output a range of analog voltages from a microcontroller. The DAC0832 accepts single supply voltage of 5V to 5V and has a low output impedance, making it suitable for driving low impedance loads like speakers or other analog circuits. The device is easy to use and can be controlled through a simple serial interface, which makes it ideal for microcontroller applications. The DAC0832 IC(Integrated Circuit) also features an internal reference voltage, which allows for a high level of accuracy in the analog output voltage. Additionally, the device has power-saving modes to minimize power consumption during periods of inactivity.

Circuit Diagram

To build the audio player, you'll need an Arduino board, a DAC0832 DAC, a speaker, and a few passive components, such as capacitors and resistors. The circuit diagram for the project is shown below:

Here is a brief overview of the circuit:

  1. Arduino Uno: The central component of the circuit is the Arduino board, which is used to store and process the audio data control(send to DAC).
  2. DAC (DAC0832): The DAC, specifically the DAC0832, is used to convert the digital audio data from the Arduino into an analog audio signal. The Arduino audio output at its Port D is connected to the DAC digital pins as shown in the above diagram. This analog signal can then be amplified and played through a speaker.
  3. Current to Voltage Converter(LM548): The LM548 connected to DAC0832 is used here as current to voltage converter. is a low-power audio amplifier used to boost the small analog audio signal from the DAC to a level suitable for driving a speaker.
  4. 2nd Order Low Pass Filter: The output from the current to voltage converter is connected to 2nd order low pass filter.
  5. Buffer Amplifier(LM358): The output of the low pass filter is connected to the Buffer Amplifier which is implemented using LM358 operational amplifier. 
  6. Volume control Potentiometer: The output from the buffer amplifier is connected to the 10KOhm potentiometer which is used for control the volume.
  7. LM358 audio amplifier: The second operational amplifier is used as inverting amplifier with 100KOhm feedback resistor and 10KOhm input resistor. 
  8. Coupling Capacitors: The two 10uF capacitors C1 and C3, are input and output coupling capacitors.
  9. Speaker: The speaker at the end connected to coupling capacitor C3 is of 4Ohm but 8Ohm can also be used as well.

Overall, the circuit takes digital audio data from the Arduino, converts it to an analog signal using the DAC, amplifies the signal using the LM386, and then plays the amplified signal through a speaker."

Programming the Arduino

The code for the audio player using Arduino and DAC DAC0832 is shown below.

#include "soundtest.h"

volatile uint8_t i;

void setup(){	
  PORTD = 0xFF;	//set PORTD as output
  InitTimer1();	//Initialize Timer 1
}

void loop() {
 
}

//Set up timer 1 for 16384 Hz sample rate
void InitTimer1(){
	//SET UP TIMER 1
	cli(); //Disable global interrupts
	TCCR1A = 0; //Reset Timer 1 Counter Control Register A
	TCCR1B = 0; //Reset Timer 1 Counter Control Register B
	//Set Timer 1 Output Compare Register A to desired frequency
	//(16,000,000 / 16384) - 1 = 975
	OCR1A = 975;
	TCCR1B |= (1 << WGM12);  //Turn on CTC (clear timer on compare match) mode:
	TCCR1B |= (1 << CS10); //Prescalar = 1 (no prescalar used)
	//Enable timer interrupt
	TIMSK1 |= (1 << OCIE1A);
	//Enable interrupts
	sei();
}

ISR(TIMER1_COMPA_vect){
	//Read data and output to PortD
   PORTD = pgm_read_byte(&(data[i++]));
   if(i >= sizeof(data))
   i = 0;
   delayMicroseconds(100);
}

This above code written in the C programming language for the Arduino platform is used to play an audio file stored in the memory of the Arduino using the speaker connected to Port D of the Arduino.

The audio to be played is stored in header file called soundtest.h. This file is very long to include here. A small part of the code is shown below.

const unsigned char data[21920] PROGMEM=
{
	0x80,0x80,0x7F,0x80,0x7F,0x80,0x7F,0x7F,0x80,0x80,0x80,
	0x80,0x7F,0x80,0x80,0x80,0x7F,0x7F,0x80,0x80,0x80,0x80,
	0x80,0x80,0x80,0x80,0x7F,0x80,0x80,0x7F,0x7F,0x7F,0x80,
	0x80,0x80,0x80,0x80,0x7F,0x7F,0x7F,0x80,0x7F,0x7F,0x80,
	0x80,0x7F,0x80,0x80,0x80,0x7F,0x7F,0x80,0x7F,0x80,0x80,
	0x7F,0x7F,0x80,0x80,0x7F,0x7F,0x80,0x80,0x7F,0x7F,0x80
    ................
};

You can download the full audio file code soundtest.h from the following Github link:

Download

The setup function initializes Port D as an output and sets up Timer 1 to run at a frequency of 16384 Hz. The InitTimer1 function sets up Timer 1 to run with a Clear Timer on Compare Match (CTC) mode and with a prescalar of 1 (no prescalar). The timer interrupt is enabled to trigger the TIMER1_COMPA_vect interrupt service routine (ISR) when the value in the Timer 1 Output Compare Register A matches the value in the Output Compare Register.

The TIMER1_COMPA_vect ISR reads data from the audio file, outputs it to Port D, increments the data index i, and resets the index i to 0 when the end of the data is reached. The ISR also adds a delay of 100 microseconds.

Note that the audio file data is stored in the program memory of the Arduino using the pgm_read_byte function, which allows for faster access to the data compared to reading it from the data memory.

Conclusion

In this blog post, we've looked at how to build an Arduino-based audio player using a DAC0832 DAC. With its ease of use and flexible hardware options, Arduino is a popular choice for a variety of audio projects. Whether you're a beginner or an experienced maker, building an audio player using an Arduino and a DAC0832 DAC is a fun and rewarding project that can teach you a lot about audio electronics. If you don't have LM358 operational amplifier you can also use cheap and readily available general purpose bipolar junction transistor like 2N3904 or BC547 as illustrated in the tutorial 2n3904 audio amplifier.

Post a Comment

Previous Post Next Post