How to use HC-06 Bluetooth module, UNO board, MQ-5 smoke sensor, ultrasonic sensor, steering gear, buzzer, infrared tracking sensor to realize the infrared sensor to detect the lock of the door, if the lock, the window is closed; If the fire occurs when the air concentration is too high, the smoke sensor senses and controls the steering gear to open the window; When someone is too close to the window for a period of time, the buzzer alarms for a period of time and the Bluetooth module can be used to control the buzzer alarm time with the phone
Hello sunjiehao
Welcome to the worldbest forum.
This is a nice project to get started.
Keep it simple and stupid firstly.
Follow the example code that comes with the library or
run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.
Have a nice day and enjoy coding in C++.
Here is the code
#include <SoftwareSerial.h>
#include <Servo.h>
// Define pins
const int smokeSensorPin = A0;
const int ultrasonicTrigPin = 8;
const int ultrasonicEchoPin = 9;
const int infraredSensorPin = 7;
const int servoPin = 3;
const int buzzerPin = 4;
// Define variables
SoftwareSerial bluetooth(0, 1); // RX, TX
Servo servo;
int smokeThreshold = 400; // Adjust based on sensor sensitivity
int ultrasonicDistanceThreshold = 30; // Adjust as needed
int infraredSensorValue;
int buzzerTime = 5000; // Default buzzer alarm time (in milliseconds)
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
pinMode(smokeSensorPin, INPUT);
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
pinMode(infraredSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
servo.attach(servoPin);
servo.write(0); // Close window initially
}
void loop() {
// Read sensor values
int smokeLevel = analogRead(smokeSensorPin);
int ultrasonicDistance = measureDistance();
infraredSensorValue = digitalRead(infraredSensorPin);
// Door lock detection and window control
if (infraredSensorValue == LOW) { // Door locked
servo.write(0); // Close window
}
// Fire detection and window opening
if (smokeLevel > smokeThreshold) {
servo.write(90); // Open window
}
// Proximity detection and buzzer alarm
if (ultrasonicDistance < ultrasonicDistanceThreshold) {
digitalWrite(buzzerPin, HIGH);
delay(buzzerTime);
digitalWrite(buzzerPin, LOW);
}
// Bluetooth control for buzzer alarm time
if (bluetooth.available()) {
int newBuzzerTime = bluetooth.parseInt();
if (newBuzzerTime > 0) {
buzzerTime = newBuzzerTime;
Serial.println("Buzzer time set to:");
Serial.println(buzzerTime);
}
}
}
int measureDistance() {
// Send ultrasonic pulse
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrigPin, LOW);
// Read echo pulse duration and calculate distance
long duration = pulseIn(ultrasonicEchoPin, HIGH);
int distance = duration * 0.034 / 2; // Convert to centimeters
return distance;
}
I've done something similar like this before. You may take this as a reference.
I have labelled the function of each sensor. @sunjiehao
Thank you
If this works, please mark my reply as a solution.
Thanks
Post #3 has the fingerprints of ChatGPT.
I tried it, but it doesn't seem to work
I have a few issues running this program
-
When the infrared sensor senses and the window closes, the smoke alarm cannot operate if it wants to open the window
-
The buzzer is not controlled by the ultrasonic sensor and will sound continuously at a delay interval after being powered on
-
The buzzer sound is too low
Just to clarify, the buzzer I am using is controlled by a low level
Does SoftwareSerial on Uno's Hardware Serial pins work?
It doesn't work, so I redefined the pins
Can you help me ?
Hello @sunjiehao - Your projects are very interesting, involving many devices.
Okay... when you fix a major part of your sketch, post the NEW sketch for others to analyze and help you more. I assume the bluetooth part connects to your project to your browser.
How is your sketch working now? What do you expect, and what happens? From this, we can fix a one piece at a time.
By the way...
The "sketch" posted by yh_team was not written by yh_team, who can barely write a Blink sketch, takes up ten real people's time and tags himself as the solution. Most likely the sketch was from ChatGPT, which is useless.
Maybe @anon65951220 has left the internet or ChatGPT refuses to fix its own garbage.
I only changed the (0,1) pin to the (10,11) pin, and I didn't change the rest, but it doesn't seem to work, and the reality doesn't go the way I envisioned.
Okay... so you should post your new code like this... using the < CODE > button in the message box...
#include <SoftwareSerial.h>
#include <Servo.h>
// Define pins
const int smokeSensorPin = A0;
const int ultrasonicTrigPin = 8;
const int ultrasonicEchoPin = 9;
const int infraredSensorPin = 7;
const int servoPin = 3;
const int buzzerPin = 4;
// Define variables
// SoftwareSerial bluetooth(0, 1); // RX, TX
SoftwareSerial bluetooth(10, 11); // RX, TX // CHANGED TO 10, 11
Servo servo;
int smokeThreshold = 400; // Adjust based on sensor sensitivity
int ultrasonicDistanceThreshold = 30; // Adjust as needed
int infraredSensorValue;
int buzzerTime = 5000; // Default buzzer alarm time (in milliseconds)
void setup() {
Serial.begin(9600);
bluetooth.begin(9600);
pinMode(smokeSensorPin, INPUT);
pinMode(ultrasonicTrigPin, OUTPUT);
pinMode(ultrasonicEchoPin, INPUT);
pinMode(infraredSensorPin, INPUT);
pinMode(buzzerPin, OUTPUT);
servo.attach(servoPin);
servo.write(0); // Close window initially
}
void loop() {
// Read sensor values
int smokeLevel = analogRead(smokeSensorPin);
int ultrasonicDistance = measureDistance();
infraredSensorValue = digitalRead(infraredSensorPin);
// Door lock detection and window control
if (infraredSensorValue == LOW) { // Door locked
servo.write(0); // Close window
}
// Fire detection and window opening
if (smokeLevel > smokeThreshold) {
servo.write(90); // Open window
}
// Proximity detection and buzzer alarm
if (ultrasonicDistance < ultrasonicDistanceThreshold) {
digitalWrite(buzzerPin, HIGH);
delay(buzzerTime);
digitalWrite(buzzerPin, LOW);
}
// Bluetooth control for buzzer alarm time
if (bluetooth.available()) {
int newBuzzerTime = bluetooth.parseInt();
if (newBuzzerTime > 0) {
buzzerTime = newBuzzerTime;
Serial.println("Buzzer time set to:");
Serial.println(buzzerTime);
}
}
}
int measureDistance() {
// Send ultrasonic pulse
digitalWrite(ultrasonicTrigPin, LOW);
delayMicroseconds(2);
digitalWrite(ultrasonicTrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(ultrasonicTrigPin, LOW);
// Read echo pulse duration and calculate distance
long duration = pulseIn(ultrasonicEchoPin, HIGH);
int distance = duration * 0.034 / 2; // Convert to centimeters
return distance;
}
... and describe the small steps in how you want it to work.
I know what you mean, but there are many problems when I run this code. First, after I input the program, the steering gear will complete a rotation and return position by itself. I don't know if there is a problem with the size of my power input, which leads to the normal rotation of the steering gear. Second, when I run the code of the infrared sensor part, if I want to run the code of the smoke sensor part, there will be a conflict; Third, my ultrasonic sensor can not control the sound of the buzzer, and it can not meet the requirements that I want to detect anti-theft. The fourth Bluetooth module and the communication part of the mobile phone are my weakest link, which can not normally use the mobile phone to carry out the requirements I described. So, this code doesn't work as I envisioned, and I feel no further development is necessary
Start small, and get each part of the project working, before adding another feature.
To open and close the window would be a good first goal, as that is the main point.
What device do you use to move the "steering gear"? DC motor control? Stepper motor control? Servo motor?
Servo
Is the servo powerful enough to open and close the window?
If so, can you do that under program control yet?