An Example Of Doing An I2c Slave With Arduino - Gists · GitHub

Skip to content Search Gists Search Gists All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@dotdoom dotdoom/i2c.ino Created June 1, 2016 09:51 Show Gist options
  • Star (0) You must be signed in to star a gist
  • Fork (1) You must be signed in to fork a gist
  • Embed Select an option
    • Embed Embed this gist in your website.
    • Share Copy sharable link for this gist.
    • Clone via HTTPS Clone using the web URL.

    No results found

    Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/dotdoom/9ee7c65791e59fd421cf199a752d6701.js"></script>
  • Save dotdoom/9ee7c65791e59fd421cf199a752d6701 to your computer and use it in GitHub Desktop.
Code Revisions 1 Forks 1 Embed Select an option
  • Embed Embed this gist in your website.
  • Share Copy sharable link for this gist.
  • Clone via HTTPS Clone using the web URL.

No results found

Learn more about clone URLs Clone this repository at <script src="https://gist.github.com/dotdoom/9ee7c65791e59fd421cf199a752d6701.js"></script> Save dotdoom/9ee7c65791e59fd421cf199a752d6701 to your computer and use it in GitHub Desktop. Download ZIP An example of doing an i2c slave with Arduino Raw i2c.ino This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
#define I2CAddress 0x42
void setup() {
Wire.begin(I2CAddress);
// Remember to keep those handlers as time-critical as possible:
// no interrupts will be happening while these are running.
// Also the other end of i2c communication might just
// give up waiting. So keep the logic in loop() and let i2c
// handlers only operate on ready data.
Wire.onReceive(i2cReceive);
Wire.onRequest(i2cRequest);
}
volatile byte i2cRegister = 0xff;
void i2cReceive(int bytesReceived) {
i2cRegister = Wire.read();
if (bytesReceived > 1) {
// This is i2c "write" request; read data with Wire.read()...
}
// onReceive will not be invoked unless rxBuffer is empty.
// Clean it up manually.
while (Wire.available()) { Wire.read(); }
}
void i2cRequest() {
// Use Wire.write() to send the data from register i2cRegister...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Tag » Arduino Wire I2c Slave Example