Switching power with transistors, relays and microcontrollers

Check out the new site at https://rkblog.dev.

Simplest way to automate, control electronic devices is to turn them on or off with an electronic switch. We can find relays or transistors that when combined with a microcontroller board can switch power delivered to selected devices. In this article I'll showcase both components and examples of usage with pyMCU microcontroller.

Transistor

Transistors are the building blocks of computers. Each CPU has enormous amount of them on the chip. We need one plain transistor that can be bought in electronics stores. Just look on voltage and current it can handle. It must be bigger than the one you want to use. Usually a transistor can be used to switch power for low power devices like a motor in a robot car. A typical transistor will have three pins: Base, Collector and Emitter. Current applied to base opens a path for bigger current flowing from collector to emitter. In field-effect transistors there is Source, Drain and Gate which controls the flow from drain to source. On youtube there is a great veritasium clip explaining how transistors are made and how they work.

BD911 transistor

I used a BD911 transistor (B, C, E) and field-effect IRF530N (G, D, S) transistor. Digital pin from pyMCU (D1) was connected to gate/base. Two other pins got wires for the current needed to power the external device (LED for tests). Just set digital pin to high (pinHigh) to turn the switch on, and set it low (pinLow) to set it off. Not that it's not a total switch. Some current flows from base to emitter and it even can turn the led on.

Transistor connected to pyMCU

Relays

Electromagnetic Relays often look like black boxes with few pins sticking out of them. They are also found on PiFace and many other boards (like multi-relay Arduino compatible boards). A coil when powered generates electromagnetic field that pulls or pushes a metal switch. This switch opens or closes a connection on separate circuit. Relays can handle bigger currents than transistors and they physically disconnect the circuit when off. Note that various relays may requite various current for the coil. A 5V can be powered from the microcontroller board, but like 12V relay will need its own cold power supply (which can be turned on/off by a transistor). Do not play with high currents, and be careful when switching it in the final circuits.

Electromagnetic relay

You can find relays as parts and relays mounted on small boards with nice pin layout and some extra features. Those features are often optoisolation (check board description) which separates coil power lines with transoptors. That protects the microcontroller from getting burned when the relay malfunctions (and high current would go to the board. Transoptors consist out of a LED and a photodiode. The LED glows on the photodiode on which photons induce current. High current can't produce more light - it would just fry the LED protecting more expensive microcontroller.

On its own relay has two pins for the coil (one for digital pin and one for VCC) and three pins to control the current that you want to switch: COM (input), NO (normally open - output disconnected when cold) and NC (normally closed - output connected when cold). When there is no current on the coil NC is connected and NO not. When you apply current to the coil they switch. Switching can be easily heard as metal hits metal in the switch.

Relay at work

To apply current on the coil just set the digital pin to high.

Relays on boards may have more pins. There are simple thee pin configurations: +/- for power and third for signal (easy way to power high voltage coil and control it from a microcontroller). There can be also 4 pins where there are two pins for controlling the coil. One must be high and one must be down (or connected to GND). Below you can see a 4 pin board

Relay on a board

Electromagnetic relays may switch big currents, but when you really have strong devices (industry grade) then you may also want to look at SSR relays that can handle even more current.

RkBlog

Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact