Need help wiring NPN proximity sensor → Optocoupler → ESP32 → L298N Motor driver (avoid frying ESP32)

Thread Starter

crescendo93

Joined Nov 21, 2025
27
Hey everyone. I'm working on a school project and would truly appreciate some help with the wiring diagram.

ESP32 Dev Module (30-pin, USB-C version)
NPN NO Inductive Proximity Sensor (12V, 3-wire type: brown +, blue –, black output)
Optocoupler (PC817 or similar)
L298N Motor Driver module
DC Motor (powered separately from 12V source)
External 12V power supply for motor and sensor
USB for powering ESP32

I want the NPN proximity sensor to detect a metal, send its signal through an optocoupler, and then safely trigger a GPIO pin on the ESP32 (3.3V logic). When the ESP32 detects the sensor is triggered, it should stop the motor through the L298N motor driver. I understand that the proximity sensor supplies +12V, so I cannot connect it directly to the ESP32. The optocoupler protects the ESP32, but I am unsure how to correctly wire the resistors (if any are needed), pull-ups (if any are needed), and grounds when using both sensor and motor driver.

My questions:
Can i safely wire the NPN sensor to the optocoupler and then from the optocoupler to the ESP32 board without a resistor between the optocoupler and ESP32? I've watched some videos that say it is not necessary as these two circuits are separate.
Is my logic okay with this? Sensor detects → optocoupler closes → ESP32 reads LOW on pin → motor stops

Here is the code that I want to test for the system:

C-like:
const int sensorPin = 21;      // Signal coming from optocoupler transistor

const int motorEnable = 25;    // PWM pin to control motor speed (ENA on L298N)

const int motorIn1 = 26;       // IN1 on L298N

const int motorIn2 = 27;       // IN2 on L298N


void setup() {

  Serial.begin(115200);


  pinMode(sensorPin, INPUT_PULLUP); // using internal pullup for now


  pinMode(motorIn1, OUTPUT);

  pinMode(motorIn2, OUTPUT);


  ledcSetup(0, 5000, 8); // Channel 0, freq 5kHz, resolution 8-bit

  ledcAttachPin(motorEnable, 0);


  // Motor forward

  digitalWrite(motorIn1, HIGH);

  digitalWrite(motorIn2, LOW);

  ledcWrite(0, 255); // Full speed

}


void loop() {

  int sensorValue = digitalRead(sensorPin);


  if (sensorValue == LOW) {

    Serial.println("Metal detected - stopping motor!");

    ledcWrite(0, 0);

  } else {

    Serial.println("No metal detected - motor running");

    ledcWrite(0, 255);

  }


  delay(1000);

}
 
Last edited by a moderator:

Reloadron

Joined Jan 15, 2015
7,850
OK, this is what you have:

Prox Sensor.png

To use with an opto coupler, making sure to include a current limiting resistor for your opto coupler Your opto (Device) cathode would go to your sensor Black and your opto Anode would go to your VCC with a current limiting resistor. Does that make sense? You are switching the low side of your opto to common (ground).

Ron
 

panic mode

Joined Oct 10, 2011
4,864
Reloadron is correct about wiring. connection in post #8 is ok for PNP sensor. but since you are using NPN sensor it need to be modified.
1763759937399.png

note that it sensor on will be LOW state. if you want HIGH state you need to rewire the other side of the optocoupler:
1763760587920.png
 

Thread Starter

crescendo93

Joined Nov 21, 2025
27
So all I would need to do is change the sensor from NPN to PNP and the diagram I posted in #8 is okay on both sides? Just want to make sure I don't cause the board any damage.
 

sghioto

Joined Dec 31, 2017
8,633
So the only resistor I would have to use here is the 3K between the 3.3V and the PC817 module?
And by "remove jumper", what does that mean?
Are all the other wirings okay?
The 3K resistor is on the module.
The jumper is that yellow plastic piece.
 
Last edited:
Top