adds code

Getting Started with Sensors and Relays in Node-RED

Published: March 6, 2026

If you want to build practical automation projects with Node-RED, learning how to work with sensors and relays is one of the best places to begin. Sensors let your flows read real-world data like temperature, humidity, motion, or light levels. Relays let Node-RED control higher-power devices such as lamps, fans, pumps, and other electrical loads.

In this guide, you’ll learn the basics of connecting sensor data to dashboards, using logic to trigger actions, and controlling relays safely. Whether you are using a Raspberry Pi, ESP32, ESP8266, or Arduino, the same core ideas apply.


Watch: Recommended YouTube Video

This tutorial is a strong fit for beginners because it combines sensor monitoring, Node-RED dashboard control, and MQTT-based automation in a practical setup.

Watch the full video on YouTube


What You’ll Learn in This Node-RED Sensors and Relays Tutorial

  • How sensors send data into Node-RED
  • How relays let Node-RED switch devices on and off
  • How MQTT helps connect microcontrollers and automation flows
  • How to display sensor values on a Node-RED dashboard
  • How to trigger automations based on temperature, humidity, or other readings
  • How to choose beginner-friendly hardware for your first projects

What Are Sensors in Node-RED?

A sensor is a component that measures something in the environment. Common examples include:

  • Temperature sensors for room or equipment monitoring
  • Humidity sensors for climate tracking
  • Motion sensors for presence detection
  • Light sensors for automatic lighting projects
  • Water leak sensors for home safety

In a typical Node-RED setup, the sensor is connected to a microcontroller or computer, and its readings are sent into your flow using GPIO, serial, HTTP, or MQTT. Once the data is inside Node-RED, you can transform it, visualize it, store it, or use it to trigger actions.

Example Sensor Automation Ideas

  • Turn on a fan when temperature rises above a threshold
  • Send an alert if humidity gets too high in a greenhouse
  • Switch on a light when motion is detected
  • Log environmental data to a dashboard or database

What Is a Relay and Why Use One?

A relay is an electrically controlled switch. It allows a low-power signal from a controller to switch a higher-power device. In automation projects, this is how Node-RED can control things in the physical world.

For example, a relay can be used to:

  • Turn lamps on and off
  • Activate fans or pumps
  • Control solenoid valves
  • Switch small appliances
  • Trigger sirens or alarms

Important: many relays can switch mains voltage. Always follow proper electrical safety practices. If you are new to relays, start with low-voltage loads first and only work with mains power if you fully understand the risks.


How Node-RED Works with Sensors and Relays

The logic usually follows a simple pattern:

  1. A sensor measures something like temperature or humidity
  2. The reading is sent into Node-RED
  3. A flow checks the value against a rule
  4. If the condition is met, Node-RED sends a command
  5. A relay switches a device on or off

This creates a clean event-driven automation loop. You can also add:

  • Dashboards to display live values
  • Notifications through email, Telegram, or mobile alerts
  • Data logging to InfluxDB, Google Sheets, or files
  • Timers and schedules for smarter control
  • MQTT brokers for scalable IoT projects

Beginner-Friendly Hardware for Node-RED Automation Projects

If you are just getting started, these types of products make the learning curve much smoother:

  • A sensor kit so you can test different inputs
  • A relay module for switching outputs
  • A DHT22 sensor for temperature and humidity monitoring

Recommended Products

Gowoops 37-in-1 Sensor Kit

Great for beginners who want a variety of sensors to experiment with in Node-RED, Arduino, or ESP32 projects.

Why it helps: You can test motion, sound, light, moisture, and other signals without buying everything separately.

Check Price on Amazon

As an Amazon Associate I earn from qualifying purchases.

5V Relay Module 4-Channel

A practical relay board for controlling multiple outputs from one project.

Why it helps: Ideal for learning relay logic, switching devices, and building multi-output automation projects.

Check Price on Amazon

As an Amazon Associate I earn from qualifying purchases.

DHT22 Temperature Humidity Sensor

One of the most popular sensors for beginner IoT and home automation projects.

Why it helps: Perfect for learning how Node-RED can read live environmental data and trigger automations.

Check Price on Amazon

As an Amazon Associate I earn from qualifying purchases.


Simple Example: Temperature Sensor Controls a Fan

Here is a beginner-friendly automation example:

  1. A DHT22 sensor measures room temperature
  2. The value is published to MQTT or sent directly to Node-RED
  3. A switch node checks whether the temperature is above 28°C
  4. If true, Node-RED sends an output signal to a relay
  5. The relay turns on a fan
  6. When temperature drops, the relay turns the fan off

This same pattern can be reused for heating, lighting, irrigation, extraction fans, and many other DIY automation systems.


Node-RED Dashboard Ideas for Sensor Projects

A dashboard makes your project much more useful. With the Node-RED dashboard, you can create a control panel that shows live values and lets you manually override automations.

You can display:

  • Temperature and humidity gauges
  • Status indicators for relays
  • Buttons for manual on/off control
  • Charts for historical sensor data
  • Alerts when values exceed safe limits

This is especially useful in smart home, workshop, server room, and greenhouse projects.


Best Practices for Working with Relays in Node-RED

  • Start with low-voltage devices before attempting mains switching
  • Use proper power supplies for sensors and relay modules
  • Add delays or hysteresis so relays do not rapidly switch on and off
  • Use MQTT topics with clear names for easy debugging
  • Label dashboard controls clearly so manual overrides are obvious
  • Test each part separately: sensor input, logic, then relay output

Common Beginner Mistakes

  • Powering a relay board incorrectly
  • Using the wrong voltage for the sensor or controller
  • Forgetting pull-up or pull-down requirements
  • Creating automations with no debounce or threshold buffer
  • Mixing up normally open and normally closed relay terminals

Why MQTT Is So Useful for Node-RED Sensor and Relay Projects

MQTT acts like a lightweight message highway for IoT devices. Instead of directly wiring every device into one computer, microcontrollers can publish sensor data to a broker and Node-RED can subscribe to it. In the other direction, Node-RED can publish commands that devices use to switch relays.

This makes your project easier to scale. Today it may be one sensor and one relay. Tomorrow it may be ten rooms, multiple dashboards, and a whole web of automations.


Who This Tutorial Is For

This article is ideal for:

  • Node-RED beginners
  • DIY home automation hobbyists
  • Arduino and ESP32 users
  • Raspberry Pi tinkerers
  • Makers building their first sensor dashboard
  • Anyone learning practical IoT automation

Frequently Asked Questions

Can Node-RED read sensors directly?

Yes, depending on your setup. Node-RED can read sensors directly on supported hardware or receive sensor data from devices like ESP32, ESP8266, Arduino, and Raspberry Pi through MQTT, serial, HTTP, or GPIO nodes.

Can Node-RED control relays?

Yes. Node-RED can send digital output signals or MQTT commands that trigger relay modules, smart switches, and other output devices.

What is the best sensor for beginners?

A DHT22 is a popular starting point because it is inexpensive, easy to use, and gives you two useful values: temperature and humidity.

Do I need MQTT to use Node-RED with sensors?

No, but MQTT is one of the most flexible and scalable ways to connect devices and automation flows, especially in larger IoT projects.

Can I use Node-RED for home automation?

Absolutely. Node-RED is widely used for home automation, smart dashboards, sensor monitoring, and device control.


Final Thoughts

Getting started with sensors and relays in Node-RED opens the door to a huge range of automation projects. Once you understand how to read sensor data, apply logic, and switch outputs, you can build practical systems for your home, workshop, or IoT experiments.

Start small. Read one sensor. Control one relay. Then expand your flow step by step.


Tags: Node-RED, sensors, relays, MQTT, ESP32, Arduino, Raspberry Pi, home automation, IoT, dashboard, DHT22

Previous Post Next Post