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:
schematic
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.
Reading Analogue Sensors With One GPIO Pin - picture
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?
2 Answers 2
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:
schematic
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
-
\$\begingroup\$ 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. \$\endgroup\$dannyf– dannyf2017年02月01日 21:50:39 +00:00Commented Feb 1, 2017 at 21:50
-
\$\begingroup\$ 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. \$\endgroup\$dannyf– dannyf2017年02月01日 21:51:05 +00:00Commented Feb 1, 2017 at 21:51
-
\$\begingroup\$ 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) = ... \$\endgroup\$dannyf– dannyf2017年02月01日 21:53:26 +00:00Commented Feb 1, 2017 at 21:53
-
\$\begingroup\$ @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? \$\endgroup\$glen_geek– glen_geek2017年02月01日 22:31:33 +00:00Commented Feb 1, 2017 at 22:31
-
\$\begingroup\$ 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. \$\endgroup\$user51704– user517042017年02月02日 13:53:40 +00:00Commented 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.
Turn the gpio as input. This allows the capacitor to be charged up.
Count the time until the gpio goes high.
The time measurement is a function of the current.
The oldest trick there is for MCU to read analog with an ADC.
-
\$\begingroup\$ 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? \$\endgroup\$user51704– user517042017年02月01日 02:22:04 +00:00Commented Feb 1, 2017 at 2:22
-
\$\begingroup\$ 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. \$\endgroup\$dannyf– dannyf2017年02月01日 20:34:42 +00:00Commented Feb 1, 2017 at 20:34