Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new component mcp3426 7 8 #6675

Draft
wants to merge 25 commits into
base: dev
Choose a base branch
from
Draft

Add new component mcp3426 7 8 #6675

wants to merge 25 commits into from

Conversation

mdop
Copy link

@mdop mdop commented May 3, 2024

What does this implement/fix?

Adds support for the 4 channel 16 bit ADC chips of the MCP3426/7/8 family. Structure is based on the ADS1115, but the measurement is done asynchronously.

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)
  • Other

Related issue or feature (if applicable): fixes -

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

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

mcp3428:
  address: 0b1101000

sensor:
  - platform: mcp3428
    name: "Channel 1"
    multiplexer: 1
    resolution: 12
    gain: 8
    id: mcp3428_sensor1

Checklist:

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

If user exposed functionality or configuration variables are added/changed:

@codecov-commenter
Copy link

codecov-commenter commented May 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.78%. Comparing base (4d8b5ed) to head (35f6ecb).
Report is 922 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6675      +/-   ##
==========================================
+ Coverage   53.70%   53.78%   +0.07%     
==========================================
  Files          50       50              
  Lines        9408     9660     +252     
  Branches     1654     1704      +50     
==========================================
+ Hits         5053     5196     +143     
- Misses       4056     4140      +84     
- Partials      299      324      +25     

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

@mdop mdop marked this pull request as ready for review May 4, 2024 08:48
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(i2c.i2c_device_schema(None))
Copy link
Member

Choose a reason for hiding this comment

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

Devices with address pins still have a default address if the pins are left floating.

Suggested change
.extend(i2c.i2c_device_schema(None))
.extend(i2c.i2c_device_schema(0x58))

Copy link
Author

Choose a reason for hiding this comment

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

Ok, but left floating the address is 0x68, like with grounded pins. Addressed it in a separate commit.

esphome/components/mcp3428/sensor/__init__.py Outdated Show resolved Hide resolved
ESP_LOGCONFIG(TAG, "Setting up MCP3426/7/8...");
uint8_t anwser[3];
if (this->read(anwser, 3) != i2c::ErrorCode::NO_ERROR) {
this->mark_failed();
Copy link
Member

Choose a reason for hiding this comment

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

Please put useful error messages here so users can look back at logs and see why it failed.

Copy link
Author

Choose a reason for hiding this comment

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

Added context that communication failed

// leave channel at 1, gain at 1x, and resolution at 12 bit

if (this->write(&config, 1) != i2c::ErrorCode::NO_ERROR) {
this->mark_failed();
Copy link
Member

Choose a reason for hiding this comment

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

Same here

esphome/components/mcp3428/mcp3428.cpp Outdated Show resolved Hide resolved
esphome/components/mcp3428/mcp3428.cpp Outdated Show resolved Hide resolved
esphome/components/mcp3428/mcp3428.cpp Outdated Show resolved Hide resolved
Comment on lines 58 to 60
ESP_LOGCONFIG(TAG, " Multiplexer: %u", this->multiplexer_);
ESP_LOGCONFIG(TAG, " Gain: %u", this->gain_);
ESP_LOGCONFIG(TAG, " Resolution: %u", this->resolution_);
Copy link
Member

Choose a reason for hiding this comment

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

These should be converted back to the human friendly values that were entered in yaml

Copy link
Author

Choose a reason for hiding this comment

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

Seems like I forgot to change this when I changed the numerical values of the enums. Fixed in a commit.

sda: 4

mcp3428:
address: 0b1101000
Copy link
Member

Choose a reason for hiding this comment

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

Please use hex here to keep consistent with all other i2c examples.

Suggested change
address: 0b1101000
address: 0x68

@esphome
Copy link

esphome bot commented Jul 4, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@esphome esphome bot marked this pull request as draft July 4, 2024 23:03
@mdop mdop requested a review from jesserockz July 7, 2024 12:12
@mdop
Copy link
Author

mdop commented Jul 7, 2024

Not sure why CI / Component test MCP3428 (pull_request) failed, the file ./tests/test_build_components/build_components_base.esp8266.yaml does exist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants