8
\$\begingroup\$

I'm trying to count pulses/sec. on a microcontroller pin in the ~5 to 100Hz range. The µC can operate at 5V input, so I have to get the voltage level down safely.

A simple resistor comes to mind, yet that leaves any surges open directly to the µC pin - meh.

I've come across this answer, but the question remains if that circuit is capable of "fast" 100Hz changes.

Is there a proven, reliable way (by means of an IC maybe?) of contacting 5V or 3.3V pins to "dirty" 12V inputs? I have the 12V and 5V available to drive any "ready made" IC.

\$\endgroup\$
  • 2
    \$\begingroup\$ resistive divider + zener/clamping diodes? \$\endgroup\$ – Wesley Lee Jul 24 '17 at 13:55
  • 2
    \$\begingroup\$ Is this really a question that cannot be answered by a simple Google search? \$\endgroup\$ – Ale..chenski Jul 24 '17 at 14:14
  • 16
    \$\begingroup\$ It can be answered, yet I would very much like a quality answer before destroying my equipment through my own stupidity. Let's settle on "peace of mind"? \$\endgroup\$ – Christian Jul 24 '17 at 14:16
  • 8
    \$\begingroup\$ @AliChen stackexchange aims to be a canonical repository of questions and answers. Even simple questions can be good if they collect useful answers. \$\endgroup\$ – Wayne Conrad Jul 24 '17 at 15:57
  • 2
    \$\begingroup\$ 100Hz is not fast. \$\endgroup\$ – immibis Jul 25 '17 at 1:46

10 Answers 10

13
\$\begingroup\$

Use a circuit like this:

schematic

simulate this circuit – Schematic created using CircuitLab

R1 and R2 determine the voltage range, and perform the initial division. These resistors must be capable of some power. Typical is MELF 0.4W. All other can be chip resistors/capacitor.

R3 prevents any surges to cause harm to the schmitt trigger. R4 an R5 are optional to prevent any floating signals.
However, the combination R3/R4 can also be used to adjust the threshold, if necessary.

C1 and C2 determine the maximum speed. Combination R3/C2 can filter slow. C1 filters transients.

A separate schmitt trigger is used since you can get them really small and cheap. And it prevents routing a weak signal over long traces. Whilst also being a sacrificial part on major surges.

I've designed this circuit based on what I have seen inside PLC's. Above circuit is for 24V. Adjust resistors to match 12V according to IEC61131-2.

iec 61131-2
The concept of the standard is to ensure the input has to sink a minimum amount of current before considering it a '1'. The three types specify how much, and are applied based on environmental noise. This prevents glitches from touching it or nearby relays. The drawback is that R1/2 have to be of decent power rating and low resistance.

\$\endgroup\$
  • \$\begingroup\$ Wow. A very thorough answer to what initially seemed a simple question. Thank you. \$\endgroup\$ – Christian Jul 24 '17 at 16:55
  • \$\begingroup\$ I'm really curious about R4 and R5 - when will they ever do anything useful? R2+R3 > R4 anyway. Is it in case any of the "heavy duty" components break down? \$\endgroup\$ – pipe Jul 25 '17 at 9:28
  • \$\begingroup\$ @pipe R3 and R4 can help to configure the threshold, whilst providing a high impedance path to the logic. R5 is superfluous most of the time, but in the design this was used the mcu pull-down could not be used. If for some reason the the buffer would fail, the mcu input would not read 50hz hum. (Note: reliable was requested) \$\endgroup\$ – Jeroen3 Jul 25 '17 at 10:03
17
\$\begingroup\$

I would try a resistor divider solution like shown below.

enter image description here

Select the resistor ratio so that the divided voltage is at the proper level for the MCU when the input is at its nominal voltage. The zener diode voltage is selected to clamp the MCU input when the input goes above the max input. The zener will also protect the MCU if the input happens to go negative.

This solution will work great for the relatively low frequency range that you have specified.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why choose zener to be 4v7? Would 5v2 (5v1?) be better solution? \$\endgroup\$ – R.Joshi Jul 24 '17 at 14:23
  • 7
    \$\begingroup\$ @R.Joshi: If the 5V microcontroller is powered from a 10% tolerant supply (4.5-5.5V VDD), then applying 5.2V on the pin could be more than the typical VDD+0.3V absolute max. Logic high is recognized at 2V for TTL and 2/3*VDD for CMOS, so no problems with a 4V7 zener there. \$\endgroup\$ – Hans Jul 24 '17 at 15:09
  • 2
    \$\begingroup\$ I truly wish I could mark two answers as "chosen". Your way is the every day way-to-go, yet Jeroen's answer is just a tad bit more in-depth. Thank you, though, for taking the time to answer. \$\endgroup\$ – Christian Jul 26 '17 at 3:19
8
\$\begingroup\$

I would use a resistor divider and then protect the uC with a 5.1v Zener

If you put the zener between the pin and ground in parallel with, say, a 10k pull down resistor, then feed your voltage divided signal in then... zener is more than fast enough, and cheap / easy.

I often do this and divide the signal before the zener bit with a pot.

Other option is as linked, if your really worried an opto could be used, if its not a safety issue I would go with the above or have the pin normally high from 5V Vcc and pull it low with a fet (off top of my head 2N7000 should work) - but its less simple than the zener option.

\$\endgroup\$
7
\$\begingroup\$

If the signal levels are GND and 12V (or >5V), the most simple and 100% safe way is this:

schematic

simulate this circuit – Schematic created using CircuitLab

If it really serves your purpose depends on the actual impedance of the 12V signal (should be way below R1) and what you mean by "dirty".

Also, as @MichaelKaras correctly points out, the low level on the µC's input may be shifted up to the low level of the 12V signal plus Vf of the diode (up to about 0.7V). You should check if this is a problem in your case or not. If it is, you can still try and use a Schottky diode with a Vf of about 0.35V.

\$\endgroup\$
  • \$\begingroup\$ On most µCs, one could even drop the resistor and activate the internal pullup for that pin. \$\endgroup\$ – Janka Jul 25 '17 at 10:04
  • \$\begingroup\$ The proposed diode solution here is not always the best or optimal solution. The low level input voltage presented to the MCU is going to be above GND by one diode forward voltage drop plus what ever low level output voltage that is creating the 0 to 12V signal. This can be particularly an issue where the signals may carry noise and the MCU input is specified with TTL type voltage levels for V<sub>IL</sub>. Often this spec may be only 0.8V. So if this solution is used beware and at least specificy a low forward voltage drop diode like a BAT54. \$\endgroup\$ – Michael Karas Jul 25 '17 at 17:21
  • \$\begingroup\$ @MichaelKaras You're right about the shift of the low level by the diode's Vf; this needs to be accounted for. To me, for a 5V µC, V[IL] of 0.8V seems exceptionally low. I seem to generally find 0.3Vcc (~1.5V) spec'd. \$\endgroup\$ – JimmyB Jul 26 '17 at 11:46
  • \$\begingroup\$ If your MCU has a CMOS type spec for V<sub>IL</sub> then maybe it works OK. I still like to design in ways that would work even if the spec was way lower than that just to gain as much operating margin as possible. Even the difference of specifying a low drop diode is a good step toward that if you choose to use this type of circuit. Your circuit is not particularly good for cases where there may be negative excursions on the 12V input. \$\endgroup\$ – Michael Karas Jul 26 '17 at 12:23
  • \$\begingroup\$ I agree. Designing for margin is a good thing. And negative voltages on the 12V signal may indeed wreak havok on the circuit. \$\endgroup\$ – JimmyB Jul 26 '17 at 12:28
5
\$\begingroup\$

I'd use an opto-isolator, 100Hz is easily within the range of any decent one. 4n25 springs to mind as a common part number, and I know that's capable of much better than 100Hz.

\$\endgroup\$
  • \$\begingroup\$ The problem with using an opto isolator to solve this problem is that it assumes you can draw current from the 12V signal. You could buffer the 12V signal but that would then require an extra supply. \$\endgroup\$ – Jason Morgan Jul 25 '17 at 9:20
  • \$\begingroup\$ I'm sure you can get an opto that would be effective at almost the same current as a micro input, from 12v it's not going to put much load on to light a small LED. \$\endgroup\$ – John U Jul 25 '17 at 12:29
  • 1
    \$\begingroup\$ A digital input leakage is typically between 10nA and 1uA (temperature and process dependent). I've never come across an opto coupler that works at even 1uA. A typical opto coupler, marketed as low power, e.g. Broadcom ACPL-x6xL needs 1.6mA. That's between 1600 and 160000 times as much current. But then, as I state in my answer, it depends on the requirements what will work so I am not dismissing an opto solution. \$\endgroup\$ – Jason Morgan Jul 26 '17 at 13:08
  • \$\begingroup\$ xkcd.com/386 \$\endgroup\$ – John U Jul 26 '17 at 14:32
2
\$\begingroup\$

The method selected partially depends on what the input signal does, how it behaves and how that might effect the input circuit and the code that reads it?

e.g. Is it always 12V? Does it have spikes or noise? How much current can it drive? Can current be driven into it? Will taking current from it affect anything else? Is it safety critical?....

Because of this there can never be a universal answer to this question as the 'correct' solution depends on what the rest of the system does. The chosen solution that meets the requirements will have differing cost and complexity.

That said, as nobody else has yet suggested it, I'll go for a FET input.

A JFET or MOSFET can be used and either could be common source or common drain modes. For example, common drain:

schematic

simulate this circuit – Schematic created using CircuitLab

The advantage of the common drain mode is that it allows the input to be connected to both an analogue (e.g. ADC) or a digital pin. If the signal is truly digital I would enable the schmitt trigger on the CPU input (if it has one), or add an external schmitt buffer to the CPU's input pin.

Advantages

  • Very high input impedance
  • Partially isolated input (can withstand +/- 30V, depending on FET selection)
  • Analogue possible
  • Minimal effect on external signal
\$\endgroup\$
1
\$\begingroup\$

schematic

simulate this circuit – Schematic created using CircuitLab

Figure 1. Opto-isolated interface. Use internal pull-up on GPIO.

An opto-isolator solves several problems.

  • Complete electrical isolation between the 12 V circuit and the 5 V logic.
  • Handles dirty 12 V signal without risk.
  • Simplicity.
\$\endgroup\$
1
\$\begingroup\$

schematic

simulate this circuit – Schematic created using CircuitLab

R1, R2 and C1 forms a voltage divider with a 1kHz low pass filter. Any unwanted high frequency signal travelling on the 12V can be filter away. The calculation for the filter frequency is 1/(2 pi R2 C1). Note: The Base requires at least 0.7V to function properly, take care when adjusting the resistor.

BJT is being use because it's very common compare to mosfet. In the event the 12V is still active but the 5V for your uC is down, the BJT will not pass current into the pin and cause damage.

For uC programming, use a high to low trigger to count your pulse. As this circuit will reverse the pulse.

\$\endgroup\$
-2
\$\begingroup\$

Generally MCU inputs are already protected with clamp diodes, as long as you have a resistor with an optimized value (high enough for the clamps and low enough for the sampling) and have a good bypass capacity between VDD and VSS, you don't have to worry about it. So, just a resistor is well enough.

edit: Thanks to comment of PeterJ, I want to explain it a bit further. The least power the MCU draws (assuming it doesn't sleep), the bypass capacity, the resistor value; when all of these are at the compromising point -which is easily the very general case with only the condition using a resistor about 10kOhm- the sole resistor is fine for the simple application of the OP.

\$\endgroup\$
  • 4
    \$\begingroup\$ One problem I found with this many years ago is that while it might not damage anything Vcc internally can drift up a bit (until the diode conducts) and depending on the microcontroller can throw out things like ADC readings. \$\endgroup\$ – PeterJ Jul 24 '17 at 14:35
  • \$\begingroup\$ @PeterJ It works fine if you can guarantee that at least that much current is taken out of the supply. In the worst case, add a dummy resistor … \$\endgroup\$ – CL. Jul 24 '17 at 16:20
  • \$\begingroup\$ Though 'nasty' I've seen this done on a lot of cheap consumer goods. I repaired an alarm clock once where the mains was fed into a digital input through a 10M resisor as a timing reference. Not unsurprisingly the chip was dead. \$\endgroup\$ – Jason Morgan Jul 25 '17 at 9:58
-5
\$\begingroup\$

You can opt for a LM7805/LM7803 voltage regulator for 5V and 3.3V respectively.I am assuming the uC is isolated from a current demanding load, if any.

\$\endgroup\$
  • 1
    \$\begingroup\$ It is creative. But you will operate out of spec on high speed. If it was all you had lying around, perhaps. \$\endgroup\$ – Jeroen3 Jul 24 '17 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.