Maker Pro
Maker Pro

Square wave to DC level

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi all
I have a 0 to 7V square wave signal. Frequency is 4KHz.
I want to convert this into an 0 to 3V signal to be read into PIC A/D or Comparator.
My initial solution was to use a voltage divider to reduce to 3V, This still leaves me with a 3V square wave. Can i use a low pass filter then to a opamp voltage follower to the PIC micro.
If this seems ok, what are the RC values condiderations? Should they be more than the 4KHz. Im not sure how this would work as i do not fully understand the effect of Low pass filter to a square wave and how to select the values to get a DC value.
Thanks in advance.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,743
Joined
Nov 17, 2011
Messages
13,743
If you're only interested in the amplitude, not frequency or duty cycle, you can also use a peak detector circuit.
When using a low pass filter you can easily recover the DC component of the signal as shown in the link for pwm. Note, however, that the DC component of a square wave depends from two parameters: amplitude and duty cycle. If the duty cycle of your signal can vary, so can the DC content, even if the amplitude stays the same.
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
The frequency will stay the same at 4KHz. The duty cycle is 50% on off. I will have a loo at the peak detector too. Thanks for the help.
 

ayalara

Mar 5, 2010
5
Joined
Mar 5, 2010
Messages
5
The frequency will stay the same at 4KHz. The duty cycle is 50% on off. I will have a loo at the peak detector too. Thanks for the help.
A PIC with internal A/D converter is plenty fast enough to implement the peak detector in software. No need to add external hardware.
 

Frankchie

Nov 14, 2017
149
Joined
Nov 14, 2017
Messages
149
I'm not sure that I follow your question. Why do you apparently want to to convert a square wave to some kind of analog signal? What does the square wave signal represent?
 

AnalogKid

Jun 10, 2015
2,895
Joined
Jun 10, 2015
Messages
2,895
A lowpass filter will turn the signal into 1.5 V DC. A peak detector will turn the signal into a constant 3 V DC.

If the amplitude doesn't change (always 3 V peak-to-peak) and the frequency doesn't change (always 4 kHz) and the duty cycle doesn't change (always 50/50), then what are you measuring? And why?

ak
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi,
Yes it is a 7V square wave signal, 50-50 mark to space and 4kHz.
I need to reduce this to around 3V (for A/D or comparator of PIC micro) - I was thinking potential divider, then a RC filter (as explained above).
Would I need a voltage follower after the RC filter?

It is feedback from a piezo, which I need to measure and check periodically that the signal is still there.

Thanks in advance.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,743
Joined
Nov 17, 2011
Messages
13,743
If you only need to check for the presence of the signal, use it as input to a retriggerable monostable multivibrator with a period slightly longer than the period of the input signal. As long as the signal is present, the mu+onostable will be triggered and the output will be active. If the signal is lost, the monostable will no longer be triggered and the output will become inactive.
This is known as missing pulse detector.
A 555 as used in the linked circuit is not capable of 3 V operation. It will require at least 5 V. The output of the missing pulse detector therefore needs to be reduced in amplitude by e.g. a resistive divider. Connect the output of the divider to an input of your microcontroller (no adc required).
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Thanks for the info. Much appreciated.
I wanted the cheapest way to do this as cost might be an issue also PCB space is a problem. Hence the 7V signal through a resistive voltage divider, then RC low pass filter and into the PIC ADC or PIC comparator input.
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi Harald,
Many thanks for the schematic and waveform simulations. This is basically working as required i.e. when there is no pulse the capacitor charges via R2 and once it crosses the voltage set by R3/R4 it will be a high at the output of U1. Otherwise the output stays low because the capacitor is charged/discharged because the transistor is being switched on and off and never crosses the threshold set by R3/R4.
To tell you the truth I think it doesn't really matter which way the circuit works as long as there is a definite way to check for a pulse and no pulse. If I wanted to reverse this, I think I could simply have the R3/R4 junction to the non-inverting input and the R2 /C1 junction at the inverting input. So I think then when there is a pulse the output would be high and no pulse it will be a low.

Many thanks for taking the time to help me out. Much appreciated.
Many thanks for everyone else who replied and helped.
Thanks.
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi all,
I have an additional complication with the circuit. The 7V pulse signal is when everything is working correctly. However if I get a fault with the circuit, this drips down to a 2V signal (4KHz frequency equal mark to space ratio).
I just wanted to ask if the above circuit by Harald would be ok to detect 2V i.e. V1 would still be 3.3V, V2 would be 2V (pulse). Will I get a different output at 7V and 2v or the same? I think it will be the same. Can this be modified?

I need a clear way of detecting between 7V and 2V pulses.

I have the following ideas:
1. If I use a potential divider such that the voltage is reduced to less than half for each case i.e. 3V and 1V.
Can I use a PIC comparator (set the internal voltage to 2.5V) and then take the output of the voltage divider to the comparator pin? This way I think only the 3V pulses will be recognised as a '1' and anything below will be a '0'. Does this seem plausible?

2. If I use a peak detector circuit (I have attached some simulations) with 7V and 2V pulses. Could I feed these into a comparator such that only the 7V triggers the comparator output to a '1' i.e. have the negative input of the comparator set to 3V using a potential divider circuit. Hence the 2V pulse will not be detected?

3. Finally if I use a low pass RC filter (which I believe will give half of the voltage output i.e. mean of the pulse wave input, then feed this into a comparator circuit as (2 above) )?. Does my selection of RC need to be greater than the frequency of 4kHz?
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Here are pdfs of my simulations for peak detector circuits
Any help would be greatly appreciated
 

Attachments

  • peak detector 1.pdf
    13.4 KB · Views: 49
  • peak detector 2.pdf
    13.1 KB · Views: 41

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi all
I have a peak detector. This is used to detect 7V pulses of 4KHz equal duty cycle.
I have used a voltage divider to reduce this to around 2V. Then the ouput of the voltage divider is fed to s BAT85 diode, which then connects to a 220nF and 43KHz resistor. The other end of the resistor and cap go to 0V. The reason for the RC selection is that i want to check on edge of the pulses within 15ms.
Would i ve able to connect this straight to a PIC A/D. Or would it be better to go through a voltage follower circuit.

Thanks in advance.
 

Alec_t

Jul 7, 2015
3,591
Joined
Jul 7, 2015
Messages
3,591
The circuit is unclear (to me, anyway). Can you post a schematic showing the divider and following components?
 

Frankchie

Nov 14, 2017
149
Joined
Nov 14, 2017
Messages
149
I am confused, if all you want to do is detect the presence or absence of a 4 khz square wave signal the PIC can do that without any additional circuitry. Except maybe for a simple needed resistor voltage divider to adjust voltage levels to that acceptable by the PIC.

All you have to do is program the PIC to detect a transition from a "0" to "1" on any of it's input pins. You don't even need the PIC's A/D capability.

Again, no peak detectors, no filters, no retriggerable circuits, just the PIC basic capability to detect a "0" or "1".

The only caveat may be if the PIC is too busy doing other things and may miss a transition, but even then some clever programming techniques can overcome this problem.

My above comments should be obvious to many on this thread, so I apologize if I missed something. If I did miss something somebody please tell me why I am confused.
 
Top