Converting a messy square wave to digital? - Page 1
Forum section: electronics / beginners

author: ryanmills
author_rank: Unknown
timestamp: December 10, 2019, 08:51:50 pm
content: Sensor: E2B-S08KN04-WP-C2 2MDatasheet:http://www.ia.omron.com/data_pdf/cat/e2b_d116-e1_1_6_csm1012652.pdfNew to EE and I'm diving into new waters and could use some advice. I have an inductive proximity sensor i'm using to measure RPM's on a shaft. It's an NC NPN output. I need to run that output into a Pi or Feather GPIO with 5v or 3.3v logic. I will likely be running the output at 12 volts. My first thought was running it into an ADC but that input range only leaves a hand full of choices. And the ADC is probably overkill for a simple square wave. I could just use the output to drive a 2nd NPN or FET to switch to 5 volt but I'm not sure about smoothing the wave (and finding one that can switch at 100hz?). Looking at output square wave on the scope there is a bounce on the rise and a long fall time (captures attached). My idea now is a FET into a comparator but I'm not sure that solves the bounce.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?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: December 10, 2019, 08:59:44 pm
content: Hysteresis comparator.LM311 for single, say.Tim

author: GerryR
author_rank: Frequent Contributor
timestamp: December 10, 2019, 09:16:26 pm
content: Did you have a load on the output of the prox sensor? If not put one on it and that will clean it up quite a bit. Look at that output voltage / current capability of the sensor, and load it accordingly. Also, the comparator as mentioned above will square it up nicely, or a Schmitt trigger inverter / buffer made from discrete components will do so, as well.

author: Zero999
author_rank: Super Contributor
timestamp: December 10, 2019, 09:37:58 pm
content: Two Schmitt trigger inverters in series will make a non-inverting Schmitt trigger buffer. A common IC which contains six inverting buffers is the 74HC14.https://assets.nexperia.com/documents/data-sheet/74HC_HCT14.pdfIf you want to save space/cost, it comes in triple and dual surface mount packages: the 74HC3G14 and 74HC2G14https://assets.nexperia.com/documents/data-sheet/74HC_HCT3G14.pdfhttps://assets.nexperia.com/documents/data-sheet/74HC_HCT2G14.pdf

author: ryanmills
author_rank: Unknown
timestamp: December 10, 2019, 09:56:00 pm
content: Quote from: GerryR on December 10, 2019, 09:16:26 pmI just threw a 1k to test, did not think about it effecting the output. When you want to drive into something like LM311 is there a more correct way to add the load?

author: GerryR
author_rank: Frequent Contributor
timestamp: December 10, 2019, 10:51:11 pm
content: What is the part number of the prox sensor; I can tell better what to do if I know the specs. I have not had to do any "signal conditioning" on the output of prox sensors and am wondering how you set it up. The exponential "decay" shown in your picture would seem to indicate that it isn't loaded properly. Just guessing, of course, without the specs.

author: ryanmills
author_rank: Unknown
timestamp: December 10, 2019, 11:17:41 pm
content: Quote from: GerryR on December 10, 2019, 10:51:11 pmPart number and link to the datasheet was in the first post. Perhaps im not loading it correctly. I just put the 1k in for a load. Should I be doing it another way?

author: langwadt
author_rank: Super Contributor
timestamp: December 10, 2019, 11:36:52 pm
content: Quote from: ryanmills on December 10, 2019, 11:17:41 pmsince it is npn the 1k must be to supply and the rising edge is fine, odd that the falling edge would be slow with an NPN capable of 100mAmaybe make a voltage divider and use the sensor output to short the lower resistor, that should take care of the voltage issue too

author: GerryR
author_rank: Frequent Contributor
timestamp: December 11, 2019, 01:03:55 am
content: 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?

author: ryanmills
author_rank: Unknown
timestamp: December 11, 2019, 06:55:44 pm
content: Quote from: GerryR on December 11, 2019, 01:03:55 amIt'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?

author: Yansi
author_rank: Super Contributor
timestamp: December 11, 2019, 07:00:14 pm
content: Quote from: T3sl4co1l on December 10, 2019, 08:59:44 pmMore than likely to awfully oscillate when presented to a slowly varying input signal.

author: pigrew
author_rank: Frequent Contributor
timestamp: December 11, 2019, 07:17:58 pm
content: 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 suggestingQuote from: ryanmills on December 10, 2019, 08:51:50 pmUse 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.

author: ryanmills
author_rank: Unknown
timestamp: December 11, 2019, 07:51:34 pm
content: Quote from: pigrew on December 11, 2019, 07:17:58 pmExperimented 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?

author: pigrew
author_rank: Frequent Contributor
timestamp: December 11, 2019, 08:41:22 pm
content: Quote from: ryanmills on December 11, 2019, 07:51:34 pmDon'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.

author: GerryR
author_rank: Frequent Contributor
timestamp: December 11, 2019, 08:54:26 pm
content: Quote from: ryanmills on December 11, 2019, 06:55:44 pmProbably 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: