-1

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?

3

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

2
  • 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? – Hadar Sharvit Mar 27 at 19:52
  • 3
    The light you see from the LED is averaged out by your eye, which can't really see the frequencies in the hundreds of Hz used by Arduino's PWM. The oscilloscope, on the other hand, is triggered on rising/trailing edges and has a much higher resolution, so it will show more details at high frequency. Hypothetically, if you filmed the LED in slow motion, you would be able to see it turn on and off repeatedly – Dario Petrillo Mar 27 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.