Yahya05IE
Posts: 24
Joined: Tue Jul 11, 2017 9:11 pm

The Logic Behind MQx Sensors Calibration

Wed Jul 26, 2017 9:24 pm

Would you please explain the logic behind this snippet of code which is taken from here for calibrating MQx Sensor(s):

Code: Select all

def MQResistanceCalculation(self, raw_adc):
      return float(self.RL_VALUE*(1023.0-raw_adc)/float(raw_adc));
in which raw_adc is the MCP3008 reading, and RL_VALUE is the load resistance on the board, in kilo ohms and equals 5.

The thing is, as far as I understand, the ADC works according to this equation:

Code: Select all

Resolution of ADC / System Voltage = ADC Reading / Analog Voltage Measured
but I cannot see it implemented in the above-code (because I was following this tutorial but for Arduino).

Furthermore, to get the value of RS in a gas according to the second above-mentioned tutorial, we follow this equation:

Code: Select all

RS = [(VC x RL) / VRL] - RL
which also not seen in the entire code of the project that the first snippet of code comes from.

The Schema of this design is:

Code: Select all

MQx Sensor -> Logic Level Converter -> MCP3008 -> RPi

I'm sure I'm missing something, or maybe I'm mixing up things.

Any help is very much appreciated.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12130
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: The Logic Behind MQx Sensors Calibration

Thu Jul 27, 2017 11:45 am

Yahya05IE wrote:
Wed Jul 26, 2017 9:24 pm

The Schema of this design is:

Code: Select all

MQx Sensor -> Logic Level Converter -> MCP3008 -> RPi

I'm sure I'm missing something, or maybe I'm mixing up things.

Any help is very much appreciated.
The logic level converter is not needed, and it certainly doesn't go in between the sensor and the ADC (MCP3008), it should go in between a digital device that uses 5V output levels, and a PI (which cannot tolerate 5V levels), but not in this case, as the ADC "talks" to the PI using I2C signalling, which high level is determined by the voltage on the pullup resistors, which for a PI is 3V3. No (other) pullups to 5V should be added, as the PI already has its own pullups, and an MCP3008 should accept 3V3 levels.

so the scheme should be:

Code: Select all

MQx Sensor -> MCP3008 -> RPi

Yahya05IE
Posts: 24
Joined: Tue Jul 11, 2017 9:11 pm

Re: The Logic Behind MQx Sensors Calibration

Thu Jul 27, 2017 12:43 pm

mahjongg wrote:
Thu Jul 27, 2017 11:45 am
Yahya05IE wrote:
Wed Jul 26, 2017 9:24 pm

The Schema of this design is:

Code: Select all

MQx Sensor -> Logic Level Converter -> MCP3008 -> RPi

I'm sure I'm missing something, or maybe I'm mixing up things.

Any help is very much appreciated.
The logic level converter is not needed, and it certainly doesn't go in between the sensor and the ADC (MCP3008), it should go in between a digital device that uses 5V output levels, and a PI (which cannot tolerate 5V levels), but not in this case, as the ADC "talks" to the PI using I2C signalling, which high level is determined by the voltage on the pullup resistors, which for a PI is 3V3. No (other) pullups to 5V should be added, as the PI already has its own pullups, and an MCP3008 should accept 3V3 levels.

so the scheme should be:

Code: Select all

MQx Sensor -> MCP3008 -> RPi
Thank you very much for your answer.
According to your schema, I need to change Rref of ADC from 3.3 to 5v for sure because it's a reference to the maximum voltage value that this ADC can reach, so because my MQx sensor output is between 0 to 5 V, and it's connected to ADC channel, the Rref should be 5 as well.

Coming to first question: Would you please explain the first equation, because as I said , I was expecting to get the voltage measured from ADC (via the read() method) and use it to find the Rs.

Thanks.


Return to “Automation, sensing and robotics”