Adapt I2C Arduino code for particle Photon (for a moisture sensor)

Hi all,

I’m tying to adapt this arduino code for a particle photon but I get wrong values from my sensor (see below). I tried the arduino code and it works well with my uno.

Any ideas?

Thanks a lot !

#include <Wire.h>

void writeI2CRegister8bit(int addr, int value) {
  Wire.beginTransmission(addr);
  Wire.write(value);
  Wire.endTransmission();
}

unsigned int readI2CRegister16bit(int addr, int reg) {
  Wire.beginTransmission(addr);
  Wire.write(reg);
  Wire.endTransmission();
  delay(20);
  Wire.requestFrom(addr, 2);
  unsigned int t = Wire.read() << 8;
  t = t | Wire.read();
  return t;
}

void setup() {
  Wire.begin();
  Serial.begin(9600);
  writeI2CRegister8bit(0x20, 6); //reset
}

void loop() {
  Serial.print(readI2CRegister16bit(0x20, 0)); //read capacitance register
  Serial.print(", ");
  Serial.print(readI2CRegister16bit(0x20, 5)); //temperature register
  Serial.print(", ");
  writeI2CRegister8bit(0x20, 3); //request light measurement 
  Serial.println(readI2CRegister16bit(0x20, 4)); //read light register
}

Here is my Particle code:

#include "application.h"

void writeI2CRegister8bit(int addr, int value) {
  Wire.beginTransmission(addr);
  Wire.write(value);
  Wire.endTransmission();
}

unsigned int readI2CRegister16bit(int addr, int reg) {
  Wire.beginTransmission(addr);
  Wire.write(reg);
  Wire.endTransmission();
  delay(20);
  Wire.requestFrom(addr, 2);
  unsigned int t = Wire.read() << 8;
  t = t | Wire.read();
  return t;
}

void setup() {
  Wire.begin();
  Serial.begin(9600);
  writeI2CRegister8bit(0x20, 6); //reset
}

void loop() {
  Particle.publish("test", String(readI2CRegister16bit(0x20, 0))); //read capacitance register
  Particle.publish("test", String(readI2CRegister16bit(0x20, 5))); //temperature register
  writeI2CRegister8bit(0x20, 3); //request light measurement 
  Particle.publish("test", String(readI2CRegister16bit(0x20, 4))); //read light register
} 

The data i get from all the particile.publish:
{“data”:“4294967295”,“ttl”:“60”,“published_at”:“2016-02-01T20:04:17.514Z”,“coreid”:“210025001047343339383037”,“name”:“test”}

arduino to photon i2c should be about the same , but one thing I’ve noticed is you don’t have a delay in your loop and using Particle.publish which as a limit of 1 per second.

One other thing to note: on a typical Arduino, unsigned int is a 16-bit type but on Photon it is a 32-bit type. Although I don’t see how that would hurt you here, I would use a more specific type in the Photon code, with uint16_t being the best choice.

As @peter_a stated above, you are likely going over the publish limit, in which case you just won’t get your events.

Thanks for your answers, I added a delay and it turns out that I forgot the pull-up resistors :sweat: (It worked without it with an arduino uno)

1 Like

Sorry writeing in a old post but do you have your latest working code you can show Jibsgrl?

IMHO (after actually reading the thread) it seems the code runs as is (plus one extra line delay(5000); at the end of loop() and the required pull-up resistors.

BTW:

Actually on the Uno it doesn't work without them, but the Uno has them on board.