# Using digital input pin to read variable resistance from sensor requiring supply voltage

I intend to use air contaminants sensor TGS 2602. The internal resistance changes if a given gas is detected. It requires an input voltage V_C = 5V to operate.

According to the specs, the basic measurement circuit is: simulate this circuit – Schematic created using CircuitLab

The problem I am facing is I want to read the measurements with digital IO pins, specifically I want to use Raspberry Pi for this. I found a simple solution to a similar problem here: Reading Analogue Sensors With One GPIO Pin. Essentially it involves using an RC circuit and measuring time lapsed until "high" voltage occurs on our digital pin. In this case I would then be inclined to simply switch R_L with a capacitor but by doing this I would quickly cut off the supply voltage for the sensor (it requires V_C). I then thought I should maybe add the capacitor in parallel with R_L. But the R_L voltage drop is low, so I would never reach the "high" voltage on the capacitor to be able to read it with my digital pin. Should I perhaps use op-amp to drive the V_RL up? But is it not over-complicating things?

My question is then how should I measure value of R_S with a digital reader?

First, be aware that your sensor does require a 5v +-0.2 supply, both for the heater and sensor. The RPI should have +5v available.
The capacitor discharge method that you propose is one of very few available to RPI to convert an analog signal to a digital representation. RPI GPIO pins require a voltage range between 0 to +3.3v. Data sheet says load resistor minimum is 450 ohms. No maximum, so a capacitor load might be acceptable: simulate this circuit – Schematic created using CircuitLab

This circuit at left might work, if you let the circuit idle with GPIO as output, programming a logic zero forcing R1 to pull down the cell resistor R2 toward ground, until you wish to input a reading. Reading may go quickly enough not to disturb R2 cell resistance too much.

If you are reluctant to disturb cell operation from the prescribed load circuit, the amplifier at right might allow a similar capacitor charging/timing method. A quick run with LTSPICE suggests a timing range between 800 uS for a sensor resistance of 100k ohm, and about one second for sensor resistance of 10k ohm. Cell resistor R4 remains always near 5v.
However, be aware that the GPIO measurement sequence requires a polarity switch: C2 must be discharged by setting GPIO to logic high, then transistor Q1 pulls it down slowly towards logic 0

• the one on the right is a good analog amplifier. I would make three changes to it: 1) I would form that diode from the same transistor as Q1 to minimize Vbe disparities. 2) I would connect R6 to Q1's base to introduce some negative feedback. I would use a bigger value as well; 3) I would insert a smaller resistor, of the same / similar value as R5. Feb 1, 2017 at 21:50
• The circuit works because the diode provides the needed bias to lift Q1's base so it can amplify a small signal. As such, it is sensitive to difference between that diode and Q1. All I was trying to do is to design into more safe guard to minimize such differences. Feb 1, 2017 at 21:51
• To the OP: you should be careful as to the selection of R5: its value is determined by the lowest possible value of the sensor, supply voltage and the minimum voltage the sensor needs to work. with a minimum resistance of 10k, 5v rail, and 0.2v of headroom, R5's maximum value is 0.2 / (5v / 10k) = ... Feb 1, 2017 at 21:53
• @dannyf The amplifier at right gives timing results that depend on transistor HFE - MCU software calibration is suggested. Your suggestion of making D2 a similar transistor is good. Gain may be a bit higher than necessary as is. Reduce R5 perhaps? Feb 1, 2017 at 22:31
• glen_geek - I have 2 questions if I may. 1) Why don't we need to add any resistors on C2's way to ground? Won't it discharge immediately? This is a problem I have. Following @dannyf's remark I used the same transistor as Q1 instead of the diode (although I used bc548 instead of 2N2222). I can use larger capacitor to overcome this. With 10 uF it takes ~0.3 s to discharge to low at R2 = 100k and ~0.003 s at R2 = 10k. 2) Why use Vdd = 3.3 V at all? Can't I just connect it to ground? I need to use GPIO in 'high' mode to charge the capacitor anyway.
– user51704
Feb 2, 2017 at 13:53

Fairly easy. Your sensor is more like a CCS whose output current varies with the gas it detects.

So use the CCS to charge up a capacitor, and the time it takes the gpio goes high varies with the CCS current, thus it's resistsnce.

The procedure goes like thi, assuming the gpio is connected to the capacitor and your sensor.

1.output a low on the gpio and turn it as output. wait a little bit. This discharges the capacitor.

1. Turn the gpio as input. This allows the capacitor to be charged up.

2. Count the time until the gpio goes high.

3. The time measurement is a function of the current.

The oldest trick there is for MCU to read analog with an ADC.

• Yes, but I see a possible problem here. If I place the capacitor in series, then when it charges up, the sensor will be switched off almost immediatelly (it requires V_C = 5V +- 0.2V). If, on the other hand, I put it in parallel with R_L, the voltage on the capacitor will never be high enough to reach "high" on the GPIO. Or am I misunderstanding something?
– user51704
Feb 1, 2017 at 2:22
• that would be a problem. But the same principle can be applied to (analog) comparators. you can compare the voltage across the capacitor vs. a reference voltage - either externally set or internally generated. some mcus can provide voltage references down to 200mv or even less. Again, the same principle but different hardware. Feb 1, 2017 at 20:34