Ilias
(Ilias )
December 15, 2022, 10:02am
1
Hello all,
I am having a bit communication issues with i2c sensors.
Below are some examples.
MPL3115A2 connected to an ESP8266 (Wemos D1 mini).
Despite the sensor being discovered, the communication fails.
This happened after upgrading to ESPHome 2022.12.0
[09:15:31][C][i2c.arduino:053]: SDA Pin: GPIO4
[09:15:31][C][i2c.arduino:054]: SCL Pin: GPIO5
[09:15:31][C][i2c.arduino:055]: Frequency: 50000 Hz
[09:15:31][C][i2c.arduino:058]: Recovery: bus successfully recovered
[09:15:31][I][i2c.arduino:068]: Results from i2c bus scan:
[09:15:31][I][i2c.arduino:074]: Found i2c device at address 0x60
...
[09:15:31][C][mpl3115a2:035]: MPL3115A2:
[09:15:31][C][mpl3115a2:036]: Address: 0x60
[09:15:31][E][mpl3115a2:040]: Communication with MPL3115A2 failed!
BH1750 and BMP280 connected to ESP32 ( D1 mini ESP32)
For some reason BH1750 is not even discovered. 0x76 is the address for BMP280
This happened after upgrading to ESPHome 2022.12.0
[09:17:52][C][i2c.arduino:052]: I2C Bus:
[09:17:52][C][i2c.arduino:053]: SDA Pin: GPIO21
[09:17:52][C][i2c.arduino:054]: SCL Pin: GPIO22
[09:17:52][C][i2c.arduino:055]: Frequency: 50000 Hz
[09:17:52][C][i2c.arduino:058]: Recovery: bus successfully recovered
[09:17:52][I][i2c.arduino:068]: Results from i2c bus scan:
[09:17:52][I][i2c.arduino:074]: Found i2c device at address 0x76
...
[09:17:52][C][bh1750.sensor:119]: Address: 0x23
[09:17:52][E][bh1750.sensor:121]: Communication with BH1750 failed!
BMP280 only seems to work for me on ESP32 .
I have never managed to get BMP280 working on an ESP8266 as I always get:
Communication with BMP280 failed!
I canāt say that a specific release broke the communication as the issues above have been on and off present in different releases.
I can say that:
ESPHome 2022.12.0 presents all the issues I have stated above
ESPHome 2022.11.5: only issue (3) was present
ESPHome 2022.11.x (where x<5): definitely presented issue (3) but I did see issue (1) at some point not sure if it was between different releases or on the same one it worked one time and didnāt the other time.
In summary, my journey with i2c sensors started during some version before ESPHome 2022.11.5
First I saw issue (3) after which I replaced BMP280 on ESP8266 with MPL3115A2 . That was ESPHome 2022.11.3 or 4 (not sure)
BH1750 despite being discovered wouldnāt work on ESPHome 2022.11.4 but started working on ESPHome 2022.11.5
That brings us to ESPHome 2022.12.0 where all the issues are back.
p.s.
I have seen the thread Has ESPHome i2c become unreliable? but I donāt think it applies as I have used many different power supplies with no difference in the outcome. As for pulling up the i2c pins (a) the documentation states that ESPs have internal pullups that get activated with i2c configuration and (b) in ESPHome 2022.11.5 the sensors worked reliably and consistently.
Given the issues I have seen so far I will avoid i2c sensors if I can but that is not the point.
pepe59
(Pepe59)
December 15, 2022, 3:17pm
2
Iāve been using a BME280 and several BH1750s on nodemcu esp8266 boards for several years and they all work without problems.
Ilias
(Ilias )
December 15, 2022, 3:46pm
3
That is what I would expect. I am literary using copy-paste code from the documentation. I even set up a brand new ESP8266 with BMP280 and BH1750 on it to rule out that the problem is coming from other parts of the code.
What version or ESPHome are you running?
pepe59
(Pepe59)
December 15, 2022, 9:20pm
4
ESPhome version 2022.12.0
Ilias
(Ilias )
December 16, 2022, 11:26am
5
Very odd.
Hope someone else has had the issue and has solved it.
justone
December 16, 2022, 12:02pm
6
Just recompiled one of my D1mini with a BME280 using ESPhome 2022.12.1 without any problem. And yes board also defined being a nodemcu since thatās sort of a swiss army board type working. Normally Iād ask to post your yaml code but since the sensor was found all I could think of would be lowering the frequency to see is that makes any differnce.
I do remember I once had a sensor asking me to do so since it didnāt work with 50000.
Ilias
(Ilias )
December 16, 2022, 1:33pm
7
On the ESP32 board I have the BH1750 it is not discovered and I have tried to change the frequency as well with no result
justone
December 16, 2022, 2:53pm
8
Perhaps you could share your YAML code for the D1 mini? Perhaps something strange in there.
I bet you already turned the logger: debuglevel to verbose to check about whatās going on.
Ilias
(Ilias )
December 16, 2022, 4:22pm
9
I have given up on the D1 and installed a DHT22 instead.
I did get logs for the ESP32 with the missing sensor. I see nothing to sayā¦āahaā
Oh yeah, forgot to mention that when I set the BMP280 to report both Temperature and Pressure to Home Assistant I get the āCommunication Failedā to the BMP280 as well. Which is why I have it commented out (not sending to HA)
yaml
esphome:
name: "staircase-sensor"
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
level: VERY_VERBOSE
# Enable Home Assistant API
api:
ota:
password: [redacted]
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Pir-Stairs Fallback Hotspot"
password: !secret ap_password
captive_portal:
substitutions:
friendly_name: "PIR"
motion_1_pin: GPIO14
motion_2_pin: GPIO05
motion_3_pin: GPIO23
motion_4_pin: GPIO34
motion_5_pin: GPIO19
motion_6_pin: GPIO33
web_server:
port: 80
# bluetooth_proxy:
# active: true
i2c:
sda: GPIO21
scl: GPIO22
scan: true
#id: bus_a
binary_sensor:
- platform: gpio
pin:
number: ${motion_1_pin} #GPIO39
inverted: false
mode:
input: true
name: "Motion-1"
device_class: motion
- platform: gpio
pin:
number: ${motion_2_pin} #GPIO35
inverted: false
mode:
input: true
name: "Motion-2"
device_class: motion
- platform: gpio
pin:
number: ${motion_3_pin} #GPIO33
inverted: false
mode:
input: true
# pulldown: true
name: "Motion-3"
device_class: motion
- platform: gpio
pin:
number: ${motion_4_pin} #GPIO34
inverted: false
mode:
input: true
name: "Motion-4"
device_class: motion
- platform: gpio
pin:
number: ${motion_5_pin} #GPIO25
inverted: false
mode:
input: true
name: "Motion-5"
device_class: motion
- platform: gpio
pin:
number: ${motion_6_pin} #GPIO32
inverted: false
mode:
input: true
# pullup: true
name: "Motion-6"
device_class: motion
# status_led:
# pin:
# number: GPIO02
# inverted: true
button:
- platform: restart
name: "Landing ESP - Restart"
light:
- platform: status_led
name: "Landing ESP - status LED"
pin: GPIO02
sensor:
# Uptime Sensor
- platform: uptime
name: Uptime Sensor
id: sensor_uptime
internal: true
# Uptime timestamp
- platform: template
id: sensor_uptime_timestamp
name: "${friendly_name} - Uptime"
device_class: "timestamp"
accuracy_decimals: 0
update_interval: never
lambda: |-
static float timestamp = (
id(homeassistant_time).utcnow().timestamp - id(sensor_uptime).state
);
return timestamp;
- platform: bmp280
temperature:
name: "Landing - Temperature"
oversampling: 16x
filters:
- offset: -0.4
- delta: 0.09
# pressure:
# name: "Landing - Pressure"
# unit_of_measurement: "mmHg"
# filters:
# - multiply: 0.75006
address: 0x76
update_interval: 25s
- platform: bh1750
name: "Landing - Illuminance"
address: 0x23
update_interval: 10s
filters:
- delta: 0.09
# Enable time component to reset energy at midnight
# https://esphome.io/components/time.html#home-assistant-time-source
time:
- platform: homeassistant
id: homeassistant_time
timezone: "Europe/London"
on_time_sync:
- component.update: sensor_uptime_timestamp
log
NFO Waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from staircase-sensor.local using esphome API
INFO Successfully connected to staircase-sensor.local
[15:59:18][I][app:102]: ESPHome version 2022.12.1 compiled on Dec 16 2022, 15:57:24
[15:59:18][C][status_led:063]: Status Led Light:
[15:59:18][C][status_led:064]: Pin: GPIO2
[15:59:18][VV][api.service:595]: on_subscribe_home_assistant_states_request: SubscribeHomeAssistantStatesRequest {}
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
key: 2866486986
state: NO
missing_state: NO
}
[15:59:18][C][wifi:504]: WiFi:
[15:59:18][C][wifi:362]: Local MAC: C8:C9:A3:C8:1F:D8
[15:59:18][C][wifi:363]: SSID: [redacted]
[15:59:18][C][wifi:364]: IP Address: 192.168.68.125
[15:59:18][C][wifi:366]: BSSID: [redacted]
[15:59:18][C][wifi:367]: Hostname: 'staircase-sensor'
[15:59:18][C][wifi:369]: Signal strength: -58 dB āāāā
[15:59:18][V][wifi:371]: Priority: 0.0
[15:59:18][C][wifi:373]: Channel: 1
[15:59:18][C][wifi:374]: Subnet: 255.255.255.0
[15:59:18][C][wifi:375]: Gateway: 192.168.68.1
[15:59:18][C][wifi:376]: DNS1: 192.168.1.254
[15:59:18][C][wifi:377]: DNS2: 192.168.68.1
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
key: 2866486987
state: NO
missing_state: NO
}
[15:59:18][C][logger:293]: Logger:
[15:59:18][C][logger:294]: Level: VERY_VERBOSE
[15:59:18][C][logger:295]: Log Baud Rate: 115200
[15:59:18][C][logger:296]: Hardware UART: UART0
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
key: 2866486988
state: NO
missing_state: NO
}
[15:59:18][C][i2c.arduino:052]: I2C Bus:
[15:59:18][C][i2c.arduino:053]: SDA Pin: GPIO21
[15:59:18][C][i2c.arduino:054]: SCL Pin: GPIO22
[15:59:18][C][i2c.arduino:055]: Frequency: 50000 Hz
[15:59:18][C][i2c.arduino:058]: Recovery: bus successfully recovered
[15:59:18][I][i2c.arduino:068]: Results from i2c bus scan:
[15:59:18][I][i2c.arduino:074]: Found i2c device at address 0x76
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
key: 2866486989
state: NO
missing_state: NO
}
[15:59:18][VV][api.service:070]: send_binary_sensor_state_response: BinarySensorStateResponse {
key: 2866486990
state: NO
missing_state: NO
}
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-1'
[15:59:18][C][gpio.binary_sensor:015]: Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]: Pin: GPIO14
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-2'
[15:59:18][C][gpio.binary_sensor:015]: Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]: Pin: GPIO5
[15:59:18][VV][api.service:122]: send_light_state_response: LightStateResponse {
key: 3032877358
state: NO
brightness: 1
color_mode: COLOR_MODE_ON_OFF
color_brightness: 1
red: 1
green: 1
blue: 1
white: 1
color_temperature: 0
cold_white: 1
warm_white: 1
effect: ''
}
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-3'
[15:59:18][C][gpio.binary_sensor:015]: Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]: Pin: GPIO23
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-4'
[15:59:18][C][gpio.binary_sensor:015]: Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]: Pin: GPIO34
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-5'
[15:59:18][C][gpio.binary_sensor:015]: Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]: Pin: GPIO19
[15:59:18][VV][api.service:140]: send_sensor_state_response: SensorStateResponse {
key: 4216381597
state: 1.67121e+09
missing_state: NO
}
[15:59:18][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Motion-6'
[15:59:18][C][gpio.binary_sensor:015]: Device Class: 'motion'
[15:59:18][C][gpio.binary_sensor:016]: Pin: GPIO33
[15:59:18][VV][api.service:140]: send_sensor_state_response: SensorStateResponse {
key: 3424784105
state: 22.02
missing_state: NO
}
[15:59:18][C][uptime.sensor:031]: Uptime Sensor 'Uptime Sensor'
[15:59:18][C][uptime.sensor:031]: Device Class: 'duration'
[15:59:18][C][uptime.sensor:031]: State Class: 'total_increasing'
[15:59:18][C][uptime.sensor:031]: Unit of Measurement: 's'
[15:59:18][C][uptime.sensor:031]: Accuracy Decimals: 0
[15:59:18][C][uptime.sensor:031]: Icon: 'mdi:timer-outline'
[15:59:18][V][uptime.sensor:031]: Unique ID: 'c8c9a3c81fd8-uptime'
[15:59:18][VV][api.service:140]: send_sensor_state_response: SensorStateResponse {
key: 3800293995
state: nan
missing_state: YES
}
[15:59:18][C][template.sensor:023]: Template Sensor 'PIR - Uptime'
[15:59:18][C][template.sensor:023]: Device Class: 'timestamp'
[15:59:18][C][template.sensor:023]: State Class: ''
[15:59:18][C][template.sensor:023]: Unit of Measurement: ''
[15:59:18][C][template.sensor:023]: Accuracy Decimals: 0
[15:59:18][C][template.sensor:024]: Update Interval: never
[15:59:18][C][light:104]: Light 'Landing ESP - status LED'
[15:59:18][C][restart.button:017]: Restart Button 'Landing ESP - Restart'
[15:59:18][C][bmp280.sensor:098]: BMP280:
[15:59:18][C][bmp280.sensor:099]: Address: 0x76
[15:59:18][C][bmp280.sensor:111]: IIR Filter: OFF
[15:59:18][C][bmp280.sensor:112]: Update Interval: 25.0s
[15:59:18][C][bmp280.sensor:114]: Temperature 'Landing - Temperature'
[15:59:18][C][bmp280.sensor:114]: Device Class: 'temperature'
[15:59:18][C][bmp280.sensor:114]: State Class: 'measurement'
[15:59:18][C][bmp280.sensor:114]: Unit of Measurement: 'Ā°C'
[15:59:18][C][bmp280.sensor:114]: Accuracy Decimals: 1
[15:59:18][C][bmp280.sensor:115]: Oversampling: 16x
[15:59:18][C][bmp280.sensor:117]: Oversampling: 16x
[15:59:18][C][bh1750.sensor:118]: BH1750 'Landing - Illuminance'
[15:59:18][C][bh1750.sensor:118]: Device Class: 'illuminance'
[15:59:18][C][bh1750.sensor:118]: State Class: 'measurement'
[15:59:18][C][bh1750.sensor:118]: Unit of Measurement: 'lx'
[15:59:18][C][bh1750.sensor:118]: Accuracy Decimals: 1
[15:59:18][C][bh1750.sensor:119]: Address: 0x23
[15:59:18][E][bh1750.sensor:121]: Communication with BH1750 failed!
[15:59:18][C][bh1750.sensor:124]: Update Interval: 10.0s
[15:59:18][C][homeassistant.time:010]: Home Assistant Time:
[15:59:18][C][homeassistant.time:011]: Timezone: 'GMT0BST,M3.5.0/1,M10.5.0'
[15:59:18][C][captive_portal:088]: Captive Portal:
[15:59:18][C][web_server:125]: Web Server:
[15:59:18][C][web_server:126]: Address: staircase-sensor.local:80
[15:59:18][C][mdns:103]: mDNS:
[15:59:18][C][mdns:104]: Hostname: staircase-sensor
[15:59:18][V][mdns:105]: Services:
[15:59:18][V][mdns:107]: - _esphomelib, _tcp, 6053
[15:59:18][V][mdns:109]: TXT: version = 2022.12.1
[15:59:18][V][mdns:109]: TXT: mac = c8c9a3c81fd8
[15:59:18][V][mdns:109]: TXT: platform = ESP32
[15:59:18][V][mdns:109]: TXT: board = esp32dev
[15:59:18][V][mdns:109]: TXT: network = wifi
[15:59:18][V][mdns:107]: - _http, _tcp, 80
[15:59:19][C][ota:093]: Over-The-Air Updates:
[15:59:19][C][ota:094]: Address: staircase-sensor.local:3232
[15:59:19][C][ota:097]: Using Password.
[15:59:19][C][api:138]: API Server:
[15:59:19][C][api:139]: Address: staircase-sensor.local:6053
[15:59:19][C][api:143]: Using noise encryption: NO
[15:59:21][VV][scheduler:196]: Running interval '' with interval=10000 last_execution=5143 (now=15145)
[15:59:21][VV][scheduler:196]: Running interval 'update' with interval=25000 last_execution=4294957481 (now=15185)
[15:59:21][V][bmp280.sensor:125]: Sending conversion request...
[15:59:21][VV][i2c.arduino:140]: 0x76 TX F4B5
[15:59:21][VV][scheduler:026]: set_timeout(name='data', timeout=76)
[15:59:21][VV][scheduler:196]: Running timeout 'data' with interval=76 last_execution=15205 (now=15282)
[15:59:21][VV][i2c.arduino:140]: 0x76 TX FA
[15:59:21][VV][i2c.arduino:116]: 0x76 RX 7F8FD0
[15:59:21][VV][i2c.arduino:140]: 0x76 TX F7
[15:59:21][VV][i2c.arduino:116]: 0x76 RX 5A3D70
[15:59:21][D][bmp280.sensor:149]: Got temperature=22.4Ā°C pressure=1009.7hPa
[15:59:21][V][sensor:076]: 'Landing - Temperature': Received new state 22.420000
[15:59:21][VV][sensor.filter:014]: Filter(0x3ffb29c8)::input(22.420000)
[15:59:21][VV][sensor.filter:024]: Filter(0x3ffb29c8)::output(22.020000) -> 0x3ffb3534
[15:59:21][VV][sensor.filter:014]: Filter(0x3ffb3534)::input(22.020000)
[15:59:31][VV][scheduler:196]: Running interval '' with interval=10000 last_execution=15143 (now=25143)
[15:59:31][VV][api.service:470]: on_ping_request: PingRequest {}
[15:59:31][VV][api.service:043]: send_ping_response: PingResponse {}
[15:59:32][VV][api.service:470]: on_ping_request: PingRequest {}
[15:59:32][VV][api.service:043]: send_ping_response: PingResponse {}
[15:59:41][VV][scheduler:196]: Running interval '' with interval=10000 last_execution=25143 (now=35147)
[15:59:44][VV][scheduler:196]: Running interval '' with interval=60000 last_execution=4294944638 (now=37342)
[15:59:46][VV][scheduler:196]: Running interval 'update' with interval=25000 last_execution=15185 (now=40187)
[15:59:46][V][bmp280.sensor:125]: Sending conversion request...
[15:59:46][VV][i2c.arduino:140]: 0x76 TX F4B5
[15:59:46][VV][scheduler:026]: set_timeout(name='data', timeout=76)
[15:59:46][VV][api.service:470]: on_ping_request: PingRequest {}
[15:59:46][VV][api.service:043]: send_ping_response: PingResponse {}
[15:59:46][VV][scheduler:196]: Running timeout 'data' with interval=76 last_execution=40209 (now=40285)
[15:59:46][VV][i2c.arduino:140]: 0x76 TX FA
[15:59:46][VV][i2c.arduino:116]: 0x76 RX 7F9040
[15:59:46][VV][i2c.arduino:140]: 0x76 TX F7
[15:59:47][VV][i2c.arduino:116]: 0x76 RX 5A3E00
JulianDH
(Julian Hall)
December 16, 2022, 9:58pm
10
Working with me ā¦ and all my other 12c nodes
esp8266:
board: d1_mini
i2c:
sda: 04
scl: 05
scan: true
id: bus_a
sensor:
- platform: bmp280
i2c_id: bus_a
temperature:
name: "Temperature Roof Weather Box"
oversampling: 16x
accuracy_decimals: 0
pressure:
name: "Pressure"
accuracy_decimals: 0
address: 0x77
update_interval: 120s
1 Like
found this thread because of similar problems.
was able to fix it by lowering the IĀ²C bus frequency.
i2c:
scl: D2
sda: D1
scan: false # set true and watch output to find the BUS ID
id: bus_a
frequency: 100kHz
display:
- platform: ssd1306_i2c
i2c_id: bus_a
model: "SH1106 128x64" # with default IĀ²C frequencies it only works as "SH1106 128x32", hangs if set to 64!
id: "my_oled"
cheers,
p3001
(PLee)
February 22, 2024, 1:12am
12
I have been working on the āComponent xxx took a long timeā¦ā warning enabled by recent esphome versions. That is sometimes due to low i2c speed settings, like the default 50kHz. (This is unrelated to the OP issues.)
Measuring a D1-mini, with no pullup, the i2C lines rise time is around 4us. That alone would limit the i2C speed to probably no more than 100kHz (from raw signal point of view).
With 22K pullup, the rise time is around 800ns. That would raise the upper limit to no more than 500kHz.
i2c speed depends on a number of things, and pullups have a significant role. Typical sensor modules often have pullups but of various values. Therefore, for example, two bmp280 modules of different made could work differently on i2c speed. Also, long wire length can also reduce the workable speed.
This was very helpful for meā¦ even if it is an āolderā postā¦ thanks a lot.
I was struggling with two thingsā¦ finding the right board for my Wemos-D1-Mini and running a BH1750 on it (sounds simple, but took me hours)
1 Like
Brad_Ford
(Wombo1)
September 6, 2024, 3:54pm
14
Does anyone know if I can use any I2C out of the box, or is their a list of devices that are in ESPHome by default?
IE: if I find an interesting device, can I just add it in my YAML?
wjcarpenter
(Bill Carpenter)
September 6, 2024, 11:28pm
15
The short answer is no.
I2C is a low-level protocol for reading and writing bytes from and to devices. What is in those bytes is mostly dependent on what the specific device has implemented. So, to use any particular type of device, you want it to have a supporting component definition in ESPHome.
Now, technically, you can probably cobble together custom C++ code to put into your YAML file as an ESPHome script to read and write the necessary logic, but thatās at best a clumsy way to go about it. For less effort, you could implement it directly as a C++ component that you then merely reference from your YAML.
ESPHome has so many components already implemented that itās usually pretty easy to find something pretty similar (in implementation logic) to a hypothetical new device. It would be a bit beyond copy-and-paste, but an experienced developer could usually dope it out fairly easily.
clydebarrow
(Clyde Stubbs)
September 7, 2024, 12:37am
16
There is a draft PR that would add the ability to target otherwise unsupported i2c devices with purely YAML config - you could try that out.
esphome:dev
ā javawizard:jw/custom-i2c
opened 12:49AM - 19 Feb 24 UTC
So I started implementing a custom I2C component (see [this discord message](httā¦ ps://discord.com/channels/429907082951524364/524177279270780928/1204312411859849266) and [this discord thread](https://discord.com/channels/429907082951524364/1204312566231334992) for the humble beginnings) and I wanted to throw up a draft PR with what I have so far. **This is still a work-in-progress** - there's plenty of stuff left to do before it's ready for review, but I want to start a discussion and gather thoughts/feedback/requests in the mean time.
## What is it?
This is a **custom I2C component:** it allows one to talk to I2C devices that don't have an ESPHome component written for them yet.
It's easy to use: talking to a [SparkFun Qwiic Relay](https://www.sparkfun.com/products/15093), for example, is as simple as:
```yaml
custom_i2c:
devices:
- address: 0x18
switch:
- platform: template
name: "Relay"
optimistic: true
turn_on_action:
custom_i2c.write: 1
turn_off_action:
custom_i2c.write: 0
```
## Why
It turns out there's a pretty steep learning curve involved in learning to write ESPHome components from scratch - you have to know Python and C++, you have to learn ESPHome's codegen API (what the heck is a `Pvariable`?), schema validation, [etc., etc.,](https://discord.com/channels/429907082951524364/1204312566231334992/1204339559245029376) and then you have to write all the C++ stuff to do the actual thing you want your component to do.
That's all perfectly doable, but it's a bit daunting when all you want to do is "hey, there's this switch, when it turns on please write this value to this register, when it turns off please write this value to this other register, also you can poll this third register to get its current state and it's on if that register's value is 1" (a la https://www.sparkfun.com/products/15093).
The `custom_i2c` component intends to address that. My goal is to have it be possible to talk to any I2C device and both simple and easy to talk to ~80% of I2C devices. Having it be way easier/simpler/quicker to write I2C component support using `custom_i2c` than it is to write a dedicated component is [explicitly a design goal of the `custom_i2c` component](https://discord.com/channels/429907082951524364/1204312566231334992/1204340037387419648).
## Goals
- **Really easy to use:** the hardest part should be reading your device's datasheet and figuring out what you need to tell it in the first place
- **Batteries included:** support for emulating GPIO pins (and by extension binary sensors and switches) and outputs (and by extension lights) is already included, and I'm very open to adding support for other types of entities as well.
- **Flexible:** as few artificial limits as possible. (Registers whose addresses are arbitrary numbers of bytes are supported; controlling [this board](https://www.adafruit.com/product/5296) involves using both 2-byte and 3-byte register addresses, for example, and the `custom_i2c` component fully supports that.)
## Non-goals
- **100% comprehensive:** While it would be nice to support every I2C device in existence, there are plenty of devices that are way too complicated to talk to without lots of specialized device-specific logic. It's a goal for the `custom_i2c` component to allow you talk to them if you're willing to write piles upon piles of lambdas to do the heavy lifting, but it's not a goal for it to bend over backward to do the heavy lifting for you if it would be just as much work to write a dedicated ESPHome component to do it.
## Possible future goals
- **Support for device "templates":** Assuming the `custom_i2c` component does its job right, it's only a small logical leap from teaching `custom_i2c` how to talk to your devices to *sharing those configurations with others.* In particular, if `custom_i2c` turns out as good as I hope it will, I think it would be interesting to explore the idea of allowing core ESPHome support for new I2C devices to be written as `custom_i2c` configs, which would lower the barrier to official support being added for new I2C devices. It's still far too early to tell whether that's a tenable goal, however; a more manageable short term goal would be to allow hosting of, and importing, device configs from e.g. Git/GitHub repos.
## Current status
**It works!** It compiles and runs and everything, and I have it controlling a [Adafruit LED arcade button board](https://www.adafruit.com/product/5296) and a [SparkFun single relay board](https://www.sparkfun.com/products/15093) and a [SparkFun quad relay board](https://www.sparkfun.com/products/16566) and everything's working flawlessly.
Now comes the fun part. I want to:
- Get feedback from the community - I realized I'm cutting [somewhat against the grain](https://github.com/esphome/feature-requests/issues/492#issuecomment-555733954) here, so early thoughts and requests would be welcome. (I really do believe this is worth doing though.)
- Knock out all of the tasks listed below
- Then get this officially reviewed and (hopefully) merged in
And then š š š
## How can I try it out?
Easy: drop this in your config file:
```yaml
external_components:
- source: github://pr#6241
components: [ custom_i2c ]
```
Then add a `custom_i2c:` config block and go to town. (More documentation on how exactly to write a `custom_i2c` config coming soon)
## Feedback welcome!
So that's it. If you have any thoughts, requests etc., please leave a comment!
## Things left to do
- [ ] **Add more example configs to this PR summary and/or discussion thread**
- [ ] Stabilize API/config format
- [ ] Write [esphome-docs](https://github.com/esphome/esphome-docs) documentation
- [ ] Code cleanup
- [ ] Constantize a few things that aren't constantized
- [ ] Split `custom_i2c.h` and `__init__.py` into multiple files (probably)
- [ ] Implement all the `dump_config` methods
- [ ] (possibly) Split `CustomI2CPinBank`'s registers into separate components (there's a code comment on the what and why)
- [ ] Add tests
- [ ] Test on devices other than ESP32 (help appreciated from anyone who owns any!)
Brad_Ford
(Wombo1)
October 6, 2024, 2:43am
17
Thanks for that, it looks to be bring what I can doā¦ And I am not sure about others demand for this specific device!
Iāve ended up going with a 0.4 to 20 device that seems to be working quite well so far!