Arduino Uno Programming

I'm attempting to use two slot sensors, in this case Fairchild to stop and start a motor. The motor is working without the sensors. They're wired correctly. Not sure what's wrong. Here is my code:

int IN1pin = 9;
int IN2pin = 8;
int ENApin = 6;

int Bubble_fc_sensorpin1 = 12;
int Bubble_fc_sensorpin2 = 13;

void setup() {
  pinMode(IN1pin, OUTPUT);
  pinMode(IN2pin, OUTPUT);
  pinMode(ENApin, OUTPUT);

  pinMode(Bubble_fc_sensorpin1, INPUT);
  pinMode(Bubble_fc_sensorpin2, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(Bubble_fc_sensorpin1) == HIGH && digitalRead(Bubble_fc_sensorpin2) == HIGH) {

    digitalWrite(IN1pin, HIGH);
    digitalWrite(IN2pin, LOW);
    analogWrite(ENApin, 255);
    delay(500);

    digitalWrite(IN1pin, LOW);
    digitalWrite(IN2pin, LOW);
    analogWrite(ENApin, 0);
    delay(1000);
  }
}

Welcome to the forum

What happens when you run the sketch ?
What should it do ?
What does it actually do or not do ?

Is anything keeping the sensor pins in a known state at all times or are they floating at an unknown voltage ? Please post more details of the sensors

Post your schematic showing the correct wireing.

I have a disk attached to the motor shaft. There are slots cut out of the disk. This runs thru the two sensors positioned at 6 and 9 o'clock. The motor should stop when a gap passes between a sensor. I don't know how to test them. The resistor should be pulling them to a constant state?

I could simply have the motor turn off and on etc but there are three distinct positions I'd like the motor to stop at.

The only thing I get from your post 95 ohm resistor. That is probably way to low for any sensor. A schematic would help.

1 Like

Hi @tolandd ,

Welcome to the forum..

got a specific part number??

is you package like this??

this is what I got searching for Fairchild..
maybe something like??

might be better..

need to lose the delays and use non-blocking code so you actually reading the sensor quick enough, but first test the sensors, make sure you can read them properly..

good luck.. ~q

That looks like it. I can't upload the PDF Sheet on it.

Example of non blocking code would be? It looks like I need a 10k on the ground as well.

As a side, I have this setup working with Oopic and Magnevation motor controller but they're no longer supported and my board is glitchy hense my move to Arduino.

Yep, that looks like it..
Found the other pic in google images..
use-optointerrupter-arduino
led me to the above blog, quite nice..

look at "blink without delay", but first check the sensors are operating properly..

if you get stuck, let us know..

good luck.. ~q

i was bored..
try this untested sorry..

int IN1pin = 9;
int IN2pin = 8;
int ENApin = 6;

int Bubble_fc_sensorpin1 = 12;
int Bubble_fc_sensorpin2 = 13;

unsigned long lastMove;
unsigned long intervalMove = 500;

byte stateMove = 0;

void setup() {
  pinMode(IN1pin, OUTPUT);
  pinMode(IN2pin, OUTPUT);
  pinMode(ENApin, OUTPUT);

  pinMode(Bubble_fc_sensorpin1, INPUT);
  pinMode(Bubble_fc_sensorpin2, INPUT);
  Serial.begin(9600);
}

void loop() {

unsigned long now  = millis();

  if (digitalRead(Bubble_fc_sensorpin1) == HIGH && digitalRead(Bubble_fc_sensorpin2) == HIGH) {
   if (now - lastMove >= intervalMove){
    lastMove = now;
    switch (stateMove){

      case 0: digitalWrite(IN1pin, HIGH);
              digitalWrite(IN2pin, LOW);
              analogWrite(ENApin, 255);
              intervalMove = 500;
              stateMove = 1;
              break;
      case 1: digitalWrite(IN1pin, LOW);
              digitalWrite(IN2pin, LOW);
              analogWrite(ENApin, 0);
              intervalMove = 1000;
              stateMove = 0;
              break;
    }
   }
  } else{ //one of the sensors are triggered..
          //enter a stopped state..
          digitalWrite(IN1pin, LOW);
          digitalWrite(IN2pin, LOW);
          analogWrite(ENApin, 0);
          intervalMove = 1000;
          stateMove = 0;
  }
}

good luck.. ~q

I'll give it a go. Thank you kindly for all the help. I'm not sure how to test these sensors. I'll give google a try for some guidance. I've found them to be reliable in the past.

1 Like

That didn't work. I'll find a way to test the sensors and confirm they're working.

The blog I posted a link too was quite nice..
Has test sketch and a nice write up on these devices and how to interface..
They seem quite simple, maybe you have bad components or connection issues..

sorry.. ~q

I've tested both slot sensors with an LED and both are functioning correctly.


Here is my bench set up.

I ran this code:

int sensorPin1 = 2; // Connect the first opto-slot sensor to digital pin 2
int sensorPin2 = 3; // Connect the second opto-slot sensor to digital pin 3

void setup() {
  pinMode(sensorPin1, INPUT);
  pinMode(sensorPin2, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  int sensorValue1 = digitalRead(sensorPin1);
  int sensorValue2 = digitalRead(sensorPin2);
  
  Serial.print("Sensor 1: ");
  Serial.print(sensorValue1);
  
  Serial.print(" - Sensor 2: ");
  Serial.println(sensorValue2);
  
  delay(1000); // Delay for 1 second before reading the sensors again
}
and the serial monitor only showed "0" for each sensor either blocked or not. I'll add a 10k resistor to the ground and see if that helps.

That code should work..
And yes, you need both resistors, it's possible they are already damaged..
They do make ready to use boards, might want to try them..
Something like Infrared Slotted Optical Optocoupler Module Photo Interrupter Sensor maybe..

sorry.. ~q