Reading an analog sensor

We are reading an analog signal from a pressure sensor for our project. How can i convert the result to digital ? the point is , we want to read the sensor value for a robotic arm. If the arm touches the object, we want a digital signal, if not off.. in pseudo code

value= Analogread(sensor)
while ( value=0)
openRobiticArm()

how can i convert the analog value of the sensor to a digital value???

The "analog" value that you get from the sensor is a digital value. An analog sensor returns a range of values - from 0 to 1023, where the value represents the voltage output by the sensor divided by the reference voltage (typically +5V).

A "digital" sensor returns on or off. A switch is either off or on; there are no partly on values possible.

It seems that what you want to do is make some action happen when the value of the analogRead function exceeds, or falls below, some limit value. Those values will need to be experimentally determines. Either that, or I don't understand what you are trying to do.

how can i convert the analog value of the sensor to a digital value

You'll need to define a threshold value, above which a reading is considered to be a binary '1', and below which it is considered to be a binray '0'

The while loop should also use "==" not "=":

While (value == 0)
{...}

Otherwise , I believe the loop is never executing..

Psst, ideeasMan:

in pseudo code

When reading an analogue input, you need to try and get a reliable signal. Here is a bit of code which I had used (for an AVR, but I'm sure that it would be no trouble changing a few things for the Arduino).

To do this in a sumo robot I built, I made a struct for each sensor. If you don't know/use structs, they are worth wild looking into; they make your code very readable. Also, you can look at the same element for each object very quickly using and array of objects and a "for" loop.

struct      // analogue pin information
{
      unsigned char id[32];      // individual name
      unsigned int Active;      // detection flag
      unsigned int CurrentValue;      // last measured value
      unsigned int ThresholdValue;      // turn on value
      unsigned char PastReadings;      // telly of active inputs

} volatile InputStatus[8];

Notice that for this example, I have 8 different analogue inputs. These don't have to be different pins, as multiplexing could lead to different inputs from the same pin.

Using a "for loop" (and after getting each analogue input value), I could then check each sensor.

StartAnalogueIn(i);            //start up pin convertion
                  //& Put Analogue in "InputVal"

InputStatus[i].CurrentValue = InputVal;            //update current value

// If measured value is below trigger value
if(InputStatus[i].CurrentValue <= InputStatus[i].ThresholdValue)//if active input

{
  if (InputStatus[i].PastReadings != 1)                        //And the Past readings is not 0
  {

    InputStatus[i].PastReadings--;                        // subtract from telly
  }

}
else
{

  if (InputStatus[i].PastReadings != 9)                  // if not active and not 20
  {

    InputStatus[i].PastReadings++;                        // add to telly
  }

}


if (InputStatus[i].PastReadings <= 5)                        // if telly is 5 or above 
{
  InputStatus[i].Active = TRUE;                        // signal main program of input
}
else
{
  InputStatus[i].Active = FALSE;                        // else: ignore
}

Note that this set up can handle a few wrong readings without reacting to them!

IA.

thank you very much for all your replies. the project is a robotic arm and we are using pressure . My problem is if i go like (psuedo code again)

analogValue=AnalogRead(sensor)

if (analogValue < maxThreshold && analogValue> maxThreshold)

DigitalSensor=1; // sensor felt there is an object

else
DigitalSensor=0; // still arm hasnt touch the object yet

what about adjusting pressure based on object? for example not to to crush the object or too weak of the grip ... any suggestion?

if (analogValue < maxThreshold && analogValue> maxThreshold)

It can't be both!

It can't be both!

Ah but it's pseudo code, where anything is allowed and useful. :wink:

My bad! BIG typo
I meant
if (analogValue < maxThreshold && analogValue> minThreshold)

any idea?

For a quick fix solution-

Let's say

  • That the position of the robot hand is an integer called "intPosition",
  • That the pressure is an analogue input called "intPressure"
  • That there is a desired pressure "intDesiredPressure"
  • And a Pressure differential "intPressureDifferential"

If(minThreshold < intPressure < maxThreshold && enabled)

intPressureDifferential = intPressure - intDesiredPressure

If(intPressureDifferential < 0) intPossition++
If(intPressureDifferential > 0) intPossition-- (might help for stability)
If(intPressureDifferential == 0) Do nothing

//make sure that there is good signal smoothing!!!! (Capacitor or signal processing (like taking the average over the last x samples))

If this is going to be part of a professional project, I suggest that you look into 'digital control systems' (control theory stuff) and try to estimate a transfer function (sensor, motor, delay,...); take time to calculate the "root-locus" (makes sure that system is stable), and then yeild a proper solution.

I forgot to mention that intPressureDifferential will tell you if it is touching something or not.

Maybe something like:

if abs(intPressureDifferential) < 10 then Touching
else Not Touching

You can make the term "touching" flexible by allowing a greater range of pressure inputs being accepted- simply increase the number 10 to something larger.

thanks a million for your help. the thing is how do we know the desired pressure for each item. our project suppose to read the signal and then grab the object. Sensor detects that if arm has touched the object or not only!

the thing is how do we know the desired pressure for each item. our project suppose to read the signal and then grab the object. Sensor detects that if arm has touched the object or not only!

We haven't seen YOUR code, so it's nearly impossible to tell you what is wrong.

This is now an estimation problem.

The formula for grippers (Keramas J.G (1999), 'Robot Technology Fundamentals', ISBN 0-8273-8236-7, Thomson Learning, Albany, NY)

unF = wgSF

Where:
u = coeffeicent of static friction
n = Number of grippers touching the item
F = Gripper force
w = weight of the part or object being gripped
g = force ratio (1g = 9.8m/s^2)
SF = Safety factor (aim for about 1.5)

But for the sake of "getting on with it", I'd suggest that you start with a light force and experimentally raise the force until you are happy with the performance.

To do this (with my pseudo from earlier), just modify the intDesiredPressure. Start off with a small amount of pressure and move the part around: If it slides around a bit, up the pressure.

If you really feel that you need to calculate the force applied, you should probably read a few books on robotics.

:slight_smile: