Edge Computing / Programming Languages

Off-The-Shelf Hacker: Automated Yard Watering Project

28 Nov 2018 12:00pm, by

The eight-channel relay board for my latest project, the Automated Yard Watering system, recently arrived on my workbench. I don’t have a Raspberry Pi Zero W module just yet, so in the meantime, I thought it might be interesting to see what could be accomplished with a Wi-Fi-enabled Arduino clone known as the NodeMCU.

I bought three of these devices from Banggood for about $15. They are little ESP8266 boards that bring the general purpose input/outputs (GPIO) out to breadboard-friendly pins, while adding a voltage regulator and USB communication circuitry. A wireless (ESP8266-01 based) dual-relay module is available for plug and play use for about $8.

Summarizing the project, I want to control my yard sprinklers using a microcontroller, a relay board and a few 24-volt sprinkler solenoid valves. We discussed the process in a recent story.

Today we’ll look at connecting the relay board to the microcontroller and using a program to test basic relay operation. A future article will integrate control software to actuate the solenoids. When I get my hands on a Raspberry Pi Zero W, we’ll swap it in place of the NodeMCU module and give that configuration a try. Being able to use multiple solutions, with a little tweaking, is an important skill to develop for off-the-shelf hackers.

Making Connections

Ideally, we’d have a 10-pin 0.1 mm spaced connector to use to wire the relay board to the microcontroller. There wasn’t one handy so I modded the usual obsolete hard-drive cable connector down to 10 pins and soldered the wires into place. Just clamp the connector in a vise and cut the plastic connector with a small saw or Dremel abrasive disk. Here’s a close-up of the connector.

Hacked hard-drive cable connector used on relay board

I found it best to “touch” solder to both the pins and the ends of the wires before trying to join them. Old school hackers call this process “tinning.” The flux in the solder cleans the surface of the pin and makes the solder flow properly. The end of the wire is then positioned against the pin and soldered with a minimum of heat. Try to solder the pins as quickly as possible or the pins will melt the plastic in the connector and possibly short together.

For preliminary testing, I cut all the wires down to about two inches in length and used a small breadboard to connect to the NodeMCU board. Once I made sure the relay board worked, I removed the wires from the breadboard and soldered them to the top of the NodeMCU module pins. This operation is a little tricky since space is pretty tight between pins.

Wires soldered to the NodeMCU module

I prefer to use 22-gauge solid wire when working with breadboards and prototype projects. Stranded wire always separates when pushing it into a breadboard. Straight, solid wire ends go in very easily and minimize the chance of a shorted or open connection.

Finding good documentation for the eight-channel relay board is a bit sketchy on the web. I wasn’t sure if the board worked with 3.3-volt or 5.0-volt logic. Regular Arduino modules use 5.0-volt logic, while the Raspberry Pi and ESP8266 boards (NodeMCU) both use 3.3-volt logic. It turned out not to matter. I just hooked it up and gave it a try. Take a look at a generic internal schematic of the relay board.

I hooked the ground pin on the relay board up to a GND pin on the NodeMCU board. Likewise, the VCC pin on the relay board went to one of the 3.3-volt pins on the NodeMCU. Eight of the 13 digital GPIO pins are connected to the relay board for use and testing, so you can use the remainder for other purposes. There is also an analog input pin labeled A0, which we aren’t using at this time.

relay pin NodeMCU GPIO NodeMCU physical pin
N1 GPIO16 D0
N2 GPIO5 D1
N3 GPIO4 D2
N4 GPIO0 D3
N5 GPIO2 D4
N6 GPIO14 D5
N7 GPIO12 D6
N8 GPIO13 D7

Google has plenty of NodeMCU pinout images for your reference and entertainment.

Make sure the configuration jumper at the bottom right-hand side of the relay board (with the connector facing front center and the relays at the back) is positioned between the VCC and JDVcc pins. Ignore the adjacent Gnd pin. This setup should also work for the Raspberry Pi. You couldn’t ask for a cleaner or simpler layout of the NodeMCU and relay board. Connect the USB cable to a Linux notebook for the programming phase.

Upload the Test Firmware

The NodeMCU uses the Lua programming language and comes already loaded in the firmware. IBM has a little mini-intro for the NodeMCU that you might check out if you’re interested in their development kit and using Lua. I don’t have any experience with the language, so instead opted to write regular old Arduino code for this part of the project. Perhaps, we’ll investigate Lua in a future story, seeing as I have two other NodeMCU modules with which to experiment. It looks like it’s pretty easy to use.

Current versions of the Arduino IDE support the NodeMCU modules. I downloaded and installed version 1.8.7 on the ASUS Linux notebook. Be sure to select “ESPduino (ESP-13 module)” for the board type or the firmware upload will fail.

Next, I modded the example code for the ESP8266 blink program to basically blink the eight relays on the relay board. Here’s the code.

/*
  NodeMCU - relay board test program
  Sequentially turns each relay on and off for 2 seconds, loops forever
*/
  

// the setup function runs once when you press reset or power the board
void setup() {
  
  pinMode(16, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);

  digitalWrite(16, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(0, HIGH);
  digitalWrite(2, HIGH);
  digitalWrite(14, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(13, HIGH);
}

// the loop function runs over and over again forever
void loop() {

  digitalWrite(16, LOW);   // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(16, HIGH);  // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds

  
  digitalWrite(5, LOW);    // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(5, HIGH);   // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds


  digitalWrite(4, LOW);    // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(4, HIGH);   // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds


  digitalWrite(0, LOW);    // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(0, HIGH);   // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds


  digitalWrite(2, LOW);    // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds
  
  digitalWrite(2, HIGH);   // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds


  digitalWrite(14, LOW);   // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(14, HIGH);  // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds


  digitalWrite(12, LOW);   // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(12, HIGH);  // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(13, LOW);   // LOW lights optoisolator LED to ground - turns on relay
  delay(2000);             // wait for a 2 seconds

  digitalWrite(13, HIGH);  // HIGH kills optoisolator LED - turns OFF relay
  delay(2000);             // wait for a 2 seconds
}

We start by initializing the pins with pinMode statements followed by beginning states of pins HIGH. Switching the pin to LOW turns ON the relay, so by default the pins are HIGH (+3.3 volts) meaning OFF. We don’t need any special libraries at this point since we are just turning the GPIO pins on and off. We’ll need a few when integrating the wifi and MQTT functionalities, later on.

The digitalWrite statements step down through the pins, turning the relays on for two seconds, then off for two seconds, as long as the board is hooked up to power.

After uploading the firmware to the NodeMCU you should see each LED light in succession and hear the click of the relay as it is actuated.

Wrap up

We’ve looked at hooking up the NodeMCU to an eight-channel relay board. The wiring is simple and clean. Testing is pretty easy, with a brute force blink program. The switch contacts in each relay can handle 240 volts AC at 10 amps or 30 volts DC at 10 amps, making the setup suitable for a variety of power on/off jobs. I’ll use it to switch 24-volt sprinkler solenoids at about half Amp. The switched side of the relays are totally isolated from the logic side.

Look for coverage of upgrading the setup to connect to my local network and receive commands via MQTT in upcoming articles. Later on, we’ll swap in a Raspberry Pi Zero W, for even more hacking fun.


A digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.