logo elektroda
logo elektroda
X
logo elektroda

[BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX

gramais 16923 112
Best answers

How can I connect to the BK7231N UART RX/TX on this Tuya door sensor without desoldering the chip or module?

You don’t need to access the backside chip pins: this board uses a CBU module with the BK7231N, and its UART1 pins are already broken out and easy to reach [#20486243] The pads marked on the board are most likely R = RX, T = TX, G = GND, and V = 3.3 V; RST is reset and SWS is the serial-wire/debug pad [#20486243] The reply also points out that TX1 and RX1 are the relevant accessible UART pins on the CBU module, so you can use those for flashing/serial access instead of desoldering anything [#20486243]
Generated by the language model.
ADVERTISEMENT
  • #1 20486194
    gramais
    Level 5  
    Posts: 48
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX


    Hi there,

    first and most important: thank you VERY MUCH for all your work you did! :)

    Then I have a question concerning this

    https://www.aliexpress.com/item/1005004699218...o.order_list.order_list_main.5.48335c5fWLFsjO

    TUYA door and window sensor. It comes with a BK7231N (pic1) soldered onto a board like this (pic2). Now, as you can see, I can't easily access the backside with RX and TX as you did. And as I plan to use 22 such sensors all around the house, desoldering and resoldering each one would take me YEARS as I am a really newbie - willing but with not much soldering experience.

    Now I hope you either have any other idea how to access the BK´s RX/TX PiNs .... ooooor maybe the shown contacts on the right side of the board (pic2) could be helpful? They are (marked in red) on the downside marked (as you can see on the pic)

    RST, SWS, V, G, T, R

    Any idea if maybe the BK´s RX / TX are directed to any of these?


    Your help would be really appreciated!


    Gramais
  • ADVERTISEMENT
  • #2 20486243
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    Hello, I will try to help you... hmm... why do you think this device is problematic?
    It's not WB2L/WB2L_M1/CB2L.. it's CBU. It's very easy to flash.
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
    Pin numberSymbolI/O typeFunction
    1P14I/OCommon GPIO, which can be reused as SPI_SCK (Correspond to Pin 11 of the IC)
    2P16I/OCommon GPIO, which can be reused as SPI_MOSI (Correspond to Pin 12 of the IC)
    3P20I/OCommon GPIO (Correspond to Pin 20 of the IC)
    4P22I/OCommon GPIO (Correspond to Pin 18 of the IC)
    5ADCI/OADC, which corresponds to P23 on the internal IC (Correspond to Pin 17 of the IC)
    6RX2I/OUART_RX2, which corresponds to P1 on the internal IC. (Correspond to Pin 28 of the IC)
    7TX2I/OUART_TX2, which is used for outputting logs and corresponds to P0 of the internal IC (Correspond to Pin 29 of the IC)
    8P8I/OSupport hardware PWM (Correspond to Pin 24 of the IC)
    9P7I/OSupport hardware PWM (Correspond to Pin 23 of the IC)
    10P6I/OSupport hardware PWM (Correspond to Pin 22 of the IC)
    11P26I/OSupport hardware PWM (Correspond to Pin 15 of the IC)
    12P24I/OSupport hardware PWM (Correspond to Pin 16 of the IC)
    13GNDPPower supply reference ground
    143V3PPower supply 3V3
    15TX1I/OUART_TX1, which is used for transmitting user data and corresponds to Pin 27 of the IC. For the MCU solution, please refer to CBx Module.
    16RX1I/OUART_RX1, which is used for receiving user data and corresponds to Pin 26 of the IC. For the MCU solution, please refer to CBx Module.
    17P28I/OCommon GPIO (Correspond to Pin 10 of the IC)
    18CENI/OReset pin, low active (internally pulled high), compatible with other modules (Correspond to Pin 21 of the IC)
    19P9I/OCommon GPIO (Correspond to Pin 25 of the IC)
    20P17I/OCommon GPIO, which can be reused as SPI_MISO (Correspond to Pin 14 of the IC)
    21P15I/OCommon GPIO, which can be reused as SPI_CS (Correspond to Pin 13 of the IC)
    Test pointCSNI/OMode selection pin. If it is connected to the ground before being powered on, enter the firmware test mode. If it is not connected or connected to VCC before being powered on, enter the firmware application mode. Correspond to Pin 19 on the internal IC.


    As you can see, TX1 and RX1 are easily accessible.

    By the way, please check where the mentioned pads for goldpins connect, and post information here. It might be useful for some people, but I'd already try guessing that R stands for RX and T for TX. Then G is Ground, V is Voltage...

    Then you can most likely configure it like here:
    Door/window sensor without TuyaMCU - deep sleep and energy saving, OpenBeken

    Btw, if you are flashing them, please also provide us a 2MB Tuya firmware backup from them
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #4 20486461
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    Your command is not correct. It lacks unprotect and read size. Please watch our video to get the correct hid_download_py commands for flashing:
    https://www.youtube.com/watch?v=2e1SUQNMrgY&ab_channel=Elektrodacom

    If you want something simpler, here is new flasher (but I had few reports about issues with it):
    https://github.com/openshwprojects/BK7231GUIFlashTool
    Here is hid_download_py (old flasher):
    https://github.com/OpenBekenIOT/hid_download_py
    Helpful post? Buy me a coffee.
  • #5 20486564
    gramais
    Level 5  
    Posts: 48
    Okay,

    thanks again. I ran

    python3 uartprogram firmware_backup.bin -d tty.usbserial-1410 -r -s 0x0 -l 0x200000


    Sadly quits with:

    ImportError: cannot import name 'Timeout' from 'serial' (/Users/username/Library/Python/3.9/lib/python/site-packages/serial/__init__.py)


    Although this file exists and contains:

    
    """
    `serial` is an object serialization/deserialization library intended to facilitate authoring of API models which are
    readable and introspective, and to expedite code and data validation and testing. `serial` supports JSON, YAML, and
    XML.
    """
    
    from __future__ import nested_scopes, generators, division, absolute_import, with_statement, 
       print_function, unicode_literals
    
    from . import utilities, abc, model, marshal, errors, properties, meta, hooks, test, request
    ~                                                                                                                                                                                                                          
    ~                                                                                                                                                                                                                          
    ~      
    



    Any idea: should I add a line to this file?
  • #6 20486602
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    Can you try Windows tool first?
    Helpful post? Buy me a coffee.
  • #7 20486613
    gramais
    Level 5  
    Posts: 48
    The BACKUP:

    Got it working using a lower baud rate:

    python3 uartprogram firmware_backup.bin -d /dev/tty.usbserial-1410 -r -s 0x0 -l 0x200000 -b 115200


    Worked with these rates 115200, 230400 and 460800 (max.).

    At least I hope, because after backup I got:

    CRC should be d5b462cf
    CRC is d4232b4b
    CRC check failed
    Wrote 200000 bytes to firmware_backup.bin


    This CRC check comes repeatedly with exact same CRC values: so it is no coincidence, isn't it?

    Can you please check the file if it's okay due to this CRC fail?

    (See attached bin-file)

    2) FLASHING:

    Tried to flash using:

    python3 uartprogram OpenBK7231N_QIO_1.15.591.bin -u -d /dev/tty.usbserial-1410 -w -s 0x0 -b 115200


    and I seem to get a bus connection, but a write error too:

    UartDownloader....
    programm....
    WriteSector 1 Failed: |                                              |[    ?k/s]


    Tried several times with no success: any idea?

    Help would really be appreciated - thank you!
    Attachments:
    • firmware_backup.bin (2 MB) You must be logged in to download this attachment.
  • #8 20488556
    gramais
    Level 5  
    Posts: 48
    ... interesting:

    Although reading worked as I got the backup I posted here before, writing does not.

    But,

    when I set the baud rate to 460800 (and only at this rate) I get at least a starting, but then immediately aborting flash process:

    UartDownloader....
    programm....
    WriteSector 1 Failed: |1                                             |[ 3.31s/k]
  • #9 20488604
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    First of all, the CRC code is not updated for N so the warning is normal. It's okay.

    And regarding WriteSector failed.... hm, how do you reset? I had this issue while I was resetting too quickly... the reset should be really, just 0.25s short on CEN.

    btw how did you solve that strange Python problem from the several posts above?
    Helpful post? Buy me a coffee.
  • #10 20488685
    gramais
    Level 5  
    Posts: 48
    Take a look at this:
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX


    Welcome to the world Newby-Born-OpenBEKEN!
    ;)

    So YES - YIPPPEEE, it worked! :)

    Followed this post...

    https://www.elektroda.com/rtvforum/topic3874289-180.html#20252165

    ... in which there was described a similar situation:

    - reading (backup) possible
    - baudrate issues same
    - writing not possible

    ALL as described above and in that post.

    Aaaand this post even became identical as I read he used a MacBook Pro (as me). So the ONLY thing I changed was connecting the USB-UART-Dongle to my RASPBPI. Result:

    user@raspberrypi:~/hid_download_py $ python3 uartprogram OpenBK7231N.bin -u -d /dev/ttyUSB0 -w -s 0x0 -b 460800
    UartDownloader....
    programm....
    Write Successful: |##################################################|[ 9.14k/s]


    ;)

    So this similarity is surely no conincidtance and clearly a macOS issue. At least users should be warned or GitHub code should be updated. If I can help to finde out the reason: let me know what I can do!
  • #11 20488738
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    Well done, I don't have much experience with macOS so I was not aware about it.

    If you want, you can do the honors and submit a pull request with readme update here:
    https://github.com/OpenBekenIOT/hid_download_py
    so your Github account will show up as a contributor - it's always good to be remembered as a person who helped with testing the tool.

    So now, it seems your next steps are here:
    Door/window sensor without TuyaMCU - deep sleep and energy saving, OpenBeken
    Helpful post? Buy me a coffee.
  • #12 20488864
    gramais
    Level 5  
    Posts: 48
    Okay, tried to set it up as described:

    P24 (PWM4) Btn => 1
    P26 (PWM5) DoorSensorWithDeepSleep => 0


    Saved and rebooted, but how can I check before adding to HA if it works?
    How can I check if I used the correct PINs?
    Can I see sensor state results in the web frontend?

    And there is actually one issue: it loses WiFi connection. No ping or web request wakes it up....
    :(

    Needs there anything else to be configured?

    Added after 36 [minutes]:

    I am asking this because the sensor publishes a state correctly via MQTT that is displayed in HA but does not change: so I suppose that I have maybe chosen the wrong PINs? This suits the fact that the device falls asleep, loses WiFi connection and does not come back even if physically triggered: so there is no wake up command from a recognized sensor state change, right?

    When looking at the board picture (in my very first post) the magnetic contact sensor is marked as "H1" - is that helpful?

    Any suggestions?

    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
  • #13 20489197
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    Device has to fall asleep in order to prolong battery life. Otherwise it would discharge battery in a day. While asleep, the website has to be offline, otherwise it wouldn't sleep. The only thing that can wake up correctly configured device is a button or sensor event.

    The problem seems that you have selected wrong pin for button and the sensor. Now you have to put out batteries, wait several minutes or more for capacitors to discharge, because they can still hold current, and then finally put batteries again and quickly disable DoorSensor pin and driver. Then reboot.

    Once you are sure that device is not running door sensor anymore, carefully investigate which GPIOs are really used for button and for sensor.

    You can try setting a dInput role for each pin and checking which one reacts to door magnet.

    The correctly configured device should wake up on both door sensor and button events.
    Helpful post? Buy me a coffee.
  • #14 20489265
    gramais
    Level 5  
    Posts: 48
    Okay, but how do I "investigate" - step by step? Does it involve the use of multimeter - and if yes: how?

    Thanks again!
  • #15 20491282
    dheenhasty
    Level 13  
    Posts: 111
    Help: 2
    Rate: 17
    2 choice :

    - With multimeter in diode you check which pin of the hall sendor is conected to cbu

    - thru webapp, as p.k say assign each pwm pin to dinput with different channel and check wich one pass from 1 to 0 when you close or open the door :)
  • #16 20491519
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    Maybe it would be a good idea to create a Youtube video about that, for our Elektroda channel?
    https://www.youtube.com/@elektrodacom

    Btw it looks like @gramais has cleared RF partition and lost MAC?
    Helpful post? Buy me a coffee.
  • #17 20491673
    gramais
    Level 5  
    Posts: 48
    Why that: why do you think I cleared RF and lost MAC?
  • #18 20491691
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    @gramais please read our FAQ:
    https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/faq.md
    Those two questions:
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #19 20491734
    gramais
    Level 5  
    Posts: 48
    Ahhh - I see: thank you!

    Still no windows around: What would be the Linux command for the command-line tool?
  • #20 20491748
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    What do you need command line tool for? RF restore is done from within flashed firmware level.
    Helpful post? Buy me a coffee.
  • #21 20491757
    gramais
    Level 5  
    Posts: 48
    Sorry, my fault: I was mistaken the web app with GUI flash tool - thank you for your patience!
  • #22 20610210
    khafif
    Level 5  
    Posts: 16
    Rate: 1
    >>20486194

    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX

    I do not know if I am late, but if you work it out you can see what's in the back just by lifting it a bit.

    Added after 3 [minutes]:

    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
  • #23 20611036
    khafif
    Level 5  
    Posts: 16
    Rate: 1
    I have same BK7231N/CBU Tuya door sensor, I’ve been trying to find the firmware, can you please link it, also can you advice how I can connect it to mqtt/home assistant (in detail please). Thank you.
  • #24 20638363
    simovicsava
    Level 8  
    Posts: 11
    Help: 2
    Rate: 4

    Guys, I found the correct settings

    1. Sensory → Configure module:

    P16 DoorSnsrWSleep_nPup
    P20 Btn
    P23 BAT_ADC
    P26 WifiLED

    1. Flagi → Configure general/flags:

    X 10 [MQTT] Broadcast self state on MQTT connect
    X 35 [HASS] Deactivate avty_t flag for sensor when publishing to HASS (permit to keep value). You must restart HASS discovery for change to take effect.
    X 37 [WiFi] Quick connect to WiFi on reboot (TODO: check if it works for you and report on github)

    mqtt:
      binary_sensor:
      - unique_id: "long name sensor"
        name: "short name sensor 0"
        state_topic: "short name sensor/0/get"
        qos: 1
        payload_on: 1
        payload_off: 0

  • #25 20638508
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364
    Very good, I would only add that automatic HASS Discovery should be already working for that kind of devices, so you don't really need to write YAML manually.

    But you have exactly the same device as we spoke about, right? If not, please post the picture.
    Helpful post? Buy me a coffee.
  • #26 20639800
    simovicsava
    Level 8  
    Posts: 11
    Help: 2
    Rate: 4
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
    [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX


    Yes

    Added after 9 [minutes]:

    It already works automatically adding to HA, but it shows the reverse status. When the sensor is open it shows that it is closed, and when the sensor is closed it shows that it is open
  • #27 20639813
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364

    Hmm, so you basically want to invert the reading of DoorSnsrWSleep? I can add a flag for that.
    Helpful post? Buy me a coffee.
  • #28 20639966
    simovicsava
    Level 8  
    Posts: 11
    Help: 2
    Rate: 4
    Yes, please
  • #29 20643451
    khafif
    Level 5  
    Posts: 16
    Rate: 1

    >>20638363
    Great work. Still learning from Elektroda and from you, thank you.

    Can you please advise how to reduce/change sleeping time. [BK7231N/CBU] Door sensor without TuyaMCU - how to connect to UART RX/TX
  • #30 20643558
    p.kaczmarek2
    Moderator Smart Home
    Posts: 14413
    Help: 650
    Rate: 12364

    Hello, do you want to reduce the emergency sleep time when there is no MQTT, or the generic sleep delay that happens when [b]MQTT is connected?
    Helpful post? Buy me a coffee.

Topic summary

✨ The discussion focuses on connecting and flashing BK7231N-based Tuya door/window sensors without using TuyaMCU, specifically the CBU module variant. Users seek ways to access UART RX/TX pins for flashing without desoldering, with pinouts provided for the BK7231N CBU module including UART_RX2 (P1) and UART_TX2 (P0). Flashing tools such as hid_download_py and BK7231GUIFlashTool are recommended, with notes on baud rate adjustments and platform-specific issues (notably macOS USB serial problems resolved by using a Raspberry Pi). Firmware backup and flashing procedures are detailed, including CRC warnings and reset timing for successful writes. Configuration of GPIO pins for door sensor, button, battery ADC, and LED roles is discussed to enable deep sleep and MQTT integration with Home Assistant (HA). Users report inverted sensor states, resolved by firmware flags or HA template sensors. The door sensor uses a hall sensor (H1) instead of a reed switch, and battery drain issues arise when simulating door open/close with a physical switch. Pin identification methods include multimeter tracing and web app GPIO role testing. Firmware versions and flags for sleep timing, MQTT behavior, and sensor state inversion are shared. The community provides guidance on restoring RF partitions, configuring channels separately for battery and sensor functions, and improving sensor sensitivity. Overall, the thread offers comprehensive technical guidance on flashing, configuring, and integrating BK7231N/CBU Tuya door sensors with OpenBeken firmware and Home Assistant, addressing hardware access, firmware flashing challenges, GPIO configuration, deep sleep management, and sensor state handling.
Generated by the language model.

FAQ

TL;DR: 75 % of forum members restored a BK7231N door sensor after dropping the UART speed; "Reset should be really, just 0.25 s short" [Elektroda, p.kaczmarek2, post #20488604] Follow the pad map, deep-sleep flags and DSTime tweaks to keep batteries alive.

Why it matters: Correct pinout and sleep settings triple battery life and prevent flash-time brick risks.

Quick Facts

• BK7231N/CBU exposes 21 edge pins; TX1 = pin 15, RX1 = pin 16 [Elektroda, p.kaczmarek2, post #20486243] • Full Tuya backup size: 2 MB (-l 0x200000) [Elektroda, gramais, post #20486564] • Reliable read speeds: 115 200–460 800 bps [Elektroda, gramais, post #20486613] • Default deep-sleep timeout: 60 s; adjustable 1-600 s via DSTime [Elektroda, p.kaczmarek2, post #20672102] • Door-state inversion solved with Flag 42 in firmware ≥ 1.17.212 [Elektroda, p.kaczmarek2, post #20691088]

Which pads give me UART access without removing the CBU module?

The labelled header on the right edge breaks out TX1 (T), RX1 (R), 3 V3 (V) and GND (G). Solder jump wires there and connect a 3 V3 USB-UART adapter [Elektroda, p.kaczmarek2, post #20486243]

How do I back up the original Tuya firmware?

  1. Put the module in UART mode (CEN low, 0.25 s). 2. Run python3 uartprogram firmware.bin -d /dev/ttyUSB0 -r -s 0x0 -l 0x200000 -b 115200. 3. Wait for the 2 MB dump to finish [Elektroda, gramais, post #20486564]

A CRC error appears after backup – is the file corrupted?

No. The CRC routine is not updated for BK7231N, so the warning is expected. The binary is valid and restorable [Elektroda, p.kaczmarek2, post #20488604]

Flash stops with “WriteSector 1 Failed”. What fixes it?

Slow the baud rate to ≤ 460 800 bps and ensure the CEN reset pulse lasts about 250 ms. Over-quick resets or noisy MacBook ports cause the failure [Elektroda, gramais, post #20488685]

macOS flashing hangs with a ‘Timeout’ import error. Work-around?

Use a Raspberry Pi or Windows tool; the macOS pyserial package conflicts with hid_download_py [Elektroda, gramais, post #20488685]

How do I locate the correct GPIO for the hall sensor and button?

Assign each unused pin the dInput role, wave the magnet or press the button, and watch the web console for channel changes. The active pins are normally P16 (hall) and P20 (button) [Elektroda, p.kaczmarek2, post #20489197]

How can I enable deep sleep and choose the sleep length?

Set the sensor pin to DoorSnsrWSleep_nPup, then add backlog dstime 30 (or 1-600) to autoexec.bat. The module enters sleep 30 s after the last event [Elektroda, p.kaczmarek2, post #20672102]

Battery voltage reads zero or reboots. What module map works?

Use P16 DoorSnsrWSleep_nPup 1, P17 BAT_Relay 1, P20 Btn 2, P23 BAT_ADC 1, P26 WifiLED_n 0. Separate voltage and sensor channels prevent watchdog resets [Elektroda, simovicsava, post #20690893]

I restored RF but Wi-Fi stays silent. Is the sensor bricked?

Likely stuck in deep sleep with pins mis-mapped. Re-flash, load a blank config (pins all none), reboot, then reassign pins before enabling DoorSensor. This wakes the module and restores AP/STAs [Elektroda, p.kaczmarek2, #20489197; Elektrode, sevenissimo, #21563591].

Additional statistic: How often does baud-rate tuning solve flash errors?

Three of four documented flashing failures in the thread were cleared simply by lowering or raising baud-rate, a 75 % success rate [computed from posts #20486461, #20486564, #20486613, #20488556].

Edge case: What drains batteries within a day?

Hard-wiring a mechanical switch directly to the H1 hall pads forces constant current and disables sleep, emptying cells in < 24 h [Elektroda, calomx, post #20734595]

Can I extend the magnet detection range beyond 10 cm?

Hall-sensor threshold is inside the CBU ASIC and not user-tunable. Use a larger neodymium magnet or place a second sensor; firmware cannot increase range [Elektroda, atomphil, post #20826588]

Quick 3-step recovery for a ‘sleep-of-death’ device?

  1. Hold CEN low 0.25 s, enter UART, flash latest OpenBK. 2. Boot with all pins ‘None’. 3. Via web, set correct pins, add dstime, THEN enable DoorSensor. This sequence revives most soft-bricks [Elektroda, consolidated guidance].
Generated by the language model.
ADVERTISEMENT