Page 1 of 1
How to code for the Relay Expansion?
Posted: Wed Feb 04, 2015 10:57 pm
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
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 5:58 am
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
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 8:07 am
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.
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 5:26 pm
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
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 6:18 pm
by rimai
Your id needs to start at 0 not 11
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 6:30 pm
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!
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 6:41 pm
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 ~
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 7:03 pm
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);
}
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 7:07 pm
by rimai
Ahhh.
You forgot to initialize the library.
Place this in setup()
Re: How to code for the Relay Expansion?
Posted: Thu Feb 05, 2015 7:10 pm
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