588,201 active members*
4,696 visitors online*
Register for free
Login
Page 1 of 2 12
Results 1 to 20 of 28
  1. #1
    Join Date
    Apr 2011
    Posts
    73

    Optical Limit Switch

    I am trying to get my optical limit switches to work. I have listed what I have below. Think that I have found out how to hook one up to my arduino but I don't quite know how to see if it is working.

    I have an Arduino Mega 2560 and plan on using a pin above 13 because the lower ones are used for a working stepper and some other things.

    The optical switch that I have is:
    Opto Interrupter IRT9608-F [SEN020] - $1.50 : iStore, Make Innovation Easier

    I tried using this code but didn't have any luck.

    #define optOutPin 51
    #define optInPin 53

    void setup()
    {
    Serial.begin(9600);
    pinMode(optOutPin, OUTPUT);
    pinMode(optInPin, INPUT);
    }

    void loop()
    {

    int val1;
    int val2;
    digitalWrite(optOutPin, HIGH);
    //digitalWrite(optInPin, HIGH);
    val1 = digitalRead(optOutPin);
    val2 = digitalRead(optInPin);

    Serial.println(optOutPin);
    Serial.println(optInPin);
    Serial.println(val1);
    Serial.println(val2);
    if(val2==HIGH)
    {
    Serial.println("HIGH");
    }
    else
    {
    Serial.println("LOW");
    }
    delay(1000);
    }

    Can anyone please help me?

  2. #2
    Join Date
    Jan 2010
    Posts
    2141
    Can you diagram your connections to the device?

    Show where you are connecting power (what voltage?), values of any series resistors, and where you are connecting the arduino in and out pins?

    If you haven't already done so, you might want to experiment with using the part "manually" (using a multimeter to check its operation) before you hook it up to the arduino.

  3. #3
    Join Date
    Apr 2011
    Posts
    73
    This is what was written to me and this is what I am trying to follow except that I am using 51 for 51, 53 for 3 and I use the two grounds at the end of the digital row on the end of the Mega board.

    Pin (5)1, the anode of the diode should wire to a 250 ohm resistor, the other end of the resistor to a arduino output pin. Pin 2(ground), the cathode should wire to a arduino ground pin. Pin (5)3, collector, should wire to a arduino digital input pin that you also enable the internal pull-up resistor, or use a external pull-up resistor, say 10k ohms, from the digital input pin to +5vdc. Pin 4, emitter, wires to arduino ground pin.

  4. #4
    Join Date
    Jan 2010
    Posts
    2141
    Why is the following line commented out:

    //digitalWrite(optInPin, HIGH);


    You need to set the input pin HIGH in order to use the arduino's built-in pull-up resistor, which needs to be activated, otherwise you will need to add an external pull-up resistor from the collector to +5 volts.

    It is possible that the internal pull-up resistor might have too high a value, in which case it might be necessary to to use a lower value external pull-up resistor.

    The easiest way to determine that would probably be to work with the opto interrupter in a breadboarded circuit, rather than hooked up to the arduino. That way you can make sure that the device is working, and make sure that the resistors are set to appropriate values.

  5. #5
    Join Date
    Jan 2010
    Posts
    2141
    One other thing that you can do is figure out if the IR illumination LED is actually illuminated.

    If you have a video camera or point-and-shoot camera, first make sure that it can view IR in its viewfinder screen by pointing an IR remote control (visible in the viewfinder) at the camera lens, pressing a button on the remote control, and verifying that you can see the IR light signal coming from the remote.

    If you can indeed see the IR light in the viewfinder, then point the camera lens to the IR emitter portion of the opto interrupter, and see whether you can see a similar light in the viewfinder. (My guess is that even though the opto emitter probably has a narrow field of transmission, you should be able to see the light, either directly or in reflection off of the other side, by looking at it from various angles).

  6. #6
    Join Date
    Apr 2011
    Posts
    73
    I thought that I would turn digitalWrite over right any information that I would get from the sensor. How do I read the sensor?

    I don't have a bread board.

    I didn't get to see your last post before adding this. That is cool. I can see the blinking light of remove. Now I have to hook the sensor back up.

  7. #7
    Join Date
    Jan 2010
    Posts
    2141
    You should be able to read the input pin exactly the way that you are doing in your program. But you must either activate the internal pull-up resistor on that input pin by uncommenting that line of code, or else you must connect an external pull-up resistor to that pin. The idea is that you want a "weak" high signal on the input pin when the opto emitter is unblocked, but it will be pulled down to a low signal when the opto emitter is blocked (or vice versa, I haven't thought through the polarities involved).

    See http://www.arduino.cc/en/Tutorial/DigitalPins where it describes the technique of doing a digital write of HIGH to an input pin in order to activate the arduino's internal pull-up resistor on that input. Disregard the part of the discussion where they talk about dimly lighting an LED through the pull-up resistor - you are not driving an LED that way.

    As for a breadboard, the hookup is simple enough that all you would need is a few alligator clip leads, a couple of resistors, and a battery or 5-volt DC power supply (plus a multimeter) to do the testing.

  8. #8
    Join Date
    Apr 2011
    Posts
    73
    I edited in some thoughts in the post below.

    I bought a volt meter today so I pulled the sensor out of the board. Pins 51 and 53 are putting out voltage.

    Now I will hook it back up.

  9. #9
    Join Date
    Apr 2011
    Posts
    73
    I don't see a light in the view finder light when I push a button on the remote. I have 5 of these sensor and I need three but if I have success before I burn them all out, I can just order more.

    I do have a 100 ohm tied to a 150 between the anode and pin one.

  10. #10
    Join Date
    Jan 2010
    Posts
    2141
    OK, so you won't be able to use the camera viewfinder trick to view the IR, but you should be able to get it working anyway.

    I don't think that you need to worry about burning anything out unless you have a short circuit (or close to it) between the anode and +5 volts.

    Supposedly the arduino can source up to 40ma. of current from an output pin. The rated forward current of the LED in the opto is 50ma., but 40ma should light it up nicely. With a 250 ohm resistance, you will probably have less than half of that, but my guess is that that will also work OK. If you want to boost the current through the LED, but still keep it within the opto's rating as well as the arduino pin's rating, you could use just the 150 ohm resistor and you should be safe.

  11. #11
    Join Date
    Apr 2011
    Posts
    73
    I used the 150 ohm resister and changed the code. I just added another "if" so I can get val one and well a two. I don't get a change when I interrupt the beam.
    I get back:
    51
    53
    1
    0
    Val 1 High
    Val 2 Low

    From this code:
    #define optOutPin 51 //no ; here
    #define optInPin 53 //no ; here

    void setup()
    {
    Serial.begin(9600);
    pinMode(optOutPin, OUTPUT);
    pinMode(optInPin, INPUT); // set pin to input
    }

    void loop()
    {

    int val1;
    int val2;
    digitalWrite(optOutPin, HIGH);
    digitalWrite(optInPin, HIGH); // turn on pullup resistors
    val1 = digitalRead(optOutPin);
    val2 = digitalRead(optInPin); // turn on pullup resistors

    Serial.println(optOutPin);
    Serial.println(optInPin);
    Serial.println(val1);
    Serial.println(val2);
    if (val1 == HIGH)
    {
    Serial.println("Val 1 HIGH");
    }
    else
    {
    Serial.println("Val 1 LOW");
    }
    if (val2 == HIGH)
    {
    Serial.println("Val 2 HIGH");
    }
    else
    {
    Serial.println("Val 2 LOW");
    }

    delay(1000);
    }

  12. #12
    Join Date
    Jan 2010
    Posts
    2141
    I believe that val2 low is the condition that you should see when nothing is blocking the opto interruptor.

    And that val2 high should be what you see when you are blocking it.

    You might see val2 low even when you are blocking it if the pull-up resistor is not activated.

    What happens if you put a delay right after this line:

    digitalWrite(optInPin, HIGH); // turn on pullup resistors

  13. #13
    Join Date
    Apr 2011
    Posts
    73
    No change.

  14. #14
    Join Date
    Apr 2011
    Posts
    73
    If I disconnect pin one Val 2 goes high.

  15. #15
    Join Date
    Jan 2010
    Posts
    2141
    Next, use your voltmeter to measure things.

    Make a shorter program that just does the following:

    #define optOutPin 51 //no ; here
    #define optInPin 53 //no ; here

    void setup()
    {
    pinMode(optOutPin, OUTPUT);
    pinMode(optInPin, INPUT); // set pin to input

    digitalWrite(optOutPin, HIGH);
    digitalWrite(optInPin, HIGH); // turn on pullup resistors

    delay(10000); //or delay forever
    }




    Look for close to 4 to 5 volts on the opto's anode, 0 volts on the opto's cathode.

    Look for close to +5 volts on the opto's collector when the interrupter is blocked, and close to 0 volts on the opto's collector when the interruptor is unblocked. There should be 0 volts on the opto's emitter.

    (are you certain which pin of the opto is which? are you looking at the bottom or top of the opto when you are identifying the pins?)

  16. #16
    Join Date
    Jan 2010
    Posts
    2141
    Quote Originally Posted by herring_fish View Post
    If I disconnect pin one Val 2 goes high.
    That's a good sign that it should be working. Blocking the interruptor should have the same effect as disconnecting pin one.

  17. #17
    Join Date
    Apr 2011
    Posts
    73
    I set the volt meter to 10. I get 1 on pin 51 and I get about 4 on pin53

  18. #18
    Join Date
    Jan 2010
    Posts
    2141
    1 volt on pin 51 sounds pretty low. You should expect it to be close to 5 volts.

    If you are only showing 1 volt on pin 51, and you have 150 ohms in series with the anode of the opto, you may not have enough current to turn on the IR LED.

    Are you sure that your resistor is 150 ohms? What kind of resistor is it?

    If you disconnect the resistor from pin 51, do you get close to 5 volts on that pin?

  19. #19
    Join Date
    Apr 2011
    Posts
    73
    150=Ohn 1/2 watt 5%tol is the wattage wrong?

  20. #20
    Join Date
    Apr 2011
    Posts
    73
    I have 10, 100. 150, 270 and 330 to choose from, all are 1/2 watt.

Page 1 of 2 12

Similar Threads

  1. I guess this is an optical limit switch...how to wire?
    By HereinCS in forum DIY CNC Router Table Machines
    Replies: 17
    Last Post: 03-09-2011, 05:56 PM
  2. Optical Switch
    By boozeboy in forum CNC Machine Related Electronics
    Replies: 5
    Last Post: 05-07-2010, 12:40 PM
  3. Optical switch, on when I expected off
    By will gilmore in forum CNC Machine Related Electronics
    Replies: 9
    Last Post: 04-23-2010, 07:49 PM
  4. optical switch
    By natchez in forum Xylotex
    Replies: 3
    Last Post: 05-01-2004, 01:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •