How to Use Relays and Transistors With Arduino - Lesson #18
by lucascreator in Circuits > Arduino
23 Views, 1 Favorites, 0 Comments
How to Use Relays and Transistors With Arduino - Lesson #18
In this lesson, we move beyond reading sensors and blinking LEDs and begin controlling real hardware safely.
The goal is simple but powerful: allow a small microcontroller to command devices that operate at higher voltages or draw more current than it can supply directly.
To achieve that, we explore two essential electronic components that make this possible in countless real-world systems: relays and transistors.
This article corresponds to lesson #18 of the 24-part Arduino for Beginners series and demonstrates how these components allow an Arduino UNO to control external loads such as motors while remaining protected from electrical stress.
Supplies
For today's project, you'll need:
- DFRobot MindPlus Arduino Coding Kit
- 1 x Arduino UNO
- 1 x I/O expansion shield
- 1 x LM35 sensor
- 1 x Push button module
- 1 x I2C LCD module
- 1 x Relay module
- 1 x Small motor
- 1 x Diode (H23 FR207G)
- 2 x Resistors (2k and 5kΩ)
- 1 x Transistor (BC547)
- 1 x LED
- 1 x Breadboard
- 1 x Battery
- A few connecting wires
YouTube Tutorial
I've recently posted a tutorial about this project on YouTube explaining everything you can read on this article. You can watch it right above.
Understanding the Relay
A relay is best understood as an electrically controlled switch. Instead of a finger flipping a lever, an electromagnetic coil performs the switching action.
When current flows through the coil (see the last image), it produces a magnetic field that pulls a metal contact into a new position. That physical movement either opens or closes an electrical path. Although the relay is triggered electronically, the switching mechanism itself is mechanical.
This property is extremely valuable because a microcontroller operates with limited current and voltage. Its digital output pins are designed to send small control signals, not to power motors, pumps, or lamps.
Attempting to drive such loads directly from a microcontroller can damage the board. A relay solves this limitation by separating the low-power control circuit from the higher-power load circuit. The microcontroller merely decides whether the relay connects or disconnects the external power source.
A typical relay module has two clearly separated sides. The control side connects to the microcontroller and powers the relay coil. The switching side behaves like a conventional switch placed in series with the load's power supply.
On the control side, three pins are present: VCC, GND, and a signal pin. When the signal pin receives a HIGH logic level, the coil energizes and the relay changes state. When the signal returns LOW, the relay returns to its resting position.
On the switching side, three terminals define how the load is connected. The COM terminal is the common connection that receives power from the external supply.
The NO terminal, meaning normally open, connects to COM only when the relay is activated. The NC terminal, meaning normally closed, is connected to COM when the relay is inactive. Choosing between NO and NC determines whether the load turns on or off when the relay is energized.
Sponsor
Before we build the project, I'd like to take a moment to thank today's sponsor: DFRobot.
DFRobot is one of the leading global providers of open-source hardware for makers, students, and engineers.
Throughout this entire Arduino for Beginners series, I'm using the MindPlus Arduino Coding Kit.
The kit includes sensors, modules, and accessories that make it easier to build any Arduino project you can imagine.
If you want to follow along and get the most out this series, I highly recommend checking out a kit like this one.
Thank you again to DFRobot for sponsoring this course and helping make STEM education more accessible to everyone.
Project 1 - Temperature-Controlled Fan Using a Relay
The practical demonstration expands a temperature monitoring system built in the last lesson. That earlier system used an LM35 sensor to measure temperature and display the value on an I2C LCD.
The new version introduces automatic control: when the measured temperature exceeds a defined threshold, a motor activates and behaves like a cooling fan. When the temperature falls below the threshold, the fan stops. This setup represents a simplified thermostat.
For this project, you'll need:
- Arduino UNO
- I/O expansion shield
- LM35 temperature sensor
- I2C LCD module
- Relay module
- Push button module
- A small motor
- A diode (I chose one called H23 FR207G)
- A battery
- And a few jumper wires
Start by placing the I/O expansion shield on top of the Arduino.
Connect the LM35 sensor to analog pin A3.
Connect the push button to digital pin 2.
Then attach the LCD module: SCL to A5, SDA to A4, VCC to VCC, and GND to ground.
Now we add the relay. Connect the VCC pin of the relay module to five volts on Arduino, and GND to ground. Then connect the signal pin of the relay to digital pin 3.
Next comes the motor circuit. We will power the motor using an external battery. Take the positive wire from the battery and connect it to the COM terminal of the relay.
Then connect NO, normally open, to the positive wire of the motor. The negative wire of the motor goes back to the negative terminal of the battery.
There is one very important component we must add here: a flyback diode. We will use the diode H23 FR207G.
Motors are inductive devices, and when they turn off, they generate a high voltage spike that can damage transistors, relays, and other electronics. The flyback diode provides a safe path for the transient energy, preventing damage to the relay and other electronics.
Place the diode directly across the motor terminals. The striped side of the diode goes to the motors positive terminal, and the other side goes to the motor's negative terminal
Under normal operation, the diode does nothing. When the motor turns off and the voltage tries to spike, the diode conducts and protects the circuit.
If you need additional help, check the schematics above.
After finishing the wiring, go to the GitHub repository for this series. Open the folder for lesson 18, download the file thermostat-prototype, and open it in the Arduino IDE.
Before uploading the sketch, make sure the DFRobot_RGBLCD1602 library is installed. Once the library is ready, upload the code and watch your board.
At first glance, this project looks similar to the one we built in the previous lesson. When the system starts, the LCD shows the temperature in Celsius. Pressing the push button switches the display to Fahrenheit, and pressing it again changes it back to Celsius.
Now here's the interesting part. Bring a lighter close to the LM35 sensor to raise its temperature. You'll see the value increasing on the LCD in real time.
Once the temperature passes a defined threshold, the fan starts spinning. After some time, when the temperature drops again, the motor stops.
Very interesting, right?
Now that you've seen the project working properly, let's go back to the sketch and understand how everything works behind the scenes.
Code
Most of the code is the same as in the previous tutorial. We initialize the LCD, read the temperature from the LM35, and handle the button that switches between Celsius and Fahrenheit.
The new part is the control logic for the relay. We define a temperature threshold, for example 50ºC. Then, inside the loop, after reading the temperature, we compare the measured value with that threshold.
If the temperature is higher than the threshold, we set digital pin 3 to HIGH. That sends a signal to the relay module, energizes the coil, and closes the path between COM and NO. The battery is now connected to the motor, and the fan starts spinning.
If the temperature is below the threshold, we set pin 3 to LOW. The relay coil is deactivated, the internal contact opens, and the motor loses power.
This is the key idea: Arduino is not powering the motor. It is only deciding whether the motor's own power source is connected or disconnected.
Understanding Transistors
Relays are effective but not always ideal. Because they rely on mechanical movement, they produce audible clicks, operate relatively slowly, and eventually wear out. For applications requiring rapid or silent switching, transistors provide a better solution.
A transistor is a semiconductor device that controls current flow using an electrical signal rather than mechanical motion. Instead of moving parts, it relies on the behavior of charge carriers within a semiconductor material. This allows extremely fast switching speeds, silent operation, and long service life.
The transistor used in this lesson is the BC547, a common NPN bipolar junction transistor. It has three terminals: base, collector, and emitter.
The base acts as the control input. When a small current enters the base, it allows a larger current to flow from collector to emitter. In this sense, the transistor behaves as an electronically controlled valve. The current through the load is not supplied by the base signal; the base merely permits or blocks the main current path.
But if the transistor is a PNP type, the base requires a negative signal to allow current to flow from the emitter to the collector. As you may noticed, one is the opposive of the other.
This behavior can be understood through current amplification as well. A tiny base current influences a much larger collector current according to the transistor's gain.
Because of this property, transistors are not only switches but also amplifiers. They can control power levels far beyond what a microcontroller pin can deliver directly, provided the transistor is properly chosen for the load.
We had a lot of theory about transitors so far. Let's get our hands dirty and build another cool project.
Project 2 - Temperature Indicator Using a Transistor
To demonstrate transistor control, the thermostat concept will be implemented again, but this time we'll replace the relay with a transistor driving an LED. Unfortunetely, the transistor we'll use today is not powerful enough to work with motors. That's why the LED will replace the motor.
In addition to the materials we used previously, you will need:
- An breadboard
- Two resistors (2k and 5kΩ)
- A transistor (I chose one called BC547)
- And a LED
Start by removing the relay connections. Place the BC547 transistor on the breadboard. Then attach the LED, a 5k resistor on base, and the 2k resistor on the LED.
After that, connect the positive coming from Arduino's 5V pin on the 2k resistor and the negative wire goes to emitter.
In the end, connect a jumper wire from digital port 3 to transistor's base through the 5k resistor.
The rest of the circuit, including the sensor, LCD, and button, remains the same. You can also upload the same code.
If the wiring sounds confusing, just follow the schematic above.
Now that's ready, let's test the project.
When digital pin 3 outputs HIGH, a small base current flows and the transistor turns on, allowing current to pass through the LED to ground. When the pin outputs LOW, the transistor turns off and the LED goes dark.
Although the demonstration uses an LED, the same principle applies to many DC loads. For higher current applications such as motors, a different type of transistor known as a MOSFET is typically used because of its efficiency and ability to handle larger currents.
Relay VS Transistor - Choosing the Right Component
The choice between relay and transistor depends on the electrical requirements of the system.
Relays provide electrical isolation between control and load circuits, making them suitable for switching high voltages, AC power, or systems requiring complete separation.
They are conceptually simple and behave like physical switches, which can be advantageous in mixed-voltage environments.
Transistors excel when switching must occur rapidly, silently, or repeatedly over long periods. They are compact, efficient, and capable of precise control.
In modern electronics, transistor-based switching dominates because of reliability and speed. However, relays remain indispensable where isolation or compatibility with AC systems is required.
From a system design perspective, both components serve the same conceptual role: they allow a low-power control signal to govern a higher-power circuit.
The difference lies in how that control is implemented. Relays rely on magnetism and mechanical movement, while transistors rely on semiconductor physics. Understanding both approaches provides flexibility when designing real-world control systems.
Conclusion
Relays and transistors appear repeatedly in electronics projects because they bridge the gap between control logic and physical action.
Mastering them marks an important step toward designing systems that interact with the real world safely and reliably. These components transform a simple microcontroller into a true control unit capable of commanding motors, lights, and other devices.
To strengthen your foundation before moving forward, read the article for lesson #6 of the series, which provides a complete beginner-friendly introduction to Arduino programming and explains the core concepts that make projects like this possible.
Thanks for reading, and I'll see you in the next lesson!