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

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?

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

• Thank you for the comment - just a few clarifications: the led light does oscillate as expected (According to a sine wave), but the oscilloscope does not display that. what is the reason for that?