/*
Arduino UNO / Mega
├─ LCD1602 I2C → SDA(A4/20), SCL(A5/21), 5V, GND
├─ Button → Pin 2 ↔ GND
├─ PIR → Pin 3, 5V, GND
├─ MQ-135 → A0, 5V, GND
├─ HC-SR04 → TRIG-4, ECHO-5, 5V, GND
├─ Hall → Pin 6, 5V, GND
└─ MPU6050 → SDA(A4/20), SCL(A5/21), 5V, GND
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MPU6050.h>
// LCD1602
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Sensor pinlari
const int buttonPin = 2;
const int pirPin = 3;
const int gasPin = A0;
const int trigPin = 4;
const int echoPin = 5;
const int hallPin = 6;
// Holat o‘zgaruvchilar
int mode = 0;
int lastButtonState = HIGH;
// MPU6050
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(pirPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(hallPin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Multi-Sensor");
delay(2000);
Wire.begin();
mpu.initialize();
}
void loop() {
// Button bosilganda sensor almashadi
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
mode++;
if (mode > 4) mode = 0;
delay(300);
}
lastButtonState = buttonState;
// Tanlangan sensorni ko‘rsatish
switch (mode) {
case 0: showPIR(); break;
case 1: showGas(); break;
case 2: showUltrasonic(); break;
case 3: showHall(); break;
case 4: showMPU6050(); break;
}
}
// ===== PIR =====
void showPIR() {
int pirValue = digitalRead(pirPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Harakat Sensori");
lcd.setCursor(1, 1);
if (pirValue == HIGH) {
lcd.print(" Harakat bor!");
} else {
lcd.print(" Harakat yo'q!");
}
delay(500);
}
// ===== MQ-135 =====
void showGas() {
int gasValue = analogRead(gasPin);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Gaz turi");
lcd.setCursor(2, 1);
if (gasValue > 700) {
lcd.print("CO2 / CO");
} else if (gasValue > 500) {
lcd.print("NH3/Benzol");
} else if (gasValue > 300) {
lcd.print("Spirt/Tutun");
} else {
lcd.print("Havo toza");
}
delay(1000);
}
// ===== Ultrasonic =====
void showUltrasonic() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("Masofa");
lcd.setCursor(5, 1);
lcd.print(distance);
lcd.print(" cm");
delay(500);
}
// ===== Hall sensor =====
void showHall() {
int hallValue = digitalRead(hallPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Magnit Sensor");
lcd.setCursor(1, 1);
if (hallValue == LOW) {
lcd.print("Magnit yaqin");
} else {
lcd.print("Magnit yo'q");
}
delay(500);
}
// ===== MPU6050 =====
void showMPU6050() {
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Tezlanish
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" AX:");
lcd.print(ax / 1000);
lcd.print(" AY:");
lcd.print(ay / 1000);
lcd.setCursor(6, 1);
lcd.print("AZ:");
lcd.print(az / 1000);
delay(1000);
// Gyroskop
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" GX:");
lcd.print(gx / 1000);
lcd.print(" GY:");
lcd.print(gy / 1000);
lcd.setCursor(5, 1);
lcd.print("GZ:");
lcd.print(gz / 1000);
delay(1000);
// Temperatura
int16_t tempRaw = mpu.getTemperature();
float tempC = (tempRaw / 340.0) + 36.53;
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Harorat");
lcd.setCursor(6, 1);
lcd.print(tempC, 1); // 1 kasr joy
lcd.print((char)223); // ° belgisi
lcd.print("C");
delay(1500);
}