Optical Limit Switch

I have looked for installation and code for an optical limit switch but haven't had much luck. I saw information about debouncing but not too much on this kind of switch specifically.

Can you point me to information on how to hook one up, like do I need some resistor on one side of the switch or the other, on its way to some ground pin. Also, is there some code that I could use to get started?

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:
http://iteadstudio.com/store/index.php?main_page=product_info&cPath=4&products_id=243

I didn't properly add a link to the switch that I bought. Sorry I edited the link back into my initial post. I have also added it below as well.

OK It doesn't look like anyone that has read this thread has experience with an optical limit switch. What would be close or a good starting point that might help me learn about limit switches?

Can anyone help?

Pretty simple to hook up. Pin 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, the cathode should wire to a arduino ground pin. Pin 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.

When the optical path is blocked the digital input pin will read as a HIGH, and it will read as a LOW when the path is not blocked.

Lefty

Lefty,

Thank you very much. You gave me everything that I need.

When I was reading your answer however, I had to laugh at myself. I am so uninformed that everything that you said was unintelligible to me. Because it was so clearly laid out, I will be able to handle it. I think that as I go about reading the switch spec. sheet, buying the resistors and read (like I should have already) about those pins, it will be very clear to me what to do so I won’t ask you to take your time to explain it. I remember reading most of that stuff and will have to review it. I have to actually learn some of this stuff.

Sincerely, it is very wonderful to know that someone like me can get led along by the nose to a successful conclusion despite myself.

What you said was very clear …but funny.

Thanks again.

What you said was very clear …but funny.

Your welcome, and that comment would make a fair gravestone marker. :wink:

Lefty

It might not seems so at first, but the page....

... will tell you a lot of what you need to know.

The optoISOLATOR, which is what the page is about, it electronically identical to what you are working with. In your case, instead of the LED and photosensor being inside a "little cave" inside a blob of plastic, they are on the two pillars of your limit switch. But they work just the same as they would inside the little cave, apart from the fact that there's a way to block the light from the LED on its way to the light sensor.

OK, I have some reading to do. Thanks

I got it running. Thank you both.

Hello, Can you please share the code for this optical switch? that would be great help. :slight_smile:

Yes, I don't assert that it isany good but it works for me and you are welcome to it. I will try to post something tonight or tomarrow night.

Hi Asa -
I am very curious to see your code to accomplish this as well. Can you post?

Thanks!

Sorry,

I was busy and forgot.

This is what I used for testing.

#define optOutPin1 48 
#define optInPin1 49 
#define optOutPin2 51 
#define optInPin2 53 
#define optOutPin3 50 
#define optInPin3 52

void setup()
{
  Serial.begin(9600);
  pinMode(optOutPin1, OUTPUT);
  pinMode(optInPin1, INPUT);           // set pin to input
  pinMode(optOutPin2, OUTPUT);
  pinMode(optInPin2, INPUT);           // set pin to input
  pinMode(optOutPin3, OUTPUT);
  pinMode(optInPin3, INPUT);           // set pin to input  
  
}

void loop()
{

int val11;
int val12;
int val21;
int val22;
int val31;
int val32;

digitalWrite(optOutPin1, HIGH);
digitalWrite(optInPin1, HIGH);       // turn on pullup resistors
digitalWrite(optOutPin2, HIGH);
digitalWrite(optInPin2, HIGH);       // turn on pullup resistors
digitalWrite(optOutPin3, HIGH);
digitalWrite(optInPin3, HIGH);       // turn on pullup resistors

val11 = digitalRead(optOutPin1);
val12 = digitalRead(optInPin1);       // turn on pullup resistors
val21 = digitalRead(optOutPin2);
val22 = digitalRead(optInPin2);       // turn on pullup resistors
val31 = digitalRead(optOutPin3);
val32 = digitalRead(optInPin3);       // turn on pullup resistors

Serial.println(optOutPin1);
Serial.println(optInPin1);
Serial.println(optOutPin2);
Serial.println(optInPin2);
Serial.println(optOutPin3);
Serial.println(optInPin3);
Serial.println(val11);
Serial.println(val12);
Serial.println(val21);
Serial.println(val22);
Serial.println(val31);
Serial.println(val32);

if (val11 == HIGH)
  {
  Serial.println("Val 11 HIGH");
  }
  else
  {
  Serial.println("Val 11 LOW");
  }
if (val12 == HIGH)
  {
  Serial.println("Val 12 HIGH");
  }
  else
  {
  Serial.println("Val 12 LOW");
  }
  
  if (val21 == HIGH)
  {
  Serial.println("Val 21 HIGH");
  }
  else
  {
  Serial.println("Val 21 LOW");
  }
if (val22 == HIGH)
  {
  Serial.println("Val 22 HIGH");
  }
  else
  {
  Serial.println("Val 22 LOW");
  }
  
  if (val31 == HIGH)
  {
  Serial.println("Val 31 HIGH");
  }
  else
  {
  Serial.println("Val 31 LOW");
  }
if (val32 == HIGH)
  {
  Serial.println("Val 32 HIGH");
  }
  else
  {
  Serial.println("Val 32 LOW");
  }

delay(1000);
}

This it how I applied it.

void moveToNextStation(int stepsToNextStation, int DIR)
{

   Serial.println("Start");
   Serial.println(stepsToNextStation);
  {
  
     int opticalSensor1; // Go Home censor, used at start up for calibration and at end of cycle
     int opticalSensor2; // Boom Up censor, used to make sure that 
                               // second axis will clear obstacles while first axis is moving axis 2 carriage left and right.  
     int opticalSensor3; // All Stop censor, used if carriage runs to end of travel because of error.
         
     


     int startSlowing = stepsToNextStation - 60;
     int fullSpeed = 0;
     int currentSpeed = 0;
     int time = 0;
     int distplayDigits = 0;
     distplayDigits = stepsToNextStation;

     long lastDebounceTime = 0;
     long debounceDelay = 200; // Only tried denounce on one because the 
                                          // second axis out traveled and ricocheted at top. 
     
     int start;
     int finished;
     int elapsed;
    
     
     digitalWrite(optOutPin2, HIGH);
     digitalWrite(optInPin2, HIGH);               // turn on pullup resistors  
     digitalWrite(optOutPin3, HIGH);
     digitalWrite(optInPin3, HIGH);               // turn on pullup resistors 
  
         for (int i=1; i <= startSlowing; i++)     // Loops until it reaches the next station count.
            {
              //opticalSensor1 = digitalRead(optInPin1); // turn on pullup resistors ... setup censor not needed here
              opticalSensor2 = digitalRead(optInPin2); // turn on pullup resistors
              opticalSensor3 = digitalRead(optInPin3); // turn on pullup resistors
                           
              if (opticalSensor3 == HIGH) 
                 lastDebounceTime = millis();
                 opticalSensor3 = digitalRead(optInPin3); // turn on pullup resistors
                if (opticalSensor3 == HIGH) //&& (lastDebounceTime > debounceDelay)              
                   {                     
                   allStop();
                   }
     
              if (currentSpeed <= maxRPM && fullSpeed <= 1 ) 
                {                                         
                   if (currentSpeed <= maxRPM-1)          // If current speed is less than maxRPM
                     {                                           
                          currentSpeed = currentSpeed + 1;   // add one to speed it up
                       }
                         else
                       {
                          fullSpeed = 1;           // else flag as full speed reached
                        }
                }
                 
                         stepper.setSpeed(currentSpeed);// Set speed   
                         analogWrite(EN1_PIN, PWM);     // Do the stepping PIN1
                         analogWrite(EN2_PIN, PWM);     // Do the stepping PIN2
                         stepper.step(DIR);         //  This is a busy-wait loop until the inter-step time passes             
                         totalSteps = totalSteps + 1; //  For debugging
                         location = totalSteps;
                                      
                    if (DIR == -1)
                      {                     
                      distplayDigits = distplayDigits - 1;
                      LCDrightJustifiedNumbers(distplayDigits);
                      }
                      else
                      {
                      LCDNumberTotalSteps(totalSteps);
                      }
            }

         for (int j=59; j >= 0; --j)               // Decelerate   Loop until stop point is reached
            {
                 if (currentSpeed >= 5)
                   {
                     currentSpeed = currentSpeed - 2; // Subtract one
                   }
                     stepper.setSpeed(currentSpeed);  // Set speed   
                     analogWrite(EN1_PIN, PWM);    // Do the stepping
                     analogWrite(EN2_PIN, PWM);    // Do the stepping
                     stepper.step(DIR);            // This is a busy-wait loop until the inter-step time passes
                     totalSteps = totalSteps + 1;
                     location = totalSteps;
                    
                    if (DIR == -1)
                      {                    
                      distplayDigits = distplayDigits - 1;
                      LCDrightJustifiedNumbers(distplayDigits);
                      }
                      else
                      { 
                      LCDNumberTotalSteps(totalSteps);
                      //Serial.println(totalSteps);
                      }
                                   
            }
           
     analogWrite(EN1_PIN, 0);
     analogWrite(EN2_PIN, 0);      
   }
  Serial.println("Going Back");
}