Skip to content

Instantly share code, notes, and snippets.

@neophob
Created April 4, 2013 23:11
Show Gist options
  • Save neophob/5315194 to your computer and use it in GitHub Desktop.
Save neophob/5315194 to your computer and use it in GitHub Desktop.
Arduino I2c slave
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(115200); // start serial for output
Serial.println("hello!");
}
void loop()
{
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
int a=0;
while(Wire.available()>0) // loop through all but the last
{
Wire.read(); // receive byte as a character
a++;
}
Serial.print("read bytes: ");
Serial.println(a); // print the integer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment