user41359: I am as new to Arduino as it can get. To start off, I am trying to build a DAC using an Arduino Leonardo. I have a digital signal coming from my dual differential comparator. I want to convert this to an analog signal.
First, I will read the digital signal in a digital pin (can someone point me which is good? There are so many of them. I was overwhelmed). Then I send out a PWM signal (confused with the pin again). Then I use a simple RC circuit to filter out the PWM signal to produce a simple sine wave.
Am I going towards the correct direction? If not, please any suggestion would be helpful.


user19409: Yes you are going in the right direction.
Comparators
You haven't as yet detailed how you plan to connect the 2 comparators to the Arduino, but I imagine it would be something similar to this:

simulate this circuit – Schematic created using CircuitLab
In the circuit above, X and Y are either high or low based on the analog voltages of A, B, C & D. I have created a truth table below with some arbitrary voltages to output. The value column corresponds to the value needed for the analogWrite() function.

Note: If you need your analog output to be able to source some current you can add an Op-Amp in a unity gain configuration at the output of the RC filter.

Examples To Read
To output the analog value, the 4 best suited inbuilt examples/tutorials to look at are:
Dimmer: Send serial values to change the brightness of an LED.
Fading: Use an analog output (PWM pin) to fade an LED.
Analog In Out Serial: Read an analog input pin, map the result, and then use that data to dim or brighten an LED.
Read ASCII String: Parse a comma-separated string of integers to fade an LED.  
What all of these examples have in common is the analogWrite() function:
// writes the analog value (range from 0 to 255):
analogWrite(pin, value);

Dimmer is a simple example where a serial value is read and then output on one of the PWM pins.
Fading is probably the most basic example of using the analogWrite() function (PWM output).
I included Analog In Out Serial as it uses the map() command.
I included Read ASCII String as it shows how to parse multiple values on the serial string.

PWM Pins
You can use pins 3, 5, 6, 9, 10, 11, and 13 on the Arduino Leonardo to output the PWM using the analogWrite() function.

Control
I recommend the following tutorials for your control. You could either use the if-else command or the switch-case command.

41359: <p>I am as new to Arduino as it can get. To start off, I am trying to build a DAC using an Arduino Leonardo. I have a digital signal coming from my dual differential comparator. I want to convert this to an analog signal.</p>

<p>First, I will read the digital signal in a digital pin (can someone point me which is good? There are so many of them. I was overwhelmed). Then I send out a PWM signal (confused with the pin again). Then I use a simple RC circuit to filter out the PWM signal to produce a simple sine wave.</p>

<p>Am I going towards the correct direction? If not, please any suggestion would be helpful.</p>

<p><a href="https://i.stack.imgur.com/34ear.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/34ear.jpg" alt="enter image description here"></a></p>
 19409: <p>Yes you are going in the right direction.</p>

<p><strong>Comparators</strong><br>
You haven't as yet detailed how you plan to connect the 2 comparators to the Arduino, but I imagine it would be something similar to this:</p>



<p><img src="https://i.stack.imgur.com/5ELsW.png" alt="schematic"></p>



<p><sup><a href="/plugins/schematics?image=http%3a%2f%2fi.stack.imgur.com%2f5ELsW.png">simulate this circuit</a> &ndash; Schematic created using <a href="https://www.circuitlab.com/" rel="nofollow">CircuitLab</a></sup></p>

<p>In the circuit above, X and Y are either high or low based on the analog voltages of A, B, C &amp; D. I have created a truth table below with some arbitrary voltages to output. The <code>value</code> column corresponds to the value needed for the <a href="https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/" rel="nofollow noreferrer"><code>analogWrite()</code></a> function.</p>

<p><a href="https://i.stack.imgur.com/DNf0h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DNf0h.png" alt="truth table"></a></p>

<p>Note: If you need your analog output to be able to source some current you can add an Op-Amp in a <a href="https://en.wikipedia.org/wiki/Buffer_amplifier#Op-amp_implementation" rel="nofollow noreferrer">unity gain</a> configuration at the output of the RC filter.</p>

<hr>

<p><strong>Examples To Read</strong><br>
To output the analog value, the 4 best suited inbuilt examples/tutorials to look at are:</p>

<p><a href="https://www.arduino.cc/en/Tutorial/Dimmer" rel="nofollow noreferrer">Dimmer</a>: Send serial values to change the brightness of an LED.<br>
<a href="https://www.arduino.cc/en/Tutorial/Fading" rel="nofollow noreferrer">Fading</a>: Use an analog output (PWM pin) to fade an LED.<br>
<a href="https://www.arduino.cc/en/Tutorial/AnalogInOutSerial" rel="nofollow noreferrer">Analog In Out Serial</a>: Read an analog input pin, map the result, and then use that data to dim or brighten an LED.<br>
<a href="https://www.arduino.cc/en/Tutorial/ReadASCIIString" rel="nofollow noreferrer">Read ASCII String</a>: Parse a comma-separated string of integers to fade an LED.  </p>

<p>What all of these examples have in common is the <a href="https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/" rel="nofollow noreferrer"><code>analogWrite()</code></a> function:</p>

<pre><code>// writes the analog value (range from 0 to 255):
analogWrite(pin, value);
</code></pre>

<p><em>Dimmer</em> is a simple example where a serial value is read and then output on one of the PWM pins.<br>
<em>Fading</em> is probably the most basic example of using the <a href="https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/" rel="nofollow noreferrer">analogWrite()</a> function (PWM output).<br>
I included <em>Analog In Out Serial</em> as it uses the <a href="https://www.arduino.cc/reference/en/language/functions/math/map/" rel="nofollow noreferrer">map()</a> command.<br>
I included <em>Read ASCII String</em> as it shows how to parse multiple values on the serial string.</p>

<hr>

<p><strong>PWM Pins</strong><br>
You can use pins 3, 5, 6, 9, 10, 11, and 13 on the Arduino Leonardo to output the PWM using the <a href="https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/" rel="nofollow noreferrer"><code>analogWrite()</code></a> function.</p>

<hr>

<p><strong>Control</strong><br>
I recommend the following tutorials for your control. You could either use the <a href="https://www.arduino.cc/reference/en/language/structure/control-structure/if/" rel="nofollow noreferrer">if</a>-<a href="https://www.arduino.cc/reference/en/language/structure/control-structure/else/" rel="nofollow noreferrer">else</a> command or the <a href="https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/" rel="nofollow noreferrer">switch-case</a> command.</p>
