Introduction: NodeMCU Relay Controlled Solenoid Valve
In this tutorial we will show you how to use a NodeMCU and a relay board that can be used to control things like solenoid valves. The nodeMCU uses the ESP8266 Chip from Espressif. It has built in Wifi and is compatible with the Arduino IDE. If you haven't used a NodeMCU before check out this quick start guide.
We are going to use the readiness.io platform to observe a sensor (in this case a water level sensor) and if it is low, open our solenoid valve to let a tank fill up.
The board we have created is fairly generic and provides for two inputs - that could be from push buttons or level switches or some other type of switch. It also has two outputs that are connected to two relays. There is also a DC-DC power module that allows you to power the NodeMCU from an external power supply up to 25VDC and provides the output for the relay board.
Step 1: Components
- NodeMCU
- Bread Board. (We've used Adafruit Perma-Proto BreadBoards)
- 5V 2 Channel Relay Module
- DC-DC Power Module
- 12 or 24VDC Solenoid Valve
- 10K Ohm Resistor
- LED
- 220 Ohm Resistors
- Screw terminals
- Enclosure & Cable Glands
- 2.1mm barrel DC jack
- 12 or 24 VDC Power supply depending on your valve
Step 2: Wiring It Up
Here is the basic wiring diagram for the relay unit. You can see what it looks like once it has been built at the end of the instructable.
D5 and D6 are connected to two terminal block that you can connect external sensors or buttons to. These are optional and we haven't used them in this project (although the code has been included for them)
Step 3: Source Code
For the relay unit - we aren't posting any data - but reading a particular topic from our channel and then making a decision on what to do. i.e. open or close the the valve.
Hence in our config file we have a TOPIC that we can write to, and also a LISTEN-TOPIC that we are listening to, to change the position of the valve.
In addition to the relays we also have two inputs. These could be external buttons (which you could use to open and close the valve) or switches like a level-switch or Hall effect sensor. The code for these is included - but has been disabled for this example.
In our loop - we check the LISTEN-TOPIC every 5 seconds and return the latest reading from the topic. If the reading is 0 we open the valve. If it is anything else we close it.
- You can download the full source code here
Step 4: The Finished Product
Here is the enclosure of the finished unit (valve not included). It has one cable gland for the valve wires to enter. As it also has a barrel jack for the power supply this version is recommended for use in exposed areas - but you could easily replace it with another cable gland and wire directly to the board to ensure it water tightness.