Trouble with Relay Module

Hello world,

This is my first post to these forums and if this is not the appropriate place to ask this kind of question would you kindly direct me the correct sub-forum. Thanks!

So I purchased this relay module recently: https://www.amazon.ca/dp/B06XCKQ1M9/ref=pe_3034960_236394800_TE_dp_3

Of all the things I'm having trouble with, between it, an ethernet sheild, and an SHT20 temp/humidity sensor, the relay is causing the problems.

This is the code I'm running:

char relay = A3;

void setup() {
 pinMode(relay, OUTPUT);
 analogWrite(relay, HIGH);
 }

void loop() {
 analogWrite(relay, LOW);
 delay(1000);
 analogWrite(relay, HIGH);
 }

Having tried both digital and analogue pins and neither work.

After reading this post from 2011 I'm suspicious that I have done damage to the Arduino: digitalWrite does not trigger Relay - Programming Questions - Arduino Forum

However the picture from the amazon page doesn't show any extra circuits necessary, and there's no documentation I can find forthe mA requirements to trip the magnet in the relay. What do you all think? Have I probably done damage to the arduino, do I have a faulty relay module or have I just missed something obvious and essential?

As a bonus question, I'm looking for clarification on the purpose of the "Optocoupler". All I've found is that it's intended to protect the arduino from the higher currents flowing through each channel. Is that the case or is there another purpose?

Thanks in advance for any assistance!

Try this sketch.

const byte relayPin = A3;

void setup() {
  pinMode(relayPin, INPUT_PULLUP); // first enable pull up, to stop relay chatter during bootup
  pinMode(relayPin, OUTPUT); // then set pin to output
}

void loop() {
  digitalWrite(relayPin, LOW); // relay 'on'
  delay(1000);
  digitalWrite(relayPin, HIGH); // relay 'off'
  delay(1000);
}

One relay active/powered from Arduino's 5volt pin might just be ok, but more is not.
Relays (coils), when active, draw about 75mA (Arduino pin current is just 2mA).
How do you power the Arduino + ethernet shield now.

The 4-relay module needs it's own 5volt/500mA power supply, connected to JD-VCC (jumper removed) and relay ground. Relay VCC (not JD-VC) must be connected to Arduino's 5volt pin, and DO NOT connect Arduino ground to relay ground if you're using a separate relay supply.
Leo..

Thank you for the timely reply!

I've left the space now where I had all my stuff set up so I'll run your code. Obviously changing to analogWrite/digitalWrite where appropriate though.

Why do you define the relayPin as a byte variable? is that to conserve storage space on the arduino?

So even though the Amazon photo shows no power supply attached to the module, and all 4 channels pinned, a power supply is still necessary? Yes I know photos are bad to go by for instructions, but when there's no documentation on their website that was the best I had to go off of.

To answer your question I am powering my arduino from my laptop USB port. If I were running it off of a dedicated power supply would would I still need a second power supply for the relay module?

My friend has a Pi connected to a relay module and it runs just fine off of the GPIO pins. From this forum post: What is the maximum current the GPIO pins can output? - Raspberry Pi Stack Exchange

I gather that the Pi has a total of 50mA/17GPIO pins is just under 3mA.

Why would my friend's Pi, running on a dedicated power supply (not USB power), would not require a second power supply for his module? Could it be just because I'm running my arduino off of usb power?

Something else I should maybe have mentioned is that the first few times I used the relay it clicked. Loudly. But doesn't anymore, even when the LED lights up.

The GPIO pins only supply enough current to turn on the LED in an optocoupler (3 or 4 mA) the relay coil current (75 mA) comes from Vcc, look at this schematic of 1 channel.
OptoRelayChannelData-575.jpg

‘char’ variables can be -128 to 127, you don’t need a negative in your case.
‘byte’ variables can be 0 to 255 .
Both are one byte of storage.
Get into the habit of selelecting the appropriate ‘type’ necessary for your variables.

Wawa is using ‘const byte’, const say this is a constant which will not change.
The compiler reserves no memory for constants.

If you want to maintain the isolation on the relay board, follow the suggested wiring changes.

Here is an 8 relay, similar, board and the suggested wiring:

"const byte" (0-255) for pin numbers uses the least amount of memory.
The compiler knows pin A0-A5 on an Uno is pin14-19.

The analogue pins are just digital pins with the added functionality of analogue-in.
Here we use them as digital output pins, so "digitalWrite" is used.
Corrected a typo in the code of post#1, so please re-upload.

Four relays (actively on) could draw 300-320mA from the 5volt pin.
That, and a ~160mA ethernet shield, and the 50mA from Uno itself, is more than the absolute max you can draw from USB.
If you do this too long, then the onboard 500mA USB polyfuse could trip.

Powering that relay board like we said has the added benefit of opto isolation.
That extra layer of safety might be important if you're using the relays to switch mains power.
Leo..

Thanks for the awesomely detailed diagram Larryd! Thats perfect!

Thanks for the added info about the variable types Wawa. I had just seen some sketch somewhere using analogue pins as a const. Makes sense though as a byte.

So do you think I damaged my Uno by over drawing power?

Big-board Arduinos are as tough as old boots.
And the 500mA USB fuse is an auto-healing type.
But we don't know what exactly you are using, and what you have done.
Just try the code, and let us know if/how it misbehaves.
Leo..

I'll try the code when I get the power supplies now.

Here's a great article I found for anyone that sees this forum in the future who's looking for detailed power descriptions: https://www.open-electronics.org/the-power-of-arduino-this-unknown/

So what kind of power supply should I be looking for? Find a specific one designed for the arduino to make sure the connector is compatible at 9-12V & >=0.5A, or just any typical one that meets that output will work?

As for the relay power supply; my Pi PS is a 5V & 2.5A adapter: https://www.memoryexpress.com/Products/MX66806

Could I just use that, cut the connector, and connect it to a bread board?

An old 5volt >=500mA cellphone charger with it's lead chopped off is perfect to power that relay board.
Same for the Uno, but get one with USB socket, so you can plug the USB lead of the Uno straigh in there.
Overkill to get an adapter >1Amp, because of the 0.5A USB fuse on the Uno.

You can also power the Uno via the DC socket, but anything more than 7.5volt (regulated) is going to reduce current capability of the onboard 5volt supply.
Don't expect the ethernet shield to work for long if you use a 12volt supply.
Because the 5volt regulator converts that 7volt difference into heat, overheats, and shuts down after some time.
Leo..

Success! Your code ran Wawa

Thanks everyone for your input!

I don't have a dedicated PS yet but I tried my usb phone charger from samsung.

DC OutPut = 5V&2A or 9V&1.67A

I will get one as I continue to prototype my project. Ultimately it will be a total of 2 sensors, the relay module, and ethernet capability (sheild/module). I'll do the math on total power draw when I settle on parts.

Thanks again for all your help everyone,

Cheers!