-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ds18b20): Add sensor read example
- Loading branch information
1 parent
81a20ca
commit 1677ca4
Showing
6 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# The following five lines of boilerplate have to be in your project's | ||
# CMakeLists in this exact order for cmake to work correctly | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
set(COMPONENTS main) | ||
project(ds18b20-read) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# DS18B20 sensor example | ||
|
||
This example shows how to use the 1-Wire temperature sensor [DS18B20](https://www.analog.com/media/en/technical-documentation/data-sheets/ds18b20.pdf) component. | ||
|
||
## How to use the example | ||
|
||
To add the component latest version to a project, you can use the following command: | ||
|
||
```bash | ||
idf.py add-dependency "espressif/ds18b20" | ||
``` | ||
|
||
To create a new project from this example, use: | ||
|
||
```bash | ||
idf.py create-project-from-example "espressif/ds18b20:ds18b20-read" | ||
``` | ||
|
||
### Hardware Required | ||
|
||
* An ESP development board with RMT peripheral (e.g ESP32, ESP32-C3, ESP32-S3, etc) | ||
* An DS18B20 sensor connected to GPIO 18. To use a different pin, modify `EXAMPLE_ONEWIRE_BUS_GPIO` in *ds18b20-read.c* file. | ||
* An USB cable for power supply and programming | ||
|
||
### Example Output | ||
|
||
```bash | ||
... | ||
I (336) DS18B20: Device iterator created, start searching... | ||
I (456) DS18B20: Found a DS18B20[0], address: 990417C1D080FF28 | ||
I (456) DS18B20: Searching done, 1 DS18B20 device(s) found | ||
I (456) main_task: Returned from app_main() | ||
I (1266) DS18B20: temperature read from DS18B20[0]: 27.94C | ||
I (4076) DS18B20: temperature read from DS18B20[0]: 27.81C | ||
I (6886) DS18B20: temperature read from DS18B20[0]: 27.75C | ||
I (9696) DS18B20: temperature read from DS18B20[0]: 27.69C | ||
I (12506) DS18B20: temperature read from DS18B20[0]: 27.62C | ||
I (15316) DS18B20: temperature read from DS18B20[0]: 27.56C | ||
I (18126) DS18B20: temperature read from DS18B20[0]: 27.44C | ||
I (20936) DS18B20: temperature read from DS18B20[0]: 27.38C | ||
I (23746) DS18B20: temperature read from DS18B20[0]: 27.31C | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
idf_component_register(SRCS "ds18b20-read.c" | ||
INCLUDE_DIRS ".") |
82 changes: 82 additions & 0 deletions
82
components/ds18b20/examples/ds18b20-read/main/ds18b20-read.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Unlicense OR CC0-1.0 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include "esp_log.h" | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "ds18b20.h" | ||
#include "onewire_bus.h" | ||
|
||
#define EXAMPLE_ONEWIRE_BUS_GPIO 18 | ||
#define EXAMPLE_ONEWIRE_MAX_DS18B20 2 | ||
|
||
static int s_ds18b20_device_num = 0; | ||
static float s_temperature = 0.0; | ||
static ds18b20_device_handle_t s_ds18b20s[EXAMPLE_ONEWIRE_MAX_DS18B20]; | ||
|
||
static const char *TAG = "DS18B20"; | ||
|
||
static void sensor_detect(void) | ||
{ | ||
// install 1-wire bus | ||
onewire_bus_handle_t bus = NULL; | ||
onewire_bus_config_t bus_config = { | ||
.bus_gpio_num = EXAMPLE_ONEWIRE_BUS_GPIO, | ||
}; | ||
onewire_bus_rmt_config_t rmt_config = { | ||
.max_rx_bytes = 10, // 1byte ROM command + 8byte ROM number + 1byte device command | ||
}; | ||
ESP_ERROR_CHECK(onewire_new_bus_rmt(&bus_config, &rmt_config, &bus)); | ||
|
||
onewire_device_iter_handle_t iter = NULL; | ||
onewire_device_t next_onewire_device; | ||
esp_err_t search_result = ESP_OK; | ||
|
||
// create 1-wire device iterator, which is used for device search | ||
ESP_ERROR_CHECK(onewire_new_device_iter(bus, &iter)); | ||
ESP_LOGI(TAG, "Device iterator created, start searching..."); | ||
do { | ||
search_result = onewire_device_iter_get_next(iter, &next_onewire_device); | ||
if (search_result == ESP_OK) { // found a new device, let's check if we can upgrade it to a DS18B20 | ||
ds18b20_config_t ds_cfg = {}; | ||
// check if the device is a DS18B20, if so, return the ds18b20 handle | ||
if (ds18b20_new_device(&next_onewire_device, &ds_cfg, &s_ds18b20s[s_ds18b20_device_num]) == ESP_OK) { | ||
ESP_LOGI(TAG, "Found a DS18B20[%d], address: %016llX", s_ds18b20_device_num, next_onewire_device.address); | ||
s_ds18b20_device_num++; | ||
} else { | ||
ESP_LOGI(TAG, "Found an unknown device, address: %016llX", next_onewire_device.address); | ||
} | ||
} | ||
} while (search_result != ESP_ERR_NOT_FOUND); | ||
ESP_ERROR_CHECK(onewire_del_device_iter(iter)); | ||
ESP_LOGI(TAG, "Searching done, %d DS18B20 device(s) found", s_ds18b20_device_num); | ||
} | ||
|
||
void sensor_read(void) | ||
{ | ||
for (int i = 0; i < s_ds18b20_device_num; i ++) { | ||
ESP_ERROR_CHECK(ds18b20_trigger_temperature_conversion(s_ds18b20s[i])); | ||
ESP_ERROR_CHECK(ds18b20_get_temperature(s_ds18b20s[i], &s_temperature)); | ||
ESP_LOGI(TAG, "temperature read from DS18B20[%d]: %.2fC", i, s_temperature); | ||
} | ||
} | ||
|
||
void sensor_readTask(void *pvParameters) | ||
{ | ||
while (1) { | ||
sensor_read(); | ||
vTaskDelay(1000 / portTICK_PERIOD_MS); | ||
} | ||
} | ||
|
||
void app_main(void) | ||
{ | ||
// Detect the DS18B20 sensor in the bus | ||
sensor_detect(); | ||
// Start task to read the temperature from DS18B20 sensor | ||
xTaskCreate(&sensor_readTask, "sensor_readTask", 4096, NULL, 5, NULL); | ||
} |
4 changes: 4 additions & 0 deletions
4
components/ds18b20/examples/ds18b20-read/main/idf_component.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dependencies: | ||
espressif/ds18b20: | ||
version: "*" | ||
override_path: '../../../' |