手机蓝牙控制继电器实验

手机蓝牙控制继电器实验

实验现象

利用手机无线开关继电器

理论学习

电磁继电器是继电器中应用最早、最广泛的一种继电器。电磁继电器一般由铁心、电磁线圈、衔铁、复位弹簧、触点、支座及引脚等组成。电磁继电器是一种电子控制器件,它具有控制系统(又称输入回路)和被控制系统(又称输出回路),通常应用于自动控制电路中,它实际上是用较小的电流。较低的电压去控制较大电流。较高的电压的一种“自动开关”。故在电路中起着自动调节、安全保护、转换电路等作用
在这里插入图片描述
继电器一般是低压控制高压的一种装置,一般需要通过三极管隔离驱动,实验模块使用NPN的三极管驱动。当控制引脚给高电平时,继电器常开端吸合/D2点亮;当控制引脚给低电平的时,继电器常开端断开/D2熄灭

原理图

在这里插入图片描述

代码编写

arduino UNO R3板子代码:

//arduino UNO R3板子代码
String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete
int relay = 2;
void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(9600);//初始化串口设置波特率
  pinMode(13, OUTPUT);
  pinMode(relay, OUTPUT);
}
void loop() {
    
    
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
void serialEvent() {
    
    
  while (Serial.available()) {
    
    
    // get the new byte:
    char inChar = (char)Serial.read();
    if (inChar == 'A' || inChar == 'a') {
    
    
      digitalWrite(relay, HIGH);
    } else if (inChar == 'B' || inChar == 'b') {
    
    
      digitalWrite(relay, LOW);
    }
  }
}

arduino Leonardo板子代码:

//arduino Leonardo板子代码
String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete
int relay = 2;
void setup() {
    
    
  // put your setup code here, to run once:
  Serial1.begin(9600);//初始化串口设置波特率
  pinMode(13, OUTPUT);
  pinMode(relay, OUTPUT);
}
void loop() {
    
    
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
void serialEvent1() {
    
    
  while (Serial1.available()) {
    
    
    // get the new byte:
    char inChar = (char)Serial1.read();
    if (inChar == 'A' || inChar == 'a') {
    
    
      digitalWrite(relay, HIGH);
    } else if (inChar == 'B' || inChar == 'b') {
    
    
      digitalWrite(relay, LOW);
    }
  }
}

猜你喜欢

转载自blog.csdn.net/qq_45671732/article/details/109543940
今日推荐