Using DAC’s on an Arduino

The video below should be viewed in conjunction with this article as it contains many details not present below.

What are DAC’s?
DAC’s (Digital to Analogue Converters) unlike ADC’s are not on many MCU’s. The rather excellent ESP32 has two as standard but if your using another type of MCU (i.e. Arduino variant) then you can be out of luck. If your not sure what they do then very simply they convert a digital value (i.e. a number) to a corresponding voltage on an output pin..

Resolution
This is basically the range of numbers that you can send to your DAC and they start at 0 to a maximum value. The maximum value depends on the individual DAC and it’s usually quoted in bits, i.e. 8 bit, 10 bit and 12 bit, these are common values. If it’s 8 bit then the max is 255 (i.e. 0 to 255, 256 values), if 10 bit then 1023 (1024 values), if 12 bit then 4095. The higher the value then them more finite control you have over the voltage.

So if we had an 8 bit DAC and a max voltage of 4V then sending “0” to the DAC would give 0V, 127 would give 2V (approx) and 255 would give 4V. With obviously every value in between being available. You could work out how much the voltage will increase for every value by dividing the max voltage by the resolution, so for the above it would be 4v/256 which equals 0.015625 volts per value.

Uses
Wherever you need to control a voltage precisely you could use a DAC. Remember though that the MCU itself cannot drive much current at all and you should always have some sort of driver for whatever is you are using. This might take the form of a simple transistor or more complex buffer circuitry.

However by far and away the most common use of a DAC is for audio. We store sounds as numbers in our digital equipment no matter what it is but at some point we want to listen to that and at this point we can use a DAC to translate it to a voltage that then can be amplified and sent to a speaker. That is what I will be using the DAC for.

The Circuit

The Code
Below is the code shown in the video. Just copy and paste into the Arduino IDE.

Sine Wave

Saw-Tooth

Triangle Wave

Advanced Triangle Wave

That’s if for this post, look out for more articles on this series as I will be developing some software similar to the ESP32 DacAudio software and I will be looking at other DAC’s as well that are SPI rather than I2C which should mean an increase in speed.