How to code for the Relay Expansion?

Do you have a question on how to do something.
Ask in here.
Post Reply
fsamir
Posts: 8
Joined: Mon Oct 13, 2014 9:59 pm

How to code for the Relay Expansion?

Post by fsamir »

Hi chaps,

can someone help me to write a simple Arduino sketch to turn ON/OFF all relays, using the I2C relay expansion?

I couldn't clearly understand what the Relay.cpp class does.

Thanks,
Frank
User avatar
cosmith71
Posts: 1437
Joined: Fri Mar 29, 2013 3:51 pm
Location: Oklahoma City

Re: How to code for the Relay Expansion?

Post by cosmith71 »

The standard relay box is like this, using Port1 through Port8

Code: Select all

ReefAngel.Relay.On(Port1);
ReefAngel.Relay.Off(Port1);
The expansion relay boxes are like this, with Box1 being the first expansion (add-on) box. Box2 is the second, etc.

Code: Select all

ReefAngel.Relay.On(Box1_Port1);
ReefAngel.Relay.Off(Box1_Port1);
Hope this helps.

--Colin
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: How to code for the Relay Expansion?

Post by lnevo »

You can also set ReefAngel.RelayData to 255. RelayData is a byte with each bit representing a relay port. If the bit is true then the relay is on. The ReefAngel.RelayData is also bitwise ORd and ANDed with the RelayMaskOn and RelayMaskOff to manage the manual overrides of the port. The Relay class abstracts the management of all these bits by giving functions like On, Off, Override, etc.
fsamir
Posts: 8
Joined: Mon Oct 13, 2014 9:59 pm

Re: How to code for the Relay Expansion?

Post by fsamir »

Hi,

thanks for the explanation, it helped me to understand further. However, still not enough to make it work.
I forgot to mention I don't want to use ReenfAngel code because I will be using python on a RPI.

That is the Arduino sketch I am using to test the basic logic. Can you point what is wrong with it?

Code: Select all

#include "Wire.h"

#define I2CExpModule        0x38 // 0x38-3f
#define START		11
#define END		18

void setup(){
  Serial.begin(9600);
}

void loop(){
  byte RelayData = 255;
  byte RelayMaskOn = 0;
  byte RelayMaskOff = 0xff;

  byte TempRelay = RelayData;
  //TempRelay &= RelayMaskOff;
  //TempRelay |= RelayMaskOn;

  for ( byte relayId = START; relayId < END; relayId++ ){
    Wire.beginTransmission(I2CExpModule);
    Wire.write(~TempRelay);
    byte present = Wire.endTransmission();

    Serial.print(relayId);
    Serial.print(" present: ");
    Serial.println(present);
    delay(500);
  }
}

This code outputs 4 for the present flag and doesn't turn on the relays.
ps. I have got a single RA Relay expansion, detected by the i2c scanner.

Thanks
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: How to code for the Relay Expansion?

Post by rimai »

Your id needs to start at 0 not 11
Roberto.
fsamir
Posts: 8
Joined: Mon Oct 13, 2014 9:59 pm

Re: How to code for the Relay Expansion?

Post by fsamir »

Roberto,

there must be something else. That loop is a left over and does nothing.
My understanding from lnevo's comment is that RelayData represents the ports, so 255 should be "all relays on".

This is how the code looks like without the loop:

Code: Select all

#include "Wire.h"

#define I2CExpModule        0x38 // 0x38-3f

void setup(){
  Serial.begin(9600);
}

void loop(){
  byte RelayData = 255;

  Wire.beginTransmission(I2CExpModule);
  Wire.write(~RelayData);
  byte present = Wire.endTransmission();

  Serial.print(" present: ");
  Serial.println(present);
  delay(500);
}
Thanks!
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: How to code for the Relay Expansion?

Post by rimai »

Are you using one of the Reef Angel expansion modules?
Do you have the id set to 0?
We use inverted data to prevent the relays from turning on and off at start up.
If you are not using, you need to use 0 and not 255 or remove the ~
Roberto.
fsamir
Posts: 8
Joined: Mon Oct 13, 2014 9:59 pm

Re: How to code for the Relay Expansion?

Post by fsamir »

Hi Roberto,
Yes, I am using a Relay Expansion Module.
If the ID you are talking about is the DIP switch, I have it set to #1 (all off) which results in the I2C address 0x38, confirmed by the i2cScanner.

I have tried with 0 and 255, none worked.
Any other clues?

This is the new test:

Code: Select all

#include "Wire.h"

#define I2CExpModule        0x38 // 0x38-3f

void setup(){
  Serial.begin(9600);
}

void loop(){
  byte RelayData = 0;

  Wire.beginTransmission(I2CExpModule);
  Wire.write(RelayData);
  Wire.endTransmission();

  delay(1500);

  RelayData = 255;
  Wire.beginTransmission(I2CExpModule);
  Wire.write(RelayData);
  Wire.endTransmission();
  
  delay(1500);
}


rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: How to code for the Relay Expansion?

Post by rimai »

Ahhh.
You forgot to initialize the library.
Place this in setup()

Code: Select all

Wire.begin();
Roberto.
fsamir
Posts: 8
Joined: Mon Oct 13, 2014 9:59 pm

Re: How to code for the Relay Expansion?

Post by fsamir »

I am felling so dumb right now :)

That worked. Thank you very much!

For future reference, here is the working code:

Code: Select all

#include "Wire.h"

#define I2CExpModule        0x38 // 0x38-3f

void setup(){
  Serial.begin(9600);
   Wire.begin();
}

void loop(){
  byte RelayData = 0;

  Wire.beginTransmission(I2CExpModule);
  Wire.write(~RelayData);
  Wire.endTransmission();

  delay(1500);

  RelayData = 255;
  Wire.beginTransmission(I2CExpModule);
  Wire.write(~RelayData);
  Wire.endTransmission();
  
  delay(1500);

}

Frank
Post Reply