如果继电器在 arduino 上是常闭的,超声波传感器不起作用



通常超声波传感器(HC-SR04传感器(按预期工作,但是当我关闭继电器时,它停止工作。

继电器用于另一项工作,尚无与ping传感器相关的工作。

这是代码:

/*
* created by Rui Santos, http://randomnerdtutorials.com
* 
* Complete Guide for Ultrasonic Sensor HC-SR04
*
Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) - Pin11
Echo: Echo (OUTPUT) - Pin 12
GND: GND
*/
#define relay1 10
int trigPin = 11;    //Trig - green Jumper
int echoPin = 12;    //Echo - yellow Jumper
long duration, cm;
String cmd;
void setup() {
//Serial Port begin
Serial.begin (9600);
Serial.println("Initializing Relay...");
pinMode(relay1, OUTPUT);   
Serial.println("Initializing Ultrasonic sensor...");
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{ 
if (Serial.available() > 0) {
// read the incoming byte:
cmd = Serial.readString();
if (cmd == "relayOn"){
//relay normally closed - ultrasonic sensor stop working here
digitalWrite(relay1, LOW);       
}else if (cmd == "relayOff"){
//relay normally open
digitalWrite(relay1, HIGH);
}
}    
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = (duration/2) / 29.1;
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);   
}

已编辑:当继电器闭合时,超声波传感器正好停止工作,但该传感器未连接到继电器

使用不同的电源为继电器供电(将两个电源的 Gnd 连接在一起(,过载可能会导致 Arduino 出现故障。

你的逻辑说电路是问题,而不是代码。

在没有原理图检查的情况下,将继电器引脚拉低以打开看起来像 您正在将继电器电流"通过"继电器1引脚"接地。

如果是这样,这是一个很大的禁忌。

一个小继电器在接通时可以有高达100-150mA的浪涌电流。 大型继电器更是如此。

Arduino引脚每个引脚最多只能处理40mA。

这要么将其锁定,要么最终烧坏引脚或微型本身。

将NPN晶体管从接地到继电器的末端,并使用晶体管基极的HIGH将其打开。 这样,引脚仅使用几mA电流,继电器电流通过晶体管。

如果阻断二极管尚未在电路中,请勿在继电器线圈上安装阻断二极管。

网络上到处都有这方面的原理图。

从你的示意图来看,我的猜测是对的。 您正在通过微型引脚拉动继电器电流。

看看 https://www.electroschematics.com/wp-content/uploads/2013/07/arduino-control-relay-schematic.png 1K不是,晶体管基座可以放在任何你想要的数字引脚上,引脚2只是一个例子。

在继电器线圈上包括二极管,它会阻止可能造成损坏的反激电压。

晶体管将通过继电器电流并将微电流与该电流隔离,并停止锁定。

任何通用NPN晶体管都可以在这里工作。 只需检查引脚排列的接线。

哦,顺便说一下;使用晶体管时,您的 Relay1 引脚逻辑将反转,然后 HIGH 将打开。

最新更新