ESP32-S2 恒温控系统:电阻加热与 NST112 传感器实战ESP32-S2 Thermostatic Control With Resistive Heating and NST112


AI free token/AI免费积分

ESP32-S2 恒温控系统:电阻加热与 NST112 传感器实战

基于电阻加热与 NST112 的 ESP32-S2 恒温控制

在 ESP32-S2 开发套件上搭建一个闭环加热板:NST112 I2C 温度传感、TFT_eSPI 仪表盘、通过 LEDC PWM 驱动加热 MOSFET,以及通过电阻阶梯(resistor-ladder)ADC 通道实现编码器/按键输入。

本项目在一块 ESP32-S2 学习套件上实现恒温控制回路:电阻式加热器加热一小块隔热区域,NST112 数字温度传感器上报板卡温度,1.44 英寸 ST7735 LCD 以仪表盘样式显示读数。PWM 驱动 MOSFET 加热开关,PID 式调节器调整占空比。一个共用 ADC 引脚读取电阻阶梯网络,用于按键和旋转编码器。

前提:具体的 GPIO 分配、原理图中的网络名称以及 TFT_eSPI 的完整 User_Setup.h 都与您的供应商开发板套件相匹配——请将下面的代码片段视为参考模式,而非可直接烧录的完整固件。

ESP32-S2 是一款单核 Xtensa LX7 SoC,侧重 2.4 GHz Wi-Fi、丰富的 GPIO,以及在 ESP-IDF 或 Arduino 下的 USB 可编程能力。典型的开发板还会配备 USB-C、按钮、LED,以及两组引出可用 IO 的排针。

4 MB 闪存(根据具体模块不同,还有可选的 PSRAM 版本)。

按键与旋转编码器(通过模拟电阻网络接入)。

当 MOSFET 导通时,加热器使板卡温度升高。固件读取温度,与设定值(setpoint)比较,并更新 PWM 占空比以调节平均功率。供应商针对加热网络的计算(示例):在 3.3 V 供电和并联 68 Ω 元件的情况下,(P ≈ U² / R_eq) 的量级约为 ~0.6 W——在做好隔热时足以形成一个演示用的热点(请在您的原理图上核实)。

使用 PlatformIO + VS Code 进行构建、烧录和库管理。

使用一个串口终端(CoolTerm、PuTTY、PIO monitor 等)查看调试跟踪信息。

固件在 ESP32-S2 上采用 Arduino 框架,以便快速上手调试。

使用 TFT_eSPI 渲染 UI(仪表盘样式的视图改编自 TFT_eSPI 的模拟仪表盘示例)。

快速采样 ADC,从电阻阶梯网络中解码编码器的步进和按键状态。

NST112-DSTR 是一款小型数字传感器,具有简单的寄存器模型。下面是一个精简的初始化/读取模式;7 位地址和寄存器字节必须与您的开发板接线及数据手册配置相匹配。

注意:请根据 Novosns 数据手册并按您的精度目标配置转换速率/平均次数;上面的常量沿用原始课程设计草稿中的设置。

课程设计的 UI 使用 TFT_eSPI,并配有模拟仪表盘渲染器和活动指针(analogMeter()、plotNeedle()、位图资源)。这段代码较长,且与精灵(sprite)尺寸和字体索引紧密耦合——请将其保留在您的代码仓库中,并针对 ST7735、SPI 引脚和旋转方向调整 User_Setup.h。

使用 LEDC 为 MOSFET 驱动生成稳定的 PWM:

占空比越高,加热器的平均功率越大;占空比越低则越小。务必遵守安全温度限制和绝缘要求——演示开发板并非工业烤箱。

原项目采用带积分分离的位置式 PID 以限制超调;在测试中,由于被控对象(plant)响应慢且具有惯性,类似 PD 的整定通常就足够了。

针对常见课程设计代码粘贴错误的修复:constrain() 会返回一个值——请将其赋值回目标变量。

如果传感器更新路径为 8 Hz,则让调节器以相同的节奏(125 ms)运行,使测量与执行保持同步——通常使用硬件定时器或 Ticker 回调,例如:

串口遥测修复:使格式说明符与参数相匹配(原始代码在某个分支中用不匹配的格式字符串打印了两个浮点数)。

扩展板通过电阻分压网络将编码器相位和按键复用(multiplex)到一个 ADC 节点上。在约 200 Hz 的采样率下,顺时针与逆时针旋转的步进模式不同;固件将原始码值与校准后的 ADC 中心值进行阈值比较。

课程设计中的 ADC 轨迹示例(顺时针旋转、逆时针旋转、编码器按下、按键):

解码逻辑会测量基准 ADC 区间(原代码中为 ADC_Val[]),应用一个容差窗口,并根据各状态之间极小值的出现顺序推断旋转方向。如果您要在不同开发板之间切换,请将校准值保存在 flash 或 EEPROM 中。

按键:选择您要编辑设定值的哪一位(在原始 UX 中为十位/个位/十分位)。

当 |error| < ~1 °C(比源笔记中更早的课程设计规格 ±3 °C 更严格)时,UI 显示 OK,状态 LED 反映调节状态。

ADC 阶梯解码是 UX 中最棘手的部分:您需要快速、干净的采样,以及针对每块开发板校准的阈值。

热惯性很大:需要依赖微分作用(derivative action)和保守的积分饱和(integrator windup)限制;如果您在意绝对精度,务必用真实温度探头进行验证。

完善 HMI,增强 ADC 通道的抗噪声能力,并按设备持久化保存校准常量。

积分分离 PID 的背景资料(博客,中文)

本文来源:https://www.esp32cube.com/post/esp32-s2-thermostatic-heating-control
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

ESP32-S2 Thermostatic Control With Resistive Heating and NST112

Build a closed-loop plate heater on an ESP32-S2 kit: NST112 I2C sensing, TFT_eSPI dashboard, LEDC PWM into a heating MOSFET, and encoder/key input through a resistor-ladder ADC channel.

This project implements a constant-temperature control loop on an ESP32-S2 learning kit: a resistive heater warms a small insulated area, an NST112 digital temperature sensor reports board temperature, and a 1.44" ST7735 LCD shows a gauge-style readout. PWM drives a MOSFET heater switch; a PID-style regulator adjusts duty cycle. A shared ADC pin reads a resistor-ladder network for keys and a rotary encoder .

Assumption: Exact GPIO assignments, schematic net names, and complete User_Setup.h for TFT_eSPI match your vendor board package—treat the snippets below as patterns , not a full firmware drop-in.

The ESP32-S2 is a single-core Xtensa LX7 SoC focused on 2.4 GHz Wi‑Fi , rich GPIO , and USB programmability under ESP-IDF or Arduino . Typical dev boards add USB-C , buttons, LEDs, and two headers that break out usable IO.

4 MB flash (and optional PSRAM variants depending on exact module).

Keys and rotary encoder (routed through an analog resistor network ).

The heater raises board temperature when the MOSFET conducts. Firmware reads temperature, compares to a setpoint , and updates PWM duty to regulate average power. Vendor math for the heater network (illustrative): (P \approx U^2 / R_\text{eq}) with 3.3 V rails and parallel 68 Ω elements yields on the order of ~0.6 W —enough for a demo hotspot when insulated (verify on your schematic).

PlatformIO + VS Code for build, flash, and libraries.

A serial terminal ( CoolTerm , PuTTY , PIO monitor , and so on) for debug traces.

Firmware uses the Arduino framework on ESP32-S2 for fast bring-up.

Render UI with TFT_eSPI (gauge-style view adapted from TFT_eSPI analog meter examples).

Sample ADC quickly to decode encoder steps and button states from the resistor ladder.

The NST112-DSTR is a small digital sensor with a simple register model. Below is a compact init/read pattern; 7-bit address and register bytes must match your board wiring and datasheet configuration.

Note: Configure conversion rate / averaging per the Novosns datasheet for your accuracy goals; the constants above follow the original coursework sketch.

The coursework UI uses TFT_eSPI with an analog meter renderer and a moving needle ( analogMeter() , plotNeedle() , bitmap assets). That code is lengthy and tightly coupled to sprite sizes and font indices —keep it in your repository and tune User_Setup.h for ST7735 , SPI pins , and rotation .

Use LEDC to generate a stable PWM for the MOSFET driver:

High duty increases average heater power; low duty reduces it. Always respect safe temperature limits and insulation —demo boards are not industrial ovens.

The original project uses a position PID with integral separation to limit overshoot; in testing, PD-like tuning was often sufficient because the plant is slow and inertial .

Bugfix vs a common coursework paste: constrain() returns a value— assign it back to the target variable.

If the sensor update path is 8 Hz , run the regulator on the same cadence ( 125 ms ) so measurement and actuation stay aligned—typically a hardware timer or a Ticker callback that:

Serial telemetry fix: match format specifiers to arguments (original code printed two floats with a mismatched format string in one branch).

The expansion board multiplexes encoder phases and buttons into one ADC node via resistor dividers. At ~200 Hz sampling, step patterns differ for clockwise vs counter-clockwise rotation; the firmware thresholds raw codes against calibrated ADC centers .

Example ADC traces from the coursework (CW rotation, CCW rotation, encoder push, key):

Decoding logic measures baseline ADC bins ( ADC_Val[] in the original), applies a tolerance window, and infers rotation direction by the sequence of minima between states. Keep calibration values in flash or EEPROM if you move between boards.

Key: selects which digit of the setpoint you edit (tens / ones / tenths in the original UX).

When |error| < ~1 °C (stricter than an earlier ±3 °C coursework spec in the source notes), the UI shows OK and status LEDs reflect regulation state.

ADC ladder decoding is the trickiest UX piece: you need fast, clean sampling and calibrated thresholds per board.

Thermal inertia is large: expect to lean on derivative action and conservative integrator windup limits; validate always with a real temperature probe if you care about absolute accuracy.

Polish the HMI, harden noise rejection on the ADC channel, and persist calibration constants per device.

Integral-separated PID background (blog, Chinese)

本文来源:https://www.esp32cube.com/post/esp32-s2-thermostatic-heating-control
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。

暂无评论,快来发表第一条评论吧!No comments yet. Be the first!

📮 需求咨询📮 Inquiry