3
votes

I am creating my first Arduino program on the UNO r3. I have played with the Arduino Uno before just with petty example programs, etc. I am using two analog inputs to sense distance using 2 laser sensors with 0-5vdc scaling. These two inputs are 0-5vdc and I have ensured common grounding throughout. The two sensors are named left and right and are input to A0 and A1 respectively. I also have a differential POT which uses a 10K ohm POT wiper voltage as an input on A2. The theory of the program is to take the absolute value of the difference in input voltages between the left and right lasers then determine if the result is greater than or equal to the voltage on pin A2 from the POT wiper. Based on the resulting math, turn on or off a relay interposed to pin D13 via a transistor driver circuit.

The PROBLEM: I cannot achieve accurate changes in voltage on the scale (0-1023) on pins A0, A1, or A2. I have utilized the serial monitor to diagnose this problem. Not sure what the problem is, any help would be great. Also, I cannot achieve a 0 value on any of the above analog pins, even the POT wiper!!!

Here's my code:

    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
Which voltage level are you trying to measure? Being Arduino able to give out 1023 points on analog pins, the minimum theoretically you can measure is 5V/1023=0.0049V. But if the voltage is not there the analog pin is floating and you will get random values.FeliceM
What is the output from the laser sensors, min value and max value? Are they actually able to output 0 to 5 volts? Have you tested them individually in a small test program to get a feel for the output with respect to distance?user2019047
Did you try increasing the delay between reads, like 20ms? May be the SAR ADC is not settling. Did you try enabling the internal pull up on the analog inputs?Thanushan
Felice, I only need a precision of about .01V, however, the more precision I have the better. I do realize the accuracy of a 10 bit A-D thank You.GoJo2332
user2019047, The lasers output 0-10V. I have the output of the sensors going through a voltage divider which in turn gives me 0-5v. I have used a meter to measure the voltage on the analog input pins. all good there. just cant get the a-d to coorelate with my multimeter readings.GoJo2332

4 Answers

2
votes

afaict, this should really be due to the floating pins surrounding the measuring pins, having erratic values, hence perturbating your measures. You should look at your values using arduinoscope, which will show you the interfering effects of the other floating pins on your measuring pins.

The easy workaround for this is to ground all analogical input pins you're not using, and put as much space as you can between both your inputs, so they don't interfere with each other.

1
votes

I realize this thread is somewhat old not, but perhaps this will help someone. If you power the Arduino with only 5V, as you say you did with a regulator, you will get very erratic behavior, particularly from the analog pins. This is because you will start to brown out the internal voltage regulators that provide the AREF, 3.3, and 5.0 outputs. I've tested this for a robotics project I'm working on, and right around 6.5 volts, everything begins to go wrong. I suppose if you always provided 5.0 input voltage you could compensate for this effect, but in my case I used a LiPo battery that could range from 8.4 volts down to 6.0 volts, and everything goes crazy at 6.5 volts.

0
votes

The minimum current that arduino sinks in during the sampling from potentiometer should not disturb the actual open input volts at the wiper.

-1
votes

Initialize the pins in pull up mode to avoid garbage values or 'floating' pins or use your own pull down/up resistors at the pins :)