Thread: moving multiple servos through Arduino board

  1. #16
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    There are many uses for resistive dividers (basically 2 resistors in series). Generating a (not so accurate) reference is one of them. Using a resistive sensor is another.

    There are many types of light sensors, do you know which type you have? Do you have the datasheet for it?

    Most common types are photo-resistors (resistance change based on light), photo-transistor (current change based on light), and photovoltaic cells (voltage change based on light). They require very different circuits, and behave differently.

    It's unusual to use a digital input to read from a sensor. Using an ADC input would make more sense.

  2. #17
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    first, thanks for the All About Circuits link, It seems very complete, sure I'll use it a lot.

    for the sensors I use, as I said, I took them from cheap night-lights, but they seem very common, here's a link to a picture I found which is exactly the same:

    http://nomadicresearchlabs.com/store...-photocell.jpg

    So it's a CdS (cadmium sulfide) photo conductive cell (ambient light) and seems to be of the photo-resistive family, this is how I got the idea of using them, taking them apart and using a multimeter to see how it worked.

    I also thought the idea of using this kind of sensor on a digital input was not very conventional, this is why I mentioned it, but still, it works! And quite well also, I've been working on this project for a while now and there has been no issues about them.

    I thought that, since the resistance drops as light increases, the starting circuit would have a high resistance and no current would go through and as light gets stronger, there would be a point where the board would start getting current, just as if a button had been pushed. So I guess this is how it works. And it's the cheapest presence detector I could build which was the most important prerequisite...

    Do you know what the major drawbacks of using these detectors this way, in other words, what kind of trouble am I looking for?

    thanks, again

  3. #18
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The problem with that is, a photo-resistor is not really a switch. A switch should have (near) infinite impedance when open, and (near) 0 impedance when closed.

    A photo-resistor always has a resistance somewhere in-between.

    Therefore, the output voltage of the resistive divider is always somewhere in-between.

    In digital logic, you are guaranteed 2 voltage levels (you can find them in the datasheet) - Vih (minimum input high voltage) and Vil (maximum input low voltage), meaning, if input voltage is higher than Vih, it's guaranteed to be interpreted as a one, and 0 if lower than Vil.

    For the arduino, let's look at the microcontroller's datasheet -
    http://www.atmel.com/dyn/resources/p...ts/doc8025.pdf

    On page 309 -
    Vil = 0.3*Vcc = 0.3 * 5 = 1.5V
    Vih = 0.6*Vcc = 0.6 * 5 = 3.0V

    That means, Atmel guarantees that anything lower than 1.5V will register as low, and higher than 3.0V will register as high.

    Between 1.5V and 3.0V, it's undefined behaviour. It could be one or zero, or sometimes one and sometimes zero, depends on the phase of the moon. In reality, there is a voltage, somewhere between 1.5V and 3.0V, where the actual switching happens (Vth, the threshold voltage).

    In your circuit, you are depending on this voltage, which is bad, because it's not a specified parameter, meaning, for example, for this batch of chips, it could be 2.0V, and for the next batch, it could be 2.5V. It's generally bad to rely on unspecified parameters. The behaviour of your circuit will change when Atmel changes their manufacturing process for example, even though their chips still meet the specs.

    Another problem is, if the voltage is very close to Vth, and there is noise (there is always noise), the input can oscillate very quickly between 1 and 0. Theoretically, that can lead to device destruction (the reason is a bit more involved, but it will be clear if you study how CMOS gates work). In reality, it probably won't, but it's still bad design practice. If you really want to use a digital input, you should at least use something called a Schmitt trigger, that will prevent this oscillation.

    The "right" solution is to keep your current circuit, but feed the output into an ADC (analog input) pin. Then you can do all the processing in code, and behaviour of the circuit is fully guaranteed.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cyberfish View Post

    Between 1.5V and 3.0V, it's undefined behaviour. It could be one or zero, or sometimes one and sometimes zero, depends on the phase of the moon. In reality, there is a voltage, somewhere between 1.5V and 3.0V, where the actual switching happens (Vth, the threshold voltage).
    Not exactly. These days most digital inputs are schmidt triggers which do not have ambiguous intermidiate behaviors. What happens is that as voltage rises, it will switch 0 to 1 at 3.0v then as it falls it will switch from 1 to 0 at 1.5v... between those voltages it will retain it's previous state. The bad old days of using 7400 chips as "fuzz box" amplifiers are long gone.

    In your circuit, you are depending on this voltage, which is bad, because it's not a specified parameter, meaning, for example, for this batch of chips, it could be 2.0V, and for the next batch, it could be 2.5V. It's generally bad to rely on unspecified parameters. The behaviour of your circuit will change when Atmel changes their manufacturing process for example, even though their chips still meet the specs.
    Hense the reason for schmidt triggers....

    For the OP ... if you can get some cheap photo transistors... you'd be further ahead as they are much closer to being a switch than an analog photoresistor (i.e. they have gain).

  5. #20
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    So I guess The easiest, and the right, way to solve this is to wire my sensors to the analog pins of the Arduino. Easy does it!

    But then, as I remember CommonTater saying :

    Quote Originally Posted by CommonTater View Post
    No not replace... add a cap from the junction of the resistor and switch to ground. The cap stores a small charge so that when the button is pressed, if the contact breaks or chatters, the cap can fill it in, bounceproofing the switch.
    then will the threshold voltage value (say 2V) I will code for be like ''the moment the button is pressed''? I mean, if I tell the Arduino to switch on at 2V, can it ''chatter'' there? Therefore I would still need to put a capacitor in parallel to the resistor. But it seems strange to me because, how could it ''know'' when to discharge? hmmm.

    I know, All About Circuits..

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cross-side View Post
    So I guess The easiest, and the right, way to solve this is to wire my sensors to the analog pins of the Arduino. Easy does it!

    But then, as I remember CommonTater saying :



    then will the threshold voltage value (say 2V) I will code for be like ''the moment the button is pressed''? I mean, if I tell the Arduino to switch on at 2V, can it ''chatter'' there? Therefore I would still need to put a capacitor in parallel to the resistor. But it seems strange to me because, how could it ''know'' when to discharge? hmmm.

    I know, All About Circuits..
    Actually... I'm a big fan of an old saw... "If it ain't broke, don't fix it!"

    From a purely technical standpoint it's an analog device that should be on an analog pin... BUT... by the time you got your software to correctly provide hysterisis for it and got it sufficiently debounced, that input port would be little more than a software version of the schmidt triggers on your digital inputs... really.

    I know it's off topic but I hope the mods will permit me a minor digression here...

    Lets consider the voltage on that pin when you first expose the cell to light...

    It's pulled up to nearly 5 volts (easily checked with a voltmeter), now the cell is exposed and the voltage begins to roll down until it passes viH and traverses the indeterminate range, nothing happens. It continues down until it hits viL at which point the input unquestioningly switches from 1 to 0.

    Now the thing about hysterisis is that even if the voltage drifts up and down, in and out of the so called "indeterminate" range between viH and ViL that bit will stay 0.

    So now you cover the cell, the voltage begins creeping up past viL through the indeterminate region, nothing happens until it rises above viH whereupon it switches to 1.

    Again hysterisis is your friend; that voltage can dribble around hiH and the indeterminate region all it wants, unless it gets below viL it's going to stay a 1.

    Laughably this is exactly what you would have to program onto the analog bits to get proper performance... So other than an interesting programming experience, what have you actually gained?

    About the capacitor... on a switch where the contacts can make and break several times while the button is in motion, the capacitor is needed to fill in any very fast transitions your voltage might take as the contacts misbehave... Essentially making the switch act just like your photocell does... Nice smooth transitions from one voltage to another, with no false triggering.

    So... do we actually want to fix this? Probably not.

  7. #22
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Now that is a site worth bookmarking.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #23
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    So I guess The easiest, and the right, way to solve this is to wire my sensors to the analog pins of the Arduino. Easy does it!
    Exactly.


    It's pulled up to nearly 5 volts (easily checked with a voltmeter), now the cell is exposed and the voltage begins to roll down until it passes viH and traverses the indeterminate range, nothing happens. It continues down until it hits viL at which point the input unquestioningly switches from 1 to 0.

    Now the thing about hysterisis is that even if the voltage drifts up and down, in and out of the so called "indeterminate" range between viH and ViL that bit will stay 0.

    So now you cover the cell, the voltage begins creeping up past viL through the indeterminate region, nothing happens until it rises above viH whereupon it switches to 1.

    Again hysterisis is your friend; that voltage can dribble around hiH and the indeterminate region all it wants, unless it gets below viL it's going to stay a 1.
    That is not guaranteed, and I would be very surprised if that's actually the case. Certainly won't be the case for CMOS inputs.

    Actually, I have an Arduino around. I will be right back.

  9. #24
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Took me longer than expected because my oscilloscope decided it doesn't want to talk to my computer...

    Anyways, I had one digital output pin output a square wave, and RC filter it to make the input waveform (yellow).

    Then I have another digital output (blue) copy the value of a digital input at very high speed (100000 samples per period).

    Attached is what I got.

    So yes, it does look like there is some input hysteresis (350mV), though it's far away from Vil and Vih.

    Reading the datasheet again, it does actually mention this!

    On page 335-336, there are pretty graphs of Vth vs Vcc, and hysteresis vs Vcc. At 25C and 5V Vcc, the values are as predicted (2.5V-2.6V Vth, 375mV hysteresis).

    HOWEVER, if you scroll up a few pages (322), Atmel also tells you to not rely on these values, as they are not guaranteed, and can change. The values are only provided as "typical values".

    The following charts show typical behavior. These figures are not tested during manufacturing.
    And because these parameters are not intended to be relied upon, Atmel also doesn't really optimize for them when they design their chips. As you can see, they vary quite a bit with temperature, for example. An ADC would be temperature-compensated, because ADC parameters are guaranteed values.

    For some hobbyist fooling around, this will probably do. But this is not good design practice, and should not be used in "serious" designs. If you intend to do any kind of "serious" designs later on, I suggest doing it right from the beginning.

    Yes, it's probably extra work for no obvious benefit in this case.

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cyberfish View Post
    For some hobbyist fooling around, this will probably do. But this is not good design practice, and should not be used in "serious" designs. If you intend to do any kind of "serious" designs later on, I suggest doing it right from the beginning.

    Yes, it's probably extra work for no obvious benefit in this case.
    Excellent...

    FWIW... engineers have been putting analog signals on digital pins for a lot of years. Hysterisis is now almost universally counted on for accurate switching... Take a look at the POR (Power On Reset) circuits on most of these chips and you'll usually find an RC divider there to provide a couple of hundred milliseconds delay when power is first applied. Then there's the crystal/inverter oscillators that rely heavily upon hysterisis for their function. The actual switchpoints viL and viH may not be at guaranteed voltages but since the design incorporates Schmidt trigger inputs (which provide the hysterisis) you can reasonably assume there will be some differential switching to work with.

    Anyway... we should probably get back to C...

    Thanks to the moderators for allowing this very interesting side trip...

  11. #26
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    POR is special because it's supposed to be fed an analog input - supply voltage(s). It's probably more of an analog circuit than a digital one.

    It's probably safe to assume some hysteresis, but with Vth and hysteresis range that can vary that much, and are unspecified, I wouldn't rely on it.

    I would at least use a comparator with hysteresis.

    It's usually a good idea to design circuits based only on guaranteed worst case parameters. Now that's just in theory and may not always be possible. In this case it's very possible, though.

  12. #27
    Registered User
    Join Date
    Feb 2011
    Posts
    11
    I wanted to make things move. Bought an Arduino, some servos and a power supply. Slowly got into learning c, electronics and the interaction of both.

    And here I am! The web is a crazy thing, really amazing when you stop and think of it.

    As for my project, I will use it as is for now since it works and I need it soon. Also, this project is (was) an introduction, I learned a lot through it, and to make a point, I think I'll use it as an exercise and redo it again from scratch with better ''design practices'' in mind, soft and hardware wise.

    Thanks again to all for everything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. Moving files from one directory to multiple dirs
    By csj561 in forum C Programming
    Replies: 7
    Last Post: 03-18-2005, 03:52 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM