1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Electronics Logic Gates - Am I missing something?

Discussion in 'Modding' started by Byron C, 7 Jan 2012.

  1. Byron C

    Byron C No liability accepted as a result of this post

    Joined:
    12 Apr 2002
    Posts:
    5,651
    Likes Received:
    744
    First, some background. I'm turning an RC car into an autonomous vehicle with an Arduino. At the moment I have three IR sensors in order to detect/avoid obstacles (left, right & front). These sensors usually output 5v; when an object is detected, the output then drops to zero (or thereabouts). However these sensors have quite a narrow detection range and one sensor per side doesn't detect all objects in the way; the car is relatively large, compared to most simple robotics kits/chassis.

    Therefore I want to use two sensors for each side (2 front, 2 left, 2 right, etc). Problem is that I don't want to use too many pins on the Arduino, as I have other plans for it (such as controlling a parallel LCD). I had the idea of using logic gates so that the sensors can be "paired" - therefore still only using one Arduino pin per side.

    As far as I can tell, I'd first need to invert the output of both sensors (i.e. a NOT gate) and feed that into an OR gate; when either sensor detects an object, the output of the OR gate becomes active. Only trouble is, this involves using two ICs - one NOT and one OR. Is there a better/more efficient way to do this?

    I could of course use sensors with a wider detection range but I'd likely need ultrasonic sensors, and they aren't cheap. The sensors I'm using cost less than a fiver each, but ultrasonic sensors start at around £20 each. Plus I'm not really interested in range detection at the moment, just non-contact obstacle detection.
     
  2. tehBoris

    tehBoris New Member

    Joined:
    30 Jan 2011
    Posts:
    616
    Likes Received:
    25
    Connect the sensors in parallel, install diodes to prevent any damage to the sensors.
     
  3. Byron C

    Byron C No liability accepted as a result of this post

    Joined:
    12 Apr 2002
    Posts:
    5,651
    Likes Received:
    744
    But that would mean that the Arduino is getting a 5v source on one pin from two different components when there are no obstructions; when an object is detected on one sensor the supply from that sensor would go low, but the Arduino would still be getting 5v from the other sensor - therefore it wouldn't detect any change in the overall state of that pin.

    I can see how it would work if the sensor output went high when there's an object in the way - it wouldn't care which component is sending the signal, it would just register a change in the pin state - but unfortunately the sensors work the other way around. Though I could just use a NOT gate on each sensor then connect the gate output to the Arduino...
     
  4. tehBoris

    tehBoris New Member

    Joined:
    30 Jan 2011
    Posts:
    616
    Likes Received:
    25
    It was not clear that the sensors sent 5v when the was nothing detected. In that case what I suggested dose not work. You should be able to find some relays to act a not gates easily enough to correct this issue.
     
  5. meh

    meh Member

    Joined:
    8 Nov 2008
    Posts:
    31
    Likes Received:
    0
    What you are in fact looking for is a NOR gate - An OR gate with a built in inverter! A 4001 is a CMOS quad dual-input NOR, so should meet your needs nicely :)

    It is indeed a NAND gate to get it to get it to go high on detection! Teach me to not fully read the question :wallbash:
     
    Last edited: 8 Jan 2012
  6. Byron C

    Byron C No liability accepted as a result of this post

    Joined:
    12 Apr 2002
    Posts:
    5,651
    Likes Received:
    744
    I thought about a NOR at first, but the problem with that is that in order for a NOR output to be high, both inputs have to be low - I want the output to be high whenever either one of the inputs goes low.

    I could just run the output of each sensor through a NOT gate and, as tehBoris mentions, feed those into one Ardiuno pin (via a diode) in parallel. Methinks I'll have to pick up some components and do some bench tests when I get paid...

    Of course the easiest answer is to use better sensors, but I'm being cheap! :)
     
  7. Raptor77

    Raptor77 New Member

    Joined:
    15 Dec 2009
    Posts:
    58
    Likes Received:
    2
    This is what you get from the NOT and OR idea, right?

    A B | Z
    0 0 | 1
    0 1 | 1
    1 0 | 1
    1 1 | 0

    i.e. you get a high result unless both inputs are high (no objects).

    You can do that same operation with a NAND gate.



    Alternatively, if you want the outputs to match the inputs, i.e. a low is detection, you could use an AND gate to get:

    A B | Z
    0 0 | 0
    0 1 | 0
    1 0 | 0
    1 1 | 1
     
    Last edited: 8 Jan 2012
    Byron C likes this.
  8. Dreamslacker

    Dreamslacker New Member

    Joined:
    13 Sep 2006
    Posts:
    132
    Likes Received:
    8
    When reading the Pin state with an Arduino, you simply take an input and process based on that input. The normal state does not affect the operation since you're simply processing an input and doing something based on that input.
    i.e. You can simply read the Pin as if(digitalread(x)==LOW) rather than if(digitalread(x)) which would need inversion of the signal input.

    A low signal would imply an obstruction. In this case, you just need an 'AND' gate for the sensor pair.

    Even if you're using interrupts, you can select a high-to-low transition.
     
    Last edited: 8 Jan 2012
  9. r3loaded

    r3loaded Well-Known Member

    Joined:
    25 Jul 2010
    Posts:
    1,095
    Likes Received:
    31
    I'd personally prefer converting the sensors to active high logic with a NAND gate, where a high signal stands for "obstruction detected". Or you could use an AND gate as described above for active low logic, with a high signal meaning "no obstruction".

    It's really a matter of preference, and partly depends on how you'll use this signal further on in the circuit. The main thing is to remain consistent with its usage.
     
  10. Byron C

    Byron C No liability accepted as a result of this post

    Joined:
    12 Apr 2002
    Posts:
    5,651
    Likes Received:
    744
    Huh. Didn't think of that; knew I was missing something, the NAND truth table is indeed exactly what I'm looking for. The 4011 chip seems ideal, it would service sensors on all four sides (at the moment, I only have sensors on three sides, but that's because I fried one of the sensors by hooking it up to the wrong power source - oops). Plus it's only 80p, even at Maplin.

    To be honest, that's what I'd prefer - it makes more logical sense.

    Thanks for all the help chaps (and maybe chapesses indeed).
     
  11. heh-

    heh- curses.

    Joined:
    31 May 2007
    Posts:
    185
    Likes Received:
    10
    You could just just use a parallel-in serial-out chip and connect all your sensors individually using 3 arduino pins. The 74HC165 chip provides 8 inputs on just one chip.
     

Share This Page