9
\$\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\$
5
  • 2
    \$\begingroup\$ resistive divider + zener/clamping diodes? \$\endgroup\$
    – Wesley Lee
    Jul 24, 2017 at 13:55
  • 2
    \$\begingroup\$ Is this really a question that cannot be answered by a simple Google search? \$\endgroup\$ Jul 24, 2017 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, 2017 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\$ Jul 24, 2017 at 15:57
  • 2
    \$\begingroup\$ 100Hz is not fast. \$\endgroup\$
    – user253751
    Jul 25, 2017 at 1:46

9 Answers 9

15
\$\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\$
3
  • \$\begingroup\$ Wow. A very thorough answer to what initially seemed a simple question. Thank you. \$\endgroup\$
    – Christian
    Jul 24, 2017 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, 2017 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, 2017 at 10:03
18
\$\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\$
3
  • 1
    \$\begingroup\$ Why choose zener to be 4v7? Would 5v2 (5v1?) be better solution? \$\endgroup\$
    – R.Joshi
    Jul 24, 2017 at 14:23
  • 8
    \$\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, 2017 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, 2017 at 3:19
9
\$\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\$
5
  • \$\begingroup\$ On most µCs, one could even drop the resistor and activate the internal pullup for that pin. \$\endgroup\$
    – Janka
    Jul 25, 2017 at 10:04
  • 1
    \$\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\$ Jul 25, 2017 at 17:21
  • 1
    \$\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, 2017 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\$ Jul 26, 2017 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, 2017 at 12:28
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\$
0
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\$
4
  • \$\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\$
    – Jay M
    Jul 25, 2017 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, 2017 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\$
    – Jay M
    Jul 26, 2017 at 13:08
  • \$\begingroup\$ xkcd.com/386 \$\endgroup\$
    – John U
    Jul 26, 2017 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\$
-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
  • 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, 2017 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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