Go Down

Topic: Relay module is active low...how to reverse that (Read 28415 times) previous topic - next topic

corsavec

My relay module works fine now, when I jumped out from 13th pin of Arduino Mega. The commands are reversed, but it boots without clicks. I have used this relay module with one relay.

Budi_Agasi

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  2  // Arduino Digital I/O pin number
#define Relay_2  3
#define Relay_3  4
#define Relay_4  5
void setup()   /****** SETUP: RUNS ONCE ******/
{
//-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF); 
 
//---( THEN set pins as outputs )---- 
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT); 
  pinMode(Relay_3, OUTPUT); 
  pinMode(Relay_4, OUTPUT);   
  delay(4000); //Check that all relays are inactive at Reset



I found those code while search for how to make active low relay off while restarting arduino...
it work for me... thanks to the guy who created it... :D

aarg

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ...
You have posted code without using code tags. The code tags make the code look
Code: [Select]
like this
when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.  The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

chefslot

Please guys be nice noob here,,,, now i've spent the last two nights figuring out this sketch and taking bits, understanding what they do (kind off) and have ended up with the same problems as i think the OP had, Not to sure to create a new thread but once you guys started talking about this boot up high low code i'm not to sure if this is going be my solution.
I'm not controlling any high-tec garage doors or anything, but if i run the code like below everything is fine, once i switch the HIGH / LOW to how it should be i get what the op has had going on, relay externally or jumper powered. Relay being used http://www.ebay.co.uk/itm/5V-2-Channel-Relay-Module-Shield-For-Arduino-ARM-PIC-AVR-DSP-MCU-Electronic-BS-/192216021688?hash=item2cc0f7bab8

I also get once reset button is pressed a quick on off state on the relay, not to sure if i should be looking into this any further or for my purpose of using a relay to turn on and off a fan is ok?
sketch code

Code: [Select]

#include "DHT.h"                                        // Libary for the DHT22
#include "Relay.h"                                      // Libary for the Relay
#define DHTPIN 2                                        // what digital pin DHT22 is connected to
#define DHTTYPE DHT22                                   // DHT type = 22
DHT dht(DHTPIN, DHTTYPE);                               // Init DHT sensor.
#define Relay1 7                                         //added ????

void setup()
{
  Serial.begin(9600);                                   // Setup Serial connection speed (was 115200)
  dht.begin();                                          // Init DHT22
  pinMode(Relay1, OUTPUT);                              // Set Digital Pin 7 as an Output (Relay Control)
}

void loop()
{
  float h = dht.readHumidity();                         // DHT22 deciaml point
  float t = dht.readTemperature();                      // DHT22 deciaml point
  Serial.print("Humidity: ");                           // DHT22
  Serial.print(h);                                      // DHT22
  Serial.print(" %\t");                                 // DHT22
  Serial.print("Temperature: ");                        // DHT22
  Serial.print(t);                                      // DHT22
  Serial.println(" *C ");                               // DHT22

delay (1000);                                         // Wait one second before repeating

    if(t < 25) digitalWrite(Relay1, HIGH);             // turn the relay off       // if temp is less than 
        else if(t > 25)digitalWrite(Relay1, LOW); // turn the relay on to turn on the light   
}


Regards

edgemoron

#34
Oct 05, 2017, 11:13 am Last Edit: Oct 05, 2017, 11:27 am by edgemoron
One thing that might help, in setup():
Code: [Select]
digitalWrite(Relay1,HIGH); // set the pin HIGH first
pinMode(Relay1,OUTPUT); // then make it an output

chefslot

One thing that might help, in setup():
Code: [Select]
digitalWrite(Relay1,HIGH); // set the pin HIGH first
pinMode(Relay1,OUTPUT); // then make it an output

Hi fella, been playing around with this and still no difference

Code: [Select]

#include "DHT.h"                                        // Libary for the DHT22
#include "Relay.h"                                      // Libary for the Relay
#define DHTPIN 2                                        // what digital pin DHT22 is connected to
#define DHTTYPE DHT22                                   // DHT type = 22
DHT dht(DHTPIN, DHTTYPE);                               // Init DHT sensor.
#define Relay1 7                                         //added ????

void setup()
{
  Serial.begin(9600);                                   // Setup Serial connection speed (was 115200)
  dht.begin();                                          // Init DHT22
  
  digitalWrite(Relay1,HIGH);                            // set the pin HIGH first
  pinMode(Relay1,OUTPUT);                               // then make it an output

  
}

void loop()
{
  float h = dht.readHumidity();                         // DHT22 deciaml point
  float t = dht.readTemperature();                      // DHT22 deciaml point
  Serial.print("Humidity: ");                           // DHT22
  Serial.print(h);                                      // DHT22
  Serial.print(" %\t");                                 // DHT22
  Serial.print("Temperature: ");                        // DHT22
  Serial.print(t);                                      // DHT22
  Serial.println(" *C ");                               // DHT22

delay (1000);                                         // Wait one second before repeating

    if(t < 25) digitalWrite(Relay1, LOW);             // turn the relay off       // if temp is less than  
        else if(t > 25)digitalWrite(Relay1, HIGH); // turn the relay on to turn on the light  
}

edgemoron

The relay module you have is designed to work that way, that is the best way to do it, just invert your HIGH and LOW in digitalWrite()s.
Code: [Select]
if(t < 25) digitalWrite(Relay1, HIGH);             // turn the relay off       // if temp is less than  
        else if(t > 25)digitalWrite(Relay1, LOW); // turn the relay on to turn on the light  

}

chefslot

The relay module you have is designed to work that way, that is the best way to do it, just invert your HIGH and LOW in digitalWrite()s.

Ok cool thanks

frodo_eggens

#38
Apr 12, 2018, 02:31 am Last Edit: Apr 12, 2018, 04:32 am by frodo_eggens
Hiya,

Coming back to the original question.

The answer of which I´m interested in because I want to be able to use a 3 way toggle (left on, nothing on, right on) to:
  • turn on the relay, without arduino having anything to do with it
  • make sure the relay is not powered at all, without arduino having anything to do with it
  • turn the relay on through arduino


I figured I can only do this if the relay comes ON when input is HIGH.

So I took that 2-relay module and did the following
  • cut the copper on the circuit board between the 2 resistors that feed VCC to both the optop-coupler-leds
  • In1 and in2 go straight to GND


So now I can power the first opto coupler by putting an arduino HIGH on the standard VCC input
And I can power the second opto coupler by putting an arduino HIGH on the (before jumpered) VCC that resides next to the JD-VCC.
And I keep being able to power the relays separately.

It takes a little while to figure out where to cut it, because there is copper on both sides of the PCB. You can follow the paths from one side to the other side by tiny holes that connect paths that are on opposite sides of the board. Very cool.

To figure out where i had to cut I also used this schematic: https://howtomechatronics.com/tutorials/arduino/control-high-voltage-devices-arduino-relay-tutorial/

So is this going to get me in trouble somehow?
I've been thinking hard on why it would be a bad idea to operate this relay module on HIGH input instead of LOW, but didn't come up with anything :-(

chefslot

So the only use for a opticoupler is to give protection to the low side of the circuit, I think as long as you still have that protection then all should be good.
Test it to make sure thou, then check for continuity, or do the latter first.

Southpark

#40
Apr 12, 2018, 02:50 am Last Edit: Apr 12, 2018, 02:51 am by Southpark
Reversing the 'active low' by any means we can is fine.... as long as there aren't any situations or glitches that could cause issues ..... such as when resetting, or re-powering up, etc. As long as all the possibilities are taken care of.... then ok.

outsider

Why not put the toggle sw between relay "in" terminal and GND? Can you post a drawing?

rizakuzgun

Hi everyone, I also had the same problem, if you know how to solder, I explained how it was done in the photographs that I added, maybe it can be a solution to your problem.

https://photos.app.goo.gl/BDNmmjuzC85xvJCl2

johnnycatt

I know I'm really late to this discussion, but...

I solved this problem for me by writing the code backwards and it works perfectly.  (for clarification, I'm using an Arduino Nano with the relay connected on pin 3 and the code checked using pins, 10, 11 and 12 -- all with the same results)

I have my relay in the "normally open" position (so no energy is used for the relay to be in that position), If the Vcc is DISconnected from the relay board, the LED is OFF!!  This means the coil of the relay would have to energize to CLOSE the circuit and turn ON the LED.

One would think the following code would turn the LED on for 5 seconds and OFF for 1 second, it doesn't -- it does the opposite (the code below gives: off for 5sec, on for 1sec, off for 5sec, on for 1sec, ad infinitum)

Code: [Select]


void setup() {
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);


void loop() {
  digitalWrite(3, HIGH);
  delay(5000);
  digitalWrite(3, LOW);
  delay(1000);



SOOOO... If I switch my "HIGH's" and LOW's", it actually works perfectly (LED is on for 5sec, off for 1sec, on for 5sec, off for 1sec, ad infinitum). 

Now, the code looks like this:

Code: [Select]

void setup() {
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);

void loop() {
  digitalWrite(3, LOW);
  delay(5000);
  digitalWrite(3, HIGH);
  delay(1000);



The code I was writing when I experienced this problem with the relay board was far more complex than what I have written above, but even that works: I write the pin "LOW" and the Coil energizes... Not sure why, but it works on my project perfectly....

Since my project will be battery-powered and the relay will normally be open, I did not want to use the coil to "hold" the relay open (it would just waste energy.)  This solved it.

Wawa

  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);

Should be

  digitalWrite(3, HIGH);
  pinMode(3, OUTPUT);

This avoids possible relay chatter during bootup.
Leo..

Go Up