Why do my relays energise on powering up mega 2560

Hi, I am a complete novice and have adapted a sketch I found to operate two 4 relay modules.

The problem I have is that when the Mega 2560 powers up the relays energise and stay on even though the Cayenne dashboard shows the relevant buttons as off.

The only way I can resolve the issue is to switch on all buttons and then back off again. The relays all then work as they should do. The problem reoccurs when the Mega is switched off and then back on again.
I have very little understanding of the coding aspect so any help would be appreciated - code as follows:

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;

#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 22 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 26 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 28 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 30 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 36 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

//#define VIRTUAL_CHANNEL_9 9
//#define ACTUATOR_PIN_9 38 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_2, OUTPUT);
pinMode(ACTUATOR_PIN_3, OUTPUT);
pinMode(ACTUATOR_PIN_4, OUTPUT);
pinMode(ACTUATOR_PIN_5, OUTPUT);
pinMode(ACTUATOR_PIN_6, OUTPUT);
pinMode(ACTUATOR_PIN_7, OUTPUT);
pinMode(ACTUATOR_PIN_8, OUTPUT);
//pinMode(ACTUATOR_PIN_9, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)

{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_1, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_1, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_2)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_2, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_3, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_3, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_4)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_4, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_4, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_5)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_5, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_5, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_6)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_6, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_6, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_7)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_7, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_7, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_8)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_8, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_8, LOW);
}
}

//CAYENNE_IN(VIRTUAL_CHANNEL_9)
//{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
//if (getValue.asInt() == 0) {
//digitalWrite(ACTUATOR_PIN_9, HIGH);
//}
//else {
//digitalWrite(ACTUATOR_PIN_9, LOW);
//}
//}

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;

#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 22 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 26 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 28 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 30 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 36 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

//#define VIRTUAL_CHANNEL_9 9
//#define ACTUATOR_PIN_9 38 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_2, OUTPUT);
pinMode(ACTUATOR_PIN_3, OUTPUT);
pinMode(ACTUATOR_PIN_4, OUTPUT);
pinMode(ACTUATOR_PIN_5, OUTPUT);
pinMode(ACTUATOR_PIN_6, OUTPUT);
pinMode(ACTUATOR_PIN_7, OUTPUT);
pinMode(ACTUATOR_PIN_8, OUTPUT);
//pinMode(ACTUATOR_PIN_9, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)

{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_1, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_1, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_2)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_2, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_3, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_3, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_4)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_4, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_4, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_5)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_5, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_5, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_6)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_6, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_6, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_7)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_7, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_7, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_8)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_8, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_8, LOW);
}
}

//CAYENNE_IN(VIRTUAL_CHANNEL_9)
//{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
//if (getValue.asInt() == 0) {
//digitalWrite(ACTUATOR_PIN_9, HIGH);
//}
//else {
//digitalWrite(ACTUATOR_PIN_9, LOW);
//}
//}

make the relay OFF in the setup, so that every time it starts it will turn OFF them.

Thanks for your reply shramik. How do I implement that? I mentioned I am a complete novice so if you could demonstrate where the code should be altered that would be a real help.

here it is. what more you need?

Thanks again for the reply. I’ve done this which has solved the initial problem on start up but the the relays don’t respond in the dashboard now - any thoughts?
Just restating that I am a complete novice so coding and syntax is a complete mystery at this stage!

Serial.begin(9600);
pinMode(ACTUATOR_PIN_1, LOW);
pinMode(ACTUATOR_PIN_2, LOW);
pinMode(ACTUATOR_PIN_3, LOW);
pinMode(ACTUATOR_PIN_4, LOW);
pinMode(ACTUATOR_PIN_5, LOW);
pinMode(ACTUATOR_PIN_6, LOW);
pinMode(ACTUATOR_PIN_7, LOW);
pinMode(ACTUATOR_PIN_8, LOW);
//pinMode(ACTUATOR_PIN_9, LOW);
Cayenne.begin(username, password, clientID);
}

learn the basics from here https://www.arduino.cc/reference/en/language/functions/digital-io/digitalwrite/

I’ve had a look at the example the link points to but I simply don’t understand how it relates to the sketch I have. I enjoy messing around with electronics and microcontrollers but simply cannot get my head around the coding element. I understand the teach a man to fish approach but I’m too long in the tooth and not clever enough to get it!
I’m a copy and paste user at the moment but just wanted to get my project working as soon as possible.
Thanks anyway for trying to help me Shramik

it is simply as

#define ACTUATOR_PIN_1 22  //initalize the pin

void setup(){
pinMode(ACTUATOR_PIN_1, OUTPUT); //make the pin output
pinMode(ACTUATOR_PIN_1, LOW);  //Turn the output low 
}

I’ve changed code to this now and it uploads ok but still relays will not switch on from the Cayenne dashboard:

void setup(){
//Serial.begin(9600);
pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_1, LOW);
pinMode(ACTUATOR_PIN_2, OUTPUT);
pinMode(ACTUATOR_PIN_2, LOW);
pinMode(ACTUATOR_PIN_3, OUTPUT);
pinMode(ACTUATOR_PIN_3, LOW);
pinMode(ACTUATOR_PIN_4, OUTPUT);
pinMode(ACTUATOR_PIN_4, LOW);
pinMode(ACTUATOR_PIN_5, OUTPUT);
pinMode(ACTUATOR_PIN_5, LOW);
pinMode(ACTUATOR_PIN_6, OUTPUT);
pinMode(ACTUATOR_PIN_6, LOW);
pinMode(ACTUATOR_PIN_7, OUTPUT);
pinMode(ACTUATOR_PIN_7, LOW);
pinMode(ACTUATOR_PIN_8, OUTPUT);
pinMode(ACTUATOR_PIN_8, LOW);
//pinMode(ACTUATOR_PIN_9, OUTPUT);
Cayenne.begin(username, password, clientID);
}

Any thought’s?

can you share the entire code?

Here it is Shramik,

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;

#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 22 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 26 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 28 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 30 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 36 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

//#define VIRTUAL_CHANNEL_9 9
//#define ACTUATOR_PIN_9 38 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup(){
Serial.begin(9600);
pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_1, LOW);
pinMode(ACTUATOR_PIN_2, OUTPUT);
pinMode(ACTUATOR_PIN_2, LOW);
pinMode(ACTUATOR_PIN_3, OUTPUT);
pinMode(ACTUATOR_PIN_3, LOW);
pinMode(ACTUATOR_PIN_4, OUTPUT);
pinMode(ACTUATOR_PIN_4, LOW);
pinMode(ACTUATOR_PIN_5, OUTPUT);
pinMode(ACTUATOR_PIN_5, LOW);
pinMode(ACTUATOR_PIN_6, OUTPUT);
pinMode(ACTUATOR_PIN_6, LOW);
pinMode(ACTUATOR_PIN_7, OUTPUT);
pinMode(ACTUATOR_PIN_7, LOW);
pinMode(ACTUATOR_PIN_8, OUTPUT);
pinMode(ACTUATOR_PIN_8, LOW);
//pinMode(ACTUATOR_PIN_9, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)

{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_1, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_1, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_2)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_2, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_3, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_3, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_4)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_4, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_4, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_5)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_5, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_5, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_6)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_6, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_6, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_7)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_7, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_7, LOW);
}
}

CAYENNE_IN(VIRTUAL_CHANNEL_8)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_8, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_8, LOW);
}
}

//CAYENNE_IN(VIRTUAL_CHANNEL_9)
//{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
//if (getValue.asInt() == 0) {
//digitalWrite(ACTUATOR_PIN_9, HIGH);
//}
//else {
//digitalWrite(ACTUATOR_PIN_9, LOW);
//}
//}

Does it turn ON when you turn OFF the button on the cayenne dashboard?

No Shramik, the dashboard buttons have no effect whether on or off

Add #define CAYENNE_DEBUG in your code and see what you get in the serial monitor when you press the button on cayenne dashboard.

I included it at the beginning of the sketch as follows:

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
#define CAYENNE_DEBUG

Serial monitor just shows MAC, IP, connecting and finally connected - nothing else

Can you add this on the very top

Done that and ran serial monitor with an excerpt as follows:

[96414] Message received:topic 2, channel 1
[96415] In: value 1, channel 1
[96429] publishstate: topic 1, channel 1,
[96472] Publish: topic 1, channel 1, value 1,subkey, key
[96536] Send response: I2ylsG4rwFppJvE
[97698] Connection ok

Serial monitor does respond to pressing dashboard buttons on/off for each channel - can’t copy and paste full output but is responding but still no activity from relays themselves

the code is fine. check your connection and see if you can turn the relay on with a basic digital output code.