ESP32 AGS10 Sensor
The AGS10 is a gas sensor known for detecting a range of gases, including methane, propane, and hydrogen. Designed with stability and sensitivity, it’s suitable for industrial safety and environmental monitoring. The sensor offers a rapid response time, high sensitivity, and low power consumption, often used in applications like leak detection and air quality monitoring systems.
Jump to Code Examples
Quick Links
AGS10 Sensor Technical Specifications
Below you can see the AGS10 Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C
- Interface: I2C
- Measuring range: 0~99999 ppb
- Accuracy: 25% reading
- Operating Range: 0~50 ℃, 0~95%RH
- Voltage: 3.0 ± 0.1V DC
AGS10 Sensor Pinout
Below you can see the pinout for the AGS10 Sensor. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
AGS10 Wiring with ESP32
Below you can see the wiring for the AGS10 Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
Code Examples
Below you can find code examples of AGS10 Sensor with ESP32 in several frameworks:
ESP32 AGS10 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the AGS10 Sensor:
#include <Wire.h>
#define I2C_ADDRESS 0xXX // Replace with the AGS10 I2C address
#define SDA_PIN 21 // I2C SDA pin for ESP32
#define SCL_PIN 22 // I2C SCL pin for ESP32
void setup() {
Wire.begin(SDA_PIN, SCL_PIN);
Serial.begin(115200);
Serial.println("AGS10 I2C Test");
}
void loop() {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(0x00); // Example register or command
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 2);
if (Wire.available() == 2) {
int highByte = Wire.read();
int lowByte = Wire.read();
int sensorValue = (highByte << 8) | lowByte;
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
}
delay(1000);
}
This Arduino code demonstrates how to interact with the AGS10 sensor via I2C communication. It initializes the I2C interface using specified SDA and SCL pins, then communicates with the sensor by sending a command and requesting two bytes of data. The received high and low bytes are combined into a 16-bit integer to represent the sensor's value, which is printed to the Serial Monitor. The loop repeats every second, providing regular sensor updates.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 AGS10 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the AGS10 Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "driver/i2c.h"
#define I2C_MASTER_SCL_IO 22 // GPIO for SCL
#define I2C_MASTER_SDA_IO 21 // GPIO for SDA
#define I2C_MASTER_NUM I2C_NUM_1
#define I2C_MASTER_FREQ_HZ 100000
void app_main() {
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = I2C_MASTER_SDA_IO;
conf.scl_io_num = I2C_MASTER_SCL_IO;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
i2c_param_config(I2C_MASTER_NUM, &conf);
i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
// Add code to read from AGS10 via I2C
}
This ESP-IDF code initializes the I2C master interface on an ESP32, configuring the SDA and SCL pins along with pull-up resistors and clock speed. The i2c_param_config()
and i2c_driver_install()
functions set up the I2C master for communication. You can add commands to communicate with the AGS10 sensor by sending and receiving I2C data after the initialization.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 AGS10 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the AGS10 Sensor
sensor:
- platform: ags10
tvoc:
name: TVOC
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 AGS10 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
ESP32 AGS10 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the AGS10 Sensor:
#include <Wire.h>
#define I2C_ADDRESS 0xXX // Replace with the AGS10 I2C address
#define SDA_PIN 21 // I2C SDA pin for ESP32
#define SCL_PIN 22 // I2C SCL pin for ESP32
void setup() {
Wire.begin(SDA_PIN, SCL_PIN);
Serial.begin(115200);
Serial.println("AGS10 I2C Test");
}
void loop() {
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(0x00); // Example register or command
Wire.endTransmission();
Wire.requestFrom(I2C_ADDRESS, 2);
if (Wire.available() == 2) {
int highByte = Wire.read();
int lowByte = Wire.read();
int sensorValue = (highByte << 8) | lowByte;
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
}
delay(1000);
}
This code is the same as the Arduino code but is intended for use with the PlatformIO environment. It initializes the I2C communication with the AGS10 sensor, reads data, and prints it to the Serial Monitor. Ensure the PlatformIO environment is correctly set up with the specified settings in the platformio.ini
file.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 AGS10 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the AGS10 Sensor with your ESP32.
from machine import I2C, Pin
from time import sleep
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
address = 0xXX # Replace with the AGS10 I2C address
while True:
# Write command and read data
i2c.writeto(address, b'\x00') # Example register or command
data = i2c.readfrom(address, 2)
# Combine high and low bytes into a 16-bit value
sensor_value = (data[0] << 8) | data[1]
print(f"Sensor Value: {sensor_value}")
sleep(1)
This MicroPython script uses the I2C protocol to communicate with the AGS10 sensor. It initializes the I2C instance with specified SDA and SCL pins, sends a command to the sensor, and reads two bytes of data. The received bytes are combined into a 16-bit integer to represent the sensor's value, which is printed to the console every second. Replace the placeholder I2C address with the correct address for your AGS10 sensor.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of AGS10 Sensor, its pinout, connection with ESP32 and AGS10 Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.