Scroll down for more guides

Part 5 · Advanced Home Automation 6 min read

Sensors wired to a board and a smooth analogue signal turned into digital steps

Turning the Physical World Into Reliable Numbers

Every automation depends on information from the real world: a button being pressed, a door being opened, the temperature of a room or the level of water in a tank.

Microcontrollers and controllers read this information through inputs. Some inputs only know on and off, others measure a value, and many modern sensors send their readings as digital data.

This guide explains the main types of inputs, how to connect sensors correctly and how to turn raw readings into values you can rely on.

1. Digital Inputs

A digital input reads one of two states: high or low. It is used for buttons, switches, door contacts, float switches, motion sensor outputs and potential-free contacts from other equipment.

  • Use a pull-up or pull-down resistor – often available inside the microcontroller – so that the input has a defined state when the switch is open.
  • Keep the input voltage within the chip’s limits; ESP32 inputs work at 3.3 V.
  • For signals from other equipment or long cables, consider an optocoupler to separate the circuits and protect the microcontroller.

2. Debouncing

When a mechanical switch or button closes, its contacts bounce for a few milliseconds, creating several quick on-off changes. Without precautions, one press can be counted as several.

Debouncing solves this, either in software (accepting a change only when it has been stable for a short time) or in hardware (a small capacitor and resistor). Firmware such as ESPHome includes debouncing filters that can be enabled in the configuration.

3. Analogue Inputs and the ADC

An analogue input measures a voltage within a range. An analogue-to-digital converter (ADC) turns the voltage into a number.

  • The resolution is the number of steps. A 12-bit ADC divides the range into 4,096 steps.
  • The input range is the voltage that can be measured. On ESP32 chips it depends on the variant and the attenuation setting – for the original ESP32, Espressif recommends staying within roughly 0.15–2.45 V for accurate readings, while some newer variants reach about 3.1 V. Never apply more than the chip’s supply voltage, and check the documentation for your exact chip.
  • The accuracy is often lower than the resolution suggests. The ESP32’s ADC is known to be less accurate and not perfectly linear, especially near the ends of its range; Espressif provides calibration functions that improve the results.

Analogue inputs are used for potentiometers, light-dependent resistors, some soil moisture sensors and simple voltage measurements. For precise measurements, a digital sensor or an external ADC chip is usually the better choice.

4. Voltage Dividers

To measure a voltage higher than the input can handle – for example a 12 V battery – two resistors form a voltage divider that reduces the voltage in a fixed ratio.

Example: with 100 kΩ and 22 kΩ resistors, 12 V becomes about 2.2 V at the input. The software multiplies the reading back by the same ratio.

  • Choose the ratio so that the highest expected voltage stays safely below the input limit.
  • Use resistors with good tolerance, or calibrate against a multimeter.
  • Never use a voltage divider to measure mains voltage. Use certified energy meters or isolated measuring modules instead.

5. Digital Sensors and Buses

Many modern sensors contain their own measuring electronics and send the result digitally. This is usually more accurate and less sensitive to noise than an analogue reading:

InterfaceHow It WorksTypical Sensors
I²CTwo wires shared by several sensors, each with its own addressTemperature, humidity, pressure, light, air quality
SPIA faster bus with more wiresDisplays, high-speed sensors, some ADCs
1-WireOne data wire shared by several sensorsDS18B20 temperature sensors, also on longer cables
UART (serial)Two wires for sending and receivingCO₂ and particle sensors, meter interfaces, radar modules

I²C is designed for short distances, typically on the same board or a short cable. For sensors far away, choose 1-Wire, a dedicated bus driver or place a separate microcontroller near the sensor.

6. Industrial Signals: 0–10 V and 4–20 mA

Professional sensors and building equipment often use standard analogue signals:

  • 0–10 V – the value is represented by a voltage. It must be reduced with a voltage divider or a suitable module before it reaches a 3.3 V microcontroller.
  • 4–20 mA – the value is represented by a current. It is robust over long cables, and a reading below 4 mA indicates a broken wire. A precision resistor or a dedicated converter module turns the current into a voltage for measurement.

Using ready-made, isolated input modules for these signals is often safer and more accurate than building the circuit yourself.

7. Noise and Filtering

Real measurements always contain some noise – small random variations caused by electronics, cables and the environment.

  • Take several readings and use the average or the median.
  • Ignore values outside the physically possible range.
  • Report only when the value has changed by a meaningful amount, to reduce network traffic.
  • Keep sensor cables away from mains cables and motors, and use shielded or twisted cable for longer runs.
  • Place a small capacitor close to an analogue input to smooth fast disturbances.

8. Calibration and How Often to Measure

Even good sensors may show a constant offset. Compare the reading with a trusted reference and correct it in the software. For sensors with a non-linear response, a calibration table with several reference points gives better results.

How often you measure depends on the purpose:

MeasurementTypical Interval
Room temperature and humidityEvery 30 seconds to a few minutes
Water leak, door contact, buttonImmediately, as events
Electricity consumptionEvery few seconds
Soil moisture, tank levelEvery few minutes to every hour

Battery-powered sensors should measure as seldom as their purpose allows, to save energy.

Final Thoughts

Inputs connect automation to the physical world. Digital inputs read switches and contacts, analogue inputs measure voltages, and digital sensor buses deliver accurate values with little noise.

Protect inputs against voltages they cannot handle, debounce mechanical contacts, and filter and calibrate measurements before you use them in rules.

With careful connection and sensible processing, raw readings become reliable numbers – the foundation of every good automation.

MISOLIMA® – Technology Made Practical.

Practical knowledge for understanding, designing and building home automation.