top of page

Interfacing NodeMCU with a Solenoid Motor using a Relay

Updated: Aug 17, 2023

NodeMCU, a popular IoT development board, provides a flexible platform for connecting and controlling various devices. In this article, we will explore how to interface a NodeMCU with a 9V solenoid motor using a relay module and a 9V battery. This setup allows for safe and efficient control of the motor, making it suitable for a wide range of projects.

Solenoid

Components Required: | NodeMCU |


To get started, gather the following components:


1. NodeMCU development board

2. 9V solenoid motor

3. Relay module (suitable for the motor's voltage and current)

4. 9V battery or suitable alternative power supply

5. Jumper wires

6. Breadboard (optional, for easier connections)


Circuit Connections:


Follow these steps to connect the components correctly:


1. Connect the positive terminal of the 9V battery to the VCC pin of the relay module.

2. Connect the negative terminal of the battery to the GND pin of the relay module.

3. Connect the input signal pin (IN) of the relay module to a digital output pin of the NodeMCU (e.g., D1).

4. Connect the solenoid motor's positive terminal to the common (COM) pin of the relay module.

5. Connect one end of a diode (e.g., 1N4007) to the solenoid motor's positive terminal.

6. Connect the other end of the diode to the normally open (NO) pin of the relay module.

7. Connect the solenoid motor's negative terminal to the GND pin of the relay module.


Code Explanation


The code starts by defining a constant variable "relayPin" as the digital output pin (D1) of the NodeMCU, which is connected to the relay module. In the "setup()" function, the relay pin is initialized as an output. Within the "loop()" function, the motor is turned on by setting the relay pin to HIGH, and a delay of 2 seconds is added to keep the motor running. Then, the motor is turned off by setting the relay pin to LOW, followed by another 2-second delay to keep the motor off. This loop continues indefinitely, providing repetitive control over the motor.


Code



Working Principle:

The relay acts as a switch to control the solenoid motor's power supply. When the NodeMCU sends a HIGH signal to the relay module's input pin, the relay closes its normally open (NO) contact, allowing current from the 9V battery to flow through the motor. This activates the motor and causes it to operate. When the NodeMCU sends a LOW signal, the relay opens the contact, cutting off the current and turning off the motor.



Check out our Free NodeMCu Projects Playlist - NodeMCu Projects


Want us to help you with the projects, write your inquiry here - Order Projects



81 views2 comments
bottom of page