Skip to content

Additional ADC pins for BK7231N #334

@Savjee

Description

@Savjee

I recently bought an Tuya WiFi Thermostat that uses a BK7231N chip (AliExpress listing).

I was able to flash ESPHome onto the device by soldering leads onto the TX and RX pins.

I was also able to get in touch with the manufacturer who shared the list of PINs they used to connect everything to the MCU.

From this, I learned that the NTC sensor is attached to P21, which according to their diagram is ADC6 on the BK7231N.

Image

This is odd, because I can't find any datasheets of the BK7231N with ADC6 marked on it. While inspecting the PCB, I can confirm that the NTC is indeed connected to P21.

Using this pin in my ESPHome configuration leads to an error. I patched ESPhome and libretiny to allow P21 as an ADC input.

In ESPHome's boards.py file, I added ADC6:

"generic-bk7231n-qfn32-tuya": {
     "ADC6": 21
}

In libretiny boards/_base/ic/bk7231-qfn32.json I added the pin as well:

{
	"pcb": {
		"ic": {
			"19": {
				"C_NAME": "GPIO21",
				"GPIO": "P21",
				"IRQ": null,
				"I2C": "1_SDA",
				"JTAG": "TMS",
				"I2S": "MCLK",
				"FLASH": "^FCS",
				"ADC": 6
			},
        ...

This got rid of the errors, but the ADC value is always 0. Even when I detach the NTC, which should make the ADC float.

Relevant parts of my ESPHome configuration:

---
substitutions:
  devicename: "office-thermostat"
  friendly_name: "Office Thermostat"

packages:
  esphome: !include common/esphome.yaml
  api: !include common/api.yaml
  logger: !include common/logger.yaml
  wifi: !include common/wifi.yaml

bk72xx:
  board: "generic-bk7231n-qfn32-tuya"
  framework:
    version: 0.0.0
    source: https://github.com/Savjee/libretiny.git#master

sensor:
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    update_interval: 60s

  - platform: ntc
    sensor: resistance_sensor
    name: "Office Temperature"
    calibration:
      b_constant: 3380
      reference_temperature: 25°C
      reference_resistance: 10kOhm

  - platform: resistance
    id: resistance_sensor
    sensor: source_sensor
    configuration: DOWNSTREAM
    resistor: 100
    name: Resistance Sensor

  - platform: adc
    id: source_sensor
    # pin: P23
    pin: ADC6
    name: "ADC Raw value"
    unit_of_measurement: "V"
    update_interval: 1s

switch:
  - platform: gpio
    id: relay
    name: thermostat_relay
    pin: P22

Any thoughts on how I could fix this? Are there other changes I have to make to either ESPHome or Libretiny?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BK7231Beken BK72xx familybugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions