function setConfiguration(refreshRate){
server.log(“Configuration”);
local Hz_LSB;;
switch (refreshRate){
case 0:
Hz_LSB=0x3f;
break;
case 1:
Hz_LSB=0x3e;
break;
case 2:
Hz_LSB=0x3d;
break;
case 4:
Hz_LSB=0x3c;
break;
case 8:
Hz_LSB=0x3b;
break;
case 16:
Hz_LSB=0x3a;
break;
case 32:
Hz_LSB=0x39;
break;
default:
Hz_LSB=0x3e;
}
ex_en is the a pin in imp that enables I2C
I have to write 4 bytes of data
I receive error
Sending response------------------------------ -3
When I replace the write line with
local resp = i2c.write(0x60 << 1, “”+"");
i get response
Sending response------------------------------ 0
local resp = i2c.write(0x60 << 1, “\x03”+"");
give sresponse
Sending response------------------------------ 0 first time
running again gives Sending response------------------------------ -4 but I did not made any changes in code
and 0 and -4 response keep alternating
I have made all iteration for writing data. nothing worked.
I am porting code from arduino for MLX90621 melexis infrared sensor.
in the code line
local resp1 = i2c.write(0xc0, “\x04”+"\xaa"+"\x54"+"\x56"+"\x00");
\x04–> 0x04 is a command not sub address.
The actual address is 0x93
I am trying to write oscillator trimming value using this command.
Thanks for helping
I am using Pin-C of electric imp to start I2C communication by writing 0, creating the start of the I2C communication and pulling it high again to stop the communication.
There is an EEPROM and RAM in the sensor both are on I2C. I am ableto read data from EEPROM but writing to the RAM gives error. I think the pullups are working fine as I am able to read from EEPROM
…regardless, scope traces of the i2c bus are likely needed to progress further. I’d also check the power supply to the device is ok (does it have local bypass caps, etc?)
function readEEPROM(){
server.log(“read EEPROM data::::::::::::::::::::::::::::::::::::::::::::::”);
local cmd = blob();
cmd.writen(0x00, ‘b’);
server.log(“Pin P state”+ex_en.read());
server.log(“Pin Q state”+ex_en1.read());
imp.sleep(0.05);
server.log(“Sending:”);
server.log( cmd.tostring());
local resp = i2c.write(0x50 << 1, cmd.tostring());
server.log("Sending response------------------------------ "+resp);
if(resp==0){
server.log(“Successfully send command”);
eepromData = i2c.read(0x50 << 1, “”, 256);
server.log(“length of data---------------------”+(eepromData.len()));
}
server.log(“Pin P state”+ex_en.read());
server.log(“Pin Q state”+ex_en1.read());
}
readEEPROM();
Result:
2018-07-08 00:28:03 +05:30
[Device]
Pin P state0
2018-07-08 00:28:03 +05:30
[Device]
Pin Q state0
2018-07-08 00:28:03 +05:30
[Device]
Sending:
2018-07-08 00:28:03 +05:30
[Device]
binary: 00
2018-07-08 00:28:03 +05:30
[Device]
Sending response------------------------------ 0
2018-07-08 00:28:03 +05:30
[Device]
Successfully send command
2018-07-08 00:28:03 +05:30
[Device]
length of data---------------------256
2018-07-08 00:28:03 +05:30
[Device]
Pin P state0
2018-07-08 00:28:03 +05:30
[Device]
Pin Q state0
When I2C comunication starts?
Please explain when there is no pull on the sensor, like the “SA56004X temperature sensor” given in I2C documentation, How we should put pullup resistor? Please sight any example, I searched but i could not get one.
Also please explain the result I am getting
Any clue will be a great help.
Oscilloscope reading will take some while. I have to order one.
I2C is an open drain bus. It requires pull up resistors. Please add these and retest - it’s rather a miracle it’s working at all without them. 4k7 to 3.3v should be fine.
Datasheets for slave devices may not always show pull-ups, but these are always required - you have one set of pullups per bus not one set per device.
Hi Hugo,
Thanks a lot
I tried with the pullup resistors on the data, and clock line, Now I can write data it is giving ack(0).
so one problem solved.
But
This sensor works like charm with Arduino. In that also external pullup was required. now same pullups I have given here.
Now I got the read error MASTER_RECEIVE_SELECT_ERROR (-10)
I was also surprised why it was giving EEPROM data. In manufacturer’s manual it says no internal pullup, external pull up is required. No matter what I do EEPROM data is reading.
Now I am able to write comands but after writing a command, the value to be read, there it is giving error.
One issue here is that your read command is sending “\x00” when nothing is expected. Also, you are issuing the read IR data command, telling it that you’re about to read 64 bytes, then reading 3 of them after this spurious write.
To read IR data, this should work:
// Issue IR data read command, step 1, 0x40 bytes, and read them
local irdata = i2c.read(0x60 << 1, "\x02\x00\x01\x40", 64);
if (irdata == null) server.log("i2c error "+i2c.readerror());
else {
server.log("read "+irdata.len()+" bytes");
}
Thank you very much, It is working, thanks a lot. It was really a pain.
Could you please explain
i2c.read(0x60 << 1, “\x02\x00\x01\x40”, 64);
“\x02\x00\x01\x40” This is register address?
For reading any data, I need to send the string sequence in place of register address?
Any ways thanks a lot it is working.