## The Logic Behind MQx Sensors Calibration

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

### The Logic Behind MQx Sensors Calibration

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):
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.

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

### Re: The Logic Behind MQx Sensors Calibration

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

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``
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.

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

### Re: The Logic Behind MQx Sensors Calibration

chaitanyaalur
Posts: 1
Joined: Wed Mar 24, 2021 5:07 am

### Re: The Logic Behind MQx Sensors Calibration

Yahya05IE wrote:
Thu Jul 27, 2017 3:21 pm

https://raspberrypi.stackexchange.com/q ... 0398#70398
Thanks for the detailed explanation and it's quite useful.

One basic question through,

What does ADCReading means in the above explanation? Is the the output channel value from the ADC or the output voltage from ADC?
I'm getting the following output from ADS1115 (gain=4) and which of these is ADCreading to be used in the above resistance calculation formula?

|| Raw(Value) || Voltage(V) ||
|| 2818 || 0.08810 ||
|| 2837 || 0.08847 ||
|| 2816 || 0.08841 ||