Skip to content

[hmc5883l] Fix wrong gain for 88uT range#14281

Merged
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-hmc5883l-88ut-gain
Feb 25, 2026
Merged

[hmc5883l] Fix wrong gain for 88uT range#14281
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix-hmc5883l-88ut-gain

Conversation

@swoboda1337
Copy link
Member

What does this implement/fix?

Fix incorrect gain value for the 88µT range. The code had 0.073 mG/bit instead of 0.73 mG/bit, causing readings to be 10x too low.

The datasheet (page 14) specifies a gain of 1370 LSb/Gauss for this range: 1000 / 1370 = 0.73 mG/bit. All other ranges were correct.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

Pull request in esphome-docs with documentation (if applicable):

N/A

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • LN882x
  • nRF52840

Example entry for config.yaml:

sensor:
  - platform: hmc5883l
    address: 0x1E
    field_strength_x:
      name: "HMC5883L Field Strength X"
    field_strength_y:
      name: "HMC5883L Field Strength Y"
    field_strength_z:
      name: "HMC5883L Field Strength Z"
    heading:
      name: "HMC5883L Heading"
    range: 88uT
    update_interval: 1s

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

The 88uT range gain was 0.073 mG/bit instead of 0.73 mG/bit,
causing readings to be 10x too low. The datasheet specifies
1370 LSb/Gauss for this range, giving 1000/1370 = 0.73 mG/bit.

Fixes esphome#14276

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 25, 2026 17:40
@github-actions
Copy link
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#14281
    components: [hmc5883l]
    refresh: 1h

(Added by the PR bot)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes incorrect field strength scaling for the HMC5883L 88µT range so reported magnetic field values are no longer 10× too low.

Changes:

  • Corrected the gain constant for HMC5883L_RANGE_88_UT from 0.073f to 0.73f (mG/bit), aligning with the datasheet’s 1370 LSB/Gauss value.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.28%. Comparing base (ede8235) to head (519ff1d).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #14281      +/-   ##
==========================================
+ Coverage   74.25%   74.28%   +0.02%     
==========================================
  Files          55       55              
  Lines       11595    11595              
  Branches     1583     1583              
==========================================
+ Hits         8610     8613       +3     
+ Misses       2580     2578       -2     
+ Partials      405      404       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Contributor

Memory Impact Analysis

Components: hmc5883l
Platform: esp8266-ard

Metric Target Branch This PR Change
RAM 28,944 bytes 28,944 bytes ➡️ +0 bytes (0.00%)
Flash 287,431 bytes 287,431 bytes ➡️ +0 bytes (0.00%)

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

@swoboda1337 swoboda1337 merged commit 5dffced into esphome:dev Feb 25, 2026
43 checks passed
@swoboda1337
Copy link
Member Author

Thanks

jesserockz pushed a commit that referenced this pull request Feb 26, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jesserockz jesserockz mentioned this pull request Feb 26, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HMC5883L sensor wrong readings on 88uT range

5 participants