user76751: trying to create this simple circuit where a rectangular voltage input is transferred to a sine wave output.
the process is quite simple, but I couldn't see the actual sine wave when connecting an oscilloscope.
I'm currently working on a simulator online, so I thought it would be best to share the circuit with the c++ code, and perhaps you would have any insights on why I keep seeing rectangular output voltage rather than sine when the data is indeed a sine wave:
here's an image of the circuit for some specific time:

and the code that generates the sine wave is
#include <math.h>

const int numSamples = 500;
const float pi = 3.1415;
int f = 2; // signal frequency
float sf = 500.0; // sample frequency > 2*f (Nyquist)
int sineSignal[numSamples];

void setup(){
    pinMode(13, OUTPUT);
    // creating sine signal:
    for (int i = 0; i < numSamples; i++){
        float t = i / sf;
        // +1 shift to get values from 0 to 2
        sineSignal[i] = (int) (127 * (sin(2 * pi * f * t) + 1.0));
    }
}

void loop(){
    int pinToConnect = 9;
    for (int i = 0; i < numSamples; i++){
        analogWrite(pinToConnect, sineSignal[i]);
        delay(2);
    }
}

ps. this is all based on this video: How to Generate Sine Wave using Arduino?

user75467: It's working as intened.
The arduino UNO has no DAC (digital to analog converter) onboard, so all it can produce are digital signals, either 0 or 5v. The analogWrite function uses Pulse Width Modulation to produce an output that, on average, has the requested voltage, by quickly alternating between logic low and high with different durations.
This is done at the hardware level using the Atmega's timers, and it's very useful in driving LEDs, servos, steppers and more, but it doesn't produce a steady analog output.
I would suggest either moving to a chip that has an onboard DAC, using an external DAC chip such as this one, or building a simple DAC yourself.
An even simpler solution would be building a RC lowpass filter on the output pin, which would provide a simple, but probably imprecise, way to smooth out the signal and make it look like an analog one

76751: <p>trying to create this simple circuit where a rectangular voltage input is transferred to a sine wave output.</p>
<p>the process is quite simple, but I couldn't see the actual sine wave when connecting an oscilloscope.</p>
<p>I'm currently working on a simulator online, so I thought it would be best to share the circuit with the c++ code, and perhaps you would have any insights on why I keep seeing rectangular output voltage rather than sine when the data is indeed a sine wave:</p>
<p>here's an image of the circuit for some specific time:</p>
<p><a href="https://i.stack.imgur.com/DfxFK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DfxFK.png" alt="circuit simulator" /></a></p>
<p>and the code that generates the sine wave is</p>
<pre><code>#include &lt;math.h&gt;

const int numSamples = 500;
const float pi = 3.1415;
int f = 2; // signal frequency
float sf = 500.0; // sample frequency &gt; 2*f (Nyquist)
int sineSignal[numSamples];

void setup(){
    pinMode(13, OUTPUT);
    // creating sine signal:
    for (int i = 0; i &lt; numSamples; i++){
        float t = i / sf;
        // +1 shift to get values from 0 to 2
        sineSignal[i] = (int) (127 * (sin(2 * pi * f * t) + 1.0));
    }
}

void loop(){
    int pinToConnect = 9;
    for (int i = 0; i &lt; numSamples; i++){
        analogWrite(pinToConnect, sineSignal[i]);
        delay(2);
    }
}
</code></pre>
<p>ps. this is all based on this video: <a href="https://www.youtube.com/watch?v=ojkUaCSFM30" rel="nofollow noreferrer">How to Generate Sine Wave using Arduino?</a></p>
 75467: <p>It's working as intened.</p>
<p>The arduino UNO has no DAC (digital to analog converter) onboard, so all it can produce are digital signals, either 0 or 5v. The analogWrite function uses <a href="https://www.arduino.cc/en/Tutorial/Foundations/PWM" rel="nofollow noreferrer">Pulse Width Modulation</a> to produce an output that, <em>on average</em>, has the requested voltage, by quickly alternating between logic low and high with different durations.</p>
<p>This is done at the hardware level using the Atmega's timers, and it's very useful in driving LEDs, servos, steppers and more, but it doesn't produce a steady analog output.</p>
<p>I would suggest either moving to a chip that has an onboard DAC, using an external DAC chip <a href="https://www.adafruit.com/product/935" rel="nofollow noreferrer">such as this one</a>, or <a href="https://create.arduino.cc/projecthub/Arduino_Scuola/build-a-simple-dac-for-your-arduino-4c00bd" rel="nofollow noreferrer">building a simple DAC yourself</a>.</p>
<p>An even simpler solution would be building a RC lowpass filter on the output pin, which would provide a simple, but probably imprecise, way to smooth out the signal and make it look like an analog one</p>
