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 Arduino board. 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.

Relay Parts

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.

Relays

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.

NPN Transistor

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.
transistor cbe

Here is a picture of the BC337-40 transistor above. It comes in a TO-92 package, 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

Common Emitter

Looking at the circuit above, an increase of voltage at Vin will get VBE to increase until it reaches 0.7 volts. Since no current flows through RBase, 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, Vin 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.

Transistor Relay Switch

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.

Simple Transistor Relay Switch Plot

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.

Connected Transistor Relay Switch

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.

Transistor Driven Relay Switch_bb

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.

LED Toggle with a Push-Button Switch

This new circuit is an evolution of the LED blinker circuit and program. Instead of using a delay to alternately turn the LED on and off, we are going to use a push-button to signal the system to toggle an LED on or off.

Hooking Up a Push-Button Switch to the Arduino

A push-button switch is a very simple electro-mechanical device that has a spring-loaded contact, keeping the circuit open, that is unconnected, until the button is pressed, forcing a metal piece to bridge internal contacts thus closing the electrical circuit and allowing current to flow through. The circuit re-opens as the pressure on the push button is released, preventing current from flowing.

Floating Inputs

Let’s connect normally open push-buttons as in the following diagram.

Push Button

While the button is pushed, the circuit works. S2 is connected to the power supply, providing a HIGH value to the input pin; S1 is connected to ground, providing a LOW value to the input pin. When the push-button is not depressed, what is the value at the input pin? It is undefined, neither HIGH or LOW, or possibly fluctuating between the two values as the unconnected input acts like an antenna, picking up signals from the surrounding electromagnetic noise. This situation is known as floating inputs and must be corrected.

There is a solution. As shown in the following diagram, we can connect a resistor to ground, called a pull-down resistor, for the switch connected to the power supply; or connect a resistor to the power supply, called a pull-up resistor, for the switch connected to the ground. These resistors will ensure that the value at the input pin is always in a known state.

Pull-up Pull-down
Pull-up and pull-down resistors

The value of R can be very high as the Arduino’s digital pin input impedance, that is its opposition to current when a voltage is applied, is very high.  My current SparkFun kit has 10K resistors that will do the job. Setting R to 10K will limit the current to 0.5mA (remember, V = RI, I = V/R) when the switch is closed, allowing current to flow. The Arduino offers an input pin mode that programmatically attaches a 20K pull-up resistor to the input pin, limiting the current to 0.25 mA when the switch is closed. This mode, of course, only works if the push-button is connected to ground.

The LED Toggle Circuit

The circuit that we will use is shown in the diagram below. The push-button switch is connected to the Arduino‘s input pin 12 and to a 10K pull-up resistor. The LED‘s anode is connected to digital pin 13 and its cathode, to ground through a 330Ω current limiting resistor, exactly as was done in the LED blinker circuit.

LED Toggle
LED Toggle’s complete circuit

One thing that must be said about push-buttons is that because they are electromechanical devices, the electrical contact is not instantaneous and electrical noise is produced every time the switch is closed or opened. This is caused by the way spring-loaded metal pieces bounce as they make contact with metal connectors closing the circuit within the switch. This bouncing of metal against metal makes the contact close and open repeatedly for a few milliseconds. This happens as the switch closes and as it opens. There are debouncing electronic circuits that can be used to counteract this effect. In this post, we will only use a software solution explained as part of the program below.

Breadboarding

The following picture depicts how to connect the different parts using a solderless breadboard, jumper wires, an LED, a push button, a 10K resistor and a 330Ω resistor.

LED Toggle_bb

The LED Toggle Program

You can copy the following code directly in the Arduino IDE.

Following the usual header, you will find definitions for the INPORT, input port value, and DEBOUNCE_DELAY, the time in milliseconds to allow the switch to stabilize after it changes state when it closes or opens. Finally, the Boolean value outputValue holds the on and off LED values, HIGH for turned on and LOW for turned off.

First, we prepare the board circuitry in the setup() function. The pin mode for pin INPORT is set to INPUT, which will allow us to read the switch value. Digital I/O pins on the Arduino board can be set to either output a value or input a value, not both at the same time. The circuit described previously uses a 10K pull-up resistor. We could have specified the pin to be INPUT_PULLUP instead, which would have programmatically installed a 20K pull-up resistor at the digital input pin. The pin mode for pin LED_BUILTIN is set to OUTPUT, allowing the program to output HIGH or LOW values to the LED circuitry. Within setup(), the outputValue is set to LOW and sent to the output pin, thus extinguishing the LED.

/* Light Toggle
   Uses an LED connected to pin LED_BUILTIN as a light
   source toggled on and off at the press of a button.
   This sketch was written by Michel Lagacé, 2018-09-16
   This code is in the public domain. */

// Button value will be read from pin 12
#define INPORT 12

// 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(LED_BUILTIN,OUTPUT);
    outputValue = LOW;
    digitalWrite(LED_BUILTIN,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(LED_BUILTIN, outputValue);
    }
}

The waitForEdge() function waits until the switch value changes from HIGH to LOW or LOW to HIGH and then returns the new switch value. The function first reads the current switch value using the digitalRead() built-in function. It then sets the newValue variable to be the same as the value just read and loops, using a while loop, until the value of the switch becomes different from the first value read. The program then waits for DEBOUNCE_DELAY milliseconds to let the switch settle. I have measured the duration of the bounce noise produced by a micro push-button switch and found that it was never more than approximately 4 milliseconds. I believe that it is therefore safe to let the switch settle for twice that amount of time. Since the switch cannot be depressed manually for more than 50 times per second, it is also safe to assume that a 10 millisecond wait will not prevent normal operation of the switch. The limitation that a 10 millisecond delay imposes is that we will not be allowed to close and open the switch within a 20 millisecond lapse of time, having to wait 10 milliseconds when the switch is closed and another 10 milliseconds when the switch is open. The waitForEdge() function returns the last switch value read.

The while loop works similarly to the for loop seen in the Morse code generator. It executes the code contained between the curly braces until the specified condition is not met, or false. Unlike the for loop, the while loop lacks initializer and post-processing statements. it has the following structure:

    while (condition) {
        // Execute the code within the curly braces
    }

Finally, the main loop() function repeatedly waits for a switch value change through a call to the waitForEdge() function. If the returned value is LOW, that is the push-button has been depressed making the!value condition true, the LED outputValue is inverted, or toggled, and then output to digital pin LED_BUILTIN.

What Next?

This circuit shows how one can read a digital value using the digitalRead() function, how switch noise can be removed with a debouncing delay, and how the while loop control structure works. You can experiment with the circuit and program to see first what happens if you remove the pull-up resistor and move your hand around the circuit. The LED will turn off or on unexpectedly without you having touched the switch. You can also try to touch the open digital input attached to the switch with your finger. The LED will start flickering as your body acts as an antenna and picks up the 50 or 60 Hz electromagnetic signals surrounding us. As another experiment, comment out the delay() line within the waitForEdge() function and depress the switch repeatedly. At some point, the LED will remain as it was, without toggling, or will briefly flicker, quickly turning on and off.

Morse Code Generator

Lets Beef-Up the ‘Blink’ Program

A blinking circuit may be useful as a signal to warn others of dangerous situations or to draw people’s attention. Another use for a blinking light is to communicate with someone over a distance. The following program makes use of the blinking light circuit to implement a Morse code generator.

Around 1837, the American Samuel F. B. Morse invented an early version of what was to become Morse code. Morse code is a method of transmitting information as a series of on-off tones, lights, or clicks. Basically, letters, numbers, and punctuation marks are translated to a variable length collection of dots and dashes, of shorter and longer bursts of sound or light. The duration of the dot, the short burst, is the unit of time by which all other elements of Morse code are defined. A dash, the long burst, is a signal whose duration is three times that of the dot. The time between dots and dashes within an encoded character is one unit of time. the time between characters is three units of time and the time between words is seven units of time.

International Morse code is thus composed of five elements:

  • short mark, dot or ‘dit’: one time unit long
  • longer mark, dash or ‘dah’: three time units long
  • gap between the dots and dashes within a character: one time unit long
  • gap between letters of a word: three time units long
  • gap between words: seven time units long

Following, is the international Morse code equivalent, in dot (.) and dash (-) notation for each alphabetical and numerical character:

315px-International_Morse_Code.svg

For instance, if we are to show the character sequence “PARIS” in Morse code, we can represent it in text as “.–.  .-  .-.  ..  …” and if we want to represent it as a series of on and off signals, arbitrarily represented by an equal sign “=” and an underscore “_” respectively, each one time unit in duration and we follow the rules stated above, we get the following signal:

___=_===_===_=___=_===___=_===_=___=_=___=_=_=___

The following program, written in “C” for the Arduino, converts a character string of alphabetical characters into a series of short and long blinking LED signals corresponding to their morse code equivalent. You can reconstruct the program in the Arduino IDE (Integrated Development Environment) by copying the code from this post and pasting it in sequence, as it appears in this text, in the text editing pane of the IDE.

The Program Header

The program header, a few paragraphs down, contains a comment describing the nature and purpose of the program as well as a copyright notice. I am the author of the code and I have put it in the public domain. Comments are character sequences ignored by the program interpreter or compiler. Comments can be formatted as any text between a slash-asterisk, ‘/*‘, and asterisk-slash, ‘*/‘, character sequences. This is the original way to enter comments in the ‘C’ language. Text enclosed between these two-character sequences can span several lines and are ignored. Comments can also be entered after a double-slash, ‘//‘, character sequence. All text after the double-slash character sequence, until the end of line, is considered a comment and is ignored by the program interpreter or compiler.

We define the digital output PORT as LED_BUILTIN, the digital output port corresponding to the Arduino’s built-in LED. The UNIT_TIME value corresponds to the time in milliseconds of the ‘on’ duration of the dot. The #define statement allows programmers to associate text to a name. When the program is compiled, the text replaces the name whenever it appears in the program. At compile time, PORT is replaced by the Arduino LED_BUILTIN  constant and UNIT_TIME is replaced by the integer value 100. Note that LED_BUILTIN is also a definition and it gets substituted by the appropriate integer value depending on the target Arduino board.

The characters String variable and codedCharacters String array variable work in tandem as the list of supported characters and their corresponding Morse code equivalent. The Morse code equivalent is encoded as a character string containing a series of period (‘.‘) and dashes (‘‘) representing the short and long bursts of Morse code. Each string in the codedCharacters array corresponds to the character at the same index in the characters string variable.

/* Morse Code Generator
   Blink an LED connected to pin LED_BUILTIN to display morse
   code corresponding to text entered. Repeats forever.
   This sketch was written by Michel Lagacé, 2018-09-02
   This code is in the public domain */

// Output port to display morse code
#define PORT LED_BUILTIN

// unit time length of the morse encoding
#define UNIT_TIME 100

// Characters to be encoded
static String characters = "abcdefghijklmnopqrstuvwxyz";

// Morse code sequences for each character
static String codedCharacters[] = { 
    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....",
    "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",
    "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
    "-.--", "--.." };

In ‘C++‘, an array is a collection of elements accessible through the use of an index. A String is a specialized array for collections of characters. Thus, each character in a String and each element of an array can be accessed through the use of an index. An index is an integer value enclosed in square brackets ([ ]) right after the variable name. The first character of a string or element in an array has index 0. The second character or element has index 1 and each successive character or element’s index is one more than the preceding index.

In the code above, character ‘c’ is at index 2 in the characters String. The corresponding Morse code sequence, “-.-.”, is a String at index 2 in the codeCharacters String array. The characters variable is declared as static meaning that the variable’s memory is allocated once at the start of the program and remains for the duration of the program. Non-static variables in functions are allocated in memory only for the duration of the execution of the function, then released back to the pool of memory a program can use. This is very useful for very large programs as only the necessary memory is used at any time. The characters String is initialized with the character string “abcdefghijklmnopqrstuvwxyz”. codedCharacters is also declared as a static String, but the variable name is followed by square brackets. These square brackets indicate that the variable is an array. The size of the array is set to the number of elements it is initialized with. In this case, 26 elements. If an uninitialized array is to be created, the size can be specified as a value within the square brackets.

Digital Output Setup

The setup of the digital output to blink an LED is identical to the setup in the ‘BLINK’ circuit demonstration. We set the pin mode to OUTPUT.

// Setup the board. Digital port LED_BUILTIN in output mode
void setup() {
    pinMode(PORT, OUTPUT);
}

Outputting Dots and Dashes

Code can be, and should be, segregated into functions that provide specific well-defined functionality to a program. Functions, also called subroutines, are sequences of code that can be called by name. In the ‘C’ language, functions must be defined before they are used and thus must appear in the code before any code that uses them. The simplest functions have the following structure:

void functionName() {
    // Sequence of code the function performs when called
}

The void reserved word specifies that the function does not return any value. The we have the function name, followed by a sequence of code enclosed in curly braces, ‘{‘ and ‘}‘. Elsewhere in the program, if we want to call up the sequence of code contained i the function, we simply use the function name, followed by parentheses and a semicolon:

    functionName();

In the Morse code program, we first define two functions, one to output a dot, put the LED on for one time unit, then off for one time unit; and the other to output a dash, put the LED on for three time units, then off for one time unit. In the following code, you will find both functions. They are very similar to the code found in the original ‘BLINK’ program: lights on, lights off.

/* Function to output a dot: one unit on, one unit off */
void outputDot() {
    digitalWrite(PORT,HIGH);
    delay(UNIT_TIME);
    digitalWrite(PORT,LOW);
    delay(UNIT_TIME);
}

/* Function to output a dash: three units on, one unit off */
void outputDash() {
    digitalWrite(PORT,HIGH);
    delay(UNIT_TIME*3);
    digitalWrite(PORT,LOW);
    delay(UNIT_TIME);
}

Encoding a Character in Morse Code

Next, we have a function that outputs a character as a series of dots and dashes. Before having a look at this function, let’s go through a few concepts that will help people unfamiliar with ‘C++’ understand the code better.

‘C++’ Classes

In ‘C++’, functions can be attached to data structures called classes. I will not delve on classes, but suffices to say that these functions are called methods. The String data type is actually a class called String. Variables created with a class are usually called objects. In the case of the String class, because it is used as a data type, I will still call them variables. The String class has several methods associated with it. in the following function, we use the indexOf() and length() methods. The indexOf() method returns the index of the first occurrence of the character passed as an argument in the String. The length() method returns the number of characters in the String. To use a method associated to a variable, use the variable name followed by a period, followed by the method name, parentheses, and any parameter required by the method. It is like calling a function, but preceded by the variable name. Thus, to get the index of the position of letter ‘f’ within the characters String and initialize the index variable with it, use the following code:

    int index = characters.indexOf('f');

index will be initialized with the value 5, the index of the letter ‘f’ in the String characters.

The outputCharacter function introduces us to two fundamental programming structures. The for-loop, and the if-then-else structures.

if-then-else Control Structure

The if-then-else control structure allows a program to conditionally execute a portion of a program. It has the following structure:

    if (condition) {
        // Code to execute if condition is true
    }
    else if (other condition) {
        // Code to execute if other condition is true
    }
    else {
        // Code to execute if all preceding conditions are false
    }

Only the first if block is mandatory; the else if and else blocks are optional. The execution flow is as follows: if the condition specified within parenthesis after the first if is true, then the code within the curly braces following the if statement is executed; if the condition within the first if statement is false then the condition in the else if statement is checked; if the condition in the else if statement is true, then the code within the curly braces following the else if statement is executed; if the condition in the else if statement is false, then the code contained within the curly braces after the else statement is executed. There can be several else if statements, each testing a different condition.

for-loop Control Structure

The for-loop control structure allows the program to iterate through code several times until a condition is met. It has the following structure:

    for (initialization; condition; post-processing {
        // Code to execute until condition is met
    }

where initialization is a statement executed just before entering the loop; condition is a test performed at the beginning of the loop, if true, code within the for-loop curly braces is executed, if false, execution continues after the loop; and post-processing, is a statement executed after each iteration through the code within the curly brackets. For instance, in the following for-loop control structure:

    for (int i = 0; i < 10; i++) {
        // Code executed 10 times
    }

Variable i is initialized to 0 before entering the loop; i is tested if smaller than 10, since it is, the code within the curly braces is executed and i is available for use within this code; after the code within the curly braces is executed, the variable i is incremented by 1. A second iteration of the loop then starts and i is tested if smaller than 10. The loop continues until the condition i < 10 is false. The loop executes 10 times with values of i from 0 to 9.

outputCharacter Function

Looking at the function header, we find that it is similar to the previous functions, but has a parameter within the parentheses: char c. This defines a single character parameter to the function. Parameters allows the calling program to pass values to the function. In this case, the character to be output as Morse code. Within the outputCharacter function, variable index is set to the index of character c in the characters String. If the character is found, that is if index is greater or equal to 0, then we output the Morse code. To do so, we get the Morse code associated with the character using index to retrieve the Morse code string from codedCharacters. We then iterate through each character of the Morse code and output a dash, if the character is a hyphen or a dot otherwise using the outputDash() and outputDot() functions respectively. At the end of the character output, we wait an extra 2 units of time, totaling the 3 units of time required at the end of a Morse code character. The wait totals 3 units of time since we already introduced a single unit of time delay at the end of the dash or dot.

/* Function to output a single character */
void outputCharacter(char c) {

    // Find index of character to encode
    int index = characters.indexOf(c);
 
    // Ignore unencodable characters
    if (index >= 0) {

        // Encode Morse code and output it
        String code = codedCharacters[index];
        for (int i = 0; i < code.length(); i++) {
            if (code[i] == '-') {
                outputDash();
            }
            else { // if not '-', must be '.'
                outputDot();
            }
        }

        // wait 3 units at the end of the letter
        // (2 units assuming previous dot or dash)
        delay(UNIT_TIME*2);
    }
}

Encoding Text into Morse Code

The sentence function’s purpose is to output text as Morse code. It accepts the single String parameter: text. First, the function gets the length of the text and stores it in the len variable. It then iterates through the whole text String using i as the index in a for-loop structure from 0 until len – 1. It gets the character indexed by i and turns it to lowercase using the tolower() built-in function. If the character is not a space, it is output using outputCharacter(). If the character is a space, we wait an extra 4 units of time, totaling the 7 units of time required at the end of a word. The wait amounts to 7 units of time since we already added 3 units of time at the end of the previous Morse code character output.

// Function to encode a whole string
void sentence (String text) {
    // Compute length of character string
    int len = text.length();

    // Output each character in turn
    for (int i = 0; i < len; i++) {

        // Only lower case characters are encoded
        char c = tolower(text[i]);
        if (c != ' ') {
            outputCharacter(c);
        }
 
        // Spaces are encoded as 7 units,
        // (4 units assuming a previous character)
        else {
            delay(UNIT_TIME*4);
        }
    }
}

The Main Loop

Finally, this is the main loop(). This code is repeated over and over. It outputs the text “Mikes Electro Shack” in Morse code, then waits 28 units of time corresponding to 4 spaces between words. The delay is actually specified as 25 units of time since the last character output already added a 3 units of time delay. You can of course replace the text by any text you see fit.

void loop() {
    sentence("Mikes Electro Shack");
    delay(UNIT_TIME*25); // Wait 4 spaces at the end
}

What Next?

Through the implementation of this fairly simple program, we have seen two extremely important control structures, the if-then-else control structure and the for-loop control structure. We have seen that the String data type is implemented as a class and that there are methods associated with it. We also have seen a few String methods such as indexOf() and length().

This program can be put to use to learn Morse code, which is required to get one’s ham radio (amateur radio) license. Complete the list of characters to include numbers and punctuation marks and their equivalent Morse code to complete the experience.

There is Beauty in Geeky Things!

Welcome to my blog about sharing my life long passion with electronics, software design and my newfound passion with micro-controllers and their open hardware and software platforms. This blog is a series of tutorials and experiments to allow hobbyists understand the basics of electronic design and software programming and be able to reproduce the experiments, add to them and create their own circuits and programs.

Experiments in this blog make use of the Arduino Uno, a micro-controller, and a few electronic components that come with starter kits that can be found on the Internet. Experiments were tested using SparkFun Inventor’s Kit. The kit comes with a SparkFun RedBoard, a version of the Arduino Uno micro-controller, a solderless breadboard, jumper wires and electronic components such as LEDs (Light Emitting Diodes), resistors, light and temperature sensors, and more.

I recommend to the beginning enthusiast to procure a kit, whether from Sparkfun or other suppliers as they contain instructions on how to setup the Arduino, on how to get necessary software from the Internet, and how to use the software to program the micro-controller and make it work. Here is a non-exhaustive list of starter kits found on the Internet in no particular order.

Once equipped with a kit, we will embark on a series of circuits and programs that will complement and further your starter kit experience with explanations, tips and ideas.

Progress lies not in enhancing what is, but in advancing toward what will be. — Khalil Gibran

Place_Jacques-Cartier_Jan_2006