Pablo Lopez
/
MQ135codigo
mq135
MQ135.cpp@0:59011d343342, 2017-06-16 (annotated)
- Committer:
- ngomez
- Date:
- Fri Jun 16 15:01:55 2017 +0000
- Revision:
- 0:59011d343342
- Child:
- 1:8944058d8e95
Library created for MQ135 gas sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ngomez | 0:59011d343342 | 1 | #include "MQ135.h" |
ngomez | 0:59011d343342 | 2 | #include "mbed.h" |
ngomez | 0:59011d343342 | 3 | |
ngomez | 0:59011d343342 | 4 | MQ135::MQ135(PinName pin) : _pin(pin) { |
ngomez | 0:59011d343342 | 5 | _pin = A0; |
ngomez | 0:59011d343342 | 6 | |
ngomez | 0:59011d343342 | 7 | } |
ngomez | 0:59011d343342 | 8 | void MQ135::initialize(){ |
ngomez | 0:59011d343342 | 9 | mqR = 22000; |
ngomez | 0:59011d343342 | 10 | rO = 41763; |
ngomez | 0:59011d343342 | 11 | a = 116.6020682; |
ngomez | 0:59011d343342 | 12 | b = -2.769034857; |
ngomez | 0:59011d343342 | 13 | adc_limit = 65535; |
ngomez | 0:59011d343342 | 14 | } |
ngomez | 0:59011d343342 | 15 | float MQ135::getPPM() { |
ngomez | 0:59011d343342 | 16 | Serial pc(USBTX,USBRX); |
ngomez | 0:59011d343342 | 17 | float adcRaw=_pin.read_u16(); |
ngomez | 0:59011d343342 | 18 | pc.printf("adc: %f\n\r", adcRaw); |
ngomez | 0:59011d343342 | 19 | |
ngomez | 0:59011d343342 | 20 | rS = ((adc_limit * mqR) / adcRaw) - mqR; |
ngomez | 0:59011d343342 | 21 | pc.printf("Rs: %d\n\r", rS); |
ngomez | 0:59011d343342 | 22 | rSrO = (float)rS / (float)rO; |
ngomez | 0:59011d343342 | 23 | pc.printf("RsrO: %f\n\r", rSrO); |
ngomez | 0:59011d343342 | 24 | float ppm = a * pow(rSrO, b); |
ngomez | 0:59011d343342 | 25 | pc.printf("ppm: %f\n\r", ppm); |
ngomez | 0:59011d343342 | 26 | return ppm; |
ngomez | 0:59011d343342 | 27 | } |
ngomez | 0:59011d343342 | 28 | int MQ135::air_quality(){ |
ngomez | 0:59011d343342 | 29 | Serial pc(USBTX,USBRX); |
ngomez | 0:59011d343342 | 30 | float adcRaw=_pin.read_u16()/64; |
ngomez | 0:59011d343342 | 31 | //Aire limpio |
ngomez | 0:59011d343342 | 32 | if(adcRaw <= 55){ |
ngomez | 0:59011d343342 | 33 | pc.printf("Aire limpio \n\r"); |
ngomez | 0:59011d343342 | 34 | pc.printf("\n\r"); |
ngomez | 0:59011d343342 | 35 | return 0; |
ngomez | 0:59011d343342 | 36 | } |
ngomez | 0:59011d343342 | 37 | |
ngomez | 0:59011d343342 | 38 | //Dioxido de carbono bajo (Respiracion humana) |
ngomez | 0:59011d343342 | 39 | if( adcRaw >= 56 && adcRaw <= 73){ |
ngomez | 0:59011d343342 | 40 | pc.printf("Aire con un poco de CO2 \n\r"); |
ngomez | 0:59011d343342 | 41 | pc.printf("\n\r"); |
ngomez | 0:59011d343342 | 42 | return 1; |
ngomez | 0:59011d343342 | 43 | } |
ngomez | 0:59011d343342 | 44 | |
ngomez | 0:59011d343342 | 45 | //Dioxido de carbono |
ngomez | 0:59011d343342 | 46 | if( adcRaw >= 74 && adcRaw <= 350){ |
ngomez | 0:59011d343342 | 47 | pc.printf("Dioxido de carbono CO2 \n\r"); |
ngomez | 0:59011d343342 | 48 | pc.printf("\n\r"); |
ngomez | 0:59011d343342 | 49 | return 2; |
ngomez | 0:59011d343342 | 50 | } |
ngomez | 0:59011d343342 | 51 | |
ngomez | 0:59011d343342 | 52 | //Gas propano y butano |
ngomez | 0:59011d343342 | 53 | if(adcRaw >= 400){ |
ngomez | 0:59011d343342 | 54 | pc.printf("Propano butano \n\r"); |
ngomez | 0:59011d343342 | 55 | pc.printf("\n\r"); |
ngomez | 0:59011d343342 | 56 | return 3; |
ngomez | 0:59011d343342 | 57 | } |
ngomez | 0:59011d343342 | 58 | } |
ngomez | 0:59011d343342 | 59 | long MQ135::getRs(){ |
ngomez | 0:59011d343342 | 60 | return rS; |
ngomez | 0:59011d343342 | 61 | } |
ngomez | 0:59011d343342 | 62 | |
ngomez | 0:59011d343342 | 63 | float MQ135::getRsRo(){ |
ngomez | 0:59011d343342 | 64 | return rSrO; |
ngomez | 0:59011d343342 | 65 | } |