使用arduino模拟输入

时间:2013-06-17 15:46:17

标签: arduino

我正在UNO r3上创建我的第一个Arduino程序。我之前使用的是Arduino Uno,只需使用小型示例程序等。我使用两个模拟输入来测量距离,使用2个激光传感器,0-5vdc缩放。这两个输入是0-5vdc,我确保了整个接地。两个传感器左右命名,分别输入A0和A1。我还有一个差分POT,它使用10K欧姆POT抽头电压作为A2的输入。该程序的理论是取左右激光器之间输入电压差的绝对值,然后确定结果是否大于或等于POT抽头的引脚A2上的电压。根据得到的数学运算,通过晶体管驱动电路打开或关闭插在D13引脚上的继电器。

问题:我无法在引脚A0,A1或A2上的刻度(0-1023)上实现电压的准确变化。我已经使用串行监视器来诊断此问题。不确定问题是什么,任何帮助都会很棒。此外,我无法在任何上述模拟引脚上实现0值,即使是POT刮水器!!!

这是我的代码:

    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);
 }

4 个答案:

答案 0 :(得分:2)

afeict,这应该是由于测量引脚周围的浮动引脚,具有不稳定的值,因此扰乱了您的措施。您应该使用arduinoscope查看您的值,这将显示其他浮针对测量引脚的干扰影响。

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

答案 1 :(得分:1)

我意识到这个线程有些陈旧,但也许这会对某人有所帮助。如果你只使用5V为Arduino供电,正如你所说的使用稳压器,你会得到非常不稳定的行为,特别是来自模拟引脚。这是因为您将开始消除提供AREF,3.3和5.0输出的内部稳压器。我已经测试了这个我正在研究的机器人项目,大约6.5伏特,一切都开始出错了。我想如果你总是提供5.0输入电压,你可以补偿这种影响,但在我的情况下,我使用的锂电池电压范围从8.4伏到6.0伏,一切都疯狂到6.5伏。

答案 2 :(得分:0)

arduino在电位器采样过程中吸入的最小电流不应干扰雨刮器的实际开路输入电压。

答案 3 :(得分:-1)

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