class MQ131: #Sensor datasheet has a graphical representation of ppm of gas by Rs/R0. By obtaining this ratio and using the graphical funcion one can obtain the gas density in ppm #Rs is the resistance of the sensor that changes depending on the concentration of gas #R0 is the resistance of the sensor at a know concentration without the presence of gases (fresh air) #Rs/R0 of fresh air is 1 so R0=Rs/1 #2 points of the gas function are selected (1.2 at 10ppm) and (8 at 1000ppm) #another point is used to find the intersect (6 at 500ppm) READ_SAMPLE_INTERVAL = const(500) READ_SAMPLE_TIMES = const(10) def __init__(self, pin, R0): self.pin = pin self.R0 = R0 self.adc = ADC(bits=12) self.adc.vref_to_pin('P22') self.apin = self.adc.channel(pin=self.pin, attn=ADC.ATTN_11DB) def deinit(self): self.apin.deinit() self.adc.deinit() def MQRead(self): v=float(0) for i in range(READ_SAMPLE_TIMES): v += self.apin() sleep(READ_SAMPLE_INTERVAL/1000) return ((v/READ_SAMPLE_TIMES)*3.3/4096) #3.3v - ATTN_11DB & 4096 - 12bits def MQCalibrate_R0(self, volts): #Rs = (Vc * RL)/VRL - RL if (volts==0): return 0 RS_air = ((5.0*10.0)/volts)-10.0 R0_air = RS_air/1 #from graph, needs to be replaced with real data return R0_air def MQGet_PPB(self, volts): if (volts==0): return 0 Rs = ((5.0*10.0)/volts)-10.0 m=AdvMath.log10(1.2/10)/AdvMath.log10(8/1000) b=AdvMath.log10(6)-(m*AdvMath.log10(500)) if (Rs==0): return 0 return pow(10,(((AdvMath.log10(Rs/self.R0))-b)/m))
#ADC Calibration from pin_map import * from machine import ADC, DAC import time adc = ADC() # Output 1100mV Vref of GPIO25 (DAC1) (P22) adc.vref_to_pin(GPIO25) #DAC1 print("From now on, Vref(1100mV) output on GPIO25 for 10sec.") print("Measure Vref with most accurate Voltmeter you have.") for i in range(3): time.sleep(1) print("elapsed %d[sec] ..." % (i + 1)) # Measure Vref with most accurate Voltmeter # Note down the reading in millivolts, eg. 1113[mV] #measured_val = 1113 #print("measured_val : %d" % measured_val) # Set Calibration #adc.vref(measured_val) #see note above #time.sleep(2) # Reset NodeMCU-32S Module # Check calibration by reading a known voltage adc_c = adc.channel(pin=GPIO39, attn=ADC.ATTN_11DB) # Again Output 1100mV Vref of GPIO25 (DAC1) (P22)
import time import machine import pycom from machine import ADC from dth import DTH print("starting") pycom.heartbeat(False) th = DTH("P23", 1) time.sleep(2) adc = ADC() adc.vref_to_pin("P22") adc.vref(1100) adc_c = adc.channel(pin="P16", attn=ADC.ATTN_11DB) lora = LoRa(mode=LoRa.LORA, region=LoRa.US915) lora.init( tx_power=14, sf=7, frequency=915000000, coding_rate=LoRa.CODING_4_5, bandwidth=LoRa.BW_125KHZ, power_mode=LoRa.TX_ONLY, )
from machine import ADC from math import sqrt from utime import sleep_us adc = ADC() ''' Connects the internal 1.1v to external GPIO. It can only be connected to P22, P21 or P6. It is recommended to only use P6 on the WiPy, on other modules this pin is connected to the radio. ''' adc.vref_to_pin('P21') ''' If called without any arguments, this function returns the current calibrated voltage (in millivolts) of the 1.1v reference. Otherwise it will update the calibrated value (in millivolts) of the internal 1.1v reference. Use the ADC.vref_to_pin(*,pin) method and a voltmeter to find out the actual Vref. ''' adc.vref(1058) apin = adc.channel(pin='G5', attn=ADC.ATTN_11DB) ''' This function measures the ADC pin N times with a sampling period of delay. mean voltage, rms voltage and standard deviation are computed. ''' def measure(N=1000, delay=200): asum = 0 sqsum = 0
# bits = 10 bits = 9 # produce values from [0,512) fmt = '={:0' + str(bits) + 'b}' adc = ADC(bits=bits) vref = { 'lopy4-52e0': 1133, # does adc.vref() actually do anything? I see no effect :-P } if name not in vref: print('module', name, "has not been calibrated") import sys sys.exit() # calibration: # 1. output the internal reference voltage of 1100mV to P22 adc.vref_to_pin('P22') # 2. measture the voltage at P22 with a voltmeter # 3. enter the actual value into vref above and then re-run else: print('calibrate vref for', name) adc.vref(vref[name]) print("vref=", adc.vref(), "mV") # create an analog pin # Valid pins are P13 to P20. # attenuation suggested range: # (dB) (mV) # 0 100 ~ 950 # 2.5 100 ~ 1250 # 6 150 ~ 1750