Dallas' DS1820 family temperature sensor. For more details see [https://developer.mbed.org/users/hudakz/code/DS1820/wiki/Homepage]
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 } } }
Diff: DS1820.h
- Revision:
- 24:d683d826dccd
- Parent:
- 23:74a4ff420541
--- a/DS1820.h Mon Jul 20 08:09:58 2020 +0000 +++ b/DS1820.h Mon Dec 28 21:15:27 2020 +0000 @@ -1,7 +1,7 @@ #ifndef DS1820_H_ #define DS1820_H_ - #include <OneWire.h> + #include "OneWire.h" /** * Dallas' DS1820 family temperature sensor. @@ -9,20 +9,20 @@ * available at <http://developer.mbed.org/users/hudakz/code/OneWire/> * * Example of use: - * + * * @code - * + * * Single sensor. * * #include "mbed.h" * #include "DS1820.h" - * + * * Serial pc(USBTX, USBRX); * DigitalOut led(LED1); * DS1820 ds1820(D8); // substitute D8 with actual mbed pin name connected to 1-wire bus * float temp = 0; * int result = 0; - * + * * int main() * { * pc.printf("\r\n--Starting--\r\n"); @@ -35,30 +35,30 @@ * case 0: // no errors -> 'temp' contains the value of measured temperature * pc.printf("temp = %3.1f%cC\r\n", temp, 176); * break; - * + * * case 1: // no sensor present -> 'temp' is not updated * pc.printf("no sensor present\n\r"); * break; - * + * * case 2: // CRC error -> 'temp' is not updated * pc.printf("CRC error\r\n"); * } - * + * * led = !led; * } * } * else * pc.printf("No DS1820 sensor found!\r\n"); * } - * - * + * + * * More sensors connected to the same 1-wire bus. - * + * * #include "mbed.h" * #include "DS1820.h" - * + * * #define SENSORS_COUNT 64 // number of DS1820 sensors to be connected to the 1-wire bus (max 256) - * + * * Serial pc(USBTX, USBRX); * DigitalOut led(LED1); * OneWire oneWire(D8); // substitute D8 with actual mbed pin name connected to the DS1820 data pin @@ -66,10 +66,10 @@ * int sensors_found = 0; // counts the actually found DS1820 sensors * float temp = 0; * int result = 0; - * + * * int main() { * int i = 0; - * + * * pc.printf("\r\n Starting \r\n"); * //Enumerate (i.e. detect) DS1820 sensors on the 1-wire bus * for(i = 0; i < SENSORS_COUNT; i++) { @@ -79,20 +79,20 @@ * break; * } * } - * + * * sensors_found = i; - * + * * if (sensors_found == 0) { * pc.printf("No DS1820 sensor found!\r\n"); * return -1; * } * else * pc.printf("Found %d sensors.\r\n", sensors_found); - * + * * while(1) { * pc.printf("-------------------\r\n"); * for(i = 0; i < sensors_found; i++) - * ds1820[i]->startConversion(); // start temperature conversion from analog to digital + * ds1820[i]->startConversion(); // start temperature conversion from analog to digital * wait(1.0); // let DS1820s complete the temperature conversion * for(int i = 0; i < sensors_found; i++) { * if(ds1820[i]->isPresent()) @@ -100,32 +100,34 @@ * } * } * } - * + * * @endcode - * - * Note: Don't forget to connect a 4.7k Ohm resistor + * + * Note: Don't forget to connect a 4.7k Ohm resistor * between the DS1820's data pin and the +3.3V pin * */ class DS1820 { - OneWire *oneWire; - bool present; - bool model_s; - uint8_t data[12]; - uint8_t addr[8]; + OneWire* _oneWire; + bool _present; + bool _model_s; + uint8_t _data[12]; + uint8_t _addr[8]; + static uint8_t _lastAddr[8]; + float toFloat(uint16_t word); - static uint8_t lastAddr[8]; - + public: - DS1820(PinName pin, int sample_point_us = 13); -// DS1820(char model, PinName pin); - DS1820(OneWire* wire); - bool begin(void); - bool isPresent(); - void setResolution(uint8_t res); - void startConversion(void); - float read(void); + DS1820(PinName gpioPin, int samplePoint_us = 13); + DS1820(PinName txPin, PinName rxPin); + DS1820(OneWire* oneWire); + + bool begin(void); + bool isPresent(); + void setResolution(uint8_t res); + void startConversion(void); + float read(void); uint8_t read(float& temp); }; #endif /* DS1820_H_ */