does anything can transform sq. wave to sine?

PWM is so difficult to me, so I want to know that(same as subject).
Actually, I want to output sine wave(sounds), but how?

any IC? any circuit?

plz give me a hand>< :cry:
makes me :-/


I had a difficult time understanding what your issue is. You may want to make your question more clear to get a good answer.

Well converting a square wave to a sine wave only requires low pass filter. However PWM is not a useful method to try and make music tones as it is designed to vary duty cycle of a fixed frequency not vary the frequency. You should look over the new tone library by searching in this forum. It outputs tones of your desired frequency and can be used as is but a little harsh because of the square wave harmonics.


If you want to output waveforms, and don't mind taking up several digital pins, you could set up a resistor-ladded DAC on 6-8 pins, then do direct port access (-not- digitalWrites) to output the proper voltage levels; this can be input directly to a small speaker, but for proper usage you should run it thru an op-amp or something to re-center the waveform produced to zero.

Build waveform tables in memory (they can be short; also note that with symetrical waveforms you can take advantage of that to have smaller tables), mix multiple waveforms for multi-voice sound, add ASDR (attack-sustain-decay-release) envelope tables to do FM synthesis - the list is endless.

With an Arduino, it should be fairly easy to 2-3 channels of such sound outputs (2 if running 8 bit; 3 if running 6 bit).


U both mean that I output sq. wave sound form my arduino piano, then add low pass filter to change sq. wave almost to sine?

I absolutely know that I should add amp before output to speaker.
But how can I measure the R & C, how many ohms and F?

Next, wouldn't I use mylar capacitor just as well?

Can I transform sq. to other waveforms by using pots??
It means that whenever I tune the pots, the waveforms change.
Such as rising time, falling time. :-?

Well converting a square wave to a sine wave only requires low pass filter.

Wait, are you saying that if one sticks this:

Low-pass filter - Wikipedia

On a piezo speaker, one can transform the square wave output of the Arduino into something much more pleasant?

One way you can play sine waves on the Arduino is to hook up a speaker and then do pulse code modulation:

Here is some code to do that:

Note, the above code will not work with a piezo speaker. The reason being that the code is a kind of a hack that relies on the fact that a speaker's cone takes a little while to move in and out.

the code is a kind of a hack that relies on the fact that a speaker's cone takes a little while to move in and out.

I think that's a low-pass filter.

THANKS scswift and AWOL.

I learnt what's PCM and PWM. I clearly understood!! but I;m pretty weak in "timers" such as

void startPlayback()
    pinMode(speakerPin, OUTPUT);

    // Set up Timer 2 to do pulse width modulation on the speaker
    // pin.

    // Use internal clock (datasheet p.160)
    ASSR &= ~(_BV(EXCLK) | _BV(AS2));

    // Set fast PWM mode  (p.157)
    TCCR2A |= _BV(WGM21) | _BV(WGM20);
    TCCR2B &= ~_BV(WGM22);

    // Do non-inverting PWM on pin OC2A (p.155)
    // On the Arduino this is pin 11.
    TCCR2A = (TCCR2A | _BV(COM2A1)) & ~_BV(COM2A0);
    TCCR2A &= ~(_BV(COM2B1) | _BV(COM2B0));

I think PWM is mostly used in motor controlling, not for Piano :frowning:
does anyone answer me, can I control waveform by adding pots?

You can use opamp IC741.

Can you control waveforms using potentiometers? Sure.

If you look, that pulse code modulation program has an array to hold the sound data, a function to start playback, and a function to stop playback.

If you want to control waveforms using a potentiometer, then what you need to do is set up your potentiometers on the analog pins liks so:

Then read the analog values, and use those to control a function you have written to create your waveform. Then play the waveform back witht he PCM code.

So, you could have one potentiometer set the frequency, another the amplitude, and have a third set the type of waveform or interpolate between sine and square wave or whatever you want. Then you could have four more to control the envelope (attack, sustain, delay, release).

Here are a couple wikipedia articles to get you started:

But that's if you're gonna write a function to generate sampled sound data at 8000hz. If you're asking if you can just turn a pot to turn a sqaure wave into a sine with a circuit... without writing any code... well, I dunno. With a pot alone? Certainly not. With a pot and some other components? Well, there's that low pass filter circuit I posted. Maybe something can be done with that. The other fellow indicated you can turn a square wave into a sine wave with a low pass filter.

Oh, and PWM /PCM is not NORMALLY used for synths, that's a hack, like I said, and it limits you to 8000hz because of the processor speed and other factors.

If you want to control a speaker the proper way you need a DAC:

Which you can create by building a resistor ladder:

This is a 5 bit DAC:

You attach one pin to each bit, and set them the same way you would set a binary number. This gives you 32 possible voltage/current levels. The speaker goes between GND and Output. (I'm not sure which is being modified here, but I think it's current.)


I tried to add a 12bit SPI DAC (MCP4921) before, but it was pretty hard for me to input serial data to DAC.

the above link, I tried to build a same circuit without buying that kit, but no sound no output so I gave up!!

PWM, which's a good way to gen a sine!! but it's hard to me, I need a very long time to be deep into it.

you can get sine, triangle, saw, or any other waveform you can dream of by using this. (you would have to figure out how to adapt it to your sketch, but once you figure out what it is actually doing it is not that hard.)

Arduino sketch for high frequency precision sine wave tone sound synthesis | Adrian Freed.

you can just edit/add wavetables depending on how much memory you have in the arduino and how long you mind waiting for it to upload each time.

There is plenty of time between the ISR to generate envelopes, LFOs, add additional oscillators, and all kinds of fun.

you can use analog ins or MIDI in to control how it sounds.

on mine i just used DAC0808 parallel DACs.
on a mega it is easy to hook up 3 of them. then you can use the PWM outputs to control the DACs voltage reference, for VCA envelope control without losing bit depth.

or you can use the PWM out. in this case it is 31.25 KHZ, so its quite easy to filter it with just a resistor and a capacitor. it still sounds a lot nicer than just a square wave!

good luck!

You could look at Nebulophone on site.
They make music with an Arduino equivalent plateform.
In short they reprogram the timer and make PCM using "sound tables" and the PWM output.
If you download the Nebulophone sketch you'll find in the comments some interesting links. And the program can be of great benefit to you.

CAN YOU post a simple one to me??

I read many PWM, PCM CODES,but no one I understand...I do want to learn it, but I really dont know how to start..sorry><

Post your code first,I teach you step by step

You don't need to understand this code to use it:

You just need to know how to create your sound data and put it in the array, then call the function to start playback.

You set sounddata_length to the number of samples.
And you put your samples in sounddata_data[].

The sounds are at 8000hz, so you need 8000 samples per second.

In other words, if you want to play a 440hz sine wave, middle C, for one second, you would need 440 oscillations between 0 and 255 in that array, with each oscillation taking up approximately 18 of those 8000 samples.

You've gotta model the oscillations on a sine wave though if that's what you want. Simply stepping from 0 to 255 and back (x+=14 for each step) in a linear fashion will give you a triangle wave.

i understand your point. u mean i no need to understand all. according to link u posted,i just download all the libraries and put sound data into it. then attach all stuffs. finally a sine wave comes?

but can it be polyphonic? i heard pwm only for fixed frequency but not vary frequency, how can i mix frequencies to one then output?

i try to write code to play a noTe first, and share it t you all.

thank you. all helps are appreciated