在这个项目中,将使用蓝牙网络构建家庭照明控制系统。在这个项目中,我们使用蓝牙模块HC-05,Arduino设备,4通道继电器模块等。
硬件要求
- Arduino UNO主板
- 用于连接器Arduino UNO的USB电缆
- 蓝牙模块HC-05
- 4通道继电器模块(5V)
- 跳线电线公线对母线
- 家居灯(灯泡)
- 灯泡架
- 线
- AC 220v/120v家用电器或9v Hi-Walt电池
软件要求
- Arduino软件
Arduino蓝牙模块的工作原理
在这个项目中,使用了四个主要组件:Android智能手机蓝牙应用程序,蓝牙收发器,Arduino设备和4通道继电器模块。
Android应用程序通过单击“开启”按钮将串行数据发送到连接的蓝牙模块HC-05。蓝牙设备从应用程序接收数据并通过蓝牙模块的TX引脚发送到Arduino的RX引脚。Arduino设备读取输入数据并根据其中上传的程序对其进行处理,并将输出生成到 4 通道继电器模块。
当蓝牙应用程序的按钮打开时,它将家庭指示灯设置为ON,当蓝牙应用程序的按钮关闭时,它将家庭指示灯设置为OFF。
使用带有Android应用程序的蓝牙模块,4通道继电器模块编写Arduino程序来控制家庭照明。
String inputs;
#define relay1 2 //connect relay1 to pin 9
#define relay2 3 //connect relay2 to pin 8
#define relay3 4 //connect relay3 to pin 7
#define relay4 5 //connect relay4 to pin 6
#define relay5 6 //connect relay5 to pin 5
#define relay6 7 //connect relay6 to pin 4
#define relay7 8 //connect relay7 to pin 3
#define relay8 9 //connect relay8 to pin 2
void setup(){
Serial.begin(9600); //set rate for communicating with phone
pinMode(relay1, OUTPUT); //set relay1 as an output
pinMode(relay2, OUTPUT); //set relay2 as an output
pinMode(relay3, OUTPUT); //set relay1 as an output
pinMode(relay4, OUTPUT); //set relay2 as an output
pinMode(relay5, OUTPUT); //set relay1 as an output
pinMode(relay6, OUTPUT); //set relay2 as an output
pinMode(relay7, OUTPUT); //set relay1 as an output
pinMode(relay8, OUTPUT); //set relay2 as an output
digitalWrite(relay1, LOW); //switch relay1 off
digitalWrite(relay2, LOW); //switch relay2 off
digitalWrite(relay3, LOW); //switch relay1 off
digitalWrite(relay4, LOW); //switch relay2 off
digitalWrite(relay5, LOW); //switch relay1 off
digitalWrite(relay6, LOW); //switch relay2 off
digitalWrite(relay7, LOW); //switch relay1 off
digitalWrite(relay8, LOW); //switch relay2 off
}
void loop(){
while(Serial.available()){ //check if there are available bytes to read
delay(10); //delay to make it stable
char c = Serial.read(); //conduct a serial read
if (c == '#'){
break; //stop the loop once # is detected after a word
}
inputs += c; //means inputs = inputs + c
}
if (inputs.length() >0){
Serial.println(inputs);
if(inputs == "A"){
digitalWrite(relay1, LOW);
}
else if(inputs == "a"){
digitalWrite(relay1, HIGH);
}
else if(inputs == "B"){
digitalWrite(relay2, LOW);
}
else if(inputs == "b"){
digitalWrite(relay2, HIGH);
}
else if(inputs == "C"){
digitalWrite(relay3, LOW);
}
else if(inputs == "c"){
digitalWrite(relay3, HIGH);
}
else if(inputs == "D"){
digitalWrite(relay4, LOW);
}
else if(inputs == "d"){
digitalWrite(relay4, HIGH);
}
else if(inputs == "E"){
digitalWrite(relay5, LOW);
}
else if(inputs == "e"){
digitalWrite(relay5, HIGH);
}
else if(inputs == "F"){
digitalWrite(relay6, LOW);
}
else if(inputs == "f"){
digitalWrite(relay6, HIGH);
}
else if(inputs == "G"){
digitalWrite(relay7, LOW);
}
else if(inputs == "g"){
digitalWrite(relay7, HIGH);
}
else if(inputs == "H"){
digitalWrite(relay8, LOW);
}
else if(inputs == "h"){
digitalWrite(relay8, HIGH);
}
inputs="";
}
}
使用Arduino USB线编译并将代码上传到Arduino设备。上传代码时不要将其他设备与Arduino连接。
数字电路图
Bluetooth Module HC-05 Arduino UNO
RX --------------------------------> TX
TX --------------------------------> RX
GND -----------------------------> GND
5V --------------------------------> 3.3V
4 Channel Relay Module Arduino UNO
GND ------------------------------> GND
IN1 --------------------------------> Pin 9
IN2 --------------------------------> Pin 8
IN3 --------------------------------> Pin 7
IN4 --------------------------------> Pin 6
VCC --------------------------------> 5V
继电器模块,灯泡和输入电源之间的连接:
- 将继电器模块的公共点(com)与家用灯连接。
- 用电源连接常闭模块(nc)。
- 将剩余的一根家用灯线与电源连接。
以下是Android应用程序的Arduino蓝牙控制器的操作界面 -
输出结果: