# Rain sensor + transistor

I have a Raspberry with a rain sensor connected. In order to avoid electrolysis on the rain sensor I would put a transistor (2N3904) in the circuit to power on the rain board only few times per day, read the sensor, then power off. I would ask if the circuit I drew is right. In the scheme I put a “Vibration Motor” just for simulating the circuit (see attachments). In my case it will be substituted with the board of the rain sensor: the GPIO pin of Pi with 5v (my sensor works with 3.3v but I used 5v for showing two different sources) will be connected to Vcc pin on rain board, the Collector of the transistor will be connected to GND on the rain board and the Emitter will be connected to GND of Pi. The Base of the transistor will be connected to a GPIO pin set as output (3.3v). A python script will put this pin = True at specific intervals. Could the above circuit work? Forgive me if I made any trivial mistake. Thanks

• It looks riget; you just need to determine the current draw of the sensor to make sure to provide enough base current to turn on the transistor. Nov 27, 2019 at 21:03
• Thanks for the reply. I checked on the web and the sensor should draw 15mA at 5v but, as per supplier diagram, the connection with Pi must be made by 3.3v. My 3 B+ Pi should be able to supply 50mA minimum (everything else connected). Could this be right to work? Nov 28, 2019 at 7:06

I believe this should be able to work, and I will provide the calculation for selecting the correct base resistor to ensure the transistor is turned fully on.

You state the sensor draws 15 mA when on, so let's be conservative and aim for a collector current of 20 mA. According to the data sheet of the transistor it has a current gain of at least 100, therefore the base current Ib must be:

$$I_b=\frac{I_c}{h_{FE}}=\frac{20mA}{100}=200uA$$

The resistor is then chosen to allow this amount of current to flow when 3.3V is applied by your GPIO. The base-emitter junctions typically drops 0.7V so there will be 2.6V across the base resistor. Using ohms law the appropriate restore can be calculated:

$$R_{base} = \frac{3.3V - 0.7V}{200uA}=13k\Omega$$

Round down to 10k ohm to be sure the transistor is fully on and you should be good to go!

• Many thanks! A very useful explanation! I’ll proceed as suggested. Nov 28, 2019 at 16:45
• You are welcome! Nov 28, 2019 at 16:47