ESP32 – Moisture Sensor Transistor Switch Setup

esp32resistorstransistors

I'm doing the classic water moisture sensor project with my ESP32, but I want to be able to switch the sensor on and off since that will reduce the amount of corrosion that occurs. Can I use a BC547 transistor as suggested below to achieve turning using GPIO18?

enter image description here

I've also wired an LED in series to show when it's reading/not reading. Would I need the resistor if I didn't have this LED?

Please be easy on me – I'm a beginner to all of this.

CORRECTIONS (and removed the led):

enter image description here

Best Answer

Your proposed setup will not work, but the second (corrected) one will. Here are some issues in the first setup:

  1. Your NPN transistor is upside down. The emitter needs to be connected to GND and the collector to the GND pin of the sensor (the current path is from the sensor to GND).
  2. You can't put the LED and the resistor in the GND supply path of the sensor. They will prevent the sensor from getting the needed supply voltage between the Vcc and GND pins.

The solution would be to put the BC547 in the right way, remove the LED and 220 Ohm resistor in the GND path and put a resistor of around 2.2K between D18 and the base of the transistor. The 2.2K resistor is for limiting the base current from the GPIO pin while assuring enough current can flow through the transistor.

This way, your transistor can switch the GND current to the sensor on and off freely. If you need an indicator, you can use a different GPIO pin, and LED and a resistor to provide it.

schematic

simulate this circuit – Schematic created using CircuitLab