#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// MPU6050 I2C address
#define MPU6050_ADDR 0x68
// DS18B20 setup
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature ds18b20(&oneWire);
void setup() {
Serial.begin(115200);
// Start I2C
Wire.begin(); // defaults to SDA = 21, SCL = 22 on ESP32
initMPU6050();
// Start DS18B20
ds18b20.begin();
}
void loop() {
// Read temperature from DS18B20
ds18b20.requestTemperatures();
float temperatureC = ds18b20.getTempCByIndex(0);
Serial.print("DS18B20 Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Read accelerometer and gyroscope data from MPU6050
int16_t ax, ay, az, gx, gy, gz;
readMPU6050(ax, ay, az, gx, gy, gz);
Serial.print("Accel X: "); Serial.print(ax);
Serial.print(" Y: "); Serial.print(ay);
Serial.print(" Z: "); Serial.println(az);
Serial.print("Gyro X: "); Serial.print(gx);
Serial.print(" Y: "); Serial.print(gy);
Serial.print(" Z: "); Serial.println(gz);
Serial.println("----------");
delay(1000);
}
void initMPU6050() {
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
}
void readMPU6050(int16_t &ax, int16_t &ay, int16_t &az, int16_t &gx, int16_t &gy, int16_t &gz) {
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x3B); // starting register for accelerometer readings
Wire.endTransmission(false);
Wire.requestFrom(MPU6050_ADDR, 14, true); // request a total of 14 registers
ax = Wire.read() << 8 | Wire.read();
ay = Wire.read() << 8 | Wire.read();
az = Wire.read() << 8 | Wire.read();
Wire.read(); Wire.read(); // skip temperature registers
gx = Wire.read() << 8 | Wire.read();
gy = Wire.read() << 8 | Wire.read();
gz = Wire.read() << 8 | Wire.read();
}