# ways to control resistance by microcontroller

Q. I have a (fuel) gauge that accepts a configurable resistance range (0-500 ohm, two-wire, one to ground), which I want to control with an micro controller (Arduino Pro Mini or Nano with an Atmel 328P).

Some considerations:

• I'm already using SPI for a CAN bus, and am hesitant to add another device on the SPI bus, as the CAN bus is rather mission critical. i.e. don't want to miss CAN messages. Also it requires quite a lot of wires.
• I also already use i2c, adding an i2c device would be straight forward in this application. I mention this because I see i2c digi pots out there.
• Accuracy is somewhat needed (say <10%). I've seen that most digi-pots have an relative accuracy of max ~20%. However, they continue to explain that the relative accuracy is low, but I'm not sure what that means.. this question probably merrits its own topic though, so forget that for now unless it's a one sentence answer.
• Ideally the components required are through-hole/DIP, but this is not a hard requirement.

I think the gauge is designed to be controlled by a rheostat, as it requires a linear resistance range, correct me if my assumption is wrong).

What would be my options? Could it somehow be done using the Arduino's analog output? When researching, I've come across Digi-Pots, DACs and JFETs. But the application was never the same as what I'm doing, often using PWM and more geared towards voltages then to resistance...

Update; Some more details:

• Fuel gauge has two wires, a positive and a negative. The positive wire sends out a 5V, 17-19mA current (for those who want to know, it's a Speedhut gauge).
• Arduino is 5V and, depending on which analog PWM pin you use, outputs a PWM signal at either 490Hz or 977Hz. The duty cycle is controllable in the code between 0-100%.
• The application is automotive. The vehicle is 24V. The gauges are 12V, fed by a small buck controller. The gauge has a microcontroller, and I suspect runs at 5V.

Update; output not as expected:

I'm measuring a constant ~19kOhm on the circuit output (multi meter between "output"-wire and ground). I would expect it to vary as the PWM duty cycle changes.

• I've not hooked the fuel gauge up yet, as first I want to make sure the circuit is OK. I have hooked up my multi meter instead. Maybe it simply doesn't work like that, perhaps the multi meter current output is too weak?

• PWM output is correct according to my multi meter; it gives me a nice 490Hz 0-100% duty cycle.

I've attached a photo of my breadboard layout. My phone doesn't have a great camera, so i've added the names of the components. What's not clear on the photo is that both the 10k resistor coming from the LM and pin 3 of the Mosfet are on the same row 10, the tiny 10pF resistor is on rows 10 and 11. I think I've copied the schematic pretty accurate. I did not have some exact resistor values, so I've used close ones, which I think should be good enough to see output.

I'm not just chucking my problem over the fence, but any pointers would be welcome, I'm still only a few months into this stuff. :) Thanks!

*typo: 10pf = 10nf

• "Relative accuracy" is probably better described as "value tolerance" in this case - that is, your output value is within ±20% of the actual value (error as a ratio or percentage of the actual value). Both rheostats and potentiometers can be linear-tapered. Rheostats use only two terminals and function as variable-value resistors. Potentiometers use all three and function as a variable voltage divider. – Chris M. May 31 '17 at 17:55
• an LDR mating with a PWM-driven LED has worked well for me in such situations; they offer far wider voltage+ohm range than logic-level digi-pots, are cheap, and are easy to calibrate/compensate/customize. an "analog optoisolator" – dandavis May 31 '17 at 19:58
• @ChrisM. Still not sure if this an issue then.. I can configure the ohm range on the gauge, and I can define the ohm-range I send out. If the difference in accuracy is constant, I can compensate for it. But I'm not sure if it is. – svenema Jun 7 '17 at 11:15
• @dandavis. LDR mating; that's a very interesting idea which I will definitely look into. Thanks for the idea. – svenema Jun 7 '17 at 11:15
• @svenema There's no fixed accuracy constant due to the mechanical nature by which resistance is changed (moving of a wiper contact along the resistive element). Because there's no way to physically guarantee the exact location of the wiper each time, setting the pot to a given position multiple times won't always yield precisely the same resistance value. – Chris M. Jun 7 '17 at 12:02

If the range is 0-500 ohm for a 12V vehicle, that means that the maximum ever current is 15V/500mA = 0.03A. Most probably it is a constant current source of 10mA and 500 ohm makes 5V.

You could go the hard way and make a variable resistor, but you could also use an open collector opamp and make voltage feedback. You could use your Arduino analog output which as far as I know is actually a PWM output, to control this circuit. In your MCU firmware you can map PWM to %fuel to get better accuracy.

simulate this circuit – Schematic created using CircuitLab

Update: Inputs of LM393 are pretty good at voltages near 0, but its output will saturate to 100-200mV, (max 700mV at the widest temperature range) so you will get less accuracy near zero. If application is automotive you could use Lm2903 wide temp. version or better NCV2903 - automotive version.

Update 2: The || symbol is a short form to represent equivalen parallel resistance.

$$R3 = R1||R2 = \frac{R1.R2}{R1+R2}$$ Each OpAmp has some (small) input bias current. If the resistance on both inputs as seen by the OpAmp is not equal this will produce some voltage error on OpAmp's output. The calculated value should be 5k in this case, but using the nearest standard value 5.1k is acceptable.

One more thing - if the maximum current is 18-19mA it may be too high for LM393/LM2903's output. You can extend output current capability using a small signal MOSFET this way:

simulate this circuit

R5 acts as a pull-up resistor for OpAmp's open collector output. If you use another OpAmp with full output you may omit this resistor but be sure the OpAmp can drive its output down to 1V with respect to ground (-Vss). C2 limits the transistor's AC gain, you could experiment it's value to get maximum stability. R1 is connected to transistor's drain to close the DC feedback loop from the circuit's output. Zener diode D1 (12V) limits the maximum voltage applied on MOSFET's gate. Because M1 inverts the polarity of the signal OA1's inputs should be inverted compared to previous schematic!

• Hi Todor, thanks for the answer. Very sorry that I was not able to answer sooner as something come up. I provide the gauge with exact 12V through a buck converter (it's actually a 24V vehicle, but the gauges are 12V), but it could be that the gauge uses maybe 5V or 3.3V internally. I hooked up the gauges fuel leads to my multi-meter and it reads 18.27mA – svenema Jun 7 '17 at 11:05
• accidentally hit enter.... I hooked up the gauge fuel leads to my multi-meter and it reads 5.07V, 18.27mA. Arduino's analog outputs are indeed PWM. The application is indeed automotive. I've not yet before tangled with OpAmps, so I'm going to inform myself a bit and come back to here asap. – svenema Jun 7 '17 at 11:11
• Great! Accepted your answer, this will work. I'm ordering the Op Amps now :). The Arduino will put out 5V on the analog pin at 490Hz (or 976Hz on another pin). Using 490Hz and a 50% duty cycle, then your given values for R4 and C1 should be perfect. Could you perhaps explain the use of R3 (I'm not getting the || symbol here) and R1 and R2? I'm powering the OpAmp Vcc with 24V (it's a 24V vehicle, and this is within spec of the LM2903 and NCV2903). Are there any hidden caveats I should be aware of? – svenema Jun 7 '17 at 12:15
• @svenema see updated post. – Todor Simeonov Jun 7 '17 at 18:44
• I'm almost there understanding the circuit fully.. i think. But not all of it yet. The one thing that is most confusing to me now is current flow, maybe I've got some fundamental idea's mixed up.. The gauge outputs 5V, 18mA, to sense the resistance? (i think/assume). If this flows into the Op Amp output, then it leaves via the ground output? And a slight point of worry, related to current flow: The 24V input on the Op Amp will not flow into the gauge and destroy it? -- I've been reading for hours on end on Op Amps, but there so many types and scenario's... sorry for my ignorance ;-). – svenema Jun 8 '17 at 9:10