Arduino Tutorial

By LiterallyTheOne

8: I2C: Part 2

Arduino Tutorial, I2C: Part 2

Introduction

  • Previous tutorial: I2C part 1
  • This tutorial: I2C part 2
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621

  • Measures Temperature
    • resolution
  • I2C

temperature ds1621

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Connect to Arduino

  • DS1621: Micro/Sensors/DS1621

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Find the ID

  • With the scan code
#define DS1621_ADDRESS 0x48
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Command table

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Initiate conversation

Wire.beginTransmission(DS1621_ADDRESS);
Wire.write(0xEE);
Wire.endTransmission();
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Remove 1shot

Wire.beginTransmission(DS1621_ADDRESS);
Wire.write(0xAC);
Wire.write(0x00);
Wire.endTransmission();
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Request Read

Wire.beginTransmission(DS1621_ADDRESS);
Wire.write(0xAA);
Wire.endTransmission();
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Read 2 bytes

Wire.requestFrom(DS1621_ADDRESS, 2);
byte temp_msb = Wire.read();
byte temp_lsb = Wire.read();
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Storage method

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: Convert those two bytes

float result = temp_msb;
if (temp_lsb & 0x80)
{
  result += 0.5;
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

DS1621: result

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Arduino slave

  • We can configure an Arduino as a slave
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Connect the second Arduino

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Initialize the second Arduino

Wire.begin(8);

OnReceive

  • Works as an interrupt
Wire.onReceive(receiveEvent);
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Get all the data and set a flag

String result;
bool i2c_ready = false;

void receiveEvent(int howMany)
{
  i2c_ready = true;
  result = "";
  while (Wire.available())
  {
    char c = Wire.read();
    result += c;
  }
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Write the data in the Serial Terminal

if (i2c_ready)
{
  i2c_ready = false;
  Serial.println(result);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Send data from Master

#define ARDUINO_2 0x08
String str_temp = String(temperature);

Wire.beginTransmission(ARDUINO_2);
for (unsigned int i = 0; i < str_temp.length(); i++)
{
  Wire.write(str_temp[i]);
}
Wire.endTransmission();
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Output of 2 Arduinos

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

OnRequest

  • Triggers when a request happens
Wire.onRequest(requestEvent);
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Write two bytes

void requestEvent()
{
    Wire.write(5);
    Wire.write(6);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Receive data from Master

Wire.requestFrom(ARDUINO_2, 2);
String received_data = "";
received_data += Wire.read();
received_data += Wire.read();
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Small detail about Wire

  • onReceive gets triggered on both
    • Getting data
    • Sending data
  • At the beginning of the function
if (howMany == 0)
    return;
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

Output of receiving and sending

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, I2C: Part 2

By Ramin Zarebidoky (LiterallyTheOne)