Add NTC coefficients to measure celsius
This commit is contained in:
parent
695aa4b3c2
commit
9c114b136a
2
barista/.gitignore
vendored
2
barista/.gitignore
vendored
@ -1 +1,3 @@
|
|||||||
build/
|
build/
|
||||||
|
test_ntc
|
||||||
|
*.o
|
||||||
|
|||||||
@ -1,13 +1,21 @@
|
|||||||
|
# Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
|
||||||
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
PORT=/dev/ttyUSB0
|
PORT=/dev/ttyUSB0
|
||||||
FQBN=arduino:avr:atmega328bb
|
FQBN=arduino:avr:atmega328bb
|
||||||
OPTS=--no-color --log-level=info -v
|
OPTS=--no-color --log-level=info
|
||||||
|
#OPTS+=-v
|
||||||
PROJ=barista
|
PROJ=barista
|
||||||
BUILD=build/$(subst :,.,$(FQBN))
|
BUILD=build/$(subst :,.,$(FQBN))
|
||||||
HEX=$(BUILD)/$(PROJ).ino.hex
|
HEX=$(BUILD)/$(PROJ).ino.hex
|
||||||
|
|
||||||
|
# For host test programs
|
||||||
|
CPPFLAGS=-I.
|
||||||
|
LIBS=-lm
|
||||||
|
|
||||||
all: $(HEX)
|
all: $(HEX)
|
||||||
|
|
||||||
$(HEX): barista.ino
|
$(HEX): barista.ino ntc.c ntc.h pinout.h
|
||||||
arduino-cli compile $(OPTS) -e --fqbn $(FQBN)
|
arduino-cli compile $(OPTS) -e --fqbn $(FQBN)
|
||||||
|
|
||||||
upload: $(HEX)
|
upload: $(HEX)
|
||||||
@ -15,3 +23,13 @@ upload: $(HEX)
|
|||||||
|
|
||||||
serial:
|
serial:
|
||||||
picocom -b 115200 --lower-rts --lower-dtr /dev/ttyUSB0 --imap lfcrlf
|
picocom -b 115200 --lower-rts --lower-dtr /dev/ttyUSB0 --imap lfcrlf
|
||||||
|
|
||||||
|
test: test_ntc
|
||||||
|
|
||||||
|
test_ntc: test/test_ntc.o ntc.o
|
||||||
|
gcc $^ -o $@ $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f test/test_ntc.o ntc.o
|
||||||
|
|
||||||
|
.PHONY: test all clean
|
||||||
|
|||||||
@ -1,64 +1,8 @@
|
|||||||
/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
|
/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
|
||||||
* SPDX-License-Identifier: GPL-3.0-or-later */
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||||
|
|
||||||
/*
|
#include "ntc.h"
|
||||||
* ATmega328p
|
#include "pinout.h"
|
||||||
* +---------+
|
|
||||||
* (PCINT14/RESET) PC6 -|1 \_/ 28|- PC5 (ADC5/SCL/PCINT13)
|
|
||||||
* (PCINT16/RXD) PD0 -|2 27|- PC4 (ADC4/SDA/PCINT12)
|
|
||||||
* (PCINT17/TXD) PD1 -|3 26|- PC3 (ADC3/PCINT11)
|
|
||||||
* (PCINT18/INT0) PD2 -|4 25|- PC2 (ADC2/PCINT10)
|
|
||||||
* (PCINT19/OC2B/INT1) PD3 -|5 24|- PC1 (ADC1/PCINT9)
|
|
||||||
* (PCINT20/XCK/T0) PD4 -|6 23|- PC0 (ADC0/PCINT8)
|
|
||||||
* VCC -|7 22|- GND
|
|
||||||
* GND -|8 21|- AREF
|
|
||||||
* (PCINT6/XTAL1/TOSC1) PB6 -|9 20|- AVCC
|
|
||||||
* (PCINT7/XTAL2/TOSC2) PB7 -|10 19|- PB5 (SCK/PCINT5)
|
|
||||||
* (PCINT21/OC0B/T1) PD5 -|11 18|- PB4 (MISO/PCINT4)
|
|
||||||
* (PCINT22/OC0A/AIN0) PD6 -|12 17|- PB3 (MOSI/OC2A/PCINT3)
|
|
||||||
* (PCINT23/AIN1) PD7 -|13 16|- PB2 (SS/OC1B/PCINT2)
|
|
||||||
* (PCINT0/CLKO/ICP1) PB0 -|14 15|- PB1 (OC1A/PCINT1)
|
|
||||||
* +---------+
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* ATMEL ATMEGA8 & 168 / ARDUINO
|
|
||||||
*
|
|
||||||
* +-\/-+
|
|
||||||
* PC6 1| |28 PC5 (AI 5)
|
|
||||||
* (D 0) PD0 2| |27 PC4 (AI 4)
|
|
||||||
* (D 1) PD1 3| |26 PC3 (AI 3)
|
|
||||||
* (D 2) PD2 4| |25 PC2 (AI 2)
|
|
||||||
* PWM+ (D 3) PD3 5| |24 PC1 (AI 1)
|
|
||||||
* (D 4) PD4 6| |23 PC0 (AI 0)
|
|
||||||
* VCC 7| |22 GND
|
|
||||||
* GND 8| |21 AREF
|
|
||||||
* PB6 9| |20 AVCC
|
|
||||||
* PB7 10| |19 PB5 (D 13)
|
|
||||||
* PWM+ (D 5) PD5 11| |18 PB4 (D 12)
|
|
||||||
* PWM+ (D 6) PD6 12| |17 PB3 (D 11) PWM
|
|
||||||
* (D 7) PD7 13| |16 PB2 (D 10) PWM
|
|
||||||
* (D 8) PB0 14| |15 PB1 (D 9) PWM
|
|
||||||
* +----+
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
enum pinout {
|
|
||||||
/* Inputs */
|
|
||||||
PIN_POWER_ON = 8,
|
|
||||||
PIN_HOT = 9,
|
|
||||||
//PIN_FLOW = PIN_D5,
|
|
||||||
|
|
||||||
/* Outputs */
|
|
||||||
PIN_LED_GREEN = 5,
|
|
||||||
PIN_LED_RED = 6,
|
|
||||||
PIN_HEAT = 7,
|
|
||||||
PIN_PUMP = 12,
|
|
||||||
PIN_BUZZ = 10,
|
|
||||||
|
|
||||||
/* Analog */
|
|
||||||
PIN_NTC = PIN_A0,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum logic {
|
enum logic {
|
||||||
ON = 1,
|
ON = 1,
|
||||||
@ -66,8 +10,8 @@ enum logic {
|
|||||||
};
|
};
|
||||||
|
|
||||||
#define DEBOUNCE_TIME 20L /* ms */
|
#define DEBOUNCE_TIME 20L /* ms */
|
||||||
#define TEMP_MIN 600
|
#define TEMP_MIN 80.0f
|
||||||
#define TEMP_MAX 700
|
#define TEMP_MAX 90.0f
|
||||||
|
|
||||||
#define LED_MIN_VALUE 0
|
#define LED_MIN_VALUE 0
|
||||||
|
|
||||||
@ -114,6 +58,10 @@ int button_state[MAX_BUTTON];
|
|||||||
unsigned long button_press_t0[MAX_BUTTON];
|
unsigned long button_press_t0[MAX_BUTTON];
|
||||||
unsigned long button_release_t0[MAX_BUTTON];
|
unsigned long button_release_t0[MAX_BUTTON];
|
||||||
|
|
||||||
|
float ntc_R;
|
||||||
|
float ntc_T;
|
||||||
|
|
||||||
|
|
||||||
int read_input(int pin)
|
int read_input(int pin)
|
||||||
{
|
{
|
||||||
return !digitalRead(pin);
|
return !digitalRead(pin);
|
||||||
@ -170,6 +118,13 @@ void update_buttons()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void update_ntc()
|
||||||
|
{
|
||||||
|
int Vo = analogRead(PIN_NTC);
|
||||||
|
ntc_R = ntc_resistance(Vo);
|
||||||
|
ntc_T = ntc_temp(ntc_R);
|
||||||
|
}
|
||||||
|
|
||||||
int red_min = 50;
|
int red_min = 50;
|
||||||
int red_state = red_min;
|
int red_state = red_min;
|
||||||
unsigned long brewing_time = 3000UL; /* 3 seconds */
|
unsigned long brewing_time = 3000UL; /* 3 seconds */
|
||||||
@ -274,11 +229,25 @@ update_leds()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return the temperature in celsisus */
|
||||||
|
float
|
||||||
|
measure_temp()
|
||||||
|
{
|
||||||
|
int Vo = analogRead(PIN_NTC);
|
||||||
|
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
|
||||||
|
float R1 = 10e3; /* 10 kOhm resistor */
|
||||||
|
float R2 = R1 * (1023.0 / (float)Vo - 1.0);
|
||||||
|
float logR2 = log(R2);
|
||||||
|
float T = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
|
||||||
|
float Tc = T - 273.15;
|
||||||
|
return Tc;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
update_heater()
|
update_heater()
|
||||||
{
|
{
|
||||||
if (state == HEATING || state == HOT || state == BREWING) {
|
if (state == HEATING || state == HOT || state == BREWING) {
|
||||||
int temp = analogRead(PIN_NTC);
|
int temp = measure_temp();
|
||||||
if (temp < TEMP_MIN)
|
if (temp < TEMP_MIN)
|
||||||
relay(PIN_HEAT, ON);
|
relay(PIN_HEAT, ON);
|
||||||
else if (temp > TEMP_MAX)
|
else if (temp > TEMP_MAX)
|
||||||
@ -318,6 +287,20 @@ update_buzz()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void inputs()
|
||||||
|
{
|
||||||
|
update_buttons();
|
||||||
|
update_ntc();
|
||||||
|
}
|
||||||
|
|
||||||
|
void outputs()
|
||||||
|
{
|
||||||
|
update_leds();
|
||||||
|
update_heater();
|
||||||
|
update_pump();
|
||||||
|
update_buzz();
|
||||||
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
@ -340,12 +323,9 @@ void setup()
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
//Serial.println("Looping...");
|
inputs();
|
||||||
update_buttons();
|
|
||||||
progress();
|
progress();
|
||||||
update_leds();
|
outputs();
|
||||||
update_heater();
|
|
||||||
update_pump();
|
|
||||||
update_buzz();
|
|
||||||
delay(5);
|
delay(5);
|
||||||
}
|
}
|
||||||
|
|||||||
44
barista/ntc.c
Normal file
44
barista/ntc.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/* Steinhart-Hart Thermistor Coefficients, used to convert resistance into
|
||||||
|
* temperature.
|
||||||
|
*
|
||||||
|
* The current NTC sensor has 102kOhm at 24C but I don't know the specific
|
||||||
|
* model, so the coefficients are computed for this NTC sensor instead:
|
||||||
|
* https://www.tme.eu/Document/f9d2f5e38227fc1c7d979e546ff51768/NTCM-100K-B3950.pdf
|
||||||
|
*
|
||||||
|
* The table seems to match what I would expect. Their R2 resistor is 6.8 kOhm,
|
||||||
|
* which yields a cuttof temperature of around 98.5 C at exactly half voltage
|
||||||
|
* (where the ADC would have more precision).
|
||||||
|
*
|
||||||
|
* In any case, we can calibrate the original NTC sensor by taking three
|
||||||
|
* temperature points. See:
|
||||||
|
* https://www.thinksrs.com/downloads/programs/therm%20calc/ntccalibrator/ntccalculator.html
|
||||||
|
*/
|
||||||
|
#define C1 0.7740577674e-3
|
||||||
|
#define C2 2.073449619e-4
|
||||||
|
#define C3 1.263502259e-7
|
||||||
|
|
||||||
|
/* Return the temperature in celsisus */
|
||||||
|
float
|
||||||
|
ntc_temp(float R)
|
||||||
|
{
|
||||||
|
/* Computing the log is slow, we may want to build a table */
|
||||||
|
float logR = log(R);
|
||||||
|
float T = (1.0 / (C1 + C2*logR + C3*logR*logR*logR));
|
||||||
|
float Tc = T - 273.15;
|
||||||
|
return Tc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return resistance in Ohms */
|
||||||
|
float
|
||||||
|
ntc_resistance(int Vo)
|
||||||
|
{
|
||||||
|
float R1 = 6.8e3; /* Resistor for voltage divider */
|
||||||
|
float R2 = R1 * (1023.0 / (float)Vo - 1.0);
|
||||||
|
|
||||||
|
return R2;
|
||||||
|
}
|
||||||
18
barista/ntc.h
Normal file
18
barista/ntc.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||||
|
|
||||||
|
#ifndef BARISTA_NTC_H
|
||||||
|
#define BARISTA_NTC_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
float ntc_resistance(int Vo);
|
||||||
|
float ntc_temp(float omhs);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BARISTA_NTC_H */
|
||||||
78
barista/pinout.h
Normal file
78
barista/pinout.h
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/* Copyright (c) 2025 Rodrigo Arias Mallo <rodarima@gmail.com>
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-or-later */
|
||||||
|
|
||||||
|
#ifndef BARISTA_PINOUT_H
|
||||||
|
#define BARISTA_PINOUT_H
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ATmega328p
|
||||||
|
* +---------+
|
||||||
|
* (PCINT14/RESET) PC6 -|1 \_/ 28|- PC5 (ADC5/SCL/PCINT13)
|
||||||
|
* (PCINT16/RXD) PD0 -|2 27|- PC4 (ADC4/SDA/PCINT12)
|
||||||
|
* (PCINT17/TXD) PD1 -|3 26|- PC3 (ADC3/PCINT11)
|
||||||
|
* (PCINT18/INT0) PD2 -|4 25|- PC2 (ADC2/PCINT10)
|
||||||
|
* (PCINT19/OC2B/INT1) PD3 -|5 24|- PC1 (ADC1/PCINT9)
|
||||||
|
* (PCINT20/XCK/T0) PD4 -|6 23|- PC0 (ADC0/PCINT8)
|
||||||
|
* VCC -|7 22|- GND
|
||||||
|
* GND -|8 21|- AREF
|
||||||
|
* (PCINT6/XTAL1/TOSC1) PB6 -|9 20|- AVCC
|
||||||
|
* (PCINT7/XTAL2/TOSC2) PB7 -|10 19|- PB5 (SCK/PCINT5)
|
||||||
|
* (PCINT21/OC0B/T1) PD5 -|11 18|- PB4 (MISO/PCINT4)
|
||||||
|
* (PCINT22/OC0A/AIN0) PD6 -|12 17|- PB3 (MOSI/OC2A/PCINT3)
|
||||||
|
* (PCINT23/AIN1) PD7 -|13 16|- PB2 (SS/OC1B/PCINT2)
|
||||||
|
* (PCINT0/CLKO/ICP1) PB0 -|14 15|- PB1 (OC1A/PCINT1)
|
||||||
|
* +---------+
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* ATMEL ATMEGA8 & 168 / ARDUINO
|
||||||
|
*
|
||||||
|
* +-\/-+
|
||||||
|
* PC6 1| |28 PC5 (AI 5)
|
||||||
|
* (D 0) PD0 2| |27 PC4 (AI 4)
|
||||||
|
* (D 1) PD1 3| |26 PC3 (AI 3)
|
||||||
|
* (D 2) PD2 4| |25 PC2 (AI 2)
|
||||||
|
* PWM+ (D 3) PD3 5| |24 PC1 (AI 1)
|
||||||
|
* (D 4) PD4 6| |23 PC0 (AI 0)
|
||||||
|
* VCC 7| |22 GND
|
||||||
|
* GND 8| |21 AREF
|
||||||
|
* PB6 9| |20 AVCC
|
||||||
|
* PB7 10| |19 PB5 (D 13)
|
||||||
|
* PWM+ (D 5) PD5 11| |18 PB4 (D 12)
|
||||||
|
* PWM+ (D 6) PD6 12| |17 PB3 (D 11) PWM
|
||||||
|
* (D 7) PD7 13| |16 PB2 (D 10) PWM
|
||||||
|
* (D 8) PB0 14| |15 PB1 (D 9) PWM
|
||||||
|
* +----+
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
enum pinout {
|
||||||
|
/* Inputs */
|
||||||
|
PIN_POWER_ON = 8,
|
||||||
|
PIN_HOT = 9,
|
||||||
|
//PIN_FLOW = PIN_D5,
|
||||||
|
|
||||||
|
/* Outputs */
|
||||||
|
PIN_LED_GREEN = 5,
|
||||||
|
PIN_LED_RED = 6,
|
||||||
|
PIN_HEAT = 7,
|
||||||
|
PIN_PUMP = 12,
|
||||||
|
PIN_BUZZ = 10,
|
||||||
|
|
||||||
|
/* Analog */
|
||||||
|
PIN_NTC = PIN_A0,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BARISTA_PINOUT_H */
|
||||||
1
barista/test/Makefile
Normal file
1
barista/test/Makefile
Normal file
@ -0,0 +1 @@
|
|||||||
|
test_ntc: test_ntc.o ../ntc.c
|
||||||
15
barista/test/test_ntc.c
Normal file
15
barista/test/test_ntc.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "ntc.h"
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
for (int i = 0; i <= 1023; i++) {
|
||||||
|
float R = ntc_resistance(i);
|
||||||
|
float T = ntc_temp(R);
|
||||||
|
if (T < 96.0 || T > 100.2)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
printf("%6d %12.1f Ohm %12.1f C\n", i, R, T);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user