This program read temperature using two LM35 sensors on A0 and A1 analog inputs and displays average temperature using serial. Also, if the temp is in the range (18,22) C then the green led is on, if the temp is in the range (16,18) and (22,24) C the blue led is on, else red led will be on. You can see the connection diagram on my blog sdizdarevic.com soon
Dependencies: mbed
Fork of tempsensors by
main.cpp@0:cd3e218305bb, 2014-08-06 (annotated)
- Committer:
- sdizdarevic
- Date:
- Wed Aug 06 13:03:00 2014 +0000
- Revision:
- 0:cd3e218305bb
Program to read and display temperature using two LM35 temperature sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sdizdarevic | 0:cd3e218305bb | 1 | /* |
sdizdarevic | 0:cd3e218305bb | 2 | This program read temperature using two LM35 |
sdizdarevic | 0:cd3e218305bb | 3 | sensors on A0 and A1 analog inputs and displays |
sdizdarevic | 0:cd3e218305bb | 4 | average temperature using serial |
sdizdarevic | 0:cd3e218305bb | 5 | Also, if the temp is in the range (18,22) C then |
sdizdarevic | 0:cd3e218305bb | 6 | the green led is on, if the temp is in the range |
sdizdarevic | 0:cd3e218305bb | 7 | (16,18) and (22,24) C the blue led is on, else |
sdizdarevic | 0:cd3e218305bb | 8 | red led will be on. |
sdizdarevic | 0:cd3e218305bb | 9 | You can see the connection diagram on my blog |
sdizdarevic | 0:cd3e218305bb | 10 | sdizdarevic.com soon |
sdizdarevic | 0:cd3e218305bb | 11 | */ |
sdizdarevic | 0:cd3e218305bb | 12 | |
sdizdarevic | 0:cd3e218305bb | 13 | |
sdizdarevic | 0:cd3e218305bb | 14 | #include "mbed.h" |
sdizdarevic | 0:cd3e218305bb | 15 | DigitalOut red(LED_RED); // Red led |
sdizdarevic | 0:cd3e218305bb | 16 | DigitalOut blue(LED_BLUE); //blue led |
sdizdarevic | 0:cd3e218305bb | 17 | DigitalOut green(LED_GREEN); // green LED |
sdizdarevic | 0:cd3e218305bb | 18 | AnalogIn T1(PTB2); // temperature sensor T1 on A0 |
sdizdarevic | 0:cd3e218305bb | 19 | AnalogIn T2(PTB3); // temperature sensor T2 on A1 |
sdizdarevic | 0:cd3e218305bb | 20 | float T; // average temperature |
sdizdarevic | 0:cd3e218305bb | 21 | float tempF; //average temperature in Fahrenheit |
sdizdarevic | 0:cd3e218305bb | 22 | float K=3.3*100; // scaling coefficient for calculating |
sdizdarevic | 0:cd3e218305bb | 23 | // analog value to temperature |
sdizdarevic | 0:cd3e218305bb | 24 | |
sdizdarevic | 0:cd3e218305bb | 25 | Serial pc(USBTX,USBRX); // print temperature on console (eg. Putty) |
sdizdarevic | 0:cd3e218305bb | 26 | |
sdizdarevic | 0:cd3e218305bb | 27 | |
sdizdarevic | 0:cd3e218305bb | 28 | int main() { |
sdizdarevic | 0:cd3e218305bb | 29 | pc.printf("LM35 temperature sensor\n"); |
sdizdarevic | 0:cd3e218305bb | 30 | |
sdizdarevic | 0:cd3e218305bb | 31 | while(1) { |
sdizdarevic | 0:cd3e218305bb | 32 | red=0;//red=0; |
sdizdarevic | 0:cd3e218305bb | 33 | blue=0; //blue=0; |
sdizdarevic | 0:cd3e218305bb | 34 | green=0; //green=0; |
sdizdarevic | 0:cd3e218305bb | 35 | T=(T1+T1)/2.*K; // average temperature value in Celsius (C) |
sdizdarevic | 0:cd3e218305bb | 36 | |
sdizdarevic | 0:cd3e218305bb | 37 | |
sdizdarevic | 0:cd3e218305bb | 38 | tempF=(9.0*T)/5.0 + 32.0; //average temperature value in fahrenheit (F) |
sdizdarevic | 0:cd3e218305bb | 39 | |
sdizdarevic | 0:cd3e218305bb | 40 | |
sdizdarevic | 0:cd3e218305bb | 41 | |
sdizdarevic | 0:cd3e218305bb | 42 | wait(0.2f); //wait a little |
sdizdarevic | 0:cd3e218305bb | 43 | pc.printf("Temperature (in Celsius) is %4.2f \r\n",T); |
sdizdarevic | 0:cd3e218305bb | 44 | |
sdizdarevic | 0:cd3e218305bb | 45 | |
sdizdarevic | 0:cd3e218305bb | 46 | wait(0.2f); //wait a little |
sdizdarevic | 0:cd3e218305bb | 47 | pc.printf("Temperature (in Fahrenheit) is %4.2f \r\n",tempF); |
sdizdarevic | 0:cd3e218305bb | 48 | |
sdizdarevic | 0:cd3e218305bb | 49 | |
sdizdarevic | 0:cd3e218305bb | 50 | if ( T>=18 && T<=22 ) green=1; |
sdizdarevic | 0:cd3e218305bb | 51 | else if ((T>=16 && T<18)||(T>22 && T<=24)) blue=1; |
sdizdarevic | 0:cd3e218305bb | 52 | else red=1; |
sdizdarevic | 0:cd3e218305bb | 53 | wait(0.2); |
sdizdarevic | 0:cd3e218305bb | 54 | |
sdizdarevic | 0:cd3e218305bb | 55 | } |
sdizdarevic | 0:cd3e218305bb | 56 | } |