如果继电器是arduino上的NC,则超声波传感器不起作用

时间:2018-05-21 22:47:23

标签: c++ arduino sensor

通常情况下,超声波传感器(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);   
}

已编辑:超声波传感器在继电器闭合时完全停止工作,但此传感器未连接到继电器

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

如果没有原理图检查,将继电器引脚拉至低电平即可开启 您正在寻找继电器电流" thru"继电器1引脚接地。

如果是这样,这是一个很大的不可。

小型继电器在开启时可能具有高达100-150mA的浪涌电流。大型继电器甚至更多。

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

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

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

如果继电器线圈中没有阻塞二极管,则不要在电路中使用。

在整个网络上都有这方面的原理图。

从您的原理图中我的猜测是正确的。您正通过微型引脚拉动继电器电流。

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

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

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

任何通用NPN晶体管都适用于此。只需检查引脚排线即可。

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