Skip to content

Commit a0a8a47

Browse files
gmarullcarlescufi
authored andcommitted
samples: sensor: add MAX6675 sample application
Add a sample application to test MAX6675 cold-junction-compensated K-thermocouple to digital converter. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent cbc340c commit a0a8a47

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2021 Teslabs Engineering S.L.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.13.1)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(max6675)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})

samples/sensor/max6675/README.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
MAX6675 K-thermocouple to digital converter
2+
###########################################
3+
4+
Overview
5+
********
6+
7+
This is a sample application to read an external MAX6675
8+
cold-junction-compensated K-thermocouple to digital converter.
9+
10+
Requirements
11+
************
12+
13+
- MAX6675 wired to your board SPI bus
14+
- K-thermocouple connected to MAX6675 T+/T- inputs
15+
16+
References
17+
**********
18+
19+
- MAX6675: https://datasheets.maximintegrated.com/en/ds/MAX6675.pdf
20+
21+
Building and Running
22+
********************
23+
24+
This sample can be built with any board that supports SPI. A sample overlay is
25+
provided for the NUCLEO-F030R8 board.
26+
27+
.. zephyr-app-commands::
28+
:zephyr-app: samples/sensor/max6675
29+
:board: nucleo_f030r8
30+
:goals: build
31+
:compact:
32+
33+
Sample Output
34+
=============
35+
36+
The application will read and print sensor temperature every second. Note that
37+
temperature fetch will fail if the K-thermocouple is not connected. This is
38+
because MAX6675 is able to detect if the K-thermocouple is connected or not.
39+
40+
.. code-block:: console
41+
42+
Temperature: 25.25 C
43+
Temperature: 25.50 C
44+
45+
<repeats endlessly every second>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2021 Teslabs Engineering S.L.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&arduino_spi {
8+
max6675@0 {
9+
compatible = "maxim,max6675";
10+
reg = <0>;
11+
spi-max-frequency = <4300000>;
12+
label = "MAX6675";
13+
};
14+
};

samples/sensor/max6675/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_CBPRINTF_FP_SUPPORT=y
2+
CONFIG_SPI=y
3+
CONFIG_SENSOR=y
4+
CONFIG_MAX6675=y

samples/sensor/max6675/sample.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sample:
2+
name: MAX6675 K-thermocouple to digital converter
3+
tests:
4+
sample.sensor.max6675:
5+
tags: sensors
6+
harness: console
7+
depends_on: spi
8+
harness_config:
9+
type: multi_line
10+
ordered: true
11+
regex:
12+
- "Temperature: ([0-9\\.]+) C"
13+
fixture: fixture_spi_max6675
14+
filter: dt_compat_enabled("maxim,max6675")

samples/sensor/max6675/src/main.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2021 Teslabs Engineering S.L.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
9+
#include <zephyr.h>
10+
#include <device.h>
11+
#include <drivers/sensor.h>
12+
13+
/**
14+
* @file Sample app using the MAX6675 cold-junction-compensated K-thermocouple
15+
* to digital converter.
16+
*
17+
* This app will read and display the sensor temperature every second.
18+
*/
19+
20+
void main(void)
21+
{
22+
const struct device *dev;
23+
struct sensor_value val;
24+
25+
dev = device_get_binding(DT_LABEL(DT_INST(0, maxim_max6675)));
26+
if (dev == NULL) {
27+
printf("Could not obtain MAX6675 device\n");
28+
return;
29+
}
30+
31+
while (1) {
32+
int ret;
33+
34+
ret = sensor_sample_fetch_chan(dev, SENSOR_CHAN_AMBIENT_TEMP);
35+
if (ret < 0) {
36+
printf("Could not fetch temperature (%d)\n", ret);
37+
return;
38+
}
39+
40+
ret = sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &val);
41+
if (ret < 0) {
42+
printf("Could not get temperature (%d)\n", ret);
43+
return;
44+
}
45+
46+
printf("Temperature: %.2f C\n", sensor_value_to_double(&val));
47+
48+
k_sleep(K_MSEC(1000));
49+
}
50+
}

0 commit comments

Comments
 (0)