### Repeated Start Transaction in C++ Wire Library Source: https://context7.com/paulstoffregen/wire/llms.txt This example demonstrates a repeated start I2C transaction, writing a register address without stopping and then reading the value. It uses the Wire and Serial libraries. Input is device address (e.g., 0x68) and register (e.g., 0x75). Output is serial read value or error. Limitations: Tailored for devices like MPU6050; requires proper timing and connection. ```cpp #include void setup() { Wire.begin(); Serial.begin(9600); } void loop() { uint8_t deviceAddr = 0x68; // Example: MPU6050 IMU uint8_t regAddr = 0x75; // WHO_AM_I register // Write register address without STOP condition Wire.beginTransmission(deviceAddr); Wire.write(regAddr); uint8_t error = Wire.endTransmission(false); // false = no STOP (repeated start) if (error == 0) { // Read register value with repeated start Wire.requestFrom(deviceAddr, (uint8_t)1); if (Wire.available()) { uint8_t whoami = Wire.read(); Serial.print("WHO_AM_I register: 0x"); Serial.println(whoami, HEX); } } else { Serial.print("Transmission error: "); Serial.println(error); } delay(1000); } ``` -------------------------------- ### Multiple I2C Bus Support in C++ Wire Library on Teensy Source: https://context7.com/paulstoffregen/wire/llms.txt This code initializes and uses multiple I2C buses (Wire, Wire1, Wire2) on Teensy boards for connecting different devices. It requires the Wire and Serial libraries. Input is none; it sends example commands. Output is serial initialization message. Limitations: Specific to Teensy hardware with multiple buses; uses default pins unless customized. ```cpp #include void setup() { // Initialize all available I2C buses Wire.begin(); // Pins 18 (SDA0), 19 (SCL0) Wire1.begin(); // Pins 17 (SDA1), 16 (SCL1) Wire2.begin(); // Pins 25 (SDA2), 24 (SCL2) // Optional: customize pins (Teensy) // Wire.setSDA(18); // Wire.setSCL(19); Serial.begin(9600); Serial.println("All I2C buses initialized"); } void loop() { // Use different buses for different devices Wire.beginTransmission(0x50); // EEPROM on Wire Wire.write(0x00); Wire.endTransmission(); Wire1.beginTransmission(0x68); // IMU on Wire1 Wire1.write(0x75); Wire1.endTransmission(); Wire2.beginTransmission(0x76); // Pressure sensor on Wire2 Wire2.write(0xF6); Wire2.endTransmission(); delay(100); } ``` -------------------------------- ### Master Transmit to Slave (C++) Source: https://context7.com/paulstoffregen/wire/llms.txt Send data from master to a slave using beginTransmission, write, and endTransmission. endTransmission returns an error code: 0 = success, 2 = NACK on address, 3 = NACK on data. Requires #include . The example writes a string and a counter byte to device 9. ```C++ #include int counter = 0; void setup() { Wire.begin(); // Initialize as master Serial.begin(9600); } void loop() { Wire.beginTransmission(9); // Start transmission to device #9 Wire.write("x is "); // Send 5 bytes Wire.write(counter); // Send counter value as single byte uint8_t error = Wire.endTransmission(); // Send and release bus if (error == 0) { Serial.println("Transmission successful"); } else if (error == 2) { Serial.println("Error: NACK on address"); } else if (error == 3) { Serial.println("Error: NACK on data"); } else { Serial.print("Error code: "); Serial.println(error); } counter++; delay(1000); } ``` -------------------------------- ### Slave Transmit with onRequest Callback (C++) Source: https://context7.com/paulstoffregen/wire/llms.txt Send data to the master when polled using the onRequest callback. The callback runs in an interrupt context; keep it short and avoid blocking calls. The example transmits a 16-bit sensor value as high and low bytes. Requires #include . ```C++ #include int sensorValue = 0; void setup() { pinMode(LED_BUILTIN, OUTPUT); Wire.begin(8); // Join I2C bus as slave #8 Wire.onRequest(requestEvent); // Register callback Serial.begin(9600); } void requestEvent() { digitalWrite(LED_BUILTIN, HIGH); // Send sensor data as bytes Wire.write(sensorValue >> 8); // High byte Wire.write(sensorValue & 0xFF); // Low byte digitalWrite(LED_BUILTIN, LOW); Serial.print("Sent value: "); Serial.println(sensorValue); } void loop() { sensorValue = analogRead(A0); // Update sensor reading delay(100); } ``` -------------------------------- ### Initialize I2C Slave (C++) Source: https://context7.com/paulstoffregen/wire/llms.txt Initialize the Wire library as a slave on address 8 and register callbacks for master requests and incoming data. The onRequest callback sends data to the master; onReceive reads data from the master. Requires #include . ```C++ #include void setup() { Wire.begin(8); // Join I2C bus with address #8 Wire.onRequest(requestEvent); // Register callback for master read requests Wire.onReceive(receiveEvent); // Register callback for master write events Serial.begin(9600); Serial.println("I2C Slave #8 ready"); } void requestEvent() { Wire.write("hello "); // Send 6 bytes when master requests data } void receiveEvent(int numBytes) { while (Wire.available()) { char c = Wire.read(); Serial.print(c); } Serial.println(); } void loop() { delay(100); } ``` -------------------------------- ### Initialize I2C Master (C++) Source: https://context7.com/paulstoffregen/wire/llms.txt Initialize the Wire library in master mode and optionally set the I2C clock speed to 400 kHz. Requires #include . Once initialized, use master transmit/receive APIs in loop. On some boards, multiple buses are available as Wire1, Wire2, etc. ```C++ #include void setup() { Wire.begin(); // Initialize as master Wire.setClock(400000); // Set I2C frequency to 400kHz (optional) Serial.begin(9600); Serial.println("I2C Master initialized"); } void loop() { // Master operations go here } ``` -------------------------------- ### Slave Receive with onReceive Callback in C++ Wire Library Source: https://context7.com/paulstoffregen/wire/llms.txt This code sets up an Arduino/Teensy as an I2C slave that registers an onReceive callback to process incoming data from a master. It depends on the Wire and Serial libraries. Input includes bytes sent from the I2C master. Output is serial print of received data and LED toggle. Limitations: Requires an LED on pin LED_BUILTIN and assumes Serial connection. ```cpp #include int ledPin = LED_BUILTIN; void setup() { pinMode(ledPin, OUTPUT); Wire.begin(9); // Join I2C bus as slave #9 Wire.onReceive(receiveEvent); // Register callback Serial.begin(9600); } void receiveEvent(int numBytes) { digitalWrite(ledPin, HIGH); Serial.print("Received "); Serial.print(numBytes); Serial.print(" bytes: "); while (Wire.available() > 1) { // Process all but last byte char c = Wire.read(); Serial.print(c); } int finalByte = Wire.read(); // Last byte as integer Serial.println(finalByte); digitalWrite(ledPin, LOW); } void loop() { delay(100); } ``` -------------------------------- ### Configuring I2C Clock Speed for Fast Mode in C++ Source: https://context7.com/paulstoffregen/wire/llms.txt This snippet demonstrates setting the I2C clock to 400kHz fast mode using the Wire library on Arduino/Teensy. It initializes the Wire interface, performs a sample transaction to an I2C device at address 0x50, and measures the duration via serial output. Requires Wire.h and Serial libraries; inputs are clock frequency and device address, outputs transaction timing; limited to single bus without multi-master support. ```cpp #include void setup() { Wire.begin(); // Set clock speed (default is typically 100kHz) Wire.setClock(400000); // 400kHz Fast Mode // Wire.setClock(100000); // 100kHz Standard Mode Serial.begin(9600); Serial.print("I2C clock set to 400kHz"); } void loop() { // High-speed I2C operations unsigned long startTime = micros(); Wire.beginTransmission(0x50); Wire.write(0x00); Wire.endTransmission(); Wire.requestFrom(0x50, 16); while (Wire.available()) { Wire.read(); } unsigned long duration = micros() - startTime; Serial.print("Transaction time: "); Serial.print(duration); Serial.println(" microseconds"); delay(1000); } ``` -------------------------------- ### Master Receive from Slave (C++) Source: https://context7.com/paulstoffregen/wire/llms.txt Request and read data from a slave using requestFrom and read. requestFrom returns the number of bytes actually received. Use Wire.available() and Wire.read() to consume incoming bytes. Requires #include . ```C++ #include void setup() { Wire.begin(); // Initialize as master Serial.begin(9600); } void loop() { uint8_t bytesReceived = Wire.requestFrom(8, 6); // Request 6 bytes from device #8 Serial.print("Received "); Serial.print(bytesReceived); Serial.print(" bytes: "); while (Wire.available()) { // Read all available bytes char c = Wire.read(); Serial.print(c); } Serial.println(); delay(500); } ``` -------------------------------- ### I2C Bus Scanning in C++ Wire Library Source: https://context7.com/paulstoffregen/wire/llms.txt This snippet scans all possible I2C addresses (1-126) to detect connected devices on the bus. It requires the Wire and Serial libraries. Input is none; it initiates transmissions. Output is serial list of found devices or errors. Limitations: May produce false positives; designed for debugging and assumes Serial monitor availability. ```cpp #include void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); Serial.println("\nI2C Scanner"); } void loop() { int devicesFound = 0; Serial.println("Scanning..."); for (int address = 1; address < 127; address++) { Wire.beginTransmission(address); int error = Wire.endTransmission(); if (error == 0) { Serial.print("Device found at 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); devicesFound++; } else if (error == 4) { Serial.print("Unknown error at 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (devicesFound == 0) { Serial.println("No I2C devices found"); } else { Serial.print("Found "); Serial.print(devicesFound); Serial.println(" device(s)"); } delay(5000); // Wait 5 seconds before next scan } ``` -------------------------------- ### Digital Potentiometer Control in C++ Wire Library Source: https://context7.com/paulstoffregen/wire/llms.txt This code controls an AD5171 I2C digital potentiometer by sending command and value bytes to set resistance values. It depends on the Wire and Serial libraries. Input is potentiometer values (0-63). Output is serial confirmation of set value or error code. Limitations: Specific to AD5171 address (0x2C); assumes device is connected. ```cpp #include void setup() { Wire.begin(); // Initialize as master Serial.begin(9600); } void loop() { for (int potValue = 0; potValue < 64; potValue++) { Wire.beginTransmission(0x2C); // AD5171 address (44 decimal) Wire.write(0x00); // Instruction byte (write to RDAC) Wire.write(potValue); // Potentiometer value (0-63) uint8_t error = Wire.endTransmission(); if (error == 0) { Serial.print("Set potentiometer to: "); Serial.println(potValue); } else { Serial.print("Error setting value: "); Serial.println(error); } delay(500); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.