Help / Advice with Spark Core Relay Shield [Solved]

I’m trying to implement a small project using a relay shield, but my main powered small fan isn’t turning on. :smile: If anyone could point me in the right direction I’d greatly appreciate it! I don’t know if I’m just misunderstanding the basic wiring, or what. Thanks in advance for any help.

Here’s my current wiring setup.

I have the hot from the main coming from the outlet and into RelayShield4, then coming out of the middle port of RelayShield4 and continuing on to the fan. Then the neutral is just spliced together running from fan to main neutral. I spliced the wire because I wasn’t sure how to wire it when I cut it.

This doesn’t work at all. I’m 99% sure it’s the wiring because:

When I call the led method from particle-cli with HIGH and LOW parameters, it works fine. Also, if I wire the hots and the neutrals together, the fan comes on when I plug it in. And when I call the relay method from particle-cli, it isn’t returning an error code.

Here’s the documentation I followed:

Although I’m hesitant to call it documentation because it uses a “light bulb” as an example?

And here is the code that is running on my core right now:

#include "application.h"
// -----------------
// Read sensors
// -----------------

// Create a variable that will store the sensor values
double temperature = 0.0;
int light = 0;

int tempPin = A7;
int lightPin = A0;
int led = D7;

int RELAY1 = D0;
int RELAY2 = D1;
int RELAY3 = D2;
int RELAY4 = D3;

void setup()
  //Initilize the relay control pins as output
   pinMode(RELAY1, OUTPUT);
   pinMode(RELAY2, OUTPUT);
   pinMode(RELAY3, OUTPUT);
   pinMode(RELAY4, OUTPUT);
   // Initialize all relays to an OFF state
   digitalWrite(RELAY1, LOW);
   digitalWrite(RELAY2, LOW);
   digitalWrite(RELAY3, LOW);
   digitalWrite(RELAY4, LOW);

   //register the Spark function
   Spark.function("relay", relayControl);
   Spark.function("led", ledControl);

  // Connect the temperature sensor to A7 and configure it
  // to be an input
  pinMode(tempPin, INPUT);  
  pinMode(lightPin, INPUT);  
  pinMode(led, OUTPUT);

void loop()
  int reading = 0;
  double voltage = 0.0;

  // Keep reading the sensor value so when we make an API
  // call to read its value, we have the latest one
  reading = analogRead(A7);

  // The returned value from the Core is going to be in the range from 0 to 4095
  // Calculate the voltage from the sensor reading
  voltage = (reading * 3.3) / 4095;

  // Calculate the temperature and update our static variable
  temperature = (voltage - 0.5) * 100;
  temperature = (temperature * 9 / 5) + 32;
  light = analogRead(lightPin);

// command format r1,HIGH
int relayControl(String command)
  int relayState = 0;
  // parse the relay number
  int relayNumber = command.charAt(1) - '0';
  // do a sanity check
  if (relayNumber < 1 || relayNumber > 4) return -1;

  // find out the state of the relay
  if (command.substring(3,7) == "HIGH") relayState = 1;
  else if (command.substring(3,6) == "LOW") relayState = 0;
  else return -1;

  // write to the appropriate relay
  digitalWrite(relayNumber-1, relayState);
  return 1;

int ledControl(String command)
  int ledState = 0;

  if (command.substring(3,7) == "HIGH") ledState = 1;
  else if (command.substring(3,6) == "LOW") ledState = 0;
  else return -1;

  digitalWrite(led, ledState);
  return 1;

You should connect it this way

Mains Neutral ----> Fan Neutral
Mains Live -------> COM on the relay
NO of relay ------> Fan Live


If the code is working properly, you should here the relay switching.

Something simple like:

void setup(){

void loop(){

You can try this code without anything connected to the relays first :slight_smile:

1 Like

Welp, that took all of 30 seconds to fix. The problem was I had the mains hot in the wrong relay wire locations. Thanks!

Thread marked as solved. :wink: