1
\$\begingroup\$

I have a 12V pulse square signal coming from an RPM sensor. I would like to translate it into 5V pulses since I will need to use it as input in an Arduino. What do you guys think is the easiest way? Can a comparator be used?

\$\endgroup\$
1
  • \$\begingroup\$ Is the input going to an ADC pin or a digital pin? \$\endgroup\$
    – Reinderien
    Commented May 11, 2021 at 18:01

4 Answers 4

5
\$\begingroup\$

Several methods:

  • voltage divider (I would suggest 10k/5k, for a 4V swing. This allows for a high margin up to 15V from the sensor.)
  • resistor and zener diode
  • drive an n-channel MOSFET gate, pull up the output to 5V
  • use that same MOSFET as a down-shift level translator
  • use an optoisolator

The method you choose depends on how much electrical noise your environment has. If it’s a car or motorcycle engine, this might be a lot.

There are combo solutions such as using a voltage divider and a zener and a transistor or FET to condition the signal. That said, the optoisolator is the most robust solution as it breaks up any potential current loop between the sensor and your Arduino. It’s also more expensive, but much less than the cost of your fried board.

There's an additional benefit to using an optoisolator: by referring the diode input to the sensor ground, it will reject common-mode noise that might find its way onto the sensor wires.

More here: Detect car +12V using Arduino

And a Littelfuse appnote about automotive noise

MORE: Here's a simulation of common-mode noise with the optoisolator (try it here). I added a Zener to protect the opto LED, it's optional.

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ I used an optoisolator, is the best choice, fast and reliable. Thanks for your help! \$\endgroup\$
    – Enzo
    Commented May 14, 2021 at 21:58
3
\$\begingroup\$

You don't need a comparator. Since you're making the voltage smaller there is no need to use an amplfier or comparator.

What I would use is one of these:

schematic

Note how the resistor divider circuit on the left doesn't output 5 V exactly (if the 12 V is 12 V then the output will be 4.8 V) but that doesn't matter. An Arduino running on 5 V will consider a voltage below 2.5 V a zero and a voltage above 2.5 V a one.

To make a more precise 5.0 V from 12.0 V you'd need a 14 k (instead of 15 k as I suggest) resistor. A 14 k resistor migth be hard to find, 15 k ohm is more common.

The same is true for the circuit on the right, a 5 V zener diode could be harder to find than a 4.8 V zener. But again, that will work perfectly fine.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nope. The ATMega328 V(ih) and V(il) are 0.7*Vcc (3.5V) and 0.3*Vcc (1.5V), respectively. Not 2.5V. \$\endgroup\$ Commented May 11, 2021 at 18:09
  • \$\begingroup\$ @hacktastical Those are the guaranteed decision levels. In the factory the decision level is measured and ICs that have a level that is outside these boundries will be discarded. In practice all CMOS inputs on average have a decision level of Vdd/2. But feel free to prove me wrong by measuring this on an actual chip and showing me the results that show I'm wrong. In 25 years of dealing with CMOS circuits I still have to find an IC that doesn't have a Vdd/2 decision level (unless it is designed explicitly to not have that like a Schmitt-Trigger input). \$\endgroup\$ Commented May 11, 2021 at 20:21
  • 1
    \$\begingroup\$ If it isn't in the datasheet, you cannot count on it. Some further advice: if you want to play the seniority game with me, you will lose, because (1) I have a couple of decades on you, and (2) 'appeal to authority' arguments are fallacies. Seriously, don't go there. \$\endgroup\$ Commented May 11, 2021 at 20:57
3
\$\begingroup\$

There is an analog comparator on the ATmega328p chip, however there is no way of adding hysteresis, unfortunately.

A bulletproof way would be to take the 12V input, apply some signal conditioning, and then connect it to a comparator (eg. LM393) with some hysteresis or a Schmitt trigger gate (rather less predictable hysteresis but a bit simpler).

On the other hand, maybe a simple voltage divider would work for you. Might be reliable.

If you want to go the comparator route, something like this:

enter image description here

The blue trace represents the input 0/12V with some noise superimposed (a sine wave just for illustrative purposes). The red trace is the input divided down and filtered by C1/R3/R5.

R1/R2/R6 set the threshold at 2.5V nominal with about 450mV of hysteresis (so the thresholds are about 2.73V (rising) and 2.28V (falling).

R7 is a pullup which is required because the LM393 is open-collector.

This is only a few pennies worth of components and is pretty much bulletproof so it would be good for a volume product where failures are not to be tolerated.

\$\endgroup\$
1
  • \$\begingroup\$ there is no way of adding hysteresis [to the ATmega328 comparator] - there's no easy way, since ACO is not exposed externally. You could hack up a software solution to send ACO to a pin, and then you'd be able to have hysteresis, with a little bit of latency. \$\endgroup\$
    – Reinderien
    Commented May 11, 2021 at 18:09
1
\$\begingroup\$

Voltage divider, maybe a 14k over 10k?

\$\endgroup\$

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