#include <Wire.h> // For I2C communication
#include <SPI.h> // For SPI communication
#define MPU6050_ADDR 0x68 // I2C address for MPU6050 (typically 0x68)
void setup() {
// Start serial communication for debugging
Serial.begin(9600);
// Initialize I2C communication
Wire.begin();
// Initialize SPI communication as master
SPI.begin();
// Set SPI settings: MSB, 8-bit, clock speed of 1 MHz
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
// Wake up the MPU6050 sensor by writing to the power management register
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x6B); // Power management register
Wire.write(0); // Wake up
Wire.endTransmission(true);
}
void loop() {
// Request data from the MPU6050 (simulating I2C)
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x3B); // Register for the first accelerometer data (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU6050_ADDR, 14, true); // Request 14 bytes (6 for accelerometer, 6 for gyroscope, 2 for temperature)
// Read the accelerometer data (ACCEL_X, ACCEL_Y, ACCEL_Z)
int16_t accelX = (Wire.read() << 8) | Wire.read();
int16_t accelY = (Wire.read() << 8) | Wire.read();
int16_t accelZ = (Wire.read() << 8) | Wire.read();
// Send the accelerometer data over SPI to the second Arduino (Slave)
SPI.transfer(accelX >> 8); // Send the high byte of accelX
SPI.transfer(accelX & 0xFF); // Send the low byte of accelX
SPI.transfer(accelY >> 8); // Send the high byte of accelY
SPI.transfer(accelY & 0xFF); // Send the low byte of accelY
SPI.transfer(accelZ >> 8); // Send the high byte of accelZ
SPI.transfer(accelZ & 0xFF); // Send the low byte of accelZ
// Wait before the next loop
delay(1000);
}