Ir break beam ciruit

I am aware of the ir Libraries, but it is overkill for my purpose and there seems to be some processing delay with the libraries that I need to avoid.

What I want is a simple and fast "ir break beam detection circuit" using the ESP32 and the Arduino IDE. I want to use VS1838B sensors, so require a 38KHz ir LED sender. I am using two ESP32's (one sender, one receiver) as they will eventually be physically separated (~5feet), but for now they are only about 12" apart.

I thought this would be relatively simple, but I am getting a constant HIGH from the Sensor Pin regardless of if the 38K modulated LED is on or off. I have tested the LED and Sensor using the TinySender/TinyReceiver sketches and they work (aside from missing some data because I am polling so rapidly), so hardware appears to be valid.

I PWM an LED at 38,000Hz freq. Tested the LED will a RED LED and it appears to be working correctly, although I have not tested the modulation with an oscilloscope.

I am looking for a LOW from the VS1838B when the LED is turned on, but I never get the LOW I am expecting. I would love to know what I am doing wrong...

//ESP32 SENDER,  the number of the LED pin
const int ledPin = 23;  

// setting PWM properties
const int freq = 38000;
const int ledChannel = 0;
const int resolution = 8;
int dutyCycle = 255;
 
void setup(){
  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, ledChannel);

  dutyCycle = 255;
  ledcWrite(ledChannel, dutyCycle);
}
 
void loop(){
}
//ESP32 RECEIVER,  the number of the Sensor pin
const int sensorPin = 23;  

void setup(){
  pinMode(sensorPin, INPUT);
}
 
void loop(){
  printf("sensorPin(%d): %d\n", sensorPin, digitalRead(sensorPin));  //only get a 1 as output
  delay(100);
}

Always show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring.
Give links to components.


1 Like

dutyCycle = 255;

Tell us what this does ?

Trouble with a circuit but no schematics? How are You thinking? That helpers have crystal balls?

ledcWrite(channel, dutycycle)

This function accepts as arguments the channel that is generating the PWM signal, and the duty cycle.

not a great image and the board had extraneous stuff on it, but the circuit is very straight forward:

LED + 470ohm Resistor, 3.3v, Ground PMW from Pin23

Sensor +/- and Sensor wire to Pin23, +decoupling capacitor on Vcc

How did you figure that 23=gpio_num_16?

A duty cycle of 255%, weird.

show an image of your project.

post a schematic.

Obviously that is a comment left over from the original code. It does not affect the execution of the code. Don't let it confuse you.

255 is an 8 bit value representing full brightness

First part contradicts the second part.

Similar project:

Infrared Sensor Tutorial for Arduino, ESP8266 and ESP32 (diyi0t.com)

Other potential hits:
"ESP32" + "VS1838B"+ "break-beam" detection Arduino example at DuckDuckGo

When working a project such as yours, I like to test everything out with "similar" code. Once I know the hardware is good, it is hackin' time.

Looks like there is no connection to column 27 :thinking:


255 with an 8 bit PWM is solid 3v3, i.e. no pluses !

Change this to 127

1 Like

Thanks! This was really helpful. I'm still experimenting, but something like this seems to be working well.

void loop(){
  ledcWrite(ledChannel, 127);
  delay(40);
  ledcWrite(ledChannel, 0);
  delay(60);
}

Thanks. Yes you are right, when I plugged it back in I missed the row... And your 127 was great advice. I found this to be working well so far.

void loop(){
  ledcWrite(ledChannel, 127);
  delay(40);
  ledcWrite(ledChannel, 0);
  delay(60);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.