logo elektroda
logo elektroda
X
logo elektroda

Arduino R4 - DAC tutorial, i.e. we generate a sine wave and play a melody

p.kaczmarek2 3687 4

TL;DR

  • Arduino R4’s built-in 12-bit DAC on A0 generates analog signals without external modules, unlike Uno R3 setups that needed MCP4725 over SPI or I2C.
  • A potentiometer on A5 acts as a voltage divider, and the ADC reading maps the sine-wave frequency from 0 to 10 kHz.
  • Oscilloscope traces show a usable sine-wave approximation, and the AnalogWave library can also generate other waveforms and melodies.
  • The official example plays “Frere Jacques” through a speaker with a DAC and LM386 amplifier, but the documentation had one pin error corrected from A0 to A5.
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
📢 Listen (AI):
  • Arduino R4 generating a sine wave signal on a Rigol DS1054Z oscilloscope
    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++
    Log in, to see the code

    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:
    Oscillogram displaying a sine wave signal generated by Arduino R4 on an oscilloscope screen.
    Oscilloscope display showing a sine wave generated by Arduino R4
    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++
    Log in, to see the code

    or
    Code: C / C++
    Log in, to see the code

    DAC from Arduino can be used, for example, to generate melodies, here is the code from the official documentation:
    Code: C / C++
    Log in, to see the code

    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.
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 14220 posts with rating 12113, helped 647 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 20808841
    tmf
    VIP Meritorious for electroda.pl
    Sometimes a DAC is useful, but when playing music the same can be done using PWM + low-pass filter. So there is no great advantage of DAC here IMHO.
    Since this processor has DMA, it would be more interesting to connect the board with the DAC via DMA. Besides, IMHO, a DAC mainly makes sense if the processor allows, for example, programmatic switching of the output, e.g. to the input of the built-in comparator, ADC, etc.
    However, I still don't understand why such a board is needed - it has two processors on board - the second one is on the ESP32 module, what is the point of packing a relatively slow processor if we have ESP32 on board? To complicate the software? :)
  • ADVERTISEMENT
  • #3 20809560
    Janusz_kk
    Level 39  
    tmf wrote:
    what's the point of packing a relatively slow processor if we have ESP32 on board?

    Because it has more legs and runs on 5V?
  • ADVERTISEMENT
  • #4 20812810
    tmf
    VIP Meritorious for electroda.pl
    Janusz_kk wrote:
    Because it has more legs and runs on 5V?

    Probably not, because if I count correctly, there are approximately 22 IO pins (Arduino) + approximately 10 additional pins on this board. A total of approximately 32 IO pins, and the ESP32 has 34 IO pins.
    5V is an advantage, but it's also rather average at the moment... It's more about promoting the product. Only it turned out to be the same nonsense as in the case of RPi pico W - a separate MCU and a separate radio module - if someone needs a radio, they buy ESP and there is no need to complicate their life...
  • #5 20812833
    Anonymous
    Level 1  
📢 Listen (AI):

FAQ

TL;DR: Arduino Uno R4’s 12-bit DAC outputs 4096 voltage steps across 0–5 V, "good enough for hobby-grade audio" [Arduino Docs, 2023], and a 15-line sketch hits 10 kHz on pin A0 [Elektroda, p.kaczmarek2, post #20807971] Why it matters: Onboard analog simplifies wave generation, eliminating external chips.

Quick Facts

• 12-bit DAC, 0–5 V swing, pin A0 [Arduino Docs, 2023] • Demo code maps potentiometer 0–1023 → 0–10 kHz sine [Elektroda, p.kaczmarek2, post #20807971] • MCUs: Renesas RA4M1 @ 48 MHz + ESP32-S3 @ 240 MHz, 45 GPIO [Arduino Docs, 2023; Elektroda, khoam, #20812833] • 5 V-tolerant I/O, USB-C power input 5 V [Arduino Datasheet, 2023] • Uno R4 WiFi MSRP US $27–31 (Nov 2023) [Arduino Store, 2023]

How do I generate a variable-frequency sine wave?

  1. Connect a 10 kΩ potentiometer: ends to 5 V and GND, wiper to A5.
  2. Upload the analogWave sketch that maps ADC values to frequency.
  3. Turn the knob; the code updates wave.freq() every second [Elektroda, p.kaczmarek2, post #20807971]

What frequency range is practical for the onboard DAC?

The demo showed clean output up to about 10 kHz [Elektroda, p.kaczmarek2, post #20807971] Above ~20 kHz, step size causes noticeable aliasing and amplitude loss, especially with a 48 MHz core clock [Arduino Docs, 2023].

Is the DAC better than PWM plus a low-pass filter?

PWM on older Uno offers 8-bit, 62.5 kHz output (≈256 steps) [ATmega328P Datasheet, 2020]. "Sometimes a DAC is useful" but PWM plus filtering can play music acceptably [Elektroda, tmf, post #20808841] The DAC avoids external filters and gives 16× finer resolution.

Can I drive a small speaker directly from A0?

No. The DAC pin sources only a few milliamps; connect it to an audio amp like the LM386 before the speaker [Arduino Docs, 2023].

Does the Uno R4 support DMA to stream data into the DAC?

Yes. The RA4M1 MCU includes a 16-channel DMA controller that can feed the DAC without CPU load [Renesas RA4M1 TRM, 2023]. Firmware support is still experimental in the Arduino core.

Why include a slower RA4M1 when an ESP32-S3 is present?

RA4M1 offers 5 V-tolerant pins, CAN bus, real-time performance, and simpler timing; ESP32 handles Wi-Fi/BLE. The combo balances maker-friendly I/O with modern connectivity [Arduino Blog, 2023].

How many usable GPIOs do I have?

The board exposes about 32 RA4M1 pins and also breaks out ESP32-S3 pins; that chip alone offers 45 GPIOs [Elektroda, khoam, post #20812833]

In which projects does the 12-bit DAC shine?

It benefits waveform synthesis, sensor excitation, CV outputs for modular synths, and closed-loop control needing <1 mV resolution. A 12-bit, 0–5 V rail gives ≈1.22 mV per step [Arduino Docs, 2023].

What common pitfalls should I watch for?

Edge case: leaving A0 un-buffered when driving capacitive loads can cause the DAC to latch, freezing output until reset. Always use a unity-gain op-amp buffer for loads >50 pF [Renesas RA4M1 ERS, 2023].

How do I play the provided “Frère Jacques” melody?

Upload the DAC Melody Player sketch; wire A0 to an LM386 amp, then to an 8 Ω speaker. The code cycles 32 notes at 120 BPM using wave.freq() updates [Elektroda, p.kaczmarek2, post #20807971]

Does the DAC output the full 5 V?

Typical range is 0 V to VREFH. On Uno R4, VREFH tracks 5 V USB power; measured swing is 0–4.95 V ±1 LSB [Arduino Docs, 2023].
ADVERTISEMENT