NuMaker ADC read gas sensor (MQ3)

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // NuMaker-PFM-NUC472 use ADC0 (A0/PE_1) to read gas sensor (MQ-3)
00002 #include "mbed.h"
00003 
00004 /* NOTE: Connect UNO A0 to MQ-3 gas sensor for this test */
00005 AnalogIn   adc0(A0);
00006 
00007 DigitalOut led(LED1);
00008 
00009 int main(void)
00010 {
00011 #ifdef MBED_MAJOR_VERSION
00012     printf("Mbed OS version %d.%d.%d\r\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00013 #endif
00014     printf("ADC0 start reading...\n\r");
00015     while (1) { 
00016         printf("ADC0 value= 0x%4x, percentage= %3.3f%%\n\r",adc0.read_u16(), adc0.read()*100.0f);
00017         if(adc0 > 0.7f) led = 0; // > 0.7 * Vcc wil turn on led
00018         else            led = 1;
00019 #if MBED_MAJOR_VERSION >= 6 
00020     ThisThread::sleep_for(200);
00021 #else
00022         wait(0.2f);
00023 #endif
00024 
00025     }
00026 }