logo elektroda
logo elektroda
X
logo elektroda

[ESP32] How to change Sonoff THR320D firmware? Flashing Tasmota. Where is GPIO0?

p.kaczmarek2 4590 11
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • Sonoff THR320D thermostat with a display showing 23.8°C.
    Some time ago I showed the Sonoff THR320D thermostat with display and presented the possibilities of the eWeLink application. This time I will continue discussing this product, but here I will focus on its interior and on changing its firmware to Tasmota, so as to completely free it from the cloud and pair it with Home Assistant on our terms.

    Previous topic:
    [ESP32] Thermostat with display and WiFi - Sonoff THR320D - eWeLink application

    Information about purchasing the product is in the previous topic, so let's start with the inside.
    THR320D interior
    This product is distinguished by its solid construction. There are no breakable hooks. Unscrew the screws:
    Back cover of the Sonoff THR320D thermostat on a wooden surface. Sonoff THR320D thermostat case lying on a wooden surface.
    At first glance it's not that bad, there are some protections at the input, there is even a varistor, a filter capacitor, you can also see a relay, a programming connector and a small transformer with a characteristic Y capacitor, i.e. there is a flyback power supply inside..
    Interior of a disassembled Sonoff THR320D thermostat with visible electronic components. Interior of the Sonoff THR320D device with visible PCB and electronic components.
    There is no WiFi module inside, ESP32 is located directly on the PCB, you can see the flash memory right next to it:
    Interior of the Sonoff THR320D thermostat showing the PCB.
    The PCB can be removed and we will see the display controller and the WiFi antenna:
    Sonoff THR320D circuit board with TM1621B chip. Image of the interior of the Sonoff THR320D thermostat showing the PCB and casing.
    The display supports TM1621:
    Documentation page for the TM1621 LCD driver from Titan Micro Electronics. Pin diagram of the TM1621 LCD driver from Titan Micro Electronics. Diagram of the TM1621 LCD driver by Titan Micro Electronics
    We also see details of the power supply section and the LP3667 controller:
    Close-up of the Sonoff THR320D thermostat PCB.
    A simplified diagram of such a converter:
    Schematic diagram of a flyback converter with LP3667
    In the corner I also see a step down converter generating 3.3V for ESP, you can also tell it by the presence of a choke, by the way, it's interesting that they put so many ceramic capacitors there:
    PCB of Sonoff THR320D thermostat with TM1621B chip. Sonoff THR320D circuit board with visible electronic components
    The already mentioned WiFi antenna:
    Interior of the Sonoff THR320D thermostat casing with visible components.
    ESP:
    Close-up of the PCB from the Sonoff THR320D thermostat with visible electronic components.
    The photo above also shows programming pads, but there are only 4...

    Firmware change
    You need to start with the fact that IO0 is located on the button, so we solder 4 wires, not 5. As standard, a USB to UART converter (RX and TX) and ground and power supply (3.3V). To load the firmwarwe, first press the button and then connect the programmer's power supply so that IO0 is at low level while the device is booting.
    Then you can upload Tasmota, e.g. using the online installer:
    How to easily install Tasmota - online installer tasmota.github.io/install via a web browser
    However, I used esptool myself:
    Windows console with esptool program running for ESP32. Console with firmware loading process on ESP32. Console output showing the flashing process of an ESP32 chip using esptool.
    The THR320D template is available online:
    {"NAME":"Sonoff THR320D","GPIO":[32,0,0,0,226,9280,0,0,0,321,0,576,320,9184,9216,9312,0,0,9313,9248,0,1,0,3840,0,0,0,0,0,0,0,0,0,0,0,0],"FLAG":0,"BASE":1}
    

    GPIO roles:
    - GPIO00 - Button 1
    - GPIO04 - Relay 3
    - GPIO05 - TM1621 DAT
    - GPIO13 - Led_i 2
    - GPIO15 - LedLinks
    - GPIO16 - Led_i 1
    - GPIO17 - TM1621 CS
    - GPIO18 - TM1621 WR
    - GPIO19 - Relay_b 1
    - GPIO22 - Relay_b 2
    - GPIO23 - TM1621 RD
    - GPIO27 - Output Hi
    Template activation:
    Screenshot of Tasmota interface on ESP32-DevKit
    Then you need to select the roles for the GPIO sensors, because there may be different sensors, I only checked with DS18B20:
    Screenshot of the Tasmota configuration interface for Sonoff THR320D.
    Result:
    Tasmota user interface with DS18B20 temperature on a Sonoff THR320D thermostat
    Of course, the display still works, the template takes into account its operation, although now it no longer displays anything about humidity like the original firmware:
    Sonoff THR320D thermostat displaying temperature 34.2°C Sonoff THR320D thermostat with display showing a temperature of 35.2°C.
    And the temperature in the photo is high because I heated the sensor with a lamp to test:
    Close-up of a hand holding a wire next to an LED strip.
    Everything seems to be working.

    Summary
    I already mentioned it in the previous part, but I highly praise all IoT products that are able to operate completely without WiFi, and the ability to read the current temperature from this display without reaching for the phone is certainly a step in the right direction. The button on the housing also increases the possibility of "physical" control, although we will not change the schedules, but it is not that important.
    Of course, uploading Tasmota is only the first step, you still need to configure Home Assistant, but that shouldn't be a problem. Automatic HASS Discovery from Tasmota alone should do the trick.
    In one of the next topics, I plan to discuss how we can use our Sonoff THR320D together with HA, e.g. to control heating, but that will come later...
    To sum up - I recommend this product. Has anyone reading this already used it?

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11923 posts with rating 9986, helped 572 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 20795121
    khoam
    Level 42  
    p.kaczmarek2 wrote:
    Of course, the display still works, the template takes into account its operation, although now it no longer displays anything about humidity like the original firmware:

    You can try to install the ESPHome firmware according to the template:
    https://devices.esphome.io/devices/Sonoff-THR320D
    Then there should also be a humidity measurement.
  • ADVERTISEMENT
  • #3 20795155
    p.kaczmarek2
    Moderator Smart Home
    In this particular case, I still have the DS18B20 probe connected, so it probably won't measure humidity, I was more referring to the fact that the original firmware displayed the placeholder values with dashes, and Tasmota displays nothing... but would Tasmota handle it if it was there? I haven't checked SI7021 and not DS18B20, but it would probably work. I'll check it out when I have a chance.
    Helpful post? Buy me a coffee.
  • #4 21050030
    szczukot
    Level 14  
    Energy consumption meter displaying 320.8 kWh and 260 W with a phone app.

    Some photos from the Internet display different data.
    Is this an error or is it actually possible?
  • ADVERTISEMENT
  • #5 21050124
    p.kaczmarek2
    Moderator Smart Home
    Your photo shows a slightly different product, viz POWR316D , which, by the way, I have already discussed:
    [ESP32] Sonoff POWR316D energy meter with display - firmware change
    Helpful post? Buy me a coffee.
  • #6 21050562
    szczukot
    Level 14  
    Well, that`s all clear. Many sellers confuse them, give a photo of one and sell the other ;)
  • #7 21193012
    HellG
    Level 2  
    I would like to connect sensors via I2C to the THR320D. Do you think this is possible? Is there a spare GPIO that I could use or do I need to sacrifice e.g. one of the relays?
  • #8 21201178
    HellG
    Level 2  
    Hi,
    I digged a little bit deeper and found out, that PIN3 of the sensor connector has a measuring pad marked with RX1 in this picture https://obrazki.elektroda.pl/1409760600_1695907567.jpg (partially hidden by ESD2). It is wired close to the ESP32 to the soldering pads of R16 and R25 (see https://obrazki.elektroda.pl/4154304600_1695907654.jpg ) which are unfortunately not populated. Via R16 it is connected to GPIO26 (the other data port is connected via R14 to GPIO25). Do you have any idea, what resistor values to solder there? I think R16 should be rather small and R25 looks like a pull up or pull down resistor and should be rather high. I can't mesure the resistance while soldered.
    Cheers,
    HellG
  • #9 21201469
    p.kaczmarek2
    Moderator Smart Home
    What kind of sensor would you like to connect? I'd suggest to choose the resistors for the sensor you have.

    For example, here is how a DHT11 connection should look like:
    Diagram of DHT11 sensor connection with a Nano board, showing wiring and a resistor layout.
    Helpful post? Buy me a coffee.
  • #10 21201532
    HellG
    Level 2  
    I want to connect several sensors via I2C, that’s why I need an additional GPIO.
  • ADVERTISEMENT
  • #11 21201629
    p.kaczmarek2
    Moderator Smart Home
    Both I2C lines will require pull-up resistors, here is some random sample from the web:
    I2C circuit with pull-up resistors and devices: master and slaves.

    @ElektrodaBot which pull up resistor values should be used for I2C?
    Helpful post? Buy me a coffee.
  • #12 21201640
    ElektrodaBot
    AI OpenAI ChatGPT4
    p.kaczmarek2 wrote:
    which pull up resistor values should be used for I2C?
    Direct Answer to the User's Question

    For I2C communication, typical pull-up resistor values range from 1kΩ to 10kΩ. The most commonly used values are often in the 2.2kΩ to 4.7kΩ range. However, the optimal value depends on several factors including bus speed, bus capacitance, number of devices on the bus, and power supply voltage.

    Detailed Step-by-Step Problem Analysis

    1. Bus Speed:
    - Standard Mode (100 kHz): 1.5kΩ to 10kΩ
    - Fast Mode (400 kHz): 1.5kΩ to 4.7kΩ
    - Fast Mode Plus (1 MHz): 1kΩ to 2.2kΩ

    2. Bus Capacitance:
    - Higher capacitance requires lower resistor values to ensure the signal can rise quickly enough. Typical bus capacitance ranges from 10pF to 400pF.

    3. Number of Devices on the Bus:
    - More devices increase the total bus capacitance, which may necessitate lower resistor values.

    4. Power Supply Voltage:
    - Higher supply voltages allow for higher resistor values. For example, a 5V system might use higher resistor values compared to a 3.3V system.

    Supporting Details and Explanations

    Calculating Pull-Up Resistor Values

    1. Minimum Pull-Up Resistor Value:
    - The minimum value can be calculated using:
    $$ R_{\text{min}} = \frac{V_{\text{DD}} - V_{\text{OL\_max}}}{I_{\text{OL\_max}}} $$
    where $ V_{\text{DD}} $ is the supply voltage, $ V_{\text{OL\_max}} $ is the maximum low-level output voltage (typically 0.4V), and $ I_{\text{OL\_max}} $ is the maximum low-level output current of the weakest device on the bus.

    2. Maximum Pull-Up Resistor Value:
    - The maximum value can be calculated using:
    $$ R_{\text{max}} = \frac{t_r}{0.8473 \times C_b} $$
    where $ t_r $ is the maximum allowed rise time (e.g., 1000ns for standard mode, 300ns for fast mode), and $ C_b $ is the bus capacitance.

    Practical Example

    Assume a Fast-mode I2C bus (400 kHz) with a total bus capacitance of 200 pF. Using the I2C specified maximum rise time for Fast-mode (300 ns):

    $$ R_{\text{max}} = \frac{300 \times 10^{-9}}{0.8473 \times 200 \times 10^{-12}} \approx 1.77 \ \text{kΩ} $$

    Caveats and Additional Considerations

    1. Temperature Variations: Resistor values can change with temperature, affecting rise times.
    2. Voltage-Level Translations: When connecting devices with different voltage levels, special considerations for pull-up resistors are needed.
    3. Long Bus Lines: For very long I2C buses, active pull-up circuits or I2C bus extenders might be necessary.
    4. EMI Considerations: Lower resistor values can help reduce susceptibility to electromagnetic interference but increase power consumption.
    5. Multi-Power Domain Systems: Care must be taken when the I2C bus crosses power domains to avoid current leakage and potential damage to devices.

    Conclusion

    In practice, it is often best to start with standard values like 4.7kΩ and adjust based on oscilloscope measurements of the actual rise times on your specific hardware setup. This empirical approach can help optimize the performance of your I2C bus for your particular application. Always verify the signal integrity with an oscilloscope, especially for high-speed applications, to ensure the chosen pull-up resistors provide reliable operation.
    You can call me in any topic by writing @ElektrodaBot. Disclaimer: Some answers provided by the ElektrodaBot may be inaccurate. Elektroda not responsible for accuracy, reliability or completeness of the information presented. All ElektrodaBot responses should be verified by the user. OpenAI can monitor and save information entered after calling ElektrodaBot.

Topic summary

The discussion focuses on modifying the firmware of the Sonoff THR320D thermostat to Tasmota, enabling local control and integration with Home Assistant. Users share insights on the device's internal components, including GPIO pin configurations for sensor connections. The conversation also touches on the potential for using I2C sensors and the necessary pull-up resistor values for I2C communication. There is clarification regarding the distinction between the THR320D and a similar model, POWR316D, highlighting common confusion among sellers.
Summary generated by the language model.
ADVERTISEMENT