HUM: PHOTOINTERRUPTER

HUM: PHOTOINTERRUPTER-Uncategorized

You are a beginner with Dasduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by soldered where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect the module with Dasduino and the basic code. Everything else is left to your imagination.

INTRODUCTION

You have certainly seen a revolution counter or a sensor that detects passing objects many times, and you did not know how they work. We will introduce you to a sensor frequently used for these two applications, as well as many others. For starters, this is a photointerrupter ITR9608 that can detect an object inside it. Its use is simple, and the opportunities it provides are various and we can use it in different projects.

Sensor characteristics:

  • Diode voltage: 1.5V
  • Diode current: 50mA
  • Maximum transistor collector-emitter voltage: 30V
  • Maximum transistor emitter-collector voltage: 5V
  • Maximum transistor current: 20mA

 

HOW DOES IT WORK?

The photointerrupter has an IR diode that constantly emits light towards a phototransistor that is calibrated for that light only. The diode is located on one side of the sensor and the phototransistor on the other, and between them, there is a gap through which objects can pass. When there is no object, the phototransistor receives light from the IR diode and it is in the open (conductive) state, but when there is an object between the IR diode and the phototransistor then the phototransistor is in the closed state. If we connect a phototransistor to Dasduino, we can read when the transistor conducts current and when not to find out if there is an object between the IR diode and the phototransistor. Using this sensor, we can count objects or use them to get a simple revolution counter.

HOW TO CONNECT IT?

The sensor is easily connected to Dasduino because we only have a phototransistor that we connect to Dasduino, and we connect the IR diode to the power supply as well as the classic LED diode. The IR diode must be illuminated constantly and therefore be connected to the power supply via a resistor of the appropriate value (150-200Ω) to limit the current passing through the diode. We connect the phototransistor to the power supply as a switch, but via the resistor of higher value (10kΩ or more) and from the collector we connect the signal to the Dasduino pin in order to read when the phototransistor conducts current and when not. On the sensor, there is a labeled side indicating the location of the IR diode and the other side indicating the location of the phototransistor, and in the image, we can see the labels of the pins.

This scheme shows how to connect the sensor to Croduino.

DASDUINO CODE

The first example of the code can be used for counting various objects passing through the sensor.

int senzor=2;       //we have assigned the name "senzor" to the pin to which we connect the sensor
int stanjeSenzora;  //the variable to which we save the sensor state
int zastavica=0;    //auxiliary variable for the if loop
int brojac=0;       //the variable to which we save the number of objects that have passed through the sensor
void setup() {
pinMode(senzor,INPUT); //setting the pin to which the sensor is connected as an input pin
Serial.begin(9600);  //starting serial communication
}
void loop() {
  stanjeSenzora=digitalRead(senzor);                  //to the stanjeSenzora variable, we save the sensor value read from pin 2
  if ((stanjeSenzora==LOW)&&(zastavica==0)){  //the if loop in which we check whether the sensor is LOW and whether the flag is equal to 0
    zastavica=1;    //setting the flag to 1, and it will remain 1 as long as the object is within the sensor and it ensures that the same object is not counted multiple times
    brojac++;         //increasing the counter because an object has passed through the sensor
    Serial.println(brojac); //we display the counter state on the Serial monitor  
    }
else if(stanjeSenzora==HIGH){  //questioning whether there is an object within the sensor, and if there is no objects the value will be HIGH and this loop will be executed
  zastavica=0;    //setting the flag to 0 because an object has passed through the sensor and by that, we ensure that when the next object passes through, the first if loop can be executed 
  }
 delay(50);    //50 ms pause
}
The second example can be used for object detection or as an alarm that detects something passing through the sensor. In order to have the alarm signal on pin 3, connect the LED via the appropriate resistor.
int senzor=2;       //we have assigned the name "senzor" to the pin to which we connect the sensor
int ledica=3;       //we have assigned the name "ledica" to the pin to which we connect the LED diode
int stanjeSenzora;  //the variable to which we save the sensor state
void setup() {
pinMode(senzor,INPUT);  //setting the pin to which the sensor is connected as an input pin
pinMode(ledica,OUTPUT); //setting the pin to which the sensor is connected as an output pin
}
void loop() {
  stanjeSenzora=digitalRead(senzor);   //to the stanjeSenzora variable, we save the sensor value read from the pin 2
  if (stanjeSenzora==LOW){   //if the sensor is LOW, i.e. there is an object between the diode and phototransistor, the loop is executed
    
    digitalWrite(ledica,HIGH); //when there is an object within the sensor, we turn the LED on
    }
else {
  digitalWrite(ledica,LOW); //if there is no object, we turn the LED off
  }
}