Go Down

Topic: First code (Read 4923 times) previous topic - next topic

Morke

Have the two supplies got a common GND ?
No, that would defeat the purpose of being isolated from each other and the Arduino could be damaged if something happened to the Relays.
What, a zoologist????

Rupert909

#16
Jun 05, 2016, 10:23 pm Last Edit: Jun 05, 2016, 10:38 pm by Rupert909
No, that would defeat the purpose of being isolated from each other and the Arduino could be damaged if something happened to the Relays.
Lets see. The Arduino has a power supply, usually USB, at least while developing, then later perhaps a cell phone charger. Let's call this ArduinoPower.

Then there are the relays. Relays have two sides. There is the control connection and GND, and there is the controlled pins, 2 or 3 of those.

Now, are we talking solid-state relays that require very little current, say less than 30-40 mA, and are happy to work on 5V, or are we talking mechanical ones, which may even require 12V to operate, and definitely require more current than an Arduino pin can supply?

I seem to recall you saying something about your Arduino having to be reset, following an event involving the relays? Might this possibly be due to excessive current draw?

So ... if the Arduino can not control the relays directly, you will need an additional power supply for that, controlled via some transistor (MOSFET or other). Let's call that power supply RelayControl. Now, RelayControl and ArduinoPower must have common GND, for this to work.

If the Arduino directly controls the relay, then still the control-side of the relay must be connected to ArduinoPower, for common reference. Voltage is voltage only relative to something.

Then, finally, we come to the business end of the relays, the load to be switched on and off, and consequently requiring a power supply of its own. This power supply may be the same that I called RelayControl. But it may also be separate. Lets call it ActionPower, as it powers the "action".

(cheesy)

If ActionPower is separate from RelayControl, then at least for mechanical relays, this part may well exist electrically isolated from the control side, and needs not share GND with the beforementioned two. This is because (mechanical) relays are simple switches.

If ActionPower and RelayControl are one and the same, which means it is used both to control the relays, with transistor between relay and Arduino, and run the "action", then it must share GND with ArduinoPower.

As for solid state relays, I've never used those, so they may have other requirements.

So there we are. Any questions?? :-)

[Edited]
The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

Morke

It's a mechanical Relay, but anyway that's not the issue. The problem seems to be related to the Serial print..... that's the only thing that bugs. The arduino keeps working.
What, a zoologist????

Whandall

It's a mechanical Relay, but anyway that's not the issue.
If you know what the issue is, why ask us?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

UKHeliBob

Are there diodes across the relays to prevent back EMF when they turn off ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Morke

If you know what the issue is, why ask us?
Common ground (or not) is not the issue. I've tried both setups with no visible change. The bug happens no matter the relays and arduino share a power supply or have different ones with or without common ground.
I have no idea what's going on but at least that is not the issue.
Don't take me wrong, I didn't mean to sound agressive or disrespectful. If that's the case, please forgive me.





UKHeliBob
Im not sure about that, the 4 relay module is this one:



I suspect the issue is within the code but that's only a hunch.....
What, a zoologist????

Rupert909

#21
Jun 07, 2016, 10:56 am Last Edit: Jun 07, 2016, 11:03 am by Rupert909
Common ground (or not) is not the issue. I've tried both setups with no visible change. The bug happens no matter the relays and arduino share a power supply or have different ones with or without common ground.
I have no idea what's going on but at least that is not the issue.
Don't take me wrong, I didn't mean to sound agressive or disrespectful. If that's the case, please forgive me.





UKHeliBob
Im not sure about that, the 4 relay module is this one:



I suspect the issue is within the code but that's only a hunch.....

Do you hear clicks from it when engaging and disengaging, indicating the relays are mechanical?

I see the main block of pins containing GND, in1-4 and VCC. But there are three more pins, located by themselves. I See GND and VCC, but VCC is connected to a third pin, via a yellow jumper. What's written next to the third pin?

May it possibly be extra pins for supplying extra current, for driving the relays? My theory is still that your Arduino struggles with delivering the amount of current required to operate these. The jumper I suspect is used to select VCC from control-inputs row, for devices supplying enough current to drive the relays.

The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

Morke

Do you hear clicks from it when engaging and disengaging, indicating the relays are mechanical?

I see the main block of pins containing GND, in1-4 and VCC. But there are three more pins, located by themselves. I See GND and VCC, but VCC is connected to a third pin, via a yellow jumper. What's written next to the third pin?

May it possibly be extra pins for supplying extra current, for driving the relays? My theory is still that your Arduino struggles with delivering the amount of current required to operate these. The jumper I suspect is used to select VCC from control-inputs row, for devices supplying enough current to drive the relays.


Yes, I can hear the clicks.

The three more pins are for the external power supply

here's how the relays are powered:

What, a zoologist????

Rupert909

Yes, I can hear the clicks.

The three more pins are for the external power supply

here's how the relays are powered:


No GND connection from Arduino?? Why not?
The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

dlloyd

No GND connection from Arduino?? Why not?
To use opto-isolation. The control lines provide path to GND when switched low.

dlloyd

The following is a connection diagram and some test code. The operational and load test will sequentially turn on all relays then sequentially turn off all relays. Print monitor will display what part of the loop is running.

If you're still having USB problems, could try a different USB port and new or different USB cable.

Note that some USB ports might be current limited 100mA, 250mA, 500mA or other. The only way to guarantee 500mA is to use a USB hub with power adapter connected.   




Test code:

Code: [Select]
/* 4 Relay Module configured for Opto-isolation
   Arduino pins 3 to 6 connected to IN1 to IN4
   Connections: http://i.imgur.com/6m95zlV.png */

void setup() {
  Serial.begin(115200);
  Serial.println("In sequence:");

  for (int i = 3; i <= 6; i++) {
    pinMode(i, INPUT_PULLUP);
    pinMode(i, OUTPUT); // defaults HIGH (relays off)
  }
}

void loop() {
  Serial.println("Relays turning ON ...");
  for (int i = 3; i <= 6; i++) {
    digitalWrite(i, LOW); // energize relays until all on
    delay(1000);
  }
  Serial.println("Relays turning OFF ...");
  for (int i = 3; i <= 6; i++) {
    digitalWrite(i, HIGH); // de-energize relays until all off
    delay(1000);
  }
}

Rupert909

The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

dlloyd

#27
Jun 07, 2016, 04:43 pm Last Edit: Jun 08, 2016, 04:12 am by dlloyd
Re-worked your code using arrays, faster printing (115200), only print once per second, added conditional temp limits for relay control and averaged readings (untested):

Code: [Select]
void loop()
{
  // readings and control
  for (int i = 0; i < 4; i++) {
    sum[i] = analogRead(tempPin[i]);  // invalid
    sum[i] = 0;
    sum[i] += analogRead(tempPin[i]); // reading 1
    sum[i] += analogRead(tempPin[i]); // reading 2
    sum[i] += analogRead(tempPin[i]); // reading 3
    sum[i] += analogRead(tempPin[i]); // reading 4
    reading[i] = sum[i] / 4;          // averaged reading
    tempC[i] = reading[i] / 9.31;
    if (tempC[i] < lowerLimit) {
      digitalWrite(relayPin[i], LOW);   //relay OFF
    }
    else if (tempC[i] > upperLimit) {
      digitalWrite(relayPin[i], HIGH);  // relay ON
    }
  }
  printCheck = millis() - lastPrintTime;
  if (printCheck >= printInterval) {
    for (int i = 0; i < 4; i++) {
      Serial.print("tempC");
      Serial.print(i + 1);
      Serial.print(" ");
      Serial.println(tempC[i]);
    }
    Serial.println();
    lastPrintTime = millis(); // reset print timer
  }
}

Morke

Re-worked your code using arrays, faster printing (115200), only print once per second, added conditional temp limits for relay control (untested):

Code: [Select]
const byte tempPin[] = {A1, A2, A3, A4};
const byte relayPin[] = {6, 7, 8, 9};

// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 24;
const byte upperLimit = 26;

float tempC[4];
word reading[4];

word printInterval = 1000; // 1 second
unsigned long printCheck = 0, lastPrintTime = 0;

void setup()
{
  analogReference(INTERNAL);
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) {
    pinMode(relayPin[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF
  }
}

void loop()
{
  // readings and control
  for (int i = 0; i < 4; i++) {
    reading[i] = analogRead(tempPin[i]);
    tempC[i] = reading[i] / 9.31;
    if (tempC[i] < lowerLimit) {
      digitalWrite(relayPin[i], LOW);   //relay OFF
    }
    else if (tempC[i] > upperLimit) {
      digitalWrite(relayPin[i], HIGH);  // relay ON
    }
  }
  printCheck = millis() - lastPrintTime;
  if (printCheck >= printInterval) {
    for (int i = 0; i < 4; i++) {
      Serial.print("tempC");
      Serial.print(i + 1);
      Serial.print(" ");
      Serial.println(tempC[i]);
    }
    Serial.println();
    lastPrintTime = millis(); // reset print timer
  }
}

Now I give up  ;)
Woah, that's a lot of work. First of all thank you. I'll test it. I'm quite a newbie in programming so i'm struggling to understand all of the code.

But what I don't get is this part:
Code: [Select]

    pinMode(relayPin[i], INPUT_PULLUP);
    pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF


Why do you set the relay pin first to INPUT and right after that as OUTPUT?
What, a zoologist????

Whandall

The INPUT_PULLUP functionality is activated by writing a HIGH to the output register.
So the Arduino output will start with a HIGH level, which is the relay off state.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up