How to code for the Relay Expansion?
How to code for the Relay Expansion?
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
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?
The standard relay box is like this, using Port1 through Port8
The expansion relay boxes are like this, with Box1 being the first expansion (add-on) box. Box2 is the second, etc.
Hope this helps.
--Colin
Code: Select all
ReefAngel.Relay.On(Port1);
ReefAngel.Relay.Off(Port1);
Code: Select all
ReefAngel.Relay.On(Box1_Port1);
ReefAngel.Relay.Off(Box1_Port1);
--Colin
Re: How to code for the Relay Expansion?
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?
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?
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
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);
}
}
ps. I have got a single RA Relay expansion, detected by the i2c scanner.
Thanks
Re: How to code for the Relay Expansion?
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:
Thanks!
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);
}
Re: How to code for the Relay Expansion?
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 ~
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.
Re: How to code for the Relay Expansion?
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:
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?
I am felling so dumb right now
That worked. Thank you very much!
For future reference, here is the working code:
Frank
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);
}