The Top 10 ESPHome Sensor Connection Examples
ESPHome is an incredible tool for anyone interested in home automation. It offers a flexible platform to connect, control, and automate a wide range of sensors. From temperature to motion sensors, ESPHome provides a streamlined way to integrate these components into your home automation devices.
Here are the top 10 examples of how you can use ESPHome to connect different types of sensors.
1. Temperature & Humidity Sensors
Temperature monitoring is an important factor to consider when home automation is the goal. Together, ESPHome and a device like the DHT22 work in harmony to make monitoring temperatures an easy task. It's a great start to completing your home setup efficiently and quickly.
With convenient access to detailed temperature readings, the possibilities are endless! Connecting this sort of system to your environment has never been simpler. You could customize various parts of your living space to perfectly maintain sure temperature levels indoors all year long with such sensors playing a crucial role.
Here's a code snippet:
sensor:
- platform: dht
pin: D2
temperature:
name: "Living Room Temperature"
humidity:
name: "Living Room Humidity"
update_interval: 60s
Using ESPHome, you can very easily expose a sensor called "Living Room Temperature" that displays the current temperature. Because a DHT sensor usually has both a temperature and humidity sensor, you can also easily register this humidity sensor and expose it to read the current humidity levels of your living room.
Don't have a DHT sensor but still want to read the humidity levels of a room in your house? No problem! The DHT configuration also supports sensors such as AMS2301/AM2302 and RHT03.
For more information, head over to the DHT sensor documentation.
2. Motion Sensors
Motion sensors like the PIR sensor can be used for security systems or automated lighting. Luckily for us, Passive Infrared (PIR) Sensors are supported by the ESPHome platform and quite easy to set up.
Here's how you can set it up:
binary_sensor:
- platform: gpio
pin: D1
name: "PIR Sensor"
device_class: motion
As you can see, we need to use a binary_sensor for this. This roughly translates to: Movement detected, no movement detected, 0 or a 1. If you'd like directions on how to connect a PIR sensor to your NodeMCU or Arduino, have a look at this tutorial: Generic PIR.
Binary sensors in ESPHome are not limited to just motion detection. You'll actually find these types of sensors and configurations quite often. These versatile devices can be programmed to fulfill a variety of roles in a home automation setup. For instance, they can be used as door/window sensors, detecting whether they're opened or closed, therefore contributing to a home's security system.
Additionally, with some creativity, binary sensors can be used for less conventional purposes. For example, they can be programmed to detect if your mailbox has been opened and alert you when you've got mail. This flexibility and customization potential make binary sensors an invaluable component in any ESPHome automation system.
3. CO2 Sensors
Are you building an indoor air quality device? Then a CO2 sensor is an important part of this device! There are several sensors out there that can monitor CO2 levels in your house, but I'll focus on the MH-Z19 in this section. Most configurations are quite similar to the one I'm sharing here though.
You can monitor indoor air quality by using CO2 sensors such as the MH-Z19:
uart:
rx_pin: GPIO3
tx_pin: GPIO1
baud_rate: 9600
sensor:
- platform: mhz19
co2:
name: "MH-Z19 CO2 Value"
temperature:
name: "MH-Z19 Temperature"
update_interval: 60s
automatic_baseline_calibration: false
This configuration consists of two parts: The UART bus configuration and the actual sensor configuration. A UART bus is a way to send and receive information. It helps microcontrollers communicate with other parts of a system. The data is sent without a clock signal, which makes it fast and reliable. Start and stop bits are added to make sure the data is accurate. In ESPHome setups, the UART bus allows the microcontroller to talk to the MH-Z19 CO2 sensor.
Several other CO2 sensors work with a similar configuration: T6613/15, and ZyAura devices. The configuration of these sensors looks a lot like this configuration, with the only difference being that the platform is different and these devices have different sensors built-in. The T6613/15, for example, has no temperature sensor, while the ZyAura devices have a CO2, temperature, and humidity sensor.
Visit the MH-Z19 sensor guide for more information on calibrating this sensor and additional configuration settings.
4. Proximity Sensors
The Ultrasonic Distance Sensor allows you to use simple ultrasonic sensors like the HC-SR04 with ESPHome to measure distances. These sensors are great for measuring objects up to two meters away, and they have some cool features. With this sensor, you can send a trigger pulse and get an echo pulse back to calculate the distance. Plus, there's an option to set a timeout if no object is detected.
Here's an example configuration:
sensor:
- platform: ultrasonic
trigger_pin: D1
echo_pin: D2
name: "Ultrasonic Sensor"
To get started, you'll need to specify the trigger and echo pins, as well as give your sensor a name. You can also customize the update interval if needed. For more advanced settings, you can adjust the timeout or pulse duration. Overall, this sensor is easy to use and can add a new dimension to your projects. So why not give it a try and see what you can measure with the Ultrasonic Distance Sensor?
You can find more settings to configure on the Ultrasonic Distance Sensor page.
5. Light Sensors
The BH1750 Ambient Light Sensor is a fantastic addition to your ESPHome setup. With the bh1750 sensor platform, you can effortlessly integrate your BH1750 ambient light sensor (datasheet) into your projects. To get started, simply set up the I²C bus in your configuration.
Here's an example configuration entry to help you get going:
sensor:
- platform: bh1750
name: "BH1750 Illuminance"
address: 0x23
update_interval: 60s
In this configuration, you have the flexibility to customize various settings. The name field allows you to give a unique name to the sensor. Additionally, you can manually specify the address of the sensor and set the update_interval to control how frequently the sensor is checked.
You can find extra information on the default values and required properties for this sensor on the documentation page.
6. Pressure Sensors
The BMP280 Temperature+Pressure Sensor is a powerful tool for measuring temperature and pressure. With the bmp280 sensor platform and ESPHome, you can easily integrate the BMP280 sensor into your projects. Setting up the I²C bus is necessary to get started.
To configure the sensor, use the following example:
sensor:
- platform: bmp280
temperature:
name: "Outside Temperature"
oversampling: 16x
pressure:
name: "Outside Pressure"
address: 0x77
update_interval: 60s
We've already seen most of these configuration settings in the other examples, so I'll skip over most of them and highlight the "oversampling" property. The oversampling controls how often a measurement is done before it reports the value. So when you see the sensor saying it's 20 degrees, it'll have measured the temperature 16 times to get an accurate reading.
On the documentation page, you'll find all oversampling options to use. You can get less accurate readings more quickly or take your time and get a very accurate reading. It's up to you and your needs.
Get ready to explore endless possibilities with the BMP280 Temperature+Pressure Sensor and unleash your creativity!
7. Gas Sensors
The BME680 Temperature+Pressure+Humidity+Gas Sensor is a powerful tool that allows you to measure temperature, pressure, humidity, and gas levels with ease. By using the bme680 sensor platform with ESPHome, you can seamlessly integrate this sensor into your projects. To get started, simply set up the I²C bus configuration, and you'll be ready to go!
Let's look at an example configuration:
sensor:
- platform: bme680
temperature:
name: "BME680 Temperature"
oversampling: 16x
pressure:
name: "BME680 Pressure"
humidity:
name: "BME680 Humidity"
gas_resistance:
name: "BME680 Gas Resistance"
address: 0x77
update_interval: 60s
You'll notice the oversampling property that we've already looked at for the pressure sensor. It uses the same values you'll already be familiar with. Other than that, the configuration is quite straightforward and will look very much like the other examples we've already looked at. This is what I like so much about ESPHome.
You can have a look at some of the optional properties to configure on the bme680 sensor page. So, why wait? Get your hands on the BME680 sensor and unlock a whole new world of possibilities for your projects!
8. Vibration Sensors
Are you tired of missing out on important notifications from your appliances just because they're not "smart"? Don't worry, I've got an exciting solution for you!
Introducing the SW-420 vibration sensor - your secret weapon to transform any appliance into a smart one! Imagine attaching the SW-420 to the back of your washer and dryer, and being notified remotely whenever there's activity in your laundry room. But it doesn't stop there! This concept can be extended to other appliances too.
All you need for the vibration sensor to work is a binary_sensor configuration. Remember that one? "There is vibration" or "There is no vibration", the 0 and 1. Let's look at an example:
binary_sensor:
- platform: gpio
pin: GPIO13
name: "washer"
device_class: vibration
filters:
- delayed_on: 10ms
- delayed_off: 5min
This binary sensor has a delay built-in, so you're not getting notified when there isn't a vibration for a few seconds, for example during a washing cycle. This binary sensor will wait for 5 minutes before reporting the sensor as being "off", so you can be sure that the reading is not a fluke, but the washer is off.
This configuration is part of a "LaundryBot" project on GitHub and comes with a really easy-to-follow tutorial, so I encourage you to give this project a try and make your dumb appliances smart: LaundryBot.
9. Sound Sensors
Sound sensors are a little like vibration sensors because they're binary sensors. These sensors will only have 2 states: "There is sound" and "There is no sound". This can be a great addition to your home security system because it could detect glass breaking or doors opening.
An example configuration for this type of sensor look like this:
binary_sensor:
- platform: gpio
pin: GPIO13
name: "Sound sensor"
device_class: sound
filters:
- delayed_off: 1min
This configuration will notify you right away when a sound has been detected but will wait for a minute before letting you know it's quiet again. You can use this configuration for any sound sensor that has a digital output (HIGH/LOW).
With sound sensors, you can make a lot of creative automation. For example, you can set up a smart alert system that toggles lights or sends a notification when the sensor detects sounds. Think of a baby crying detector that sends you an alert, or an automation that switches on the lights when it hears your alarm clock in the morning.
Every sound in your home can potentially serve as a trigger for unique automations.
10. Power Sensors
Last but not least, power sensors. The CS5460A sensor platform enables you to utilize the CS5460A AC voltage, current, and power meter chip with ESPHome. To establish communication between your device and the CS5460A, you need to include an SPI bus in your configuration with the appropriate PINs assigned for the MOSI, MISO, and CLK lines. These correspond to the SDI, SDO, and SCLK pins on the CS5460A, respectively.
Meanwhile, the CS (chip-select) pin of the CS5460A can be connected to any available GPIO or wired to ground if the CS5460A is the sole device connected to your device's SPI bus. The RESET pin can also be controlled by a GPIO or wired to VCC. This is an example configuration:
spi:
clk_pin: 18
mosi_pin: 23
miso_pin: 19
sensor:
- platform: cs5460a
current:
name: "Kitchen current (RMS)"
filters:
delta: 0.1
power:
name: "Kitchen power"
filters:
delta: 5
voltage:
name: "Mains voltage (RMS)"
filters:
delta: 5
samples: 1600
current_gain: 0.01
voltage_gain: 0.000573
pulse_energy: 1 Wh
cs_pin: 33
With this configuration, you can measure all types of electrical currents. Be careful when working with electricity though, as it could be incredibly dangerous if you don't know what you're doing.
If you've looking for more information about configuring this sensor, have a look at the documentation.
Posted on: Aug 10, 2023