Analog Inputs

1. Introduction

In the steps of this tutorial

  1. the Figaro TGS2600 is electrically connected to the ESP32C3,
  2. the sensor data and the reference voltage is aquaired with the ESP32C3’s ADC, and
  3. transmitted to a smartphone via RemoteXY.

An example (images, code) can be found at the end of this document.

The sensor can be used to measure the concentration of gases, such as hydrogen. The following images show an experimental setup, used to measure the concetration of hydrogen, generated through a chemical reaction (salt water in a cotton pad through which an electrical current flows): 1, 2

2. Prerequisites

3. Implementatation

The implemation is based on the following aspects:

  1. General functionality of the sensor:
  2. General approach:

3.1 Circuit

The measurement circuit connects the sensor electrically to the microcontroller (ESP32C3).

The structure of the circuits depends on the electrical connectors (sometimes called “pins”) of the sensor and the microcontroller.

To calculate the value of the sensor’s internal resistor, two voltage need to be measured (general measurement circuit’s input voltage and a measurement load resistor’s voltage).

An overview the functionality of the microcontroller pins is shown in the following image Pinout image source Espressif ESP-IDF Programming Guide.

A detailed description can be found in the manufacturers documentation: Espressif ESP-IDF Programming Guide / ESP32C3-DevKitC-02

A description of the sensor’s pins (pinout, pin layout) is given in the
sensor datasheet on page 3 in Figure 3 “Sensor dimensions”.

A schematic of the measurement circuit is shown in the sensor datasheet on page 2 in Figure 2 “Basic measuring curcuit”.

A less technical description of the measuring circuit and the connection to the microcontroller is shown in the following images:

Important advice: Not all pins of the microcontroller can be used with the ADC. Thus, not all pins of the microcontroller can be used with the analogRead(PIN) function. Compatible pins can be found in the Pinout.

3.2 Data Aquisition and Processing

The ADC and its software interface (Application Programming Interface - API) is described in the ESP32C3 Technical Reference Manual on page 845.

The following example code calculates the measured voltage based on the measurement value.

int   const ADC_PIN = 1;
float const V_REF = 3.0;
int v_sense = analogRead(ADC_PIN);
int v_dat = V_REF / 4095 * v_sense;

The formula to calculate the resistance (which depends on the gas concentration) based on the ADC measurement value aquired with the measurement cirtcuit described in the sections above is described in the sensor datasheet on page 2.

It is important to use a variable type with sufficienct accuracy for the intermediate results. Otherwise rounding will lead to wrong (intermediate) results.

The following example code calculates the resistance of the sensor’s internal resistor.

Note: remember the general functionality

Assumptions: the code assumse the following

  // Define constants
  int const VDRCV = 10000;
  int const ADC_CV = 0;
  int const ADC_LRV = 1;
  // Measured value of (half) measurement Circuit Voltage
  int   m_cvi = analogRead(ADC_CV);
  float m_cv = m_cvi * 2.0;
  // Measured value of measurement Load Resistor Voltage
  float m_sens = (float) analogRead(ADC_LRV);
  // Calculate the Sensor's Resistance (with intermediate results)
  float ir;
  ir = v_cv - v_sense;
  ir = ir/v_sense;
  int sr = ir * VDRCV;

4. Example

The following information show an example implementation:

The following prerequisites must be met.

4.1 Example with Debug Output

The follwing Arduino example has been extended with optional debug output over the serial interface: