
Arduino R4 has a built-in 12-bit DAC (Digital-to-Analog Converter), which means it is able to generate an analog signal without connecting additional modules. This is a big step forward, because in the case of the older Uno R3 and similar boards we had to connect an external DAC such as the MCP4725 and control them usually via SPI or I2C to obtain a sine wave. And here you can generate it directly from pin A0. In this topic, I will briefly show how to run a DAC on Arduino R4 and take a look at the generated signal on an oscilloscope.
DAC - sine wave on Arduino R4
Let's consider an example from the Arduino documentation. This example reads the voltage value from the potentiometer and converts it to the frequency of the generated sine wave on the DAC. The potentiometer serves as a voltage divider, the extreme pins on VDD and GND, and the middle pins on the ADC. Below is its corrected code, because at the time of writing the topic there was an error in the Arduino documentation, the wrong pin was used once - A0 instead of A5.
Code: C / C++
The potentiometer is connected to A5, and there is a DAC output to A0.
The map function, as the name suggests, maps the read value from the ADC, which is in the range from 0 to 1024 (10-bit ADC), to the frequency, the range of which we have chosen is from 0 to 10kHz.
Here is the result:


The oscillograms show very nicely how the DAC creates an approximation of a sine wave sufficient for many applications. If we want to generate another function, we also have the following options:
Code: C / C++
or
Code: C / C++
DAC from Arduino can be used, for example, to generate melodies, here is the code from the official documentation:
Code: C / C++
The above code generates the melody "Frere Jacques", plays a sequence of notes and rhythms, using the AnalogWave library to generate sound waves. The melody is played on the speaker using a DAC and audio amplifier (LM386), and parameters such as pitch, melody, rhythm, tempo and note length are defined in the code.
The full specification of Arduino R4 (in its available versions) can be found on the Arduino website:
https://docs.arduino.cc/hardware/uno-r4-wifi
https://docs.arduino.cc/resources/datasheets/ABX00087-datasheet.pdf
Summary
It's hard to disagree that the typical beginner will have a lot to run and play with with the Arduino R4. There is much more on board this board than in the iconic R3, it even offers the DAC discussed here, which allows you to generate a sine wave without connecting external modules. Just a few contact cables are enough.
Still, I wonder how often these DACs might come in handy? Have you used DACs in your projects and if so, which ones? Or maybe in R4 it is just an unnecessary gadget? I invite you to discuss.
Cool? Ranking DIY Helpful post? Buy me a coffee.