0.1.1 MAX31850

This commit is contained in:
rob tillaart 2022-11-16 20:52:35 +01:00
parent d5ddbcd8eb
commit a3e2c91f94
18 changed files with 921 additions and 0 deletions

View File

@ -0,0 +1,39 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
# - esp32 // warning is error readbit.
# - esp8266
# - mega2560
- rpipico
# Declaring Dependent Arduino Libraries (to be installed via the Arduino Library Manager)
libraries:
- "OneWire"
unittest:
# These dependent libraries will be installed
libraries:
- "OneWire"
# fix the #include "util/crc16" problem here?

View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
github: RobTillaart

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict

View File

@ -0,0 +1,17 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -0,0 +1,18 @@
name: JSON check
on:
push:
paths:
- '**.json'
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

View File

@ -0,0 +1,18 @@
# Change Log MAX44009
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.1] - 2022-11-16
- add RP2040 in build-CI
- add changelog.md
- major rewrite based on reread of datasheet.
- initial release(?)
## [0.1.0] - 2021-06-03
- initial version (not released)

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021-2021 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,194 @@
//
// FILE: MAX31850.cpp
// AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.1.1
// DATE: 2021-06-03
// PUPROSE: Arduino library for the MAX31850 thermocouple temperature sensor.
//
// HISTORY: see changelog.md
#include "MAX31850.h"
// OneWire commands
#define STARTCONVO 0x44
#define READSCRATCH 0xBE
#define WRITESCRATCH 0x4E
// SCRATCHPAD registers
// bit 1 = reserved bit 0 = error flag
#define TC_TEMP_LSB 0
#define TC_TEMP_MSB 1
// bit 3 = reserved bit 0-2 = error codes
#define INTERN_TEMP_LSB 2
#define INTERN_TEMP_MSB 3
// 4 address bits 0..15
#define CONFIGURATION 4
#define RESERVED_1 5
#define RESERVED_2 6
#define RESERVED_3 7
#define SCRATCHPAD_CRC 8
/////////////////////////////////////////////////////////////////////////////
//
// MAX31850
//
MAX31850::MAX31850(OneWire* oneWire)
{
_oneWire = oneWire;
_addresFound = false;
_TCTemp = 0;
_internalTemp = 0;
_errorBits = 0;
_failFlag = false;
_addrBits = 0;
}
bool MAX31850::begin(void)
{
_addresFound = false;
for (uint8_t retries = 3; (retries > 0) && (_addresFound == false); retries--)
{
_oneWire->reset();
_oneWire->reset_search();
_deviceAddress[0] = 0x00;
_oneWire->search(_deviceAddress);
_addresFound = _deviceAddress[0] != 0x00 &&
_oneWire->crc8(_deviceAddress, 7) == _deviceAddress[7];
}
return _addresFound;
}
bool MAX31850::getAddress(uint8_t* buffer)
{
if (_addresFound)
{
for (uint8_t i = 0; i < 8; i++)
{
buffer[i] = _deviceAddress[i];
}
}
return _addresFound;
}
void MAX31850::requestTemperatures(void)
{
_oneWire->reset();
_oneWire->skip();
_oneWire->write(STARTCONVO, 0);
}
bool MAX31850::isConversionComplete(void)
{
return (_oneWire->read_bit() == 1);
}
float MAX31850::read(void)
{
ScratchPad scratchPad;
for (int i = 0; i < 9; i++) scratchPad[i] = 0;
// NO CRC CHECK
readScratchPad(scratchPad, 5);
_failFlag = scratchPad[TC_TEMP_LSB] & 0x01;
int16_t rawTemperature = (((int16_t)scratchPad[TC_TEMP_MSB]) << 6) | scratchPad[TC_TEMP_LSB] >> 2;
_TCTemp = 0.25 * rawTemperature;
_errorBits = scratchPad[INTERN_TEMP_LSB] & 0x07;
rawTemperature = (((int16_t)scratchPad[INTERN_TEMP_LSB]) << 4) | scratchPad[INTERN_TEMP_LSB] >> 4;
_internalTemp = 0.0625 * rawTemperature;
_addrBits = scratchPad[CONFIGURATION] & 0x0F;
return _TCTemp;
}
float MAX31850::getTempTC(void)
{
return _TCTemp;
}
float MAX31850::getTempInternal(void)
{
return _internalTemp;
}
uint8_t MAX31850::getErrorCode()
{
return _errorBits;
}
uint8_t MAX31850::getAddressPins()
{
return _addrBits;
};
// bool MAX31850::setType(char typeTC)
// {
// switch(typeTC)
// {
// case 'E':
// case 'J':
// case 'K':
// case 'N':
// case 'R':
// case 'S':
// case 'T':
// _typeTC = typeTC;
// return true;
// }
// return false;
// }
//
//
// char MAX31850::getType()
// {
// return _typeTC;
// }
/////////////////////////////////////////////////////////////////////////////
//
// PROTECTED
//
void MAX31850::readScratchPad(uint8_t *scratchPad, uint8_t fields)
{
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(READSCRATCH);
for (uint8_t i = 0; i < fields; i++)
{
scratchPad[i] = _oneWire->read();
}
_oneWire->reset();
}
/////////////////////////////////////////////////////////////////////////////
//
// MAX31851
//
MAX31851::MAX31851(OneWire * onewire) : MAX31850(onewire)
{
}
// -- END OF FILE --

View File

@ -0,0 +1,73 @@
#pragma once
//
// FILE: MAX31850.h
// AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.1.1
// DATE: 2021-06-03
// PUPROSE: Arduino library for the MAX31850 thermocouple temperature sensor.
//
#define MAX31850_LIB_VERSION (F("0.1.1"))
#include "Arduino.h"
#include "OneWire.h"
// ERROR CODES
#define MAX31850_OK 0
#define MAX31850_ERR_SHORT_OPEN 1
#define MAX31850_ERR_SHORT_GND 2
#define MAX31850_ERR_SHORT_VDD 4
typedef uint8_t DeviceAddress[8];
typedef uint8_t ScratchPad[9];
class MAX31850
{
public:
explicit MAX31850(OneWire * oneWire);
bool begin(void);
bool getAddress(uint8_t* buffer);
void requestTemperatures(void);
bool isConversionComplete(void);
// call read to get new measurement.
float read(void);
// Call read first!!
float getTempTC(void);
float getTempInternal(void);
uint8_t getErrorCode();
// TODO
uint8_t getAddressPins();
protected:
void readScratchPad(uint8_t *, uint8_t);
uint8_t _deviceAddress[8];
OneWire* _oneWire;
bool _addresFound;
char _typeTC;
float _TCTemp;
float _internalTemp;
uint8_t _errorBits;
bool _failFlag;
uint8_t _addrBits;
};
class MAX31851 : public MAX31850
{
public:
MAX31851(OneWire * onewire);
};
// -- END OF FILE --

View File

@ -0,0 +1,141 @@
[![Arduino CI](https://github.com/RobTillaart/MAX31850/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/MAX31850/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MAX14661/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/MAX31850/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/MAX14661/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MAX31850/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MAX31850.svg?maxAge=3600)](https://github.com/RobTillaart/MAX31850/releases)
# MAX31850
Arduino library for the MAX31850 / MAX31851 thermocouple sensor.
## Description
**WARNING EXPERIMENTAL** needs more testing (no hardware yet).
(feedback welcome)
his MAX31850 library is not a full featured library for the MAX31850 family.
The MAX31850/1 is a Cold-Junction Compensated, 1-Wire Thermocouple-to-Digital Converter.
The MAX31850/1 supports K-, J-, N-, T-, S-, R-, and E-type thermocouples. (to be tested).
The library supports both the MAX31850 and MAX31851 but is limited to one sensor per pin.
The only feature the class supports is the asynchronous reading of the temperature.
This allowed the class to be both minimal in size and non-blocking.
In fact the class has no support for a synchronous read in one call.
This choice will teach people how to work in a non-blocking way from the start.
Finally this library will probably make it easier to use a MAX31850 with processing
boards or IC's with small memory footprint.
## Hardware connection
```
// PROCESSOR MAX31850 THERMOCOUPLE
// +---------------+
// GND 1 | o o | T+
// DATA 2 | o |
// VCC 3 | o o | T-
// | |
// | oooo |
// +---------------+
// 4 address pins.
```
This library supports only one MAX31850 per Arduino/ MCU pin.
### Pull up resistor
Connect a pull-up resistor 4.7 KOhm between pin3 and pin2.
When the wires are longer this resistor needs to be smaller.
An **indicative** table for pull up resistors, (E12 series), to get started.
Note: thicker wires require smaller resistors (typically 1 step in E12 series)
| Length | - 5.0 Volt | - 3.3 Volt |
|--------------:|------------:|----------:|
| 10cm (4") | 10K0 | 6K8 |
| 20cm (8") | 8K2 | 4K7 |
| 50cm (20") | 4K7 | 3K3 |
| 100cm (3'4") | 3K3 | 2K2 |
| 200cm (6'8") | 2K2 | 1K0 |
| 500cm (16'8") | 1K0 | \* |
| longer | * | \* |
\* = no info, smaller
## Interface
#### Constructor
- **explicit MAX31850(OneWire \* oneWire)** constructor.
- **bool begin(void)** initialize the library.
Returns true if addresses are found.
- **bool getAddress(uint8_t \* buffer)** get the address if found.
#### Read the sensor
- **void requestTemperatures(void)** idem.
- **bool isConversionComplete(void)** idem.
- **float read(void)** read the data from the sensor.
Returns the temperature of the thermoCouple as this is most often needed.
- **float getTempTC(void)** returns temperature of the ThermoCouple.
One must call **read()** to get new measurements.
- **float getTempInternal(void)** returns internal temperature.
#### other
- **uint8_t getAddressPins()** ??
- **uint8_t getErrorCode()**
## Types of thermocouples
The MAX31850 comes in MAX31850E.. MAX31850T types reflecting the version of TC to use.
| Sensor type | SC in µV/°C | Temp Range in °C | Material | notes |
|:-----------:|:------------|:-----------------|:--------------------------|:-----------|
| E_TC | 76.373 | -270 to +1000 | Constantan Chromel |
| J_TC | 57.953 | -210 to +1200 | Constantan Iron |
| K_TC | 41.276 | -270 to +1372 | Alumel Chromel | most used |
| N_TC | 36.256 | -270 to +1300 | Nisil Nicrosil |
| R_TC | 10.506 | -50 to +1768 | Platinum Platinum/Rhodium |
| S_TC | 9.587 | +50 to +1768 | Platinum Platinum/Rhodium |
| T_TC | 52.18 | -270 to +400 | Constantan Copper |
(MAX31851 idem)
## Error codes
| name | value |
|:--------------------------|:-------:|
| MAX31850_OK | 0 |
| MAX31850_ERR_SHORT_OPEN | 1 |
| MAX31850_ERR_SHORT_GND | 2 |
| MAX31850_ERR_SHORT_VDD | 4 |
## Future
#### must
- improve documentation
- get hardware to test (sponsors welcome)
- test on different platforms
#### should
- investigate different thermocouples
- test with different platforms
#### could
- expand to multi sensor per pin.
- first get one sensor working

View File

@ -0,0 +1,60 @@
//
// FILE: MAX31850_getAddress.ino
// AUTHOR: Rob Tillaart
// PURPOSE: MAX31850 lib getAddress demo
#include "OneWire.h"
#include "MAX31850.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
MAX31850 sensor(&oneWire);
DeviceAddress da;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MAX31850_LIB_VERSION: ");
Serial.println(MAX31850_LIB_VERSION);
Serial.print("\ngetAddress: ");
Serial.println(sensor.getAddress(da));
sensor.begin();
Serial.print("\ngetAddress: ");
Serial.println(sensor.getAddress(da));
if (!sensor.getAddress(da))
{
Serial.println("No address found!");
return;
}
Serial.print("Address: ");
for (uint8_t i = 0; i < 8; i++)
{
if (da[i] < 0x10) Serial.print('0');
Serial.print(da[i], HEX);
}
Serial.println();
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,47 @@
//
// FILE: MAX31850_minimum.ino
// AUTHOR: Rob Tillaart
// PURPOSE: minimal sketch
#include "OneWire.h"
#include "MAX31850.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
MAX31850 sensor(&oneWire);
void setup(void)
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("MAX31850_LIB_VERSION: ");
Serial.println(MAX31850_LIB_VERSION);
sensor.begin();
sensor.requestTemperatures();
}
void loop(void)
{
if (sensor.isConversionComplete())
{
sensor.read();
Serial.print("TC:\t");
Serial.println(sensor.getTempTC());
Serial.print("INTERN:\t");
Serial.println(sensor.getTempInternal());
Serial.println();
sensor.requestTemperatures();
}
}
// -- END OF FILE --

View File

@ -0,0 +1,68 @@
//
// FILE: MAX31850_test_disconnect.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Minimal MAX31850 lib with async support.
#include "OneWire.h"
#include "MAX31850.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
MAX31850 sensor(&oneWire);
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("MAX31850_LIB_VERSION: ");
Serial.println(MAX31850_LIB_VERSION);
// wait until address found
if (sensor.begin() == false)
{
Serial.println("ERROR: No device found");
while (!sensor.begin()); // wait until device comes available.
}
sensor.requestTemperatures();
}
void loop()
{
start = millis();
sensor.requestTemperatures();
// wait for data AND detect disconnect
uint32_t timeout = millis();
while (!sensor.isConversionComplete())
{
if (millis() - timeout >= 1000) // check for timeout
{
Serial.println("ERROR: timeout or disconnect");
break;
}
}
sensor.read();
float temperature = sensor.getTempTC();
stop = millis();
Serial.print(stop - start);
Serial.print("\t");
Serial.println(temperature, 1); // 1 decimal makes perfect sense
delay(1000);
}
// -- END OF FILE --

View File

@ -0,0 +1,65 @@
//
// FILE: MAX31850_two_sensors.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo with two sensors (on two pins)
#include "OneWire.h"
#include "MAX31850.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
MAX31850 sensor(&oneWire);
// numbers chosen to match pin numbers..
#define ONE_WIRE_BUS2 2
#define ONE_WIRE_BUS3 3
OneWire oneWire2(ONE_WIRE_BUS2);
OneWire oneWire3(ONE_WIRE_BUS3);
MAX31850 inside(&oneWire2);
MAX31850 outside(&oneWire3);
void setup(void)
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("MAX31850_LIB_VERSION: ");
Serial.println(MAX31850_LIB_VERSION);
inside.begin();
outside.begin();
inside.requestTemperatures();
outside.requestTemperatures();
}
void loop(void)
{
// print the temperature when available and request a new reading
if (inside.isConversionComplete())
{
inside.read();
Serial.print("inside:\t\t");
Serial.println(inside.getTempTC(), 1);
inside.requestTemperatures();
}
if (outside.isConversionComplete())
{
outside.read();
Serial.print("outside:\t\t");
Serial.println(outside.getTempTC(), 1);
outside.requestTemperatures();
}
}
// -- END OF FILE --

View File

@ -0,0 +1,31 @@
# Syntax Colouring Map For MAX31850
# Data types (KEYWORD1)
MAX31850 KEYWORD1
MAX31851 KEYWORD1
OneWire KEYWORD1
DeviceAddress KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
getAddress KEYWORD2
requestTemperatures KEYWORD2
isConversionComplete KEYWORD2
read KEYWORD2
getTempTC KEYWORD2
getTempInternal KEYWORD2
getAddressPins KEYWORD2
setType KEYWORD2
getType KEYWORD2
# Constants (LITERAL1)
DS18B20_LIB_VERSION LITERAL1
DEVICE_DISCONNECTED LITERAL1
DEVICE_CRC_ERROR LITERAL1
MAX31850_CLEAR LITERAL1
MAX31850_CRC LITERAL1

View File

@ -0,0 +1,23 @@
{
"name": "MAX31850",
"keywords": "MAX31850,MAX31851,Onewire, Temperature,thermocouple",
"description": "Arduino library for the MAX31850 thermocouple temperature sensor. Restricted to a single sensor per pin. Minimalistic version.",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/MAX31850.git"
},
"version": "0.1.1",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
"headers": "MAX31850.h"
}

View File

@ -0,0 +1,11 @@
name=MAX31850
version=0.1.1
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the MAX31850 thermocouple temperature sensor.
paragraph=Minimalistic version, restricted to one sensor per pin, asynchronuous mode only.
category=Sensors
url=https://github.com/RobTillaart/MAX31850
architectures=*
includes=MAX31850.h
depends=

View File

@ -0,0 +1,78 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2021-06-03
// PURPOSE: unit tests for the MAX31850 library
// https://github.com/RobTillaart/MAX31850
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// https://github.com/Arduino-CI/arduino_ci/blob/master/cpp/unittest/Assertion.h#L33-L42
// ----------------------------
// assertEqual(expected, actual)
// assertNotEqual(expected, actual)
// assertLess(expected, actual)
// assertMore(expected, actual)
// assertLessOrEqual(expected, actual)
// assertMoreOrEqual(expected, actual)
// assertTrue(actual)
// assertFalse(actual)
// assertNull(actual)
// assertNotNull(actual)
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "MAX31850.h"
unittest_setup()
{
fprintf(stderr, "MAX31850_LIB_VERSION: %s\n", (char*) MAX31850_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constructor)
{
OneWire oneWire(4);
MAX31850 sensor(&oneWire);
sensor.begin();
assertEqual(MAX31850_CLEAR, sensor.getConfig());
sensor.setConfig(MAX31850_CRC);
assertEqual(MAX31850_CRC, sensor.getConfig());
sensor.setConfig(MAX31850_CLEAR);
assertEqual(MAX31850_CLEAR, sensor.getConfig());
}
unittest(test_try_read)
{
OneWire oneWire(4);
MAX31850 sensor(&oneWire);
sensor.begin();
for (int res = 9; res < 13; res++)
{
sensor.setResolution(res);
sensor.requestTemperatures();
delay(750);
assertFalse(sensor.isConversionComplete());
assertEqual(DEVICE_DISCONNECTED, sensor.getTempC());
}
}
unittest_main()
// --------