使用arduino模拟输入

使用arduino模拟输入,arduino,Arduino,我正在UNO r3上创建我的第一个Arduino项目。我以前玩过Arduino Uno,只是玩过一些小的示例程序等。我使用两个模拟输入,使用2个0-5vdc缩放的激光传感器来感应距离。这两个输入为0-5vdc,我确保了整个系统的公共接地。这两个传感器分别命名为left和right,分别输入到A0和A1。我还有一个差分电位器,它使用10K欧姆电位器雨刮器电压作为A2上的输入。该程序的原理是获取左右激光器之间输入电压差的绝对值,然后确定结果是否大于或等于电位计刮片针脚A2上的电压。根据计算结果,通过

我正在UNO r3上创建我的第一个Arduino项目。我以前玩过Arduino Uno,只是玩过一些小的示例程序等。我使用两个模拟输入,使用2个0-5vdc缩放的激光传感器来感应距离。这两个输入为0-5vdc,我确保了整个系统的公共接地。这两个传感器分别命名为left和right,分别输入到A0和A1。我还有一个差分电位器,它使用10K欧姆电位器雨刮器电压作为A2上的输入。该程序的原理是获取左右激光器之间输入电压差的绝对值,然后确定结果是否大于或等于电位计刮片针脚A2上的电压。根据计算结果,通过晶体管驱动电路打开或关闭插脚D13的继电器

问题:我无法在引脚A0、A1或A2的刻度(0-1023)上实现电压的精确变化。我已使用串行监视器诊断此问题。不确定问题出在哪里,任何帮助都会很好。此外,我无法在上述任何模拟引脚上实现0值,即使是电位计

这是我的密码:

    const int lf_dist = A0;          //names A0
    const int rt_dist = A1;          //names A1
    const int differential = A2;     //names A2
    const int relay = 13;            // select the pin for the relay coil
    unsigned int left = 0;              // variable to store the value coming from the left    sensor
    unsigned int right = 0;             // variable to store the value coming from the right sensor
    unsigned int diff = 0;              // variable to store the value coming from the   differential POT for maximum distance differential
    unsigned int offset = 0;            // variable that stores the value between the two laser sensors

void setup() {
  Serial.begin(9600); 
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(relay, OUTPUT);              // declare the relay pin as an OUTPUT: 
  analogReference(DEFAULT); 
}

void loop() 
{

  unsigned int left = 0;              // variable to store the value coming from the left sensor
  unsigned int right = 0;             // variable to store the value coming from the right sensor
  unsigned int diff = 0;              // variable to store the value coming from the differential POT for maximum distance differential
  unsigned int offset = 0;            // variable that stores the value between the two laser sensors



  left = analogRead(A0);          // read the value from the left laser  
  delay(5);
  right = analogRead(A1);         // read the value from the right sensor
  delay(5);
  diff  = analogRead(A2);    // read the value from the differential POT
  delay(5);

  offset = abs(left - right);

  if(offset >= diff)                   // does math to check if left and right distances are greater than the value clocked in by the differential POT
  {
    digitalWrite(relay, LOW);          // turns off the relay, opens the stop circuit, and turns on the yellow light
  }
  else
  {
    digitalWrite(relay, HIGH);        // turns on the relay if all is good, and that keeps the machine running      
  }
    Serial.print("\n left = " );                       
    Serial.print(left);
    Serial.print("\n right = " );                       
    Serial.print(right);
    Serial.print("\n differential = " );                       
    Serial.print(diff); 
    delay(1000);
 }

afaict,这应该是由于测量销周围的浮动销具有不稳定的值,因此会干扰测量。您应该使用查看您的值,这将显示其他浮动销对测量销的干扰影响


解决这个问题的简单方法是将所有不使用的模拟输入引脚接地,并在两个输入之间尽可能多地留出空间,这样它们就不会相互干扰。

我意识到这个线程有点陈旧,但可能这会对某些人有所帮助。如果您仅用5V为Arduino供电,就像您所说的使用稳压器一样,您将获得非常不稳定的行为,尤其是模拟引脚。这是因为您将开始使提供AREF、3.3和5.0输出的内部电压调节器变为棕色。我在一个机器人项目中测试了这个,在6.5伏左右,一切都开始出问题。我想如果你总是提供5.0的输入电压,你可以补偿这种影响,但在我的例子中,我使用了一个LiPo电池,电压范围从8.4伏到6.0伏,在6.5伏的电压下,一切都会变得疯狂。

从电位计采样时,arduino吸收的最小电流不应干扰雨刮器的实际开路输入电压。

在上拉模式下初始化管脚,以避免垃圾值或“浮动”管脚,或在管脚处使用您自己的下拉/上拉电阻器:)

哪个电压你想测量的水平是多少?由于Arduino能够在模拟引脚上给出1023个点,理论上可以测量的最小值为5V/1023=0.0049V。但是如果没有电压,模拟引脚会浮动,你会得到随机值。激光传感器的输出是什么,最小值和最大值?他们真的能输出0到5伏电压吗?您是否在一个小型测试程序中对它们进行了单独测试,以了解输出的距离?您是否尝试过增加读取之间的延迟,例如20ms?可能是SAR ADC未稳定。你试过在模拟输入上启用内部上拉吗?Felice,我只需要0.01V左右的精度,但是,精度越高越好。我确实意识到10位a-D的准确性谢谢。user2019047,激光输出0-10V。我有传感器的输出通过一个分压器,这反过来给我0-5v。我用一个仪表测量模拟输入引脚上的电压。那里一切都好。只是无法使a-d与我的万用表读数相关联。zmo,我已尝试将未使用的每个模拟管脚接地。还是一样的问题。我还想补充一点,我通过电压调节器电路通过Vin和Gnd引脚为我的项目供电,电压调节器电路使用to-220封装的电压调节器,具有5伏输出和24伏输入。我有足够大小的帽子在里面和外面,地面都是粘合的。你认为电源可能会导致引脚上的奇怪读数,因为它安装在我的arduino模拟引脚附近。或者,有没有办法在读数之间清除模拟引脚结果寄存器?您是否尝试使用arduinoscope读取输入,以查看所有输入的质量?这没什么可做的,而且常常是有启发性的。也许你的GND是浮动的,或者得到的信号不是你期望的那样。。。或者你的TO220可能有问题。。。我真的不知道。但是arduinoscope会告诉你出了什么问题。我不是电子领域的大师,但根据我的经验,有一件事我可以肯定:硬件故障的方式比正常工作的方式多得多:-sI今天将尝试arduinoscope。以前从未使用过它,但我确实使用标准桌面示波器检查输入。谢谢,我会告诉你我发现了什么。我今天又看了一下我的线路。在这里,我焊接到引脚A3 A4和A5,而不是A0A1和A2。我为我可能给你们带来的问题感到抱歉。愚蠢的接线错误。我有我的arduino安装正面朝下的原型“鲍勃”通过一套男性头销。谢谢大家,这是一次很好的学习锻炼。不客气!如果你认为我的帖子对你有帮助,请毫不犹豫地接受它;-)