How to Write to I2C device (linux example works)

Hi There.
I have managed to buy I2C 8 relay (230V/10A) board that I have tested with Raspberry Pi.

This works with Pi:

i2cset -y 1 0x32 10 1 0 1 i (turns on first relay)
i2cset -y 1 0x32 10 0 0 1 i (turns off first relay)

Oddly when I run the code in IMP:
for (local i = 2 ; i < 256 ; i+=2) {
if (, “”, 1) != null)
server.log(format(“Device at address: 0x%02X”, i));
Result is “Device at address: 0x64”.

I got the address 0x64, instead of Pi’s 0x32… odd
Well, now with above example, how do I write with IMP?

Yes, the imp address is in 8 bit form (bottom bit ignored), ie the address is 0x64 which actually is what the address is quoted as in the BV4627 datasheet.

This would probably work:

Turn on:
i2c.write(0x64, "\\x0a\\x01\\x00\\x01")

Turn off:
i2c.write(0x64, "\\x0a\\x00\\x00\\x01")

That’s such a strange way to use i2c, but the relay board is freaky.

To give a better general case function:

function relaycontrol(relaynr, on) { i2c.write(0x64, format("%c%c\\x00\\x01", 10+relaynr-'A', on?1:0)); }

…then you can just do things like:

`// Turn relay B on
relaycontrol(‘B’, true);

//Turn relay C off
relaycontrol(‘C’, false);

THANK YOU THANK YOU!! “relaycontrol(‘B’, true)” worked!
All the way to “H”! You ARE AWESOME!!! :slight_smile:

If I may still bother you a bit… can you help me with reading the relay states… :slight_smile:

There does not appear to be a way to read the relay states (!). I’d suggest you keep a local array - as you’re turning these on & off, presumably you know what state you set.

True, and so I did. Thank you still for the help you did <long bow>
i2c <- hardware.i2c89; // Set to desired I2C bus
local relayStatesArray = [false,false,false,false,false,false,false,false];
local relayPointerArray = [‘A’,‘B’,‘C’,‘D’,‘E’,‘F’,‘G’,‘H’];

function relaycontrol(relaynr, on) {
local pointer = relayPointerArray[relaynr];
server.log(“relayControl: Relay " + relaynr + " set to “+on);
i2c.write(0x64, format(”%c%c\x00\x01”, 10+pointer-‘A’, on?1:0));

function setRelayState(index,state){
relayStatesArray[index] = state;
server.log("setRelayState: index " + index + " set to "+state);

// Get all states as [] JSON array.
function GetStates(data){

// 1st relay ON
// Last Relay Off

Pinns 8 & 9, 10Khom pullUp resistors from both clock & data to VCC. +5V must come from external power source, like from the same as IMP gets (USB port).

When ALL relays and Imp are working, 510mA was detected…

You really don’t need “relayPointerArray” - just make relaycontrol look like this:

function relaycontrol(relaynr, on) { server.log("relayControl: Relay " + relaynr + " set to "+on); i2c.write(0x64, format("%c%c\\x00\\x01", 10+relaynr, on?1:0)); }

…I’d only done the ‘A’ etc bit because that was how the relays are named on the PCB. Glad it’s working well for you though :slight_smile:

Just splendid, thanko you again. The improved version works like a charm :slight_smile:

Still, I’m trying to read spesific relay status. In the DataSheet page 5 writing and read values…
Sending command is (as above you have illustrated)

1.send start condition 2.send 0x64 3.send command(s) 4.send stop condition

So, if writing works like “10 1 0 1 p”. To get the device ID requires that command 85 be sent before receiving 2 bytes, thus: 83 r g-2 p?
So… ? :slight_smile:

The read only returns the timer for each relay, not the state (either that it’s in now, or what it’ll be going to). See page 8…

ok… thank you Hugo again… I have written to the maker of the board If he could in the future develop smaller, like 2/4 relayboard with solidStateRelays and a way to read the HW state as well… He’ll think about it :slight_smile: