Skip to content

Instantly share code, notes, and snippets.

@dpr0 dpr0/mq135.js
Created Feb 20, 2018

Embed
What would you like to do?
pinMode(A5, 'analog');
var co2_values = [], index = 0, co2_value = 0, co2_min = 1000, co2_max = 0;
const VALUES_SIZE = 150;
const RZERO = 83.54781175;
const PARA = 116.6020682;
const PARB = 2.769034857;
const CORA = 0.00035;
const CORB = 0.02718;
const CORC = 1.39538;
const CORD = 0.0018;
const ATMOCO2 = 397.13;
function getRZero() {
return getResistance() * Math.pow((ATMOCO2/PARA), (1.0/PARB));
}
function getResistance() {
return (1.515151515151515 / analogRead(A5)) - 1;
}
function getPPM() {
return Math.round(PARA * Math.pow((getResistance() / RZERO), - PARB));
}
function getCorrectedResistance(t, h) {
return getResistance()/getCorrectionFactor(t, h);
}
function getCorrectionFactor(t, h) {
return CORA * t * t - CORB * t + CORC - (h - 33.0) * CORD;
}
function getCorrectedPPM(t, h) {
return PARA * Math.pow((getCorrectedResistance(t, h)/RZERO), - PARB);
}
setInterval(() => {
let current_co2 = getPPM();
co2_values[index] = current_co2;
if(current_co2 < co2_min) co2_min = current_co2;
if(current_co2 > co2_max) co2_max = current_co2;
co2_value = (co2_values.reduce((a, b) => { return a+b; }, 0) / co2_values.length).toFixed();
if(index >= VALUES_SIZE-1) index = 0;
else index++;
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.