![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test of DS18B20 Digital temperature sensor using k22f board.
main.cpp@0:6367a3d990f2, 2018-04-19 (annotated)
- Committer:
- javibenjas
- Date:
- Thu Apr 19 04:10:42 2018 +0000
- Revision:
- 0:6367a3d990f2
initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
javibenjas | 0:6367a3d990f2 | 1 | #include "mbed.h" |
javibenjas | 0:6367a3d990f2 | 2 | #include "DS1820.h" |
javibenjas | 0:6367a3d990f2 | 3 | |
javibenjas | 0:6367a3d990f2 | 4 | #define SLEEP_S 2 |
javibenjas | 0:6367a3d990f2 | 5 | |
javibenjas | 0:6367a3d990f2 | 6 | // I'm using DS18B20, connections |
javibenjas | 0:6367a3d990f2 | 7 | // red -> 5v |
javibenjas | 0:6367a3d990f2 | 8 | // blue -> GND |
javibenjas | 0:6367a3d990f2 | 9 | // yellow -> D6 |
javibenjas | 0:6367a3d990f2 | 10 | DigitalOut led_r(LED1); |
javibenjas | 0:6367a3d990f2 | 11 | DigitalOut led_g(LED2); |
javibenjas | 0:6367a3d990f2 | 12 | DigitalOut led_b(LED3); |
javibenjas | 0:6367a3d990f2 | 13 | DS1820 temp_probe(D6); |
javibenjas | 0:6367a3d990f2 | 14 | |
javibenjas | 0:6367a3d990f2 | 15 | //we will use the first read as environment temperature |
javibenjas | 0:6367a3d990f2 | 16 | float env_temperature = -99; |
javibenjas | 0:6367a3d990f2 | 17 | // red means higher than environment |
javibenjas | 0:6367a3d990f2 | 18 | // blue means cooler than environment |
javibenjas | 0:6367a3d990f2 | 19 | // green means same as env |
javibenjas | 0:6367a3d990f2 | 20 | |
javibenjas | 0:6367a3d990f2 | 21 | float read_temperature() { |
javibenjas | 0:6367a3d990f2 | 22 | float tmp_temp; |
javibenjas | 0:6367a3d990f2 | 23 | temp_probe.convertTemperature(true, DS1820::this_device); |
javibenjas | 0:6367a3d990f2 | 24 | do { |
javibenjas | 0:6367a3d990f2 | 25 | // TODO limit the n of tries |
javibenjas | 0:6367a3d990f2 | 26 | // if device is not responding, will cycle forever |
javibenjas | 0:6367a3d990f2 | 27 | tmp_temp = temp_probe.temperature(); |
javibenjas | 0:6367a3d990f2 | 28 | wait_ms(10); |
javibenjas | 0:6367a3d990f2 | 29 | } while(tmp_temp == -1000); |
javibenjas | 0:6367a3d990f2 | 30 | return tmp_temp; |
javibenjas | 0:6367a3d990f2 | 31 | } |
javibenjas | 0:6367a3d990f2 | 32 | |
javibenjas | 0:6367a3d990f2 | 33 | int main() { |
javibenjas | 0:6367a3d990f2 | 34 | printf("Temperature probe test\r\n"); |
javibenjas | 0:6367a3d990f2 | 35 | float temperature = 0; |
javibenjas | 0:6367a3d990f2 | 36 | temp_probe.setResolution(9); //9bits |
javibenjas | 0:6367a3d990f2 | 37 | |
javibenjas | 0:6367a3d990f2 | 38 | //read env temperature |
javibenjas | 0:6367a3d990f2 | 39 | wait(1); //wait for device to be ready |
javibenjas | 0:6367a3d990f2 | 40 | env_temperature = read_temperature(); |
javibenjas | 0:6367a3d990f2 | 41 | |
javibenjas | 0:6367a3d990f2 | 42 | while(1) { |
javibenjas | 0:6367a3d990f2 | 43 | temperature = read_temperature(); |
javibenjas | 0:6367a3d990f2 | 44 | printf("Temperature %3.1foC\r\n", temperature); |
javibenjas | 0:6367a3d990f2 | 45 | if(temperature < env_temperature) { |
javibenjas | 0:6367a3d990f2 | 46 | led_r = 1; // 1 is off in this board |
javibenjas | 0:6367a3d990f2 | 47 | led_g = 1; |
javibenjas | 0:6367a3d990f2 | 48 | led_b = 0; |
javibenjas | 0:6367a3d990f2 | 49 | } else if(temperature > env_temperature) { |
javibenjas | 0:6367a3d990f2 | 50 | led_r = 0; |
javibenjas | 0:6367a3d990f2 | 51 | led_g = 1; |
javibenjas | 0:6367a3d990f2 | 52 | led_b = 1; |
javibenjas | 0:6367a3d990f2 | 53 | } else { |
javibenjas | 0:6367a3d990f2 | 54 | led_r = 1; |
javibenjas | 0:6367a3d990f2 | 55 | led_g = 0; |
javibenjas | 0:6367a3d990f2 | 56 | led_b = 1; |
javibenjas | 0:6367a3d990f2 | 57 | } |
javibenjas | 0:6367a3d990f2 | 58 | // sleep |
javibenjas | 0:6367a3d990f2 | 59 | wait(SLEEP_S); |
javibenjas | 0:6367a3d990f2 | 60 | } |
javibenjas | 0:6367a3d990f2 | 61 | } |
javibenjas | 0:6367a3d990f2 | 62 |