Dallas' DS1820 family temperature sensor. For more details see [https://developer.mbed.org/users/hudakz/code/DS1820/wiki/Homepage]

Dependencies:   OneWire

Dependents:   BLE_nRF24L01 frdm_serialfgmp gather_sensor_data UltiSaverController ... more

Some programs using the DS1820 library:

Import programDS1820_Hello

Simple DS1820 sensor demo showing how to use the DS1820 library [https://developer.mbed.org/users/hudakz/code/DS1820/]

Import programBLE_nRF24L01

Bluetooth Low Energy (BLE) beacon with nRF24L01(+). Data is received and displayed by Android device (Android app source code is attached).

Examples of use:

Single DS1820 sensor

/*
 * Single DS1820 sensor GPIO driven
 *
 * Note: Don't forget to connect a 4.7k Ohm resistor 
 *       between the DS1820's data pin and the +3.3V pin
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |           GPIO |--------------------o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */
 
#include "mbed.h"
#include "DS1820.h"
 
Serial serial(USBTX, USBRX);
 
int main() {
    DS1820  ds1820(D8);    // substitute D8 with actual mbed pin name connected to the DS1820 data pin    
                             
    if(ds1820.begin()) {
        while(1) {
            ds1820.startConversion();     // start temperature conversion
            ThisThread::sleep_for(1000);  // let DS1820 complete the temperature conversion
            serial.printf("temp = %3.1f\r\n", ds1820.read());     // read temperature
        }
    } else
        serial.printf("No DS1820 sensor found!\r\n");
}
 

Single DS1820 sensor. Data integrity is assured by performing CRC.

/*
 * Single DS1820 sensor GPIO driven + performing CRC
 *
 * Note: Don't forget to connect a 4.7k Ohm resistor 
 *       between the DS1820's data pin and the +3.3V pin
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |           GPIO |--------------------o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */
 
#include "mbed.h"
#include "DS1820.h"
 
Serial serial(USBTX, USBRX);
 
int main() {
    DS1820  ds1820(D8);    // substitute D8 with actual mbed pin name connected to the DS1820 data pin   
    float   temp = 0;
    int     error = 0; 
                             
    if(ds1820.begin()) {
        while(1) {
            ds1820.startConversion();    // start temperature conversion
            ThisThread::sleep_for(1000); // let DS1820 complete the temperature conversion
            error = ds1820.read(temp);   // read temperature from DS1820 and perform cyclic redundancy check (CRC)
            switch(error) {
            case 0:    // no errors -> 'temp' contains the value of measured temperature
                serial.printf("temp = %3.1f\r\n", temp);
                break;
            case 1:    // no sensor present -> 'temp' is not updated
                serial.printf("no sensor present\n\r");
                break;
            case 2:    // CRC error -> 'temp' is not updated
                serial.printf("CRC error\r\n");
            } 
        }
    } else
        serial.printf("No DS1820 sensor found!\r\n");
}
 

Several DS1820 sensors connected to the same 1-wire bus.

/*
 * Multiple sensors GPIO driven
 *
 * Note: Don't forget to connect a 4.7k Ohm resistor 
 *       between the 1-wire bus data line and the +3.3V rail
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |                |                    |
 *          |           GPIO |--------------------o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */

#include "mbed.h"
#include "DS1820.h"

#define     MAX_SENSOSRS   32   // max number of DS1820 sensors to be connected to the 1-wire bus (max 256)

DS1820*     ds1820[MAX_SENSOSRS];
Serial      pc(USBTX, USBRX);
DigitalOut  led(LED1);
OneWire     oneWire(D8);        // substitute D8 with the actual pin name connected to the 1-wire bus
int         sensorsFound = 0;   // counts the actually found DS1820 sensors

int main()
{
    pc.printf("\r\n--Starting--\r\n");
    
    //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
    for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
        ds1820[sensorsFound] = new DS1820(&oneWire);
        if (!ds1820[sensorsFound]->begin()) {
            delete ds1820[sensorsFound];
            break;
        }
    }

    switch (sensorsFound) {
        case 0:
            pc.printf("No DS1820 sensor found!\r\n");
            return -1;

        case 1:
            pc.printf("One DS1820 sensor found.\r\n");
            break;

        default:
            pc.printf("Found %d DS1820 sensors.\r\n", sensorsFound);
    }

    while (1) {
        pc.printf("----------------\r\n");
        for (int i = 0; i < sensorsFound; i++)
            ds1820[i]->startConversion();        // start temperature conversion from analog to digital
        ThisThread::sleep_for(1000);             // let DS1820 sensors complete the temperature conversion
        for (int i = 0; i < sensorsFound; i++) {
            if (ds1820[i]->isPresent())
                pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature
        }
    }
}


Several DS1820 sensors connected to the same 1-wire bus. UART is used to implement the bus

/*
 * Multiple sensors UART driven:
 * 
 *          UART is driving the 1-Wire Bus Master according to Maxim Integrated application note
 *
 *          https://www.maximintegrated.com/en/design/technical-documents/tutorials/2/214.html
 *
 *          In addition to the 4.7k Ohm resistor between the 1-wire data bus/line and the +3.3V pin,
 *          a 470 Ohm resistor shall be tied to the UART's tx and rx pin. UART's rx pin is then used
 *          as 1-wire data bus/line.
 *
 *           ----------------
 *          |                |   ----------------------->  +3.3V
 *          |   MBED BOARD   |  |
 *          |                |  |   ------
 *          |          +3.3V |--o--| 4.7k |-------
 *          |                |      ------        |
 *          |                |      ------        |
 *          |        UART TX |-----|  470 |---    |
 *          |                |      ------    |   |
 *          |                |                |   |
 *          |        UART RX |----------------o---o----->  1-wire bus/line
 *          |                |
 *          |                |
 *          |            GND |-------------------------->  GND
 *          |                |
 *           ----------------
 *
 */
#include "mbed.h"
#include "DS1820.h"

#define MAX_SENSOSRS    32      // max number of DS1820 sensors to be connected to the 1-wire bus (max 256)
DS1820*     ds1820[MAX_SENSOSRS];
DigitalOut  led(LED1);
OneWire     oneWire(p9, p10);       // LPC1768 (UART Tx pin, UART Rx pin)
//OneWire     oneWire(PA_0, PA_1);    // NUCLE0-F446RE (UART Tx pin, UART Rx pin)
int         sensorsFound = 0;       // counts the actually found DS1820 sensors

/**
 * @brief
 * @note
 * @param
 * @retval
 */
int main()
{
    printf("\r\n--Starting--\r\n");

    //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
    for (sensorsFound = 0; sensorsFound < MAX_SENSOSRS; sensorsFound++) {
        ds1820[sensorsFound] = new DS1820(&oneWire);
        if (!ds1820[sensorsFound]->begin()) {
            delete ds1820[sensorsFound];
            break;
        }
    }

    switch (sensorsFound) {
        case 0:
            printf("No DS1820 sensor found!\r\n");
            return -1;

        case 1:
            printf("One DS1820 sensor found.\r\n");
            break;

        default:
            printf("Found %d DS1820 sensors.\r\n", sensorsFound);
    }

    while (1) {
        led = !led;

        printf("----------------\r\n");
        for (int i = 0; i < sensorsFound; i++)
            ds1820[i]->startConversion();   // start temperature conversion from analog to digital
        #if (MBED_MAJOR_VERSION > 5)
            ThisThread::sleep_for(1s);
        #else
            wait(1);
        #endif


        for (int i = 0; i < sensorsFound; i++) {
            if (ds1820[i]->isPresent())
                printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176);   // read temperature
        }
    }
}
Committer:
hudakz
Date:
Sun Mar 10 13:19:03 2019 +0000
Revision:
20:98c261bcb399
Parent:
14:b02fa18b294a
Child:
23:74a4ff420541
Added multidrop capability.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:433af64321d5 1 #ifndef DS1820_H_
hudakz 0:433af64321d5 2 #define DS1820_H_
hudakz 0:433af64321d5 3
hudakz 0:433af64321d5 4 #include <OneWire.h>
hudakz 0:433af64321d5 5
hudakz 14:b02fa18b294a 6 /**
hudakz 14:b02fa18b294a 7 * Dallas' DS1820 family temperature sensor.
hudakz 14:b02fa18b294a 8 * This library depends on the OneWire library (Dallas' 1-Wire bus protocol implementation)
hudakz 14:b02fa18b294a 9 * available at <http://developer.mbed.org/users/hudakz/code/OneWire/>
hudakz 14:b02fa18b294a 10 *
hudakz 14:b02fa18b294a 11 * Example of use:
hudakz 14:b02fa18b294a 12 *
hudakz 14:b02fa18b294a 13 * @code
hudakz 20:98c261bcb399 14 *
hudakz 20:98c261bcb399 15 * Single sensor.
hudakz 14:b02fa18b294a 16 *
hudakz 20:98c261bcb399 17 * #include "mbed.h"
hudakz 20:98c261bcb399 18 * #include "DS1820.h"
hudakz 20:98c261bcb399 19 *
hudakz 20:98c261bcb399 20 * Serial pc(USBTX, USBRX);
hudakz 20:98c261bcb399 21 * DigitalOut led(LED1);
hudakz 20:98c261bcb399 22 * DS1820 ds1820(D8); // substitute D8 with actual mbed pin name connected to 1-wire bus
hudakz 20:98c261bcb399 23 * float temp = 0;
hudakz 20:98c261bcb399 24 * int result = 0;
hudakz 20:98c261bcb399 25 *
hudakz 20:98c261bcb399 26 * int main()
hudakz 20:98c261bcb399 27 * {
hudakz 20:98c261bcb399 28 * pc.printf("\r\n--Starting--\r\n");
hudakz 20:98c261bcb399 29 * if (ds1820.begin()) {
hudakz 20:98c261bcb399 30 * while (1) {
hudakz 20:98c261bcb399 31 * ds1820.startConversion(); // start temperature conversion from analog to digital
hudakz 20:98c261bcb399 32 * wait(1.0); // let DS1820 complete the temperature conversion
hudakz 20:98c261bcb399 33 * result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
hudakz 20:98c261bcb399 34 * switch (result) {
hudakz 20:98c261bcb399 35 * case 0: // no errors -> 'temp' contains the value of measured temperature
hudakz 20:98c261bcb399 36 * pc.printf("temp = %3.1f%cC\r\n", temp, 176);
hudakz 20:98c261bcb399 37 * break;
hudakz 20:98c261bcb399 38 *
hudakz 20:98c261bcb399 39 * case 1: // no sensor present -> 'temp' is not updated
hudakz 20:98c261bcb399 40 * pc.printf("no sensor present\n\r");
hudakz 20:98c261bcb399 41 * break;
hudakz 20:98c261bcb399 42 *
hudakz 20:98c261bcb399 43 * case 2: // CRC error -> 'temp' is not updated
hudakz 20:98c261bcb399 44 * pc.printf("CRC error\r\n");
hudakz 20:98c261bcb399 45 * }
hudakz 20:98c261bcb399 46 *
hudakz 20:98c261bcb399 47 * led = !led;
hudakz 20:98c261bcb399 48 * }
hudakz 20:98c261bcb399 49 * }
hudakz 20:98c261bcb399 50 * else
hudakz 20:98c261bcb399 51 * pc.printf("No DS1820 sensor found!\r\n");
hudakz 20:98c261bcb399 52 * }
hudakz 20:98c261bcb399 53 *
hudakz 20:98c261bcb399 54 *
hudakz 20:98c261bcb399 55 * More sensors connected to the same 1-wire bus.
hudakz 20:98c261bcb399 56 *
hudakz 20:98c261bcb399 57 * #include "mbed.h"
hudakz 20:98c261bcb399 58 * #include "DS1820.h"
hudakz 20:98c261bcb399 59 *
hudakz 20:98c261bcb399 60 * #define SENSORS_COUNT 64 // number of DS1820 sensors to be connected to the 1-wire bus (max 256)
hudakz 20:98c261bcb399 61 *
hudakz 20:98c261bcb399 62 * Serial pc(USBTX, USBRX);
hudakz 20:98c261bcb399 63 * DigitalOut led(LED1);
hudakz 20:98c261bcb399 64 * OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin
hudakz 20:98c261bcb399 65 * DS1820* ds1820[SENSORS_COUNT];
hudakz 20:98c261bcb399 66 * int sensors_found = 0; // counts the actually found DS1820 sensors
hudakz 20:98c261bcb399 67 * float temp = 0;
hudakz 20:98c261bcb399 68 * int result = 0;
hudakz 14:b02fa18b294a 69 *
hudakz 14:b02fa18b294a 70 * int main() {
hudakz 20:98c261bcb399 71 * int i = 0;
hudakz 20:98c261bcb399 72 *
hudakz 20:98c261bcb399 73 * pc.printf("\r\n Starting \r\n");
hudakz 20:98c261bcb399 74 * //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus
hudakz 20:98c261bcb399 75 * for(i = 0; i < SENSORS_COUNT; i++) {
hudakz 20:98c261bcb399 76 * ds1820[i] = new DS1820(&oneWire);
hudakz 20:98c261bcb399 77 * if(!ds1820[i]->begin()) {
hudakz 20:98c261bcb399 78 * delete ds1820[i];
hudakz 20:98c261bcb399 79 * break;
hudakz 20:98c261bcb399 80 * }
hudakz 20:98c261bcb399 81 * }
hudakz 20:98c261bcb399 82 *
hudakz 20:98c261bcb399 83 * sensors_found = i;
hudakz 20:98c261bcb399 84 *
hudakz 20:98c261bcb399 85 * if (sensors_found == 0) {
hudakz 20:98c261bcb399 86 * pc.printf("No DS1820 sensor found!\r\n");
hudakz 20:98c261bcb399 87 * return -1;
hudakz 20:98c261bcb399 88 * }
hudakz 20:98c261bcb399 89 * else
hudakz 20:98c261bcb399 90 * pc.printf("Found %d sensors.\r\n", sensors_found);
hudakz 20:98c261bcb399 91 *
hudakz 20:98c261bcb399 92 * while(1) {
hudakz 20:98c261bcb399 93 * pc.printf("-------------------\r\n");
hudakz 20:98c261bcb399 94 * for(i = 0; i < sensors_found; i++)
hudakz 20:98c261bcb399 95 * ds1820[i]->startConversion(); // start temperature conversion from analog to digital
hudakz 20:98c261bcb399 96 * wait(1.0); // let DS1820s complete the temperature conversion
hudakz 20:98c261bcb399 97 * for(int i = 0; i < sensors_found; i++) {
hudakz 20:98c261bcb399 98 * if(ds1820[i]->isPresent())
hudakz 20:98c261bcb399 99 * pc.printf("temp[%d] = %3.1f%cC\r\n", i, ds1820[i]->read(), 176); // read temperature
hudakz 20:98c261bcb399 100 * }
hudakz 20:98c261bcb399 101 * }
hudakz 14:b02fa18b294a 102 * }
hudakz 20:98c261bcb399 103 *
hudakz 14:b02fa18b294a 104 * @endcode
hudakz 14:b02fa18b294a 105 *
hudakz 14:b02fa18b294a 106 * Note: Don't forget to connect a 4.7k Ohm resistor
hudakz 14:b02fa18b294a 107 * between the DS1820's data pin and the +3.3V pin
hudakz 14:b02fa18b294a 108 *
hudakz 14:b02fa18b294a 109 */
hudakz 0:433af64321d5 110 class DS1820
hudakz 0:433af64321d5 111 {
hudakz 20:98c261bcb399 112 OneWire *oneWire;
hudakz 8:8dfdd1603e4d 113 bool present;
hudakz 8:8dfdd1603e4d 114 bool model_s;
hudakz 0:433af64321d5 115 uint8_t data[12];
hudakz 0:433af64321d5 116 uint8_t addr[8];
hudakz 0:433af64321d5 117 float toFloat(uint16_t word);
hudakz 20:98c261bcb399 118 static uint8_t lastAddr[8];
hudakz 20:98c261bcb399 119
hudakz 0:433af64321d5 120 public:
hudakz 0:433af64321d5 121 DS1820(PinName pin);
hudakz 20:98c261bcb399 122 // DS1820(char model, PinName pin);
hudakz 20:98c261bcb399 123 DS1820(OneWire* wire);
hudakz 0:433af64321d5 124 bool begin(void);
hudakz 8:8dfdd1603e4d 125 bool isPresent();
hudakz 4:adf4e7972d73 126 void setResolution(uint8_t res);
hudakz 0:433af64321d5 127 void startConversion(void);
hudakz 0:433af64321d5 128 float read(void);
hudakz 13:b593a82ce790 129 uint8_t read(float& temp);
hudakz 0:433af64321d5 130 };
hudakz 0:433af64321d5 131 #endif /* DS1820_H_ */