keyestudio
Share your creative ideas with us here
By ran | 07 February 2025 | 0 Comments

HOW to Use Photo Interrupter on Raspberry Pi with ESP32

Description

This kit contains a photo interrupter which mainly uses 1 ITR-9608 photoelectric switch. It is a photoelectric switch optical switch sensor.

Working Principle

When the paper is put in the slot, C is connected with VCC and the signal end S of the sensor are high levels; then the red LED will be off. Otherwise, the red LED will be on.

Required Components

 

 

 

 

 

ESP32 Board*1 ESP32 Expansion Board*1 Keyestudio DIY Photo Interrupter*1 3P Dupont Wire*1 Micro USB Cable*1

Connection Diagram

Test Code

//*************************************************************************************
/*
 * Filename    : Photo_Interrupt
 * Description : Light snap sensor counting
 * Auther      : http://www.keyestudio.com
*/
int PushCounter = 0; //The count variable is assigned an initial value of 0
int State = 0; //Store the current state of the sensor output
int lastState = 0; //Stores the state of the last sensor output
void setup() {
  Serial.begin(9600);//Set the baud rate to 9600
  pinMode(15, INPUT);//Set the light snap sensor pin to input mode
}

void loop() {
  State = digitalRead(15);//Read current state
  if (State != lastState) {//If the state is different from the last read
    if (State == 1) {//block the light 
      PushCounter = PushCounter + 1;//Count + 1
      Serial.println(PushCounter);//Print count
    }
  }
  lastState = State;//Update state
}
//*************************************************************************************

Code Explanation

Logic setting:

Initial Setting

Set PushCounter to 0
Set State to 0 (value of the sensor)
Set lastState to 0

 

when an object enters the slot

lastState is 0,State turns into 1;
lastState turns into 1

Set PushCounter to PushCounter+1
print the value of PushCounter

when the object leaves the slot

lastState is 1,State becomes 0,
two data are not equal,
lastState turns into 0.

PushCounterdoesn’t change;
Don’t print the value of PushCounter

When the object goes
through this slot again

lastState is 0, State becomes 1,
two data are not equal,
lastState turns into 1.

SetPushCounter to PushCounter+1.
And print the value of PushCounter

When the object leaves
this slot again

lastState is 1,State turns into 0,
two data are not equal
lastState turns into 0

PushCounter doesn’t change;
Don’t print the PushCounter value

Test Result

Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on,open the serial monitor and set the baud rate to 9600. We need to press the reset button on the ESP32, then the serial monitor will display the PushCounter data. Every time when the object passes through the slot of the sensor, the PushCounter data will increase by 1 continuously, as shown below;

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code