[](/LICENSE) []() [](https://components.espressif.com/components/k0i05/esp_ds18b20) [](https://registry.platformio.org/libraries/k0i05/esp_ds18b20) # Maxim-Integrated DS18B20 Sensor This ESP32 espressif IoT development framework (esp-idf) i2c peripheral driver was developed for the Maxim-Integrated DS18B20 1-wire temperature sensor. Information on features and functionality are documented and can be found in the `ds18b20.h` header file and in the `documentation` folder. ## Repository The component is hosted on github and is located here: ## General Usage To get started, simply copy the component to your project's `components` folder and reference the `ds18b20.h` header file as an include. The component includes documentation for the peripheral such as the datasheet, application notes, and/or user manual where applicable. ``` components └── esp_ds18b20 ├── CMakeLists.txt ├── README.md ├── LICENSE ├── idf_component.yml ├── library.json ├── documentation │ └── datasheets, etc. ├── include │ └── ds18b20_version.h │ └── ds18b20.h └── ds18b20.c ``` ## Basic Example Once a driver instance is instantiated the sensor is ready for usage as shown in the below example. This basic implementation of the driver utilizes default configuration settings, prints DS18S20 sensors detected on the 1-wire bus, and makes a measurement request from the sensor at a user defined interval and prints the results. ``` #include <ds18b20.h> void owb0_ds18b20_task( void *pvParameters ) { // initialize the xLastWakeTime variable with the current time. TickType_t last_wake_time = xTaskGetTickCount (); // // initialize owb device configuration ds18b20_config_t dev_cfg = OWB_DS18B20_CONFIG_DEFAULT; ds18b20_handle_t dev_hdl; onewire_device_iter_handle_t dev_iter_hdl; onewire_device_t dev; // owb ds18b20 device detection onewire_device_t devs[5]; uint8_t devs_size = sizeof(devs) / sizeof(devs[0]); uint8_t devs_count = 0; // // detect ds18b20 devices on 1-wire bus esp_err_t ret = ds18b20_detect(owb0_bus_hdl, devs, devs_size, &devs_count); if(ret == ESP_OK) { ESP_LOGW(APP_TAG, "ds18b20 devices detected: %u", devs_count); for(uint8_t i = 0; i < devs_count; i++) { ESP_LOGI(APP_TAG, "ds18b20(%u), address: %016llX", i, devs[i].address); } } else { ESP_LOGE(APP_TAG, "ds18b20 device detect failed (%s)", esp_err_to_name(ret)); } // // instantiate 1-wire device iterator handle ESP_ERROR_CHECK( onewire_new_device_iter(owb0_bus_hdl, &dev_iter_hdl) ); // // get 1-wire device - assumes there is only one ds18b20 device on the bus if (onewire_device_iter_get_next(dev_iter_hdl, &dev) == ESP_OK) { // found a new device, let's check if we can upgrade it to a DS18B20 // check if the device is a ds18b20, if so, return the ds18b20 handle if (ds18b20_init(&dev, &dev_cfg, &dev_hdl) == ESP_OK) { ESP_LOGI(APP_TAG, "found a ds18b20, address: %016llX", dev.address); } else { ESP_LOGI(APP_TAG, "found an unknown device, address: %016llX", dev.address); assert(dev.address); } } // // free device iter handle ESP_ERROR_CHECK( onewire_del_device_iter(dev_iter_hdl) ); // // get/set/get resolution ds18b20_resolutions_t dev_reso; ESP_ERROR_CHECK( ds18b20_get_resolution(dev_hdl, &dev_reso) ); ESP_LOGW(APP_TAG, "ds18b20 get resolution: %d", dev_reso); ESP_ERROR_CHECK( ds18b20_set_resolution(dev_hdl, DS18B20_RESOLUTION_11BIT) ); ESP_ERROR_CHECK( ds18b20_get_resolution(dev_hdl, &dev_reso) ); ESP_LOGW(APP_TAG, "ds18b20 get resolution: %d", dev_reso); // // validate device handle if(dev_hdl == NULL) { ESP_LOGE(APP_TAG, "ds18b20 handle init failed"); assert(dev_hdl); } // // task loop entry point for( ;; ) { ESP_LOGI(APP_TAG, "######################## DS18B20 - START #########################"); // // handle sensor float temperature; esp_err_t result = ds18b20_get_measurement(dev_hdl, &temperature); if(result != ESP_OK) { ESP_LOGE(APP_TAG, "ds18b20 device read failed (%s)", esp_err_to_name(result)); } else { ESP_LOGI(APP_TAG, "temperature: %.2f°C", temperature); } // ESP_LOGI(APP_TAG, "######################## DS18B20 - END ###########################"); // // // pause the task per defined wait period vTaskDelaySecUntil( &last_wake_time, OWB0_TASK_SAMPLING_RATE ); } // // free resources ds18b20_delete( dev_hdl ); vTaskDelete( NULL ); } ``` Copyright (c) 2024 Eric Gionet (gionet.c.eric@gmail.com)
idf.py add-dependency "k0i05/esp_ds18b20^1.1.3"