breadboard
docs

Example project · Analog & circuits

Uno R4 · pot-controlled DAC sine

Turn the pot to change the A1 ADC reading and the A0 DAC sine amplitude. Probe A0 in Scope. The real DAC clips near the supply limits; the official core also briefly releases its output between updates.

The Uno R4 · pot-controlled DAC sine circuit as rendered by the simulator

How it works

The UNO R4's RA4M1 has a real 12-bit DAC on A0, which the classic Uno does not have. This sketch uses it to make a sine wave and uses the 14-bit ADC on A1 to set its size. A 10 kΩ potentiometer runs from the board's 5 V pin to GND, and its wiper goes to A1. A 100 kΩ resistor from A0 to GND gives the DAC output a light load.

setup() sets the ADC to 14 bits (0 to 16383) and the DAC to 12 bits (0 to 4095). Each pass of loop() reads A1, prints the reading as adc14 <value>, and then steps through a 32-point sine table. Each output code is 2048, the middle of the DAC range, plus the table value scaled by the pot reading, followed by a 1 ms delay. One cycle takes about 32 ms, so the sine is close to 31 Hz, a little slower because of the read and print at the start of each pass. The DAC's reference is the board's 5 V rail, which sits a little under 5 V on USB power.

What to watch

Serial prints adc14 8192 with the pot at its middle position. Probe A0 in Scope: the sine swings between about a quarter and three quarters of the 5 V rail. Turn the pot to one end and the reading becomes adc14 16383, and the wave fills almost the whole range. It clips near the supply limits, and the official core briefly releases the output between updates.

What's on the bench

  • Arduino Uno R4
  • Resistor
  • Trim Pot

How it's wired

  • Arduino Uno R4 · 5v→Trim Pot · a
  • Arduino Uno R4 · gnd2→Trim Pot · b
  • Trim Pot · wiper→Arduino Uno R4 · a1
  • Arduino Uno R4 · a0→Resistor · a
  • Resistor · b→Arduino Uno R4 · gnd2

The code

This Arduino C++ sketch lives in Code; sign in and press Compile & upload to build real firmware for the emulated board.

// SPDX-License-Identifier: MIT
// First-party UNO R4 example: A1 sets the amplitude of the A0 DAC sine.
static const int16_t kSine[] = {
  0, 399, 783, 1137, 1447, 1702, 1891, 2008,
  2047, 2008, 1891, 1702, 1447, 1137, 783, 399,
  0, -399, -783, -1137, -1447, -1702, -1891, -2008,
  -2047, -2008, -1891, -1702, -1447, -1137, -783, -399
};
void setup() {
  Serial.begin(115200);
  analogReadResolution(14);
  analogWriteResolution(12);
}
void loop() {
  const int level = analogRead(A1);
  Serial.print("adc14 ");
  Serial.println(level);
  for (int16_t sample : kSine) {
    const int code = 2048 + (int32_t)sample * level / 16383;
    analogWrite(DAC, code);
    delay(1);
  }
}

Firmware licence

Notice: this firmware uses the LGPL-licensed Arduino core library

arduino-unor4wifi-analog-wave.bin is statically linked with ArduinoCore-renesas 1.6.0, in Breadboard's port of its UNO R4 WiFi variant to Renesas FSP 5.3.0. Parts of that core are licensed under the GNU Lesser General Public License, version 2.1 or (at your option) any later version ("the Library"). Breadboard distributes this firmware under terms that permit you to modify the work for your own use and to reverse engineer it for debugging such modifications.

Try this

  • Select the pot and drag its position. The adc14 value in Serial and the size of the sine on A0 change together.
  • Move the pot until adc14 is near 0. A0 becomes an almost flat line at about half the rail.
  • In Code, change delay(1) to delay(2), then sign in and press Compile & upload. The delay per sample doubles, so the frequency roughly halves.

Related projects