Author Topic: Converting a messy square wave to digital?  (Read 1758 times)

0 Members and 1 Guest are viewing this topic.

Offline ryanmillsTopic starter

  • Supporter
  • ****
  • Posts: 78
  • Country: us
Converting a messy square wave to digital?
« on: December 10, 2019, 08:51:50 pm »
Sensor: E2B-S08KN04-WP-C2 2M
Datasheet: http://www.ia.omron.com/data_pdf/cat/e2b_d116-e1_1_6_csm1012652.pdf

New 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?

 

Offline T3sl4co1l

  • Super Contributor
  • ***
  • Posts: 22436
  • Country: us
  • Expert, Analog Electronics, PCB Layout, EMC
    • Seven Transistor Labs
Re: Converting a messy square wave to digital?
« Reply #1 on: December 10, 2019, 08:59:44 pm »
Hysteresis comparator. :-+  LM311 for single, say.

Tim
Seven Transistor Labs, LLC
Electronic design, from concept to prototype.
Bringing a project to life?  Send me a message!
 

Offline GerryR

  • Frequent Contributor
  • **
  • Posts: 256
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #2 on: December 10, 2019, 09:16:26 pm »
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.
Still learning; good judgment comes from experience, which comes from bad judgment!!
 
The following users thanked this post: ryanmills

Offline Zero999

  • Super Contributor
  • ***
  • Posts: 20363
  • Country: gb
  • 0999
Re: Converting a messy square wave to digital?
« Reply #3 on: December 10, 2019, 09:37:58 pm »
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.pdf

If you want to save space/cost, it comes in triple and dual surface mount packages: the 74HC3G14 and 74HC2G14
https://assets.nexperia.com/documents/data-sheet/74HC_HCT3G14.pdf
https://assets.nexperia.com/documents/data-sheet/74HC_HCT2G14.pdf
 
The following users thanked this post: ryanmills

Offline ryanmillsTopic starter

  • Supporter
  • ****
  • Posts: 78
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #4 on: December 10, 2019, 09:56:00 pm »
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.

I 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?
 

Offline GerryR

  • Frequent Contributor
  • **
  • Posts: 256
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #5 on: December 10, 2019, 10:51:11 pm »
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.
Still learning; good judgment comes from experience, which comes from bad judgment!!
 

Offline ryanmillsTopic starter

  • Supporter
  • ****
  • Posts: 78
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #6 on: December 10, 2019, 11:17:41 pm »
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.

Part 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?
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4857
  • Country: dk
Re: Converting a messy square wave to digital?
« Reply #7 on: December 10, 2019, 11:36:52 pm »
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.

Part 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?

since 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 100mA

maybe make a voltage divider and use the sensor output to short the lower resistor, that should take care of the voltage issue too


 

Offline GerryR

  • Frequent Contributor
  • **
  • Posts: 256
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #8 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? 
Still learning; good judgment comes from experience, which comes from bad judgment!!
 

Offline ryanmillsTopic starter

  • Supporter
  • ****
  • Posts: 78
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #9 on: December 11, 2019, 06:55:44 pm »
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?

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?
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3930
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Converting a messy square wave to digital?
« Reply #10 on: December 11, 2019, 07:00:14 pm »
Hysteresis comparator. :-+  LM311 for single, say.

Tim

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

Offline pigrew

  • Frequent Contributor
  • **
  • Posts: 680
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #11 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

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?

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.
« Last Edit: December 11, 2019, 07:22:23 pm by pigrew »
 

Offline ryanmillsTopic starter

  • Supporter
  • ****
  • Posts: 78
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #12 on: December 11, 2019, 07:51:34 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

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?

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.

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?
 

Offline pigrew

  • Frequent Contributor
  • **
  • Posts: 680
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #13 on: December 11, 2019, 08:41:22 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?

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.
 

Offline GerryR

  • Frequent Contributor
  • **
  • Posts: 256
  • Country: us
Re: Converting a messy square wave to digital?
« Reply #14 on: December 11, 2019, 08:54:26 pm »
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?

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?

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:
« Last Edit: December 11, 2019, 09:08:24 pm by GerryR »
Still learning; good judgment comes from experience, which comes from bad judgment!!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf