11"""Read the MICS6814 via an ads1015 ADC"""
22
3+ import time
34import atexit
45import ads1015
56import RPi .GPIO as GPIO
67
78MICS6814_HEATER_PIN = 24
8-
9+ MICS6814_GAIN = 6.144
910
1011ads1015 .I2C_ADDRESS_DEFAULT = ads1015 .I2C_ADDRESS_ALTERNATE
1112_is_setup = False
13+ _adc_enabled = False
14+ _adc_gain = 6.148
1215
1316
1417class Mics6814Reading (object ):
15- __slots__ = 'oxidising' , 'reducing' , 'nh3'
18+ __slots__ = 'oxidising' , 'reducing' , 'nh3' , 'adc'
1619
17- def __init__ (self , ox , red , nh3 ):
20+ def __init__ (self , ox , red , nh3 , adc = None ):
1821 self .oxidising = ox
1922 self .reducing = red
2023 self .nh3 = nh3
24+ self .adc = adc
2125
2226 def __repr__ (self ):
23- return """Oxidising: {:05.02f} Ohms
24- Reducing: {:05.02f} Ohms
25- NH3: {:05.02f} Ohms
26- """ .format (self .oxidising , self .reducing , self .nh3 )
27+ fmt = """Oxidising: {ox:05.02f} Ohms
28+ Reducing: {red:05.02f} Ohms
29+ NH3: {nh3:05.02f} Ohms"""
30+ if self .adc is not None :
31+ fmt += """
32+ ADC: {adc:05.02f} Volts
33+ """
34+ return fmt .format (
35+ ox = self .oxidising ,
36+ red = self .reducing ,
37+ nh3 = self .nh3 ,
38+ adc = self .adc )
2739
2840 __str__ = __repr__
2941
@@ -36,7 +48,7 @@ def setup():
3648
3749 adc = ads1015 .ADS1015 (i2c_addr = 0x49 )
3850 adc .set_mode ('single' )
39- adc .set_programmable_gain (6.148 )
51+ adc .set_programmable_gain (MICS6814_GAIN )
4052 adc .set_sample_rate (1600 )
4153
4254 GPIO .setwarnings (False )
@@ -46,6 +58,18 @@ def setup():
4658 atexit .register (cleanup )
4759
4860
61+ def enable_adc (value = True ):
62+ """Enable reading from the additional ADC pin."""
63+ global _adc_enabled
64+ _adc_enabled = value
65+
66+
67+ def set_adc_gain (value ):
68+ """Set gain value for the additional ADC pin."""
69+ global _adc_gain
70+ _adc_gain = value
71+
72+
4973def cleanup ():
5074 GPIO .output (MICS6814_HEATER_PIN , 0 )
5175
@@ -57,11 +81,33 @@ def read_all():
5781 red = adc .get_voltage ('in1/gnd' )
5882 nh3 = adc .get_voltage ('in2/gnd' )
5983
60- ox = (ox * 56000 ) / (3.3 - ox )
61- red = (red * 56000 ) / (3.3 - red )
62- nh3 = (nh3 * 56000 ) / (3.3 - nh3 )
84+ try :
85+ ox = (ox * 56000 ) / (3.3 - ox )
86+ except ZeroDivisionError :
87+ ox = 0
88+
89+ try :
90+ red = (red * 56000 ) / (3.3 - red )
91+ except ZeroDivisionError :
92+ red = 0
6393
64- return Mics6814Reading (ox , red , nh3 )
94+ try :
95+ nh3 = (nh3 * 56000 ) / (3.3 - nh3 )
96+ except ZeroDivisionError :
97+ nh3 = 0
98+
99+ analog = None
100+
101+ if _adc_enabled :
102+ if _adc_gain == MICS6814_GAIN :
103+ analog = adc .get_voltage ('ref/gnd' )
104+ else :
105+ adc .set_programmable_gain (_adc_gain )
106+ time .sleep (0.05 )
107+ analog = adc .get_voltage ('ref/gnd' )
108+ adc .set_programmable_gain (MICS6814_GAIN )
109+
110+ return Mics6814Reading (ox , red , nh3 , analog )
65111
66112
67113def read_oxidising ():
@@ -86,3 +132,9 @@ def read_nh3():
86132 """Return gas resistance for nh3/ammonia"""
87133 setup ()
88134 return read_all ().nh3
135+
136+
137+ def read_adc ():
138+ """Return spare ADC channel value"""
139+ setup ()
140+ return read_all ().adc
0 commit comments