NuMaker ADC read gas sensor (MQ3)

Committer:
shliu1
Date:
Thu Dec 22 14:53:37 2016 +0800
Revision:
5:fc7ae837af13
Parent:
1:605a7fdd4e93
Child:
7:e951e13ae71e
Support M453, target is numaker_pfm_m453

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rkuo2000 1:605a7fdd4e93 1 // NuMaker-PFM-NUC472 use ADC0 (A0/PE_1) to read gas sensor (MQ-3)
rkuo2000 0:1f30d2df739e 2 #include "mbed.h"
shliu1 5:fc7ae837af13 3 #if defined(TARGET_NUMAKER_PFM_NUC472)
shliu1 5:fc7ae837af13 4 AnalogIn adc0(PE_1); // A0 connected to MQ-3 gas sensor
shliu1 5:fc7ae837af13 5 #elif defined(TARGET_NUMAKER_PFM_M453)
shliu1 5:fc7ae837af13 6 AnalogIn adc0(PB_0);
shliu1 5:fc7ae837af13 7 #endif
rkuo2000 0:1f30d2df739e 8
rkuo2000 0:1f30d2df739e 9 DigitalOut led(LED1);
rkuo2000 0:1f30d2df739e 10
rkuo2000 0:1f30d2df739e 11 int main(void)
rkuo2000 0:1f30d2df739e 12 {
rkuo2000 0:1f30d2df739e 13 printf("ADC0 start reading...\n\r");
rkuo2000 0:1f30d2df739e 14 while (1) {
rkuo2000 0:1f30d2df739e 15 printf("ADC0 value= 0x%4x, percentage= %3.3f%%\n\r",adc0.read_u16(), adc0.read()*100.0f);
rkuo2000 0:1f30d2df739e 16 if(adc0 > 0.7f) led = 0; // > 0.7 * Vcc wil turn on led
rkuo2000 0:1f30d2df739e 17 else led = 1;
rkuo2000 0:1f30d2df739e 18 wait(0.2f);
rkuo2000 0:1f30d2df739e 19 }
rkuo2000 0:1f30d2df739e 20 }