Replace a mechanical relay with a NPN transistor - Page 1
Forum section: electronics / projects

author: MissingLink_NZ
author_rank: Newbie
timestamp: February 24, 2023, 09:31:01 am
content: Hi,I am currently grounding an input of 5V, 100uA circuit using a mechanical relay via a PIC MCU when an input signal dictates the conditions require me to. Due to physical PCB height constraints, I am wanting to ditch the mechanical relay for a NPN transistor (BC817). I am a novice in this area and would like to better understand if it would be safe or practical to do so without damaging the transistor? Based on the said voltage and current requirements, would a NPN transistor be a viable option?Sorry for my bad English!Thanks!

author: wraper
author_rank: Unknown
timestamp: February 24, 2023, 09:35:24 am
content: if it's really 100uA I doubt you even need a transistor. Most likely it can be connected right to MCU. Also 100uA current is so small it may cause problems with relays unless they are small signal type with gold plated contacts.

author: MissingLink_NZ
author_rank: Newbie
timestamp: February 24, 2023, 09:44:50 am
content: Hi wraper,Thank you for the quick response! The input to my MCU itself is essentially a sense wire from the external device that outputs 5V, if that 5V output to my MCU is grounded, the input device then disables another function not related to my MCU. I hope that makes sense. If I only use the MCU and no transistor, I don't understand how my MCU could directly ground the external device?Thank you

author: shapirus
author_rank: Super Contributor
timestamp: February 24, 2023, 10:20:38 am
content: Quote from: MissingLink_NZ on February 24, 2023, 09:44:50 amTry to set the pin mode to output and its level to LOW.p.s. but that sounds dodgy anyway. It's better to use a transistor to ground the input signal and the MCU (or whatever you want) to control the transistor.

author: ledtester
author_rank: Super Contributor
timestamp: February 24, 2023, 10:51:09 am
content: Quote from: MissingLink_NZ on February 24, 2023, 09:44:50 amA GPIO line is basically a transistor itself.

author: srb1954
author_rank: Super Contributor
timestamp: February 24, 2023, 10:53:06 am
content: Quote from: shapirus on February 24, 2023, 10:20:38 amYou can only directly connect the MCU pin to the external device if the MCU is also running off the same 5V supply as the external device or has outputs that are 5V tolerant. You have to be careful that the supply voltage to the MCU isn't removed while the external device still has its supply voltage on. Failing to do so can result in a "latch-up" condition in the MCU which could destroy the MCU when its power is restored.It is generally safer to use an external transistor, or MOSFET, to buffer the output of the MCU. With an external transistor or MOSFET you don't have worry about power sequencing between the MCU and external device.External transistors are also generally much more rugged than the output drivers of an MCU and protect against excessive voltages and currents so your MCU is less likely to get blown up by transient voltages and ESD.

author: shapirus
author_rank: Super Contributor
timestamp: February 24, 2023, 12:36:44 pm
content: This is an excellent explanation for the term "dodgy" that can be put in an MCU best practices article

author: Peabody
author_rank: Super Contributor
timestamp: February 24, 2023, 03:50:35 pm
content: As to your original question, you certainly can use the transistor in place of the relay. Connect the collector to the thing you want to ground, the emitter to ground, and the base to the GPIO pin though a resistor of 10K to 100K. Bring the GPIO pin high to turn on the transistor, which makes the connection to ground.