Hello everyone,
After going through some trouble with the relay module (and 74HC595) I finally have it working. Now I want to get it working with the IOT Cloud, and am running into a hurdle again.
When the Nano 33 IoT loads a new sketch or restarts all 8 relays switch on and off (once) in the blink of an eye. The relay module switches on with LOW, and stays off with HIGH. I've tried all the things I could find in other topics but nothing has worked so far, and I'm at my wit's end
The OE pin is tied to ground, and so is the latch with a 10K resistor. There is a 0.1 uF cap over VCC and GND and the 595 is powered with it's own power supply. I've tried connecting the relay module directly to the arduino, or with isolated power supply like this picture. For that I've soldered the 2 contacts on the undersude to get 5V from the USB to the 5V pin on the nano.
I've tried setting the pinModes in setup to high but it doesn't help. At some point the relay module just started going nuts on/off continuously untill I pulled the power. I think this might be related to the power from the USB-5v pin. edit: or not because it also happens when uploading a sketch while powered externally.
Is there something I'm overlooking or is this a result of the way it's wired?
/*
bool relay1;
bool relay2;
bool relay3;
bool relay4;
bool relay5;
bool relay6;
bool relay7;
bool relay8;
*/
#include "thingProperties.h"
#define dataPin 4 //SER serial Input
#define latchPin 5 // Register Clock/Latch
#define clockPin 6 // SRCLK Shift Register Clock
byte relays = 0; // Variable to hold the state of relays in a byte
void setup() {
Serial.begin(9600);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
//pinMode(dataPin, HIGH);
//pinMode(latchPin, HIGH);
//pinMode(clockPin, HIGH);
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
resetAllRelays();
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, reset); //reset dashboard buttons
}
void loop() {
ArduinoCloud.update();
}
void updateShiftRegister() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, relays);
digitalWrite(latchPin, HIGH);
}
void reset() {
relay1 = false;
relay2 = false;
relay3 = false;
relay4 = false;
relay5 = false;
relay6 = false;
relay7 = false;
relay8 = false;
}
void resetAllRelays() {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 0b11111111);
digitalWrite(latchPin, HIGH);
}
void onRelay1Change() {
if (bitRead(relays, 0) == 0) {
//Serial.println("on");
bitWrite(relays, 0, 1);
} else {
//Serial.println("off");
bitWrite(relays, 0, 0);
}
updateShiftRegister();
}
void onRelay2Change() {
if (bitRead(relays, 1) == 0) {
bitWrite(relays, 1, 1);
} else {
bitWrite(relays, 1, 0);
}
updateShiftRegister();
}
void onRelay3Change() {
if (bitRead(relays, 2) == 0) {
bitWrite(relays, 2, 1);
} else {
bitWrite(relays, 2, 0);
}
updateShiftRegister();
}
void onRelay4Change() {
if (bitRead(relays, 3) == 0) {
bitWrite(relays, 3, 1);
} else {
bitWrite(relays, 3, 0);
}
updateShiftRegister();
}
void onRelay5Change() {
if (bitRead(relays, 4) == 0) {
bitWrite(relays, 4, 1);
} else {
bitWrite(relays, 4, 0);
}
updateShiftRegister();
}
void onRelay6Change() {
if (bitRead(relays, 5) == 0) {
bitWrite(relays, 5, 1);
} else {
bitWrite(relays, 5, 0);
}
updateShiftRegister();
}
void onRelay7Change() {
if (bitRead(relays, 6) == 0) {
bitWrite(relays, 6, 1);
} else {
bitWrite(relays, 6, 0);
}
updateShiftRegister();
}
void onRelay8Change() {
if (bitRead(relays, 7) == 0) {
bitWrite(relays, 7, 1);
} else {
bitWrite(relays, 7, 0);
}
updateShiftRegister();
}