Search code examples
arduinosensorsarduino-due

Reading Speed with 2 Photoelectric sensors with Arduino and show speed after reading


I have 2 photo electric sensors with NPN output: Looking at the data sheet it might actually be LOW (page 14 NPN Output): https://mouser.com/datasheet/2/307/e3fb_photoelectric-1189416.pdf page 14.

The 2 sensors are placed 152.4 mm apart. Basically I want to start a timer when the first gate is triggered and finish when the second gate is triggered and then divide the set distance (152.4 mm) by the time to travel between each gate and output the speed in meters second. The sensors are powered by a 24 VDC power supply, the NPN output voltage can be as high as 3V DC from the collector. I first tested out the sensor to trigger the onboard LED to blink when an object is present in front of the sensor and was able to trigger the LED:

    const int sensorPin = 3; // photoelectric sensor
    const int ledPin = 13;
    boolean state1 = LOW; // state at startup

    void setup() {
      pinMode(sensorPin, INPUT_PULLUP); // sensor input with internal pullup 
      resistor enabled
      pinMode(ledPin, OUTPUT); // initialize digital pin 13 as an output.
    }

    void loop() {
      state1 = digitalRead(sensorPin);
      if (state1 == HIGH) {
        digitalWrite(ledPin, HIGH);
      }
      else {
        digitalWrite(ledPin, LOW);
      }
    }

So it looks like the the voltage is in the right range to be read as HIGH or LOW.

So next I try to read the speed. The code is below:

const int PE_01 = 2;
const int PE_02 = 3;
unsigned long start, finish, speed;
float elapsed;
boolean state1 = LOW; //state at startup
boolean state2 = LOW; //state at startup

void setup() {
  // Initialize the serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(PE_01, INPUT_PULLUP); //sensor input with internal pullup 
  resistor enabled
  pinMode(PE_02, INPUT_PULLUP); //sensor input with internal pullup 
  resistor enabled
 }

void loop() {
  state1 = digitalRead(PE_01);
  state2 = digitalRead(PE_02);
   // time = millis ();
  if (state1 == HIGH){

    start = millis();
  }
if (state2 == HIGH) {
  finish = millis();
}
elapsed = finish - start;
speed = 76200/elapsed; // unit distance (micro meters) and time (milli 
seconds)
Serial.print("initial time (ms) = ");
Serial.println(start);
Serial.print("final time (ms) = ");
Serial.println(finish);
Serial.print("Speed of object (M/s) = ");
Serial.println(speed);
 delay(500);
}

what i see in the serial monitor however is:

10:31:07.632 -> initial time (ms) = 15502
10:31:07.666 -> final time (ms) = 15502
10:31:07.666 -> Speed of object (M/s) = 4294967295
10:31:08.109 -> initial time (ms) = 16002
10:31:08.144 -> final time (ms) = 15502
10:31:08.178 -> Speed of object (M/s) = 0

The 4294967295 shows up when there is no object present and when there is it seems to output 0. I've adjusted the equation a few times but I don't seem to get anything that makes sense. Any help is greatly appreciated. Thank you!


Solution

  • The logic of code doesn't seem right. From your explanation if you sure that trigged state is HIGH my code will be like this.

    void loop() {
    
      if (digitalRead(PE_01)== HIGH){// Check if first sensor trigged
          unsigned long start = millis();
          while(digitalRead(PE_02)== LOW);// Wait until second sensor trigged
          unsigned long finish = millis();
          float speed = 76200.0f/float(finish - start ); 
          Serial.print("initial time (ms) = ");
          Serial.println(start);
          Serial.print("final time (ms) = ");
          Serial.println(finish);
          Serial.print("Speed of object (M/s) = ");
          Serial.println(speed);
          delay(500);
    }
    

    Edit

    If your sensor are active low reverse all logic.

    void loop() {

      if (digitalRead(PE_01)== LOW){// Check if first sensor trigged
          unsigned long start = millis();
          while(digitalRead(PE_02)== HIGH);// Wait until second sensor trigged
          unsigned long finish = millis();
          float speed = 76200.0f/float(finish - start ); 
          Serial.print("initial time (ms) = ");
          Serial.println(start);
          Serial.print("final time (ms) = ");
          Serial.println(finish);
          Serial.print("Speed of object (M/s) = ");
          Serial.println(speed);
          delay(500);
    }