Background
The purpose of this project is to provide simple directions on how to use I2C communication on Particle Photon. For I2C device Thermo 3 click is used. At the same time we are going to send data into Particl dashboard.
Preparation
Everything you need for making Photon projects is on cloud. Open https://build.particle.io and login with your account. Put Photon and Thermo 3 click in breadboard and make connections as follow :
- Photon 3V3 => Thermo3 3V3
- Photon GND => Thermo3 GND
- Photon D0 => Thermo3 SDA
- Photon D1 => Thermo3 SCK
Coding
Code is very simple, but I am going to explain important details.
void setup()
{
Serial.begin(9600);
Wire.begin();
Serial.println("Setup");
}
Setup procedure is simple, but one detail from Particle documentation is interesting :
The data rate for the USB device Serial
is ignored, as USB has its own negotiated speed. Setting speed to 9600 is safe for the USB device. Setting the port to 14400 baud will cause the Photon or Electron to go into DFU mode while 28800 will allow a YMODEM download of firmware.
I2C address for Thermo3 click is 0x48 HEX. For reading data from I2C slave, we must set register pointer. We do that with Wire.write(byte(0x00)). Temperature data is 2-bytes value.
Wire.beginTransmission(0x48); // transmit to device (0x48)
Wire.write(byte(0x00)); // sends instruction byte
Wire.endTransmission();
Request data from I2C slave must defined how many bytes we read from I2C :
Wire.requestFrom(0x48,2);
Finally, we are going to process received data :
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
reading = reading >> 4; // shift right 4 bits
temp = reading * 0.0625;
Serial.println(temp); // print the reading
Particle.publish("temperature", String(temp)); // send data to dashboard
}
We are sending data to Serial port and with any serial terminal (Putty) we can track temperature. But it is amazing how is simple sending data to Particle dashboard. Only one line of code is required Particle.publish(event name,data)! Open URL https://dashboard.particle.io/ and login with your credentials. You will get screen :
That's it... Fast and simple.
Comments