Sainsmart 8 Channel Relay

Connecting a Sainsmart 8 Channel relay to Blynk. Everything works well, but when the switches on the Blynk App is OFF the relay is on, and when the Blynk App is ON, the relay is off.

Anyone who can assist in getting this to work the right way round. I know that this Sainsmart relay works the “wrong way round”.


Inverse button logic will be implemented later.
Please use virtual pins to reverse the logic in your code

Thanks, at least now I know its not something that I am doing wrong on my side.

@ashraf, why don’t you use virtual pins and invert the logic in the code to control the actual digital pins?

Is there a timeline on when this will be implemented?
I’ve got this to work with virtual pins but the delay is painful. Not sure if this is just the way it is or if there is another factor in not seeing that’s causing this.

Delay should be the same… usually we don’t have issues with this.


you may try local server to avoid any network delay if any. But network delay itself is strange for Blynk. Where are you geographically?

I’m located in the U.S. Kentucky.

Hi Nick, can you share the code on how to create virtual pins for the relay. Thanks

Hello…I’m a different Nick :smile:

You can create a button in the app that connects to a virtual pin (V01 in this case).
Then use that button to turn your relay on or off (connected to D14 in this case). You can reverse the high and low if your relay is reverse polarity.

  if (param.asInt()) {
    digitalWrite(14, HIGH);
  } else {
    digitalWrite(14, LOW);

Is your relay a NO or NC? (Normal Open or Normal Closed). On my Chinese boards they have both options.

Thanks so much. Going to test it over the weekend.

Not sure, will check. Its the Sainsmart one.

I have it all working now! Something in my code was causing issues. Ive got a lot going on with my setup. So I started over and all is working now.
@ashraf- if what nickcornaglia posted doesn’t work for you my code is slightly different. I’ll try to post it sometime soon.

From what I can tell this has, same as mine, both Normal Open and Normal Closed contacts. So you can define by the way you hook up the high voltage part if it’s default on or off. You may have used the wrong side of the terminal to hook up your device. It has 3 ports for each relay, the middle one should be Common (ground for DC or whatever for AC) and to the left and right of those are either a always-on (off when switched) or always-off (on when switch with Arduino) contacts.

Waiting, thanks in advance.

Hey Nick,

Please forgive me in advance for what is probably a ridiculous newbie question, that said…

I’m doing exactly what ashraf is doing with the relay board. I gather now that the ability to select Low to High logic vs High to Low logic is not yet implemented and this can be solved with virtual pins. Now the ridiculous part, I have loaded the libraries, have the ethernet card set up, and everything running. With regard to the code and making changes, where do you ever see the code? As far as I saw, it was loaded just the first time, the sketch was very short and called all the appropriate libraries. That was it. It there a certain file I should be looking at to make these modifications?

Thanks in advance,

The beauty of Blynk is that you don’t need extra code for simple things. If you have a 4 channel relay you just plug the 4 control wires into Arduino pins and create 4 buttons in your app to control those 4 digital pins. No code needed. The library takes care of those duties.

Some relay boards light up when in the NO position. Some light when in the NC position. Depending on which side of your relay you’re external device is wired to determines whether the light coincides with your ON position. If your LED is on when you device is off, then you could simply switch your wiring.

But this may not match your Blynk buttons. On on you Blynk buttons may mean off on your device. That’s when you need to change how you’re switching your relay. If you want to reverse when the LEDs go on you then need to add a little code to the sketch.

So if you add something like my code above, you can create a button that controls a virtual pin that changes the state of the relay instead of controlling the digital pin directly.

Thanks Nick,

I’m familiar with the normally open v. normally closed concept. The problem for me is that it is counter intuitive to have the blynk button on and the relay not energized. This is particularly evident and difficult when you try to use the “Timer” widget.

I popped that bit of code in there and feel certain that it belongs in the “void loop” but had a compiling error which I couldn’t resolve.

void setup()
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth); // Here your Arduino connects to the Blynk Cloud.

// Reset all Arduino outputs to HIGH
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
pinMode(6, OUTPUT);
digitalWrite(6, HIGH);
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
pinMode(8, OUTPUT);
digitalWrite(8, HIGH);
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);

void loop()
{; // All the Blynk Magic happens here…

// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!


if (param.asInt()) {
digitalWrite(9, HIGH);
} else {
digitalWrite(9, LOW);

Your comments are appreciated

Please format the code