Determine "best" square wave?

Hello everyone. First, I should say that I'm new to MC programming and have only limited experience with ESP32. I would like to make a "calibration" device. I'd have a signal that should be a square wave between 0v and +5v. The signal's characteristics are changed via a potentiometer. Currently I connect an oscilloscope and trim the pot to achieve the highest amplitude before the square wave gets heavily distorted. The frequency can range from 10Hz - 2MHz.

Basically, I'd like to keep it simple and have either a display or three LEDs (Reverse, Best, Forward). Either would display nothing of interest to begin with, but when the signal changes, the characteristics are written to a variable. If the next reading is better, the display or the "Foward" LED should show that the direction is correct and keep going. If the next reading is worse, the display or the "Reverse" LED should show that the direction is wrong, and the direction should be reversed. Once the direction is correct, and the signal then gets worse, the display or "Reverse" LED should show that the direction needs to be reversed. Once the "best" signal is reached by going backwards, the display or "Best" LED should signal optimum.

I think each sample should be written to an array or queue and compared to the previous measurement, then we know the direction. With that information we can control the display / LEDs.

Does anyone think they could help me out with that?

???? Please postschematics showing what we're talking about.

No Arduino can do what your oscilloscope and you own eyes do. You are visually measuring the time your signal takes to go from zero volts to some positive value. The Arduino can only detect when that takes place, not how long the change is taking. The Arduino A/D converter cannot operate that fast even if your code could trigger the A/D when the square wave is supposed to begin.

With characteristics I mean the voltage and flank wall.

The signal could look like any of these or worse.

image
image
image

I'd think that the effective voltage would be key. If not, it was worth a try...

I don't know what you're measuring or seeing, but regular clipping ("overload distortion") doesn't distort a square wave or pulse. But it makes a sine wave more-like a square wave and if you "push-it" far-enough you will get a square wave.

How fast can the ESP32 sample? (All I know is that it's faster than an Uno...) Nyquist says you need to sample at twice the signal frequency, but square waves contain harmonics so you'd probably need to sample at 20MHz or higher. Digital 'scopes usually sample at about 10X the maximum signal frequency, otherwise you can't see the wave shape.

Then you can use FFT to compare the actual frequency components to an ideal square wave.

Whether you need go "forward" or "reverse" you'll need to know how that's represented in the Fourier series.

Not possible with just an ESP32 and some LEDs

That's way above my math skills...

Rather?

It is straightforward to use a built-in timer to generate a rectangular wave, of variable frequency and fixed amplitude of about 3.3V.

To convert that signal to a perfect square wave, you would pass the timer output to a discrete "flip flop" IC, which will divide the input frequency by 2 in the process.

Sorry. I must have been unclear. I don't want to make it a perfect square wave, but rather detect the best signal.

Please explain your ideas about "detect the best signal", for both "detect" and "best".

1 Like

Well, if the effective voltage could be used, and I guess a test would be the best way to determine that, a higher voltage would be a "better" signal. Looking at the waveforms above, I -assume- that the saw tooth looking form has a lower effective voltage than the other two. So may that's enough. If my assumption is wrong, perhaps not.

I maybe shouldn't open the can of worms, but my goal is to do as quick and as precise as possible ws2812b data input resistance / impedance calibration without needing an oscilloscope.

Test your assumption with a half-wave rectifier and a capacitor and a load resistor. Measure with your DVM and see if what you assume is true. No need to guess.

What does all that mean? I still have no idea what you are trying to do.

From your first post, the following line gave me the impression that you intended to generate a signal using an MCU.

I'd have a signal that should be a square wave between 0v and +5v. The signal's characteristics are changed via a potentiometer.

Perhaps you should tell us about the device or circuit that generates the signal, how you intend to measure the signal, and what you hope to learn from that.

It really is that easy... Thank you!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.