MQ-XX Sensor series is a sensitive gas detection sensor which provide a lower conductivity in a clean air. When the sensor detect a gas exist in area the sensor conductivity will increase. These sensor has a high sensitivity to Ozone, LPG, C3H8, CO, H, C2H6O and C6H6 gas detection. MQ sensor series are useful in domestic, industrial, portable gas detection and automotive. Please refer to diagram and datasheets below for further information and characteristics on each model of MQ sensor.
Required Components
- NUCLEO, Discovery, ARM CORTEX STM32F, STM32F Dev Board
- MQ Gas Sensor / Module
- Jumper Wires / DuPont Wires
- Solder Less Bread Board
Wiring Guide
Schematics Diagram & MQ Sensor Functions
Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#include "stm32f10x.h" #include "systick.h" #include <stdio.h> #include "usart.h" #define ADC1_DR_Address ((u32)0x4001244C) void USART_Configuration(void); void ADC_Configuration(void); float AD_value; vu16 ADC_ConvertedValue; void user_gpio_init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; GPIO_Init(GPIOA,&GPIO_InitStructure); } int main(void) { Delay_Init(); usart_Configuration(); ADC_Configuration(); user_gpio_init(); printf("\r\n************* 14CORE | MQXX SENSOR STM32 TEST CODE *************\r\n"); printf("\r\n************* | -------- www.14core.com --------| *************\r\n"); printf("\r\n****************************************************************\r\n"); /* Infinite loop */ while (1) { if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4)) printf("No Gas has been detected!\r\n"); else printf("Gas is detected\r\n"); /* Printf message with AD value to serial port every 1 second */ AD_value = ADC_ConvertedValue; AD_value = (AD_value/4096)*3.3; printf("@ AD value = %4.2fV \r\n", AD_value); Delay(100); /* delay 1000ms */ } } void ADC_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); /* Configure PA.06 (ADC Channel6), PA.07 (ADC Channel7) as analog input -------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); /* DMA channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel6 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_239Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif |
Downloads
- STM32F10X Project File / Configuration | Zip
- Download MQ 131 Gas Sensor Datasheet | PDF
- Download MQ 02 Gas Sensor Datasheet | PDF
- Download MQ 03 Alcohol, Ethanol Sensor Datasheet | PDF
- Download MQ 04 Methane Sensor Datasheet | PDF
- Download MQ 05 Hydrogen, Natural Gas Sensor Datasheet | PDF
- Download MQ 06 LPG, Propane Sensor Datasheet | PDF
- Download MQ 07 Carbon Monoxide Gas Sensor Datasheet | PDF
- Download MQ 08 Hydrogen Sensor Datasheet | PDF
- Download MQ 09 Carbon Monoxide Sensor Datasheet | PDF
- Download MQ 135 Benzene, Alcohol, Smoke Sensor Datasheet | PDF
- Download MQ 309A Carbon Monoxide Sensor Datasheet | PDF
- Download MQ 303A Alcohol Sensor Datasheet | PDF
Wiring the MQ-XX Sensor for Ozone, LPG, C3H8, CO, H, C2H6O, C6H6 Gas Detection on STM32F