该系统模块主要由Arduino UNO R3开发板+MQ-2烟雾浓度传感器+火焰传感器+舵机+无源蜂鸣器+风扇+步进电机+WIFI模块+RGB三色LED灯+SIM900A所组成,MQ-2烟雾浓度传感器达到不同的阈值的时候,LED灯会通过不同的颜色来进行警示。烟雾浓度增大,LED灯依次显示绿、蓝、红,且步进电机会驱动风扇进行不同挡位转速旋转,蜂鸣器会进行警报提示。烟雾浓度超过最大的阈值,或者检测到有火焰产生,红灯亮起,蜂鸣器鸣响,步进电机以最大的转速驱动风扇旋转,SIM900A会给指定用户拨打电话。
因为该模块有WiFi模块,也可以通过连接WiFi通过APP进行控制步进电机的转速驱动,也可以实现手动的控制风扇转速。
一、设备准备
Arduino UNO R3Arduino UNO R3开发板+MQ-2烟雾浓度传感器+舵机+无源蜂鸣器+风扇+步进电机+WIFI模块+RGB三色LED灯+SIM900A所构成。MQ-2烟雾浓度传感器实时检测室内烟雾浓度数值,若超过阈值,风扇进行转动
WIFI模块
MQ-2烟雾浓度传感器
火焰传感器模块
舵机
无源蜂鸣器
风扇
RGB三色LED灯
步进电机
SIM900A
二、设备连接
烟雾浓度传感器
烟雾浓度传感器 | Arduino UNO R3 |
---|---|
VCC | 5V |
GND | GND |
DO | P7 |
AO | A0 |
火焰传感器
火焰传感器 | Arduino UNO R3 |
---|---|
DO | P2 |
GND | GND |
VCC | 5V |
无源蜂鸣器
无源蜂鸣器 | Arduino UNO R3 |
---|---|
GND | GND |
I/O | P8 |
VCC | 5V |
三色LED灯
三色LED灯 | Arduino UNO R3 |
---|---|
R | P11 |
G | P12 |
B | P13 |
GND | GND |
步进电机
步进电机 | Arduino UNO R3 |
---|---|
IN3 | P9 |
IN4 | P4 |
+ | 5V |
- | GND |
风扇
风扇 | 步进电机 |
---|---|
红线 | B灯处 |
黑线 | C灯处 |
WIFI模块(局域模式)
WIFI模块 | Arduino UNO R3 |
---|---|
RX | TX |
TX | RX |
GND | GND |
5V | 5V |
SIM900A模块
SIM900A | Arduino UNO R3 |
---|---|
5VR | TX |
5VT | RX |
GND | GND |
VCC5 | 5V |
GND | GND |
舵机模块
舵机 | Arduino UNO R3 |
---|---|
黄线 | P6 |
灰线 | GND |
红线 | 5V |
三、配置网络
将代码下载到Arduino UNO R3开发板上
WIFI模块需要调节到局域模式,此时打开手机wifi热点,即可检测到WiFi模块所组成的网络,,进行连接。
APP下载路径,打开下载安装好的手机APP
之后,点击右上角的连接
连接成功之后,即可点击 开锁(49)、加速、减速、蜂鸣器开(50)、蜂鸣器关(51)、一档、二档、三档 可以对舵机、蜂鸣器以及风扇进行控制。
四、完整代码
/******************************************************* connection:* MQ-2 gas sensor Uno R3* D0 7* A0 A0* GND GND* VCC 5V* * * Dual-color LED Arduin Uno R3* R 11* G 12* B 13* GND GND* * * buzzerPin 8* GND GND* VCC 5V* * * motorIn1 = 9;* motorIn2 = 10;* *******************************************************/
#include <Servo.h> //Arduino IDE自带的舵机库文件
const int Servo_Pin = 6; //模拟开门
Servo myservo; //定义舵机对象myservoconst int digitalInPin = 2; //D0 attach to digital 2const int analogPin=A0; //the DO on the Module attach to Pin7 on the SunFounder
const int digitalPin=7; //the D0 attach to pin7
int Astate=0;
boolean Dstate=0;int redPin = 11; // select the pin for the red LED
int greenPin = 12;
int bluePin = 13; int buzzerPin = 8;//the pin of the active buzzer attach to pin8
int comdata;
const int motorIn1 = 9;
const int motorIn2 = 4;
#define rank1 150
#define rank2 200
#define rank3 250void set_tele(void)
{Serial.begin(115200); //波特率为115200delay(1000);Serial.println("AT\r\n"); //判断模块是否正常delay(1000);
}
void tele(void)
{Serial.println("AT+CPIN?\r\n"); delay(1000);Serial.println("AT+CSQ\r\n"); delay(3000);Serial.print("AT+CREG?\r\n"); delay(3000);Serial.println("AT+CGATT?\r\n"); delay(1000);Serial.println("ATD15336551683;\r\n"); delay(30000);
}void setup()
{pinMode(digitalInPin,INPUT);//set the pins state pinMode (digitalPin,INPUT);pinMode(redPin, OUTPUT); //set redPin as OUTPUTpinMode(greenPin, OUTPUT);pinMode(bluePin, OUTPUT);pinMode(buzzerPin,OUTPUT);//set the buzzer as as OUTPUTdigitalWrite(buzzerPin,LOW);//initialize the buzzerPin as LOW levelpinMode(motorIn1,OUTPUT);pinMode(motorIn2,OUTPUT);pinMode(Servo_Pin, OUTPUT); //初始化舵机为输出模式myservo.attach(Servo_Pin); //设置舵机控制引脚为6myservo.write(0); //初始化舵机位置0set_tele();Serial.begin(9600);
}
void loop()
{ boolean stat = digitalRead(digitalInPin);Serial.print("D0: "); Serial.println(stat); // print to serial monitor Serial.println(" ");Astate=analogRead(analogPin);//read the value of A0Serial.println(Astate); //peint if( stat == LOW){analogWrite(motorIn1,0);analogWrite(motorIn2,rank3);analogWrite(redPin, 255); //redanalogWrite(greenPin, 0);analogWrite(bluePin, 0);//digitalWrite(buzzerPin,LOW);myservo.write(90); //舵机旋转至90度delay(5000); // 延时3000msmyservo.write(0);//舵机旋转至0度delay(1000); // 延时3000msfor(int i = 200;i <= 500;i++) //frequence loop from 200 to 800{tone(8,i); //in pin7 generate a tone,it frequence is idelay(2); //wait for 5 milliseconds }delay(1000); //wait for 4 seconds on highest frequencefor(int i = 500;i >= 200;i--) //frequence loop from 800 downto 200{tone(8,i); //in pin7 generate a tone,it frequence is idelay(2); //delay 10ms}tele();}if((Astate>120 && Astate<150)){analogWrite(motorIn1,0);analogWrite(motorIn2,rank1);analogWrite(redPin, 0); //redanalogWrite(greenPin, 255);analogWrite(bluePin, 0);//digitalWrite(buzzerPin,LOW);myservo.write(90); //舵机旋转至90度delay(5000); // 延时3000msmyservo.write(0);//舵机旋转至0度delay(1000); // 延时3000msfor(int i = 200;i <= 500;i++) //frequence loop from 200 to 800{tone(8,i); //in pin7 generate a tone,it frequence is idelay(10); //wait for 5 milliseconds }delay(4000); //wait for 4 seconds on highest frequencefor(int i = 500;i >= 200;i--) //frequence loop from 800 downto 200{tone(8,i); //in pin7 generate a tone,it frequence is idelay(10); //delay 10ms}}else if((Astate>=150 && Astate<200)){analogWrite(motorIn1,0);analogWrite(motorIn2,rank2);analogWrite(redPin, 0); //redanalogWrite(greenPin, 0);analogWrite(bluePin, 255);//digitalWrite(buzzerPin,LOW);myservo.write(90); //舵机旋转至90度delay(5000); // 延时3000msmyservo.write(0);//舵机旋转至0度delay(1000); // 延时3000msfor(int i = 200;i <= 500;i++) //frequence loop from 200 to 800{tone(8,i); //in pin7 generate a tone,it frequence is idelay(5); //wait for 5 milliseconds }delay(2000); //wait for 4 seconds on highest frequencefor(int i = 500;i >= 200;i--) //frequence loop from 800 downto 200{tone(8,i); //in pin7 generate a tone,it frequence is idelay(5); //delay 10ms}}else if((Astate>=200)){analogWrite(motorIn1,0);analogWrite(motorIn2,rank3);analogWrite(redPin, 255); //redanalogWrite(greenPin, 0);analogWrite(bluePin, 0);//digitalWrite(buzzerPin,LOW);myservo.write(90); //舵机旋转至90度delay(5000); // 延时3000msmyservo.write(0);//舵机旋转至0度delay(1000); // 延时3000msfor(int i = 200;i <= 500;i++) //frequence loop from 200 to 800{tone(8,i); //in pin7 generate a tone,it frequence is idelay(2); //wait for 5 milliseconds }delay(1000); //wait for 4 seconds on highest frequencefor(int i = 500;i >= 200;i--) //frequence loop from 800 downto 200{tone(8,i); //in pin7 generate a tone,it frequence is idelay(2); //delay 10ms}tele();
/* Serial.print("AT+CMGD=1");Serial.print("\r\n");Serial.print("AT");Serial.print("\r\n");Serial.print("AT+CPIN?");Serial.print("\r\n");Serial.print("AT+CSQ");Serial.print("\r\n");Serial.print("AT+CREG?");Serial.print("\r\n");Serial.print("AT+CGATT?");Serial.print("\r\n");Serial.print("ATD15336551683;");Serial.print("\r\n");
*/ }else{analogWrite(redPin, 0); //redanalogWrite(greenPin, 0);analogWrite(bluePin, 0);//digitalWrite(buzzerPin,HIGH);pinMode(buzzerPin,INPUT);pinMode(motorIn1,0);pinMode(motorIn2,0);}delay(200);//delay 200mswhile (Serial.available()){comdata= Serial.read();if(comdata==49){Serial.print("open the door\n");myservo.write(90); //舵机旋转至90度delay(1000); // 延时3000msmyservo.write(0);//舵机旋转至0度delay(1000); // 延时3000ms}if(comdata==50){Serial.print("open fmq\n");for(int i = 200;i <= 500;i++) //frequence loop from 200 to 800{tone(8,i); //in pin7 generate a tone,it frequence is idelay(5); //wait for 5 milliseconds }delay(4000); //wait for 4 seconds on highest frequencefor(int i = 500;i >= 200;i--) //frequence loop from 800 downto 200{tone(8,i); //in pin7 generate a tone,it frequence is idelay(10); //delay 10ms}delay(100);}if(comdata==51){Serial.print("close fmq\n");pinMode(buzzerPin,INPUT);delay(100);}if(comdata==52){Serial.print("111\n");analogWrite(motorIn1,0);analogWrite(motorIn2,rank1);delay(100);}if(comdata==74){Serial.print("222\n");analogWrite(motorIn1,0);analogWrite(motorIn2,rank2);delay(100);}if(comdata==54){Serial.print("333\n");analogWrite(motorIn1,0);analogWrite(motorIn2,rank3);delay(100);}}
}
五、演示视频
B站演示视频
CSDN演示视频