Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active November 8, 2023 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/90bc866c7a570791641475d0edb6b961 to your computer and use it in GitHub Desktop.
Save andersevenrud/90bc866c7a570791641475d0edb6b961 to your computer and use it in GitHub Desktop.
Arduino Temperature Monitor Hub for Klipper

Arduino Temperature Monitor Hub for Klipper

I wanted to monitor temperatures in certain areas on my printer1 with Klipper and came up with the following solution.

BoM

Assumes you have access to soldering iron etc.

  • Arduino or similar
  • Jumper wires
  • 10K Ohm resistors
  • 10K Ohm NTC Thermistors (I bought some cheap ones from aliexpress2)
  • Some kind of 2 pin male + female connectors (optional)

Assembly

Each sensor requires the following circuit 3:

e956efea-a09d-4663-a63a-bdc2ff4475ff

Finished product

20231108_203642

Setup

⚠️ Source code has constants/variables that needs to be configured according to your setup.

  1. Flash the board with firmware
  2. Install serial monitor
  3. Set up systemd service
  4. Set up Klipper configuration

Screenshots

192-168-0-179

Footnotes

  1. https://gist.github.com/andersevenrud/7e08c67768e7ccdc97df01d3fd034e1d

  2. https://www.aliexpress.com/item/4000129976831.html?spm=a2g0o.order_list.order_list_main.30.36f21802RBywhF

  3. https://projecthub.arduino.cc/Marcazzan_M/how-easy-is-it-to-use-a-thermistor-81ae74

// Flash this onto an Arduino or compatible device
#define RT0 10000 // Thermistor Ω
#define B 3977 // Thermistor K
#define R 10000 // Resistor Ω
#define VCC 5 // Supply voltage
// Adjustable variables
#define DELAY 1000
#define TOTAL 6
int thermistorPins[TOTAL] = { A0, A1, A2, A3, A4, A5 };
float readVoltage(int pin) {
float VRT = analogRead(pin); // Acquisition analog value of VRT
return (5.00 / 1023.00) * VRT; // Conversion to voltage
}
float convertVoltage(float VRT) {
float VR = VCC - VRT;
float RT = VRT / (VR / R); // Resistance of RT
float ln = log(RT / RT0);
float T0 = 25 + 273.15; // Celsius to kelvin
float TX = (1 / ((ln / B) + (1 / T0))); // Temperature from thermistor
return TX - 273.15; // Conversion to Celsius
}
float readThermistor(int pin) {
float VRT = readVoltage(pin); // Acquisition analog value of VRT
float TX = convertVoltage(VRT);
return TX;
}
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < TOTAL; i++) {
float TX = readThermistor(thermistorPins[i]);
long Kv = round(TX * 1000); // Converted to format Klipper understands natively
Serial.print(Kv);
if (i < TOTAL - 1) Serial.print(" ");
}
Serial.println("");
delay(DELAY);
}
# file: /etc/systemd/system/temp-monitor.service
# installation: systemd enable temp-monitor.service
# systemd start temp-monitor.service
[Unit]
Description=Arduino Temp Monitor
[Service]
ExecStart=/home/klipper/temp-monitor/monitor.sh
WorkingDirectory=/home/klipper/temp-monitor/
RestartSec=10
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=temp-monitor
User=klipper
Group=klipper
Environment=PATH=/usr/bin:/usr/local/bin
[Install]
WantedBy=multi-user.target
#!/usr/bin/env bash
# file: /home/klipper/temp-monitor/monitor.sh
# installation: chmod +x /home/klipper/temp-monitor/monitor.sh
# Adjustable variables
DEST=`pwd`/dev
PORT=/dev/serial/by-id/usb-1a86_USB2.0-Ser_-if00-port0
TOTAL=6
while read -r LINE < $PORT; do
COUNT=$(IFS=' '; set -f -- $LINE; echo $#)
if [[ $COUNT -eq $TOTAL ]]; then
I=0
for VALUE in $LINE; do
FN="$DEST/$I"
echo "$VALUE" > $FN
I=$((I+1))
done
fi
done
# Append this to the Klipper configuration file
[temperature_sensor stepper_x_casing]
sensor_type: temperature_host
sensor_path: /home/klipper/temp-monitor/dev/0
[temperature_sensor stepper_y_casing]
sensor_type: temperature_host
sensor_path: /home/klipper/temp-monitor/dev/1
[temperature_sensor stepper_z_casing]
sensor_type: temperature_host
sensor_path: /home/klipper/temp-monitor/dev/2
[temperature_sensor stepper_e_casing]
sensor_type: temperature_host
sensor_path: /home/klipper/temp-monitor/dev/3
[temperature_sensor printer_intake]
sensor_type: temperature_host
sensor_path: /home/klipper/temp-monitor/dev/4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment