{"id":853,"date":"2024-01-19T10:14:42","date_gmt":"2024-01-19T10:14:42","guid":{"rendered":"https:\/\/arduino-developer.com\/arduino\/?p=853"},"modified":"2024-01-19T10:16:00","modified_gmt":"2024-01-19T10:16:00","slug":"master-i2c","status":"publish","type":"post","link":"https:\/\/arduino-developer.com\/arduino\/programming\/i2c\/wire-library\/master-i2c","title":{"rendered":"Master I2C"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#include &lt;Wire.h&gt;\n\n  \/\/----- INITIALISE I2C -----\n  Wire.begin();\n\n\n\n  \/\/----- WRITE TO I2C DEVICE -----\n  unsigned char value = 1;\n  Wire.beginTransmission(0x5a);\n  Wire.write(value);\n  uint8_t Result = Wire.endTransmission(true); \/\/true=send stop\n  \/\/Result: 0=success, 1=tx buff overrun, 2=NACK on address tx, 3=NACK on data tx, 4=other error \n  \n  \n  \/\/----- READ FROM I2C DEVICE -----\n  Wire.requestFrom(0x5a, 2);     \/\/Address, no of bytes\n  uint8_t BytesReceived = Wire.available();    \/\/The slave can send less than was requested\n  \/\/No need to send stop as done automatically\n  if (BytesReceived &gt;= 2)\n  {\n    byte LSB = Wire.read();\n    byte MSB = Wire.read();\n  }\n\n\n  \/\/----- WRITE THEN READ I2C DEVICE -----\n  unsigned char value;\n  Wire.beginTransmission(0x5a);\n  Wire.write(value);\n  if (Wire.endTransmission(false) == 0) \/\/false=send restart (IMPORTANT-THIS ENDTRANSMISSION DOES NOT SEND STOP!).  0=success, 1=tx buff overrun, 2=NACK on address tx, 3=NACK on data tx, 4=other error \n  {\n    Wire.requestFrom(0x5a, 2);     \/\/Address, no of bytes\n    uint8_t BytesReceived = Wire.available();    \/\/The slave can send less than was requested\n    \/\/No need to send stop as done automatically\n    if (BytesReceived &gt;= 2)\n    {\n      byte LSB = Wire.read();\n      byte MSB = Wire.read();\n    }\n  }\n  else\n  {\n    Wire.endTransmission(true); \/\/true=send stop\n  }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-853","post","type-post","status-publish","format-standard","hentry","category-wire-library"],"_links":{"self":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/comments?post=853"}],"version-history":[{"count":2,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/853\/revisions"}],"predecessor-version":[{"id":856,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/853\/revisions\/856"}],"wp:attachment":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/media?parent=853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/categories?post=853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/tags?post=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}