Skip to content

Instantly share code, notes, and snippets.

@dydx
Created November 30, 2011 04:15
Show Gist options
  • Select an option

  • Save dydx/1407987 to your computer and use it in GitHub Desktop.

Select an option

Save dydx/1407987 to your computer and use it in GitHub Desktop.
Arduino I2C setup
// Running on an Arduino Mega2560 R.3
// #20 - SDA
// #21 - SCL
#include <Wire.h>
void setup() {
pinMode(13, OUTPUT);
Wire.begin();
Serial.begin(9600);
}
void loop() {
Wire.requestFrom(2, 6); // request 6 bytes from slave #2
while(Wire.available()) {
char c = Wire.receive();
Serial.print(c);
// blink #13 to show communication
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
delay(500);
}
// Running on an Arduino Diecimila
// #4 - SDA
// #5 - SCL
#include <Wire.h>
void setup() {
pinMode(13, OUTPUT);
Wire.begin(2);
Wire.onRequest(requestEvent);
}
void loop() {
delay(100);
}
void requestEvent() {
Wire.send("hello ");
// blink #13 to show communication
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment