The circuit in question is designed to interface an Arduino UNO microcontroller with a KY-015 DHT11 temperature and humidity sensor, an MPU-6050 accelerometer and gyroscope module, and two pushbuttons. The circuit is powered by the Arduino's 5V output and includes a 10kΩ pull-up resistor for one of the pushbuttons. The Arduino UNO is programmed to read data from the DHT11 and MPU-6050 sensors and to detect button presses, which trigger immediate sensor readings. The sensor data is then output to the Serial Monitor.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2 // DHT11 data pin connected to digital pin 2
#define DHTTYPE DHT11
#define BUTTON_PIN 3 // Push button connected to digital pin 3
DHT dht(DHTPIN, DHTTYPE);
Adafruit_MPU6050 mpu;
volatile bool buttonPressed = false;
void setup() {
Serial.begin(9600);
dht.begin();
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonISR, FALLING);
}
void loop() {
static unsigned long lastReadTime = 0;
unsigned long currentTime = millis();
if (buttonPressed || (currentTime - lastReadTime >= 2000)) {
readSensors();
lastReadTime = currentTime;
buttonPressed = false;
}
}
void readSensors() {
// Read DHT11 sensor
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Read MPU6050 sensor
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Print sensor data to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C, Humidity: ");
Serial.print(humidity);
Serial.print(" %, Acceleration: ");
Serial.print("X: ");
Serial.print(a.acceleration.x);
Serial.print(" m/s^2, Y: ");
Serial.print(a.acceleration.y);
Serial.print(" m/s^2, Z: ");
Serial.print(a.acceleration.z);
Serial.println(" m/s^2");
}
void buttonISR() {
buttonPressed = true;
}
This code initializes the DHT11 and MPU6050 sensors and sets up an interrupt to detect when Pushbutton 1 is pressed. When the button is pressed or every 2 seconds, the readSensors
function is called to read and print the temperature, humidity, and acceleration values to the Serial Monitor.