Electronics > Beginners

Converting a messy square wave to digital?

<< < (3/3)

Yansi:

--- Quote from: T3sl4co1l on December 10, 2019, 08:59:44 pm ---Hysteresis comparator. :-+  LM311 for single, say.

Tim

--- End quote ---

More than likely to awfully oscillate when presented to a slowly varying input signal.

pigrew:
Pardon me if I've missed it, but could you share what resistor value, and pull-up voltage you are using?

With the open-collector output, you should be able to pull-up directly to the I/O voltage (3.3V or 5V), instead of pulling up all the way to Vdd. This likely will give you a better looking waveform. I suggest starting with 10k, and adjusting to lower resistances if your rise time is too long (or higher if the fall time is too long).


See this StackOverflow answer for a schematic of what I'm suggesting


--- Quote from: ryanmills on December 10, 2019, 08:51:50 pm ---Is the ADC the more correct choice or is there a more standard way to take that sloppy square wave and turn it into something cleaner that will place nicer with a GPIO?

--- End quote ---

Use digital GPIO, the square wave is already good enough for a digital input, assuming its voltage is between 0 and the Vdd value of your MCU. The only time I'd use an ADC for this sort of issue is if the digital signal were very small magnitude, if there is significant DC shift (such that the threshold value needs to change over time), or if complicated DSP is required.

If you require a very precise falling edge time, then you may need to introduce a comparator (and others have mentioned) because the MCU's IO pin's threshold will not be stable over time and temperature.

Another trick I've used is to drive the output pin with a BJT-based current source (from a current mirror), which will sharpen up the edges of the output signal.

ryanmills:

--- Quote from: pigrew on December 11, 2019, 07:17:58 pm ---Pardon me if I've missed it, but could you share what resistor value, and pull-up voltage you are using?

With the open-collector output, you should be able to pull-up directly to the I/O voltage (3.3V or 5V), instead of pulling up all the way to Vdd. This likely will give you a better looking waveform. I suggest starting with 10k, and adjusting to lower resistances if your rise time is too long (or higher if the fall time is too long).


See this StackOverflow answer for a schematic of what I'm suggesting


--- Quote from: ryanmills on December 10, 2019, 08:51:50 pm ---Is the ADC the more correct choice or is there a more standard way to take that sloppy square wave and turn it into something cleaner that will place nicer with a GPIO?

--- End quote ---

Use digital GPIO, the square wave is already good enough for a digital input, assuming its voltage is between 0 and the Vdd value of your MCU. The only time I'd use an ADC for this sort of issue is if the digital signal were very small magnitude, if there is significant DC shift (such that the threshold value needs to change over time), or if complicated DSP is required.

If you require a very precise falling edge time, then you may need to introduce a comparator (and others have mentioned) because the MCU's IO pin's threshold will not be stable over time and temperature.

Another trick I've used is to drive the output pin with a BJT-based current source (from a current mirror), which will sharpen up the edges of the output signal.

--- End quote ---

Experimented with what you suggested. However I noticed as I go up in value the fall time gets longer. So i went the other way and lowered it down to 330 ohms. I got a pretty usable square wave out of it. Drastically dropped the fall time (capture attached). However the 1/4 watt resistor was starting to cook. Is it safe if I went with a bigger watt resistor to use a value that low?

I looked at the example in your link and i'm confused where the level shift happens. It looks like that example would drive +12v into the GPIO?

pigrew:

--- Quote from: ryanmills on December 11, 2019, 07:51:34 pm ---Experimented with what you suggested. However I noticed as I go up in value the fall time gets longer. So i went the other way and lowered it down to 330 ohms. I got a pretty usable square wave out of it. Drastically dropped the fall time (capture attached). However the 1/4 watt resistor was starting to cook. Is it safe if I went with a bigger watt resistor to use a value that low?

I looked at the example in your link and i'm confused where the level shift happens. It looks like that example would drive +12v into the GPIO?

--- End quote ---

Don't cook the resistor..... At 12V, I wouldn't use anything under 1k (to limit to <0.15 W). At 5V, you could perhaps go down to 220ohm.

The trend you describe sounds like it would be if you had a PNP output with a pull-down resistor. Otherwise, I'm not sure what's happening.

Look at the last image in the link, the one with the Arduino. The resistor is connected between the sensor's output and 5V, so the result should be 5V max.

GerryR:

--- Quote from: ryanmills on December 11, 2019, 06:55:44 pm ---
--- Quote from: GerryR on December 11, 2019, 01:03:55 am ---Sorry - I should have looked closer at the first post.  The prox requires a 10 -30 VDC source of power; what are you using?  And, to what voltage is the 1k load connected?

--- End quote ---

It's powered on the bench at 11.5v, real world it's power by 12v but 11.5v is closer to the actual voltage it would be. I attached an ugly schematic as I have it on the bench. I sort of picked the 1k value out of the air. Is there a more specific value I should be picking?

--- End quote ---

Probably just a drawing error, but per your drawing, the scope is connected wrong;  Scope tip should connect to black of the prox and the negative clip to the the blue.  1k 5k, 10k loads should all work fine.

Added a sketch:

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod