Arduino simulator – generating sine wave from PWD (Oscilloscope read is rectangular)

  arduino, arduino-uno, c++, pwm

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:

circuit simulator

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?

Source: Windows Questions C++

LEAVE A COMMENT