-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI2C_Slave.ino
More file actions
22 lines (17 loc) · 818 Bytes
/
I2C_Slave.ino
File metadata and controls
22 lines (17 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <Wire.h> // Include the I2C library
int transmit_value = 0; // Value to send to I2C master
void transmit(int x); // Forward declaration of the transmit callback (optional)
void setup() {
Wire.begin(8); // Initialize I2C as a slave with address 8
Wire.onRequest(transmit); // Register function to call when data is requested
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
delay(500); // Wait for 500 milliseconds
transmit_value++; // Increment value to be sent
Serial.println(transmit_value); // Print current value to Serial Monitor
}
// Callback function that runs when master requests data
void transmit(int x) {
Wire.write(transmit_value); // Send the current value to the master
}