2 photoelectric sensors to measure time

Hi!

I am trying to measure the time, between a liquid that is streaming from sensor 1 to sensor 2. I have set up the wiring system, and it works, I am just not sure what my sensors actually sends out as a signal (is it volt, just LOW and HIGH, or?) I used these sensors:
[docs.rs-online.com/861c/A700000007162061.pdf] (https://docs.rs-online.com/861c/A700000007162061.pdf)

my code is also attached here, I am not very good at coding, but my main sketch is, that i want to be able to write an 's' in the serial monitor, to start the process. then when the liquid streams in front of the first sensor, the timeer should start to count, and then the timer should stop counting again when the liquid streams in front of the second sensor.

can anyone help?

code:

const int Sensor1Pin = 2;                                       
const int Sensor2Pin = 3;                                       
int SensorValue1;                                               
int SensorValue2;                                              
bool tidtaeller = false;
char input;
unsigned long timer;
unsigned long tidsvaerdi;

void setup() 
{
Serial.begin(9600);                                             
pinMode(Sensor1Pin,OUTPUT);                                    
pinMode(Sensor2Pin,OUTPUT);                                    
}

void loop() 
{
   if(Serial.available())
   {
    input = Serial.read();
    if(input=='s')                                                
    {
      Serial.print("måling kører");
      SensorValue1 = digitalRead(Sensor1Pin);                    
      SensorValue2 = digitalRead(Sensor2Pin);                    
      if((tidtaeller == false) && (SensorValue1 == 0))           
      {
        tidtaeller = true;                                         
        timer = millis();                                         
      }
      if(tidtaeller == true && SensorValue2 == LOW)                
      {
        Serial.print("tiden er:");                               
        tidsvaerdi = millis()-timer;                              
        Serial.println(tidsvaerdi);                               
        tidtaeller = false;                                        
      }
    }
    if(input=='e')
    {
      Serial.print("mĂĄling stoppet");
      tidtaeller = false;
    }
  }
}

>

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hello
Yes, and you too. Post the sketch in code tags "</>", a schematic and picture of your hardware setup.
Have a nice day and enjoy coding in C++.

pinMode(Sensor1Pin,OUTPUT);
pinMode(Sensor2Pin,OUTPUT);

Whatever the output of the sensor is then I am certain that the Arduino input pins should not be set as outputs

hi!

I am not very experienced with programming yet :slight_smile:
I want to measure the time between a liquid flow reaches sensor 1 to it reaches sensor 2. I have set up the wiring and it seems to work. i just don't know which output my sensor give me. (LOW/HIGH, a voltage, or?) i am using these sensors:
docs.rs-online.com/861c/A700000007162061.pdf

i want to start the system with writing an 's' to the serial monitor, and then when sensor 1 "see" the liquid it should start the timer and count until sensor 2 "see" the liquid.

Can anyone help me?

code:

const int Sensor1Pin = 2;                                       //definerer pin for sensor 1 (hvid/orange)
const int Sensor2Pin = 3;                                       //definerer pin for sensor 2 (orange)
int SensorValue1;                                               //definerer et tal for værdien af sensor 1 
int SensorValue2;                                               //definerer et tal for værdien af sensor 2
bool tidtaeller = false;
char input;
unsigned long timer;
unsigned long tidsvaerdi;

void setup() 
{
Serial.begin(9600);                                             //sætter baud raten til 9600
pinMode(Sensor1Pin,OUTPUT);                                     //sætter værdien af "Sensor1Pin" til outputtet
pinMode(Sensor2Pin,OUTPUT);                                     //sætter værdien af "Sensor2Pin" til outputtet
}

void loop() 
{
   if(Serial.available())
   {
    input = Serial.read();
    if(input=='s')                                                //hvis der skrives s i serial monitoren
    {
      Serial.print("måling kører");
      SensorValue1 = digitalRead(Sensor1Pin);                     //sæt værdien af sensor 1 til at være det der bliver skrevet i "Sensor1Pin"
      SensorValue2 = digitalRead(Sensor2Pin);                     //sæt værdien af sensor 2 til at være det der bliver skrevet i "Sensor2Pin"

      if((tidtaeller == false) && (SensorValue1 == LOW))              //hvis tidstælleren ikke er igang, og sensor 1 værdien er lav
      {
        tidtaeller = true;                                         //sæt da tidstælleren til true
        timer = millis();                                          //og start med at tæl ind i "tidtæller"
      }
      if(tidtaeller == true && SensorValue2 == LOW)                //hvis tidtælleren er igang, og sensor 2 værdien er høj
      {
        Serial.print("tiden er:");                                //skriv da "tiden er:"
        tidsvaerdi = millis()-timer;                              //beregn tiden lige nu, minus tiden da "tidtæller" startede
        Serial.println(tidsvaerdi);                                //udskriv denne værdi
        tidtaeller = false;                                        //sæt tidtæller til false for at stoppe denne
      }
    }
    if(input=='e')
    {
      Serial.print("mĂĄling stoppet");
      tidtaeller = false;
    }
  }
}

Output?

Is this the INPUT then?

What do you think? Do sensors supply data? Would a sensor change anything in the outside world?

okay, i see :slight_smile: it gives an input to the arduino, which i want to read in my program

So your next problem is that you never read the any of the data from your two sensors. So how can they possibly affect your code.

yes, because I am not sure what signal my sensors send out. i now that when the reciever can see the emitter, it has a low voltage, and then when the signal between the reciever and the emitter "breaks" it gets a high voltage.
So what i was think is to set the timer to count when the sensor values get HIGH

if((timer == false) && (SensorValue1 == HIGH))       
      {
        timer = true;                                         
        timemeasurer = millis();                                  
      }
      if(timer == true && SensorValue2 == HIGH)                
      {
        Serial.print("time is:");                                
        timevalue = millis()-timer;                              
        Serial.println(timevalue);                             
        timer = false;                                     
      }
    }

Then look up,the data sheet for this sensor of yours and find out.

yes, but i don't think it is written in the data sheet. what does sensors normally gives as a signal?

Yes it is. What sort of data sheet would miss out an important thing like that?

I just don't see it then :confused:

[https://dk.rs-online.com/web/p/fotoelektriske-sensorer/2043999/

That is because you are not looking at the data sheet but a product information page.

This product is just a IR proximity sensor how is that going to interstate into your system?

When posting a link, remove everything from the first ? You see. That prevents the web site spying on you.

Hi,
This is your sensor;

RS PRO Photoelectric sensor 20 m LED Cable 2 m PNP connect / break

Here is the datasheet;
A700000007162069.pdf (342.1 KB)

Specs;

Properties

Attribute Value
Sensor design Cylinder
Scan form One-way photocell
Key area 20 m
Output type PNP end / break
Electrical connection Cable 2 m
DC voltage max. 30V
Terminal type Cable 2 m
Light source Light emitting diode
IP code IP67
Reaction time <8.2 m / s
Mine. operating temperature -15 ° C
House material Nickel / copper alloy
Series PR18
Operating temperature max. + 55 ° C
Mounting type Screw nut

Tom... :smiley: :+1: :coffee: :australia:

exactly, but i just don't see anywhere in the datasheet, which signal the sensor sends out?
thanks! :slight_smile:

it only has to tell me if there is a change or not. if there is a change, it should start/stop counting the time

That is up to your software to do. By the way have you noticed you need a minimum of 10V to operate it.