// Wire Controller Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI peripheral device
// Refer to the "Wire Peripheral Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include<Wire.h>voidsetup(){Wire.begin();// join i2c bus (address optional for master)
Serial.begin(9600);// start serial for output
}voidloop(){Wire.requestFrom(8,6);// request 6 bytes from peripheral device #8
while(Wire.available()){// peripheral may send less than requested
charc=Wire.read();// receive a byte as character
Serial.print(c);// print the character
}delay(500);}
// Wire Peripheral Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI peripheral device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include<Wire.h>voidsetup(){Wire.begin(8);// join i2c bus with address #8
Wire.onRequest(requestEvent);// register event
}voidloop(){delay(100);}// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
voidrequestEvent(){Wire.write("hello ");// respond with message of 6 bytes
// as expected by master
}
// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Writes data to an I2C/TWI Peripheral device
// Refer to the "Wire Peripheral Receiver" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include<Wire.h>voidsetup(){Wire.begin();// join i2c bus (address optional for master)
}bytex=0;voidloop(){Wire.beginTransmission(4);// transmit to device #4
Wire.write("x is ");// sends five bytes
Wire.write(x);// sends one byte
Wire.endTransmission();// stop transmitting
x++;delay(500);}
// Wire Peripheral Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI Peripheral 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>voidsetup(){Wire.begin(4);// join i2c bus with address #4
Wire.onReceive(receiveEvent);// register event
Serial.begin(9600);// start serial for output
}voidloop(){delay(100);}// function that executes whenever data is received from master
// this function is registered as an event, see setup()
voidreceiveEvent(inthowMany){while(1<Wire.available())// loop through all but the last
{charc=Wire.read();// receive byte as a character
Serial.print(c);// print the character
}intx=Wire.read();// receive byte as an integer
Serial.println(x);// print the integer
}
// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
// by Nicholas Zambetti <http://www.zambetti.com>
// and James Tichenor <http://www.jamestichenor.net>
// Demonstrates use of the Wire library reading data from the
// Devantech Utrasonic Rangers SFR08 and SFR10
// Created 29 April 2006
// This example code is in the public domain.
#include<Wire.h>voidsetup(){Wire.begin();// join i2c bus (address optional for master)
Serial.begin(9600);// start serial communication at 9600bps
}intreading=0;voidloop(){// step 1: instruct sensor to read echoes
Wire.beginTransmission(112);// transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c addressing uses the high 7 bits so it's 112
Wire.write(byte(0x00));// sets register pointer to the command register (0x00)
Wire.write(byte(0x50));// command sensor to measure in "inches" (0x50)
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission();// stop transmitting
// step 2: wait for readings to happen
delay(70);// datasheet suggests at least 65 milliseconds
// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(112);// transmit to device #112
Wire.write(byte(0x02));// sets register pointer to echo #1 register (0x02)
Wire.endTransmission();// stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(112,2);// request 2 bytes from peripheral device #112
// step 5: receive reading from sensor
if(2<=Wire.available()){// if two bytes were received
reading=Wire.read();// receive high byte (overwrites previous reading)
reading=reading<<8;// shift high byte to be high 8 bits
reading|=Wire.read();// receive low byte as lower 8 bits
Serial.println(reading);// print the reading
}delay(250);// wait a bit since people have to read the output :)
}/*
// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
// usage: changeAddress(0x70, 0xE6);
void changeAddress(byte oldAddress, byte newAddress)
{
Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(byte(0xA0));
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(byte(0xAA));
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(byte(0xA5));
Wire.endTransmission();
Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(newAddress);
Wire.endTransmission();
}
*/
// I2C Digital Potentiometer
// by Nicholas Zambetti <http://www.zambetti.com>
// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/>
// Demonstrates use of the Wire library
// Controls AD5171 digital potentiometer via I2C/TWI
// Created 31 March 2006
// This example code is in the public domain.
// This example code is in the public domain.
#include<Wire.h>voidsetup(){Wire.begin();// join i2c bus (address optional for master)
}byteval=0;voidloop(){Wire.beginTransmission(44);// transmit to device #44 (0x2c)
// device address is specified in datasheet
Wire.write(byte(0x00));// sends instruction byte
Wire.write(val);// sends potentiometer value byte
Wire.endTransmission();// stop transmitting
val++;// increment value
if(val==64)// if reached 64th position (max)
{val=0;// start over from lowest value
}delay(500);}