Reading restance

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Kdees
 
Posts: 24
Joined: Wed Jan 13, 2010 8:05 pm

Reading restance

Post by Kdees »

How do I read an analog input as resistance and store it in a variable?

I need to read a sensor that varies from about 1K to about 50K

my goal is to control a stepper motor to find a given spot (say 20K) by spining a shaded wheel between it and a light source

I have the device between A0 and ground it is a CDS cell but the sample code from ADA does not seem to work for it's value


Thanks,

K

User avatar
len17
 
Posts: 394
Joined: Sat Mar 14, 2009 7:20 pm

Re: Reading restance

Post by len17 »

You can't just run the photocell from A0 to ground. Take a look at ladyada's tutorial:
http://www.ladyada.net/learn/sensors/cds.html
to see how to hook up a photocell to an Arduino.

Kdees
 
Posts: 24
Joined: Wed Jan 13, 2010 8:05 pm

Re: Reading restance

Post by Kdees »

it is what it is... the cds is circa 1989 and embeded in a device with one side to ground... has a tiny light shining on it with a shaded disk... just need to read and equate ohms from 1 to 50K to ground from analog pin

device has 3 wires... Vcc, Ground, and cds to ground

User avatar
len17
 
Posts: 394
Joined: Sat Mar 14, 2009 7:20 pm

Re: Reading restance

Post by len17 »

If the photocell is already connected to ground then you'll need a pull-up resistor (instead of pull-down as shown in the tutorial). Connect a resistor from +5V to A0, as well as connecting the photocell to A0, and it should work. Try a 1kΩ resistor, but the best value will depend on the photocell and range of light levels.

Kdees
 
Posts: 24
Joined: Wed Jan 13, 2010 8:05 pm

Re: Reading restance

Post by Kdees »

Thanks that works great! I now read A0 as a number between 0-1023 my functional range is 490 to 1010
now to tell the stepper at m2 how to find 750?

it has to find it going forward from 490 up to 750, not from from 1000 down to 750.

i have the adafruit servo board and my stepper functions just spiffy with this command:

motor.step(100, FORWARD, DOUBLE);

I want to use micro step forward and back to fine tune to 750 once we find it going up...

thanks for the help, sorry to be such a n00b...

K

User avatar
len17
 
Posts: 394
Joined: Sat Mar 14, 2009 7:20 pm

Re: Reading restance

Post by len17 »

I haven't used stepper motors so this is just off the cuff, but would something like this work?

Code: Select all

while (analogRead(0) < 750)
    motor.step(1, FORWARD, SINGLE);

Kdees
 
Posts: 24
Joined: Wed Jan 13, 2010 8:05 pm

Re: Reading restance

Post by Kdees »

indeed!
I now have a gizmo that will figure out how to hold a current course.

This is a leftover part from my auoto pilot system in my boat. I had to use it because they "tech" guy at the marine elc store told me I couldn't "Nobody but them knows how it works"
well update that list...

Code: Select all

#include <AFMotor.h>
AF_Stepper motor(48, 2);
int binn = 0;     // binn at a0
int binnReading;     // the analog reading from the sensor 

void setup() {
  Serial.begin(9600);          
  motor.setSpeed(255);  //  rpm   

   if (analogRead(0) < 748)
      while (analogRead(0) < 748)
         motor.step(20, FORWARD, SINGLE);
   if (analogRead(0) > 752)   
      while (analogRead(0) > 752)
         motor.step(20, BACKWARD, SINGLE);
 }

void loop() {

   binnReading = analogRead(binn);  
  
  Serial.print("binn = ");
  Serial.println(binnReading);     
}
binn = 749
binn = 749
binn = 749
binn = 750
binn = 749
binn = 749
binn = 750
binn = 749
binn = 750
binn = 750
binn = 750


It is still a bit clunky I need it to be sure it hits 750 when it is starts. it takes a couple shots sometimes to get it. Once it does the slightest turn of the device causes the value to change so now i need to develop "turn right if more than like 760 and turn left if less than 740

User avatar
Franklin97355
 
Posts: 23939
Joined: Mon Apr 21, 2008 2:33 pm

Re: Reading restance

Post by Franklin97355 »

Setup only runs once so you will probably need to move the decision tree out of there. You can set the variable to 750 in setup to make sure it starts there.

Kdees
 
Posts: 24
Joined: Wed Jan 13, 2010 8:05 pm

Re: Reading restance

Post by Kdees »

Well, I want this procedure to be triggered at will via a button and run twice to be sure it has fix..

this is the calibration.

once it is calibrated to 750 i want to read the output and store it

then i can make a decision based on that value in another procedure

any help on this code would be great

you can see the bigger picture in my post "ArduinoHelm"

K

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”