8 relay module inverted

hello i'm new with arduino and i programed a to activate a relay module but here is the problem all program is inverted when i using the relay module and i don't know how to resolve this problem here 's the program, and i'm using a bluetooth module to activate the pin's.

for example: to activate the port 13 i must hit the number "1" and to desactvate i hit the "a" but it happens the opposite only if i am using the relay module, why ??

void setup()
{
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);

Serial.begin(9600);
}

void loop()
{
char c = Serial.read();
if (c == '1') digitalWrite(13,HIGH);
if (c == '2') digitalWrite(12,HIGH);
if (c == '3') digitalWrite(11,HIGH);
if (c == '4') digitalWrite(10,HIGH);
if (c == '5') digitalWrite(9,HIGH);
if (c == '6') digitalWrite(8,HIGH);

if (c == 'a') digitalWrite(13,LOW);
if (c == 'b') digitalWrite(12,LOW);
if (c == 'c') digitalWrite(11,LOW);
if (c == 'd') digitalWrite(10,LOW);
if (c == 'e') digitalWrite(9,LOW);
if (c == 'f') digitalWrite(8,LOW);

delay(1000);

and i programed a to activate a relay module

Which relay module?

Please remember to use code tags when posting code, like this

   char c = Serial.read();
   if (c >= '1' && c <= '6')
     digitalWrite(13 - (c -'1'), HIGH);
 
   if (c >= 'a' && c <= 'f')
     digitalWrite(13 - (c -'a'), LOW);

I've never used a relay module but from what I read, I think they usually have opto-isolators which by their nature invert. So you will probably have to change those digitalWrites so the HIGHs are LOWs and vice versa.

You should guard the Serial.read() call by first checking if a character is available:

  if (Serial.available () > 0)
  {
    char c = Serial.read () ;
    ....
    ....
  }

then lose the "delay (1000);"

Typical schematic for arduino relay modules.

HIGH is HIGH, LOW is LOW Why do you have difficulty accepting that LOW is ACTIVE and HIGH is INACTIVE?

If that's the way your relay board works (and most of them do) , take off the blinkers and accept it. The only problem is with your erroneous preconception that "HIGH=ACTIVE".

KenF:
HIGH is HIGH, LOW is LOW Why do you have difficulty accepting that LOW is ACTIVE and HIGH is INACTIVE?

If that's the way your relay board works (and most of them do) , take off the blinkers and accept it. The only problem is with your erroneous preconception that "HIGH=ACTIVE".

That sounds a trifle harsh - the OP, single post so far, simply did not realise that it depends on how the relay board is wired and it is reasonably understandable that someone might expect modules to work in a "logical" manner. :smiley: We encounter this with great frequency in regard to the desirability of using switches pulling to ground and corresponding pull-ups.

zoomkat's diagram (which I have duly bookmarked on this PC, not the one I mostly use for discussions here) illustrates nicely what is really happening.

JimboZA:
I've never used a relay module but from what I read, I think they usually have opto-isolators which by their nature invert.

Sifting through my 52 litre bins of eBay acquisitions, I find I have well over half a dozen such relay modules of the dual relay variety from SainSmart, but have yet to actually try one out.

I think it is somewhat misleading to say "opto-isolators which by their nature invert". They no more "invert" than does a transistor which can be used in emitter-follower or common-base modes which do not invert. Because the output of the opto-isolator is essentially a two terminal device - as is the input - there is considerable flexibility in how they are used (four permutations). In particular, the opto-isolator is not limited as is an emitter-follower, in its ability to pull up to very close to the supply voltage.

1 Like

Paul__B:
That sounds a trifle harsh

Yes. Reading it back it does come across that way.

I've been suffering some considerable pain today and I'm afraid it's come across (quite unjustifiably) in my post here. I hope makyrave will not take this personally.

I can only say I'm sorry :frowning:

Paul__B:
I think it is somewhat misleading to say "opto-isolators which by their nature invert". They no more "invert" than does a transistor which can be used in emitter-follower or common-base modes which do not invert.

Fair comment.

Message to the OP though, is that his relay board seems to be wired to be upside-down from what was expected, and that can be sorted out in code by flipping the digitalWrites