Slowing down an ESP32 with ESPHome

I’m working with an ESP32 and an HX711 for measuring weight. I need to measure down to 1gm resolution.

The HX711 works at 80Mhz. The ESP32 is at 240Mhz and this is causing fluctuations in the readings.

This explains it well.

Arduino has rtc_clk_cpu_freq_set(RTC_CPU_FREQ_80M) to slow down the ESP32. Can this be done with ESPHome too?

I think the best course of action would be to open an issue for the HX711 and get the sampling clock time fixed.

However there may be a way to do it with with the ESPHome core platformio options.

This option in particular:

https://docs.platformio.org/en/latest/projectconf/section_env_platform.html#board-build-f-cpu

1 Like

Thank you, tom_I.

So, it looks like the below may be the answer.

Is my thinking correct here?

esphome:
  # ...
  platformio_options:
board_build.f_cpu = 80000000L

I have opened an issue as suggested.

Your indentation is off. Should be:

esphome:
  # ...
  platformio_options:
    board_build.f_cpu = 80000000L

It’s weird that the default is listed as 40MHz.

I assume ESPHome isn’t using this.

1 Like

Thanks, I caught that. It should also be:

esphome:
  name: feeder
  platform: ESP32
  board: esp32dev
  platformio_options:
    board_build.f_cpu: 80000000L

That’s the flash frequency though, rather than CPU speed.

So now the ESP32 CPU speed is matched to the HX7111 speed - which is progress.

Still not getting what I need from the HX711 though :confused:

Doh! Yeah, so it is.

In what way?

Right!

This seems to be working now. Nice steady readings and good sensitivity. The mismatched speeds has been the problem.

Thank you for your help.

Just needs the fine-tuning of the filters and calibrating.

sensor:
  - platform: hx711
    name: "Fish food hopper"
    id: fish_food_hopper
    dout_pin: 27
    clk_pin: 26
    filters:
      - calibrate_linear:   
        - 9800 -> 0    
        - 435000 -> 5.0
      - sliding_window_moving_average:    
          window_size: 5    
          send_every: 5
    update_interval: 2s
    unit_of_measurement: gm
    accuracy_decimals: 1

Here’s a thought.

Is there any reason why the filter settings and gain could not be changed using input_number from HA, rather than compiling and reflashing with each adjustment?

Can that be done?

Yeah there is. Those filter configuration options don’t accept lambdas.

That is real shame!

There is a lambda filter, but you would have to write the sliding window moving average equation to accept your input numbers.

1 Like

When I tried with 20MHZ

[19:47:11]E (45371) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
[19:47:11]E (45371) task_wdt:  - loopTask (CPU 1)
[19:47:11]E (45371) task_wdt: Tasks currently running:
[19:47:11]E (45371) task_wdt: CPU 0: wifi
[19:47:11]E (45371) task_wdt: CPU 1: IDLE
[19:47:11]E (45371) task_wdt: Aborting.

it is unable to boot up and constantly reboots

same for 40MHZ

WARNING Decoded 0x403775b4: esp_restart_noos at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/port/soc/esp32s3/system_internal.c:143 (discriminator 1)
[19:53:58]SPIWP:0xee
[19:53:58]mode:DIO, clock div:1
[19:53:58]load:0x3fce3808,len:0x43c
[19:53:58]load:0x403c9700,len:0xbec
[19:53:58]load:0x403cc700,len:0x2a3c
[19:53:58]entry 0x403c98d8
[19:53:58][   175][D][esp32-hal-cpu.c:244] setCpuFrequencyMhz(): XTAL: 40 / 1 = 40 Mhz, APB: 80000000 Hz
[19:53:59][  1088][D][WiFiGeneric.cpp:929] _eventCallback(): Arduino Event: 0 - WIFI_READY
[19:54:09]E (22717) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
[19:54:09]E (22717) task_wdt:  - loopTask (CPU 1)
[19:54:09]E (22717) task_wdt: Tasks currently running:
[19:54:09]E (22717) task_wdt: CPU 0: wifi
[19:54:09]E (22717) task_wdt: CPU 1: IDLE
[19:54:09]E (22717) task_wdt: Aborting.
[19:54:09]ESP-ROM:esp32s3-20210327
[19:54:09]Build:Mar 27 2021
[19:54:09]rst:0xc (RTC_SW_CPU_RST),boot:0x28 (SPI_FAST_FLASH_BOOT)
[19:54:09]Saved PC:0x403775b4
WARNING Decoded 0x403775b4: esp_restart_noos at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/port/soc/esp32s3/system_internal.c:143 (discriminator 1)

i have also tried

  platformio_options:
    board_build.f_cpu: 80000000L
    build_flags: 
      -"-D CONFIG_FREERTOS_UNICORE"

that does work

and

  framework:
    type: esp-idf
    sdkconfig_options:
      CONFIG_FREERTOS_UNICORE: y
      CONFIG_ESP32_DEFAULT_CPU_FREQ_80: y
      CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ: "80"

that does not boot up

[20:33:27]ESP-ROM:esp32s3-20210327
[20:33:27]Build:Mar 27 2021
[20:33:27]rst:0x7 (TG0WDT_SYS_RST),boot:0x28 (SPI_FAST_FLASH_BOOT)
[20:33:27]Saved PC:0x400454d5
[20:33:27]SPIWP:0xee
[20:33:27]mode:QIO, clock div:1
[20:33:27]load:0x3fce3808,len:0x164c
[20:33:27]ets_loader.c 78
[20:33:27]
ERROR Serial port closed!

with flash mode set to dio I get no output on com port (probably because esp idf needs to set logger to jtag), but device does boot