ADC read value example.

Dependencies:   mbed

Committer:
bcostm
Date:
Wed Sep 27 13:00:54 2017 +0000
Revision:
0:c8c068641d77
First version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:c8c068641d77 1 #include "mbed.h"
bcostm 0:c8c068641d77 2
bcostm 0:c8c068641d77 3 AnalogIn analog_value(A0);
bcostm 0:c8c068641d77 4
bcostm 0:c8c068641d77 5 DigitalOut led(LED1);
bcostm 0:c8c068641d77 6
bcostm 0:c8c068641d77 7 int main()
bcostm 0:c8c068641d77 8 {
bcostm 0:c8c068641d77 9 float meas_r;
bcostm 0:c8c068641d77 10 float meas_v;
bcostm 0:c8c068641d77 11
bcostm 0:c8c068641d77 12 printf("\nAnalogIn example\n");
bcostm 0:c8c068641d77 13
bcostm 0:c8c068641d77 14 while(1) {
bcostm 0:c8c068641d77 15
bcostm 0:c8c068641d77 16 meas_r = analog_value.read(); // Read the analog input value (value from 0.0 to 1.0 = full ADC conversion range)
bcostm 0:c8c068641d77 17 meas_v = meas_r * 3300; // Converts value in the 0V-3.3V range
bcostm 0:c8c068641d77 18
bcostm 0:c8c068641d77 19 // Display values
bcostm 0:c8c068641d77 20 printf("measure = %f = %.0f mV\n", meas_r, meas_v);
bcostm 0:c8c068641d77 21
bcostm 0:c8c068641d77 22 // LED is ON is the value is below 1V
bcostm 0:c8c068641d77 23 if (meas_v < 1000) {
bcostm 0:c8c068641d77 24 led = 1; // LED ON
bcostm 0:c8c068641d77 25 } else {
bcostm 0:c8c068641d77 26 led = 0; // LED OFF
bcostm 0:c8c068641d77 27 }
bcostm 0:c8c068641d77 28
bcostm 0:c8c068641d77 29 wait(1.0); // 1 second
bcostm 0:c8c068641d77 30 }
bcostm 0:c8c068641d77 31 }