You are on page 1of 22

/* Household Light Toggle

Uses a transistor connected to pin LED_BUILTIN


as

a switch for a relay that toggles on and off a

household lamp at the press of a button.

This sketch was written by Michel Lagacé, 2018-


10-08

This code is in the public domain. */

// Button value will be read from pin 12

#define INPORT 12

#define OUTPORT 11

// Time to wait in milliseconds to consider switch


debounced

#define DEBOUNCE_DELAY 10

// LED state kept across loops

static bool outputValue;

// Setup the board.


void setup() {

pinMode(INPORT, INPUT);

pinMode(OUTPORT,OUTPUT);

outputValue = LOW;

digitalWrite(OUTPORT,outputValue);

// Wait for an edge and return state

bool waitForEdge() {

bool startValue = digitalRead(INPORT);

bool newValue = startValue;

while (newValue == startValue) {

newValue = digitalRead(INPORT);

delay(DEBOUNCE_DELAY);

return newValue;

// Repeat forever
void loop() {

// Wait for a rising or falling edge

bool value = waitForEdge();

// Toggle output on dropping edge (input is LOW


when button is pressed)

if (!value) {

outputValue = !outputValue;

digitalWrite(OUTPORT, outputValue);

Skip to content

Mike's Electro Shack


Electronics and software tips and tricks
 Home
 Contact
RECENT POSTS: MIKE'S ELECTRO SHACK

A Light Activated Switch


Sensing Temperature and Humidity
CPU Interrupted
Switch Debouncing
Morse Code Reader
Raspberry Pi Speaks Arduino
Programming with Class
A Better Transistor Switch Circuit
Transistor Driven Relay Switch
LED Toggle with a Push-Button Switch

Transistor Driven Relay Switch


Up to now, we have seen how to input digital (on and off) information from a
simple electromechanical device, the push-button, and how to output a digital
signal to an LED. What if, instead of an LED, we wanted to turn on and off
an actual household light. There are obvious electrical differences between
LEDs and household lights. LEDs only require a few milliamperes
of current and work very well in low voltage circuits such as the 5 volts
provided by an Arduinoboard. A household light, on the other hand,
requires mains electricity, between 110 and 240 volts depending on your
location, and higher currents, between 200 milliamperes and one ampere. A
microcontroller digital output cannot directly provide enough power to light a
household light.
Relays are electromechanical devices, like the push-button. Instead of
requiring a mechanical force to push on the device to close or open a circuit,
it relies on an electromagnet to pull on a metal plate to close the circuit
with metal contacts with which the plate makes a connection. We can thus
close or open a higher voltage and higher current circuit by applying a
voltage to the relays electro-magnet that pulls on a metal plate to close the
circuit.
It the following sections, we will have a look at a few electronic and
electromechanical devices: the relay, the diode, and the transistor.

Relays
Relays have two distinct, electrically independent parts. The first part is an
electromagnet, a coil of insulated copper wire wound around a metal bracket,
the yoke, that becomes magnetic when a current is applied to it. The other
part is a spring-loaded metallic plate or armature resting on metal contacts.
When a current is applied to the electromagnet, enough force is applied to the
armature for it to disconnect from its resting contacts and make an electrical
connection with another set of metallic contacts on which the armature rests
for as long as there is electrical current in the electromagnet. When current
stops flowing in the electromagnet, the spring attached to the metallic
armature forces it back to its initial resting position, making an electrical
connection between the metallic plate and the resting contacts.
Relays come in a variety of sizes and ratings. Ratings are used to select a
relay to be used for specific applications. Automotive relays, for instance
have rated coil voltages of 12 volts. Relays intended to be used in household
appliances have rated coil voltages of 120 or 240 volts. Following is a list of
relay coil ratings supplied by manufacturers.

 Rated Coil Voltage – the


voltage that is intended to
be applied to the coil to
operate the relay.
 Pull-In Voltage – the
minimum voltage that can
be applied to the coil for
it to operate the relay.
 Drop-Out Voltage – the
voltage below which an
activated relay will return
to its resting state.
 Maximum Continuous
Voltage – the maximum
voltage to be applied to
the coil above which
permanent damage
occurs.
 Nominal Operating
Current – current
flowing through the coil
when the rated coil
voltage is applied.
 Nominal Operating
Power – the power used
by the coil when the rated
coil voltage is applied.
 Coil Resistance – the
continuous current
resistance of the coil in
ohms.
Within circuit diagrams, the electromechanical relay is represented as a coil
and contacts as in the following diagram depicting normally open and
normally closed relay forms.
Relay contacts have ratings stating the relay’s intended use. Following are
relay contact ratings supplied by manufacturers.

 Contact Forms – the


contact mechanism and
the number of contacts in
the contact circuit.
 Form A – normally
open (N.O.) contact.
 Form B – normally
closed (N.C.) contact.
 Form C – changeover
contacts.
 MBB – Make-Before-
Break contacts where
normally open contacts
close before normally
closed contacts break
open.
 Rated Switching
Power – the intended use
value in watts of the load
that can switched by the
contacts.
 Maximum Switching
Voltage – maximum
voltage that can safely be
switched by the contacts.
 Maximum Switching
Current – maximum
current that can safely be
switched by the contacts.
 Maximum Switching
Power – the maximum
power to be switched by
contacts above which
damage may occur.
For the current project, we will use a 5 volts miniature relay, the FRS10C-
S12, to turn on and off a household lamp. This relay has the following coil
characteristics:
 Rated coil voltage of 5
volts.
 Pull-in voltage of 3.75
volts.
 Pull-out voltage of 0.5
volts.
 Nominal operating
current of 70 mA.
 Coil resistance of 70 Ω.
It has the following contact characteristics:

 Form C contact form,


changeover contacts.
 Contact rating of 12A at
125VAC (1,500W) or
10A at 250VAC (2,500
W)
The chosen relay’s coil operates on 5 volts, suitable for an Arduino provided
power supply, but requires 70 mA to operate, which is much larger than the
rated output current of 20 mA that each Arduino digital output can provide.
We need a device that can boost the current provided by the digital output pin
to drive the relay coil. That device is a transistor.

Transistors
Transistors are semiconductor devices used to amplify or switch electronic
signals and electrical power. There are many types of transistors, but the
most common is the bipolar junction transistor (BJT). There are two types of
bipolar junction transistors, the NPN and PNP types, describing the material
and configuration used to build the device. For the current project and
tutorial, we will use an NPN transistor. There are several configurations that
transistor circuits may use. In order to explain how the transistor operates, I
will be using the common-emitter configuration, that is a transistor circuit
with its emitter directly connected to ground. Consider the following circuit.
The transistor, labelled Q1, is at the center of the diagram. It is represented
by a circle with a vertical bar from which three branches are attached. The
diagonal branch at the top is called the collector. It is connected to a resistor,
RLoad, representing the device to be switched on or off. The branch to the left
of the transistor symbol, perpendicular to the bar is called the base. It is
connected to a resistor, RBase, that controls the current flowing into the base.
Finally, the diagonal branch with the arrow pointing outward is the emitter. If
the arrow had been pointing towards the bar inside the symbol, we would
have a PNP transistor. In this circuit, the emitter is connected to ground. The
principle of operation of the transistor is that a small current flowing from the
base to the emitter of the transistor will allow a larger current flowing from
the collector to the emitter, thus amplifying the base current.
Transistors, like other electronic devices, have specifications telling us about
the electrical limitations of the devices as well as information about their
capabilities. In the circuit that we will build later on, we will use a BC337-
40 NPN Bipolar Junction Transistor. I have found the following information
from the product data sheet provided at SparkFun.
 Maximum Collector-
Base Voltage |VCES| – 50
V, the maximum voltage
drop between the
collector and the base.
 Maximum Collector-
Emitter Voltage |VCEO| –
45 V, the maximum
voltage drop between the
collector and the emitter.
 Maximum Emitter-Base
Voltage |VEBO| – 5 V, the
maximum reverse voltage
drop between the emitter
and the base.
 Maximum Collector
Current |IC| – 800 mA,
the maximum amount of
current that can flow
through the collector.
 Power Dissipation |PD| –
650 mW, the power
dissipation of the device.
 Forward Current
Transfer Ratio |hFE|,
minimum – 250, the
minimum amount of
current amplification
between the base current
and the collector current.
Here is a picture of the BC337-40 transistor above. It comes in a TO-
92package, a small plastic half cylinder with a flat face on which the
transistor markings are written and three metal pins sticking out at the bottom
of the package. When the transistor’s flat face is facing the reader, the
collector pin is at the left side of the transistor, the base is the center pin, and
the emitter pin is at the right side of the transistor.

Transistor Operation
One characteristic that all silicon transistors have is the forward bias voltage
required between the base and the emitter for the transistor to work.
Remember that in a previous post titled the Blink circuit, we saw that
the LED had a constant voltage drop across its anode and cathode. Similarly,
transistor based on silicon have a voltage drop of approximately 0.7 volts
between their base and emitter when in operation. Below that voltage, no
current flows through the base nor the collector. When the base-emitter
voltage (VBE) of the transistor is increased to 0.7 volts, current starts flowing
through the base and through the collector. The base-emitter voltage remains
at 0.7 volts while current flows through the base. The amount of current
flowing through the collector (IC) is proportional to the current flowing
through the base (IB) times the Forward Current Transfer Ratio (hFE) of the
transistor.
IC = IB = 0,   if VBE < 0.7 V
IC = hFE•IB,  if VBE ≥ 0.7 V

Looking at the circuit above, an increase of voltage at V in will get VBEto


increase until it reaches 0.7 volts. Since no current flows through R Base, the
voltage drop across the resistor is 0 volts and VBE = Vin. While VBE is less than
0.7 volts, the transistor is said to be in cut-off mode. As Vin is increased
beyond 0.7 volts, current starts flowing through the base and collector,
making the transistor enter the normal amplification operation mode.
Following Ohm’s law, I = V / R, and since the current going through the base
is the same as the current through the base resistor
IB = (Vin – 0.7 V) / RBase
In the normal amplification operation mode, the voltage across the load
resistance RLoad, VLoad, is proportional to the collector current, itself
proportional to the base current.
VLoad = IC•RLoad
VLoad = IB•hFE•RLoad
VLoad = ((Vin – 0.7 V) / RBase)•hFE•RLoad
As the voltage across RLoad increases, the voltage across the transistor’s
collector and emitter pins decreases until VCE reaches 0 volts. At that point,
any increase in the base current has no effect on the collector current and the
transistor is said to have reached saturation. At saturation, we have:
IC = VCC / RLoad
The load to be turned on and off is the relay described earlier with a coil
resistance of 70 Ω for a supply of 5 volts. At saturation, the collector current
is 5 V / 70 Ω or approximately 71 mA. When connecting the base of
transistor to an Arduino digital output pin through the base resistor, V in is 0
volts when a LOW is output to the digital output and 5 volts when a HIGH is
output to the digital output. Another way of stating this could be that any
input voltage below 2.5 volts is LOW and anything above 2.5 volts is HIGH.
Hence, we want a saturated transistor when the input voltage is above 2.5
volts. Assuming a transistor hFE of 250, we can use the equation to compute
VLoad from Vin to compute RBase
VLoad = ((Vin – 0.7 V)•hFE•RLoad / RBase)
RBase = (Vin – 0.7 V)•hFE•RLoad /  VLoad
RBase = (2.5 V – 0.7 V) •250•70 Ω / 5 V
RBase =  6,300 Ω
The closest resistor value in my kit is 10K, which should be close
enough. Lets now build the circuit. First, we replace the resistive load by the
relay coil and we replace the base resistor with a 10 K resistor. The Vin input
is replaced with the Arduino’s digital output pin 11.
Note the addition of a new device between the terminals of the relay’s coil.
Its symbol resembles that of the LED that we saw in previous projects, but
without the outward arrows. The device is called a diode. As for the LED,
current flows in the direction of the arrow, from anode to cathode. The diode
in this circuit serves as a protection for the transistor. The coil of the relay
stores energy as it is turned on and it releases that energy when it is turned
off as a voltage pulse that can damage the transistor by exceeding its
maximum rated collector voltage. The diode acts as a short, preventing the
spike from damaging the transistor. In normal operation, the diode does not
let current through.

Let’s have a look at a graph plotting the voltage drop across the relay’s coil
as a function of the voltage at the Arduino’s digital output pin.
On the plot, we see that the voltage applied to the load is 0 volts for as long
as the input voltage is below 0.7 volts. Then, as input voltage increases,
voltage at the relay coil increases until the input voltage reaches
approximately 3.5 volts, at which point the voltage drop across the relay’s
coil reaches 5 volts and the transistor enters saturation.

The vertical blue bands represent the guaranteed LOW and HIGH voltage
values output by the Arduino’s digital output pin. The top horizontal blue
band represents the voltage zone in which the relay is on and the bottom blue
horizontal band represents the voltage zone in which the relay is guaranteed
to be off. In the diagram, we note that the relay is off for all guaranteed
values for Arduino’s LOW output and that it is on for all guaranteed values
for Arduino’s HIGH output.
The maximum base current is 5 V / 10 K, or 0.5 mA, a totally acceptable
value for the Arduino’s digital output capability. The next step, is to connect
a household device to the relay.

The Final Circuit


The relay contacts are exactly like wall switch contacts and can be used to
turn on or off household appliances. In the final circuit, the relay contacts are
inserted as a switch in one of the wires of a lamp’s power cord. A push-
button is used to turn the lamp on and off using the Arduino program
described in the LED Toggle with a Push-Button Switch post.

Breadboarding
The following picture depicts how to connect the different parts using a
solderless breadboard, jumper wires, a transistor, a diode, a relay, a push
button and two 10K resistors. Connections to the household appliance are not
shown.
The Program
The following Arduino program completes the post. Cut and paste the code
in your Arduino IDE and download it to complete the project. It will toggle
the lamp on and off at each press of the pus button.

/* Household Light Toggle

Uses a transistor connected to pin LED_BUILTIN


as

a switch for a relay that toggles on and off a

household lamp at the press of a button.

This sketch was written by Michel Lagacé, 2018-


10-08

This code is in the public domain. */

// Button value will be read from pin 12

#define INPORT 12

#define OUTPORT 11

// Time to wait in milliseconds to consider switch


debounced

#define DEBOUNCE_DELAY 10

// LED state kept across loops


static bool outputValue;

// Setup the board.

void setup() {

pinMode(INPORT, INPUT);

pinMode(OUTPORT,OUTPUT);

outputValue = LOW;

digitalWrite(OUTPORT,outputValue);

// Wait for an edge and return state

bool waitForEdge() {

bool startValue = digitalRead(INPORT);

bool newValue = startValue;

while (newValue == startValue) {

newValue = digitalRead(INPORT);

delay(DEBOUNCE_DELAY);

return newValue;
}

// Repeat forever

void loop() {

// Wait for a rising or falling edge

bool value = waitForEdge();

// Toggle output on dropping edge (input is LOW


when button is pressed)

if (!value) {

outputValue = !outputValue;

digitalWrite(OUTPORT, outputValue);

WARNING:
The project in this post involves household mains high-voltages. Use caution
whenever dealing with high-voltage wiring, including following directions
carefully and following general safety practices. Safe assembly and operation
of this project is the user’s responsibility. If unsure or if local laws prohibit
the assembly of high-voltage circuits, get the help of a professional
electrician. Do not make changes to the system while the device is plugged
in.

SHARE THIS:
 Twitter
 Facebook

A Light Activated SwitchDecember 26, 2020In "Electronics"
A Better Transistor Switch CircuitOctober 10, 2018In "Electronics"
Switch DebouncingMay 30, 2020In "Electronics"
PUBLISHED BY

Michel Lagacé
More than 40 years working in the high technology sector, I now share tips
and tricks on software and electronics. I also love to cook and to write in my
spare time. View all posts by Michel Lagacé
Posted onOctober 8, 2018AuthorMichel
LagacéCategoriesElectronicsTagsArduino, Electronics,learning, push
button, relay, transistor, tutorial
Leave a Reply

Post navigation
PREVIOUSPrevious post:LED Toggle with a Push-Button Switch
NEXTNext post:A Better Transistor Switch Circuit
Website Powered by WordPress.com.

You might also like