# How can I convert a sine wave generated by PWM technique into a square wave?

I have generated a variable frequency sine wave by PWM using an Arduino Uno. I want the obtain a square wave using the generated sine wave, so I used an op-amp to remove the DC offset in the sine wave (since Arduino generates a sine wave between 0-5 V) and also to convert the sine wave into square wave. The square wave generated by the op-amp is not giving a perfect square wave due to which I'm not able to measure the frequency of the square wave using another microcontroller.

Does anyone have any idea on how to solve this problem?

• If you set the scope input to DC you may get a better square wave. Commented Apr 11 at 17:25

An LM741 needs power rails much greater than 2.5 volts to begin thinking about working (if working is a word that can be used for a 741). So, throw away your 741 and use a comparator with a little bit of hysteresis.

You can use 5 volts and 0 volts for the supply rail too and use an RC high pass filter on the output. Or use a fast rail-rail op-amp you know; something modern that has a decent specification.

due to which I'm not able to measure the frequency of the square wave using another microcontroller

Feeding a +/- 2.5 volt signal into a microprocessor stands a high probability of damaging it.

Peter has nailed the reason for your apparently non-flat high and low on the square wave.

The 741 is a particularly bad part for this application. Recommended minimum operating voltage is +/-10V according to the datasheet. That's 4 times the total voltage you have.

Using an op-amp as a comparator is also typically stepping into undocumented territory since the time to recover from saturation is most often not specified unless it is unusually good. It could be 50 microseconds on an amplifier with several MHz gain-bandwidth product.

So using a comparator is a better approach, even an old one such as the LM393. You can use 5V and 0V supply voltage. There are times when it makes sense to break this 'rule' but for now, it's good to stick to it.

• I'm always surprised by the number of people who want to use an op-amp as a comparator. Obviously you can get away with it sometimes, and sometimes you have an extra op-amp in a quad for example and don't want to use another part, but it's often not ideal. Commented Apr 11 at 19:01

What you're seeing on the oscilloscope is a normal square wave deformed by the high-pass filter that is turned on when the scope is used in AC mode.

Solution: set the scope to DC mode.

Besides that, if you want to input this square wave into another micro, then it needs to be 0V/5V or 0V/3.3V, it can't be negative, so your opamp doesn't need a negative power supply. You could use a comparator instead, powered from 5V, it will work fine as long as the comparator input is rail to rail.

A logic gate like 74HC14 with schmitt trigger will do a fine job too.

Note if you want to filter your PWM signal you need a RC filter, not just the capacitor.

• I have set the above square wave in the range of 0-5v and gave it to another Arduino for frequency calculation of the square wave... But the Arduino is not calculating the the frequency and it's showing "inf". So i tested the Arduino with another square wave generated by a IC555 timer and the Arduino is measuring the frequency and the result is correct.. so what mistake did I make in the first case ( where I/P = a converted square wave) and why the Arduino is counting frequency in second case and not in the first case..?? Commented Apr 12 at 7:00
• No idea, there's not enough information, check it with an oscilloscope Commented Apr 12 at 7:36