/ /Introduction
Relays require no human interaction in order for the switching to occur, they are electrically controlled mechanical switches. Inside the plastic box is an electro-magnet (coil), a switch, and a spring. The spring holds the switch in one position, until a current is passed through the coil. When the coil is energized with an electric pulse, a magnetic field is generated which moves the switch. The second part of a relay is a system of metallic arms which make up the physical contacts of the switch. When the relay is off, or no electric pulse is given to the relay, the arms of the switch are in one position. When the relay is on, or an electric pulse is sent to the relay, the swing or switching arm of the switch moves to another contact of the switch. The arm moves as the generated magnetic field pulls the swinging arm toward the inductor (or wire coil).
When the relay is in the "off" position, the swing arm is in contact with the normally closed contact. When the relay is activated, the magnetic field created by the inductor coil pulls the swing arm until it makes contact with the normally open contact connecting the circuit connected to the normally open contact to the circuit connected to the main contact.
What's Inside
Inside the relay are two paddles made of metal. One paddle is made of a ferrous material like steel and is free to move. The other paddle is made of copper and stationary. When these paddles touch (the closed switch state), they are capable of allowing a large amount
of power to flow - like 30A@120VAC.
The other half the relay is called the
coil. This is basically a small
electro-magnet. If you send current through the coil, a magnetic force is created, which pulls on the steel paddle causing it to move (flip) and touch the copper paddle - as if you flipped a light switch. The coil requires a small amount of power (5VDC @ 80mA). Controlling the low-power coil allows you to actually control quite a lot of power.
It is important to note the coil is physically isolated from the paddles. If you have 120VAC running through the paddles, you don't have to worry about that 120VAC sneaking back into and vaporizing your microcontroller (connected to the coil).
The paddles are capable of
carrying very large currents. Both AC or DC - the paddles don't care. A relay can be used to control a DC motor, or an AC lamp.
Wondering about Relays and Switches
You might be wondering: If a relay is a type of switch, and so is a
transistor, then why do we need them if we can just switch the
microcontroller pin on and off? The answer is
voltage and current
ratings.
A microcontroller pin can only control around 5 volts DC and
50 milliamps (0.25 watts). Common transistors like the ones we're
going to be using can control around 30 volts DC and around 600 milliamps (18
watts). The relay we're going to be using can control up to 220 volts AC or DC
and 30 amps of current (6600 watts).
*So, in your circuit, the small
amount of power from the microcontroller switches on the transistor,
which provides a little more power to the relay, which in turn
provides a lot of power to the high-power device it's connected to,
e.g., a light bulb, fan, or toaster.
* Remember, though, that for safety's sake, you shouldn't use the
relay anywhere near it's rated capacity. If you need more than half
the rated capacity, think about a bigger relay.
/ /Exercise 1
- Wire up the following schematic
One LED should be red, the other should be green
- Program the chip so that pin 2 is an output. You want to toggle pin 2 on and off
- Test the circuit
- Remove the red LED, and connect the motor in its place (bypass the 560 Ohm resistor). Change your code so the motor stays on longer
- Test the circuit/program
/ /Exercise 2
Thermistors are variable resistors whose resistance changes as the temperature changes.
To use the thermistor you need to create a voltage divider circuit
From Ardino Playground
/*
The Simple Code
============================================================*/
#include <math.h>
double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}
void setup() {
Serial.begin(115200);
}
void loop() {
Serial.println(int(Thermister(analogRead(0)))); // display Fahrenheit
delay(100);
}
The more elaborate program shows the power of the function.
It displays lots of data including the ADC (from 0 to 1023),
the voltage on the analog pin Look at the code to change that to whatever
your power supply is giving), the resistance of the Thermistor,
and the temperature in Celsius and Fahrenheit.
//The Elaborate Code
//============================================================
#include <math.h>
//Schematic:
// [Ground] ---- [10k-Resister] -------|------- [Thermistor] ---- [+5v]
// |
// Analog Pin 0
double Thermistor(int RawADC) {
// Inputs ADC Value from Thermistor and outputs Temperature in Celsius
// requires: include <math.h>
// Utilizes the Steinhart-Hart Thermistor Equation:
// Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]^3}
// where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
long Resistance; double Temp; // Dual-Purpose variable to save space.
Resistance=((10240000/RawADC) - 10000); // Assuming a 10k Thermistor. Calculation is actually: Resistance = (1024/ADC)
Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later. // "Temp" means "Temporary" on this line.
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); // Now it means both "Temporary" and "Temperature"
Temp = Temp - 273.15; // Convert Kelvin to Celsius // Now it only means "Temperature"
// BEGIN- Remove these lines for the function not to display anything
Serial.print("ADC: "); Serial.print(RawADC); Serial.print("/1024"); // Print out RAW ADC Number
Serial.print(", Volts: "); printDouble(((RawADC*4.860)/1024.0),3); // 4.860 volts is what my USB Port outputs.
Serial.print(", Resistance: "); Serial.print(Resistance); Serial.print("ohms");
// END- Remove these lines for the function not to display anything
// Uncomment this line for the function to return Fahrenheit instead.
//Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
return Temp; // Return the Temperature
}
void printDouble(double val, byte precision) {
// prints val with number of decimal places determine by precision
// precision is a number from 0 to 6 indicating the desired decimal places
// example: printDouble(3.1415, 2); // prints 3.14 (two decimal places)
Serial.print (int(val)); //prints the int part
if( precision > 0) {
Serial.print("."); // print the decimal point
unsigned long frac, mult = 1;
byte padding = precision -1;
while(precision--) mult *=10;
if(val >= 0) frac = (val - int(val)) * mult; else frac = (int(val) - val) * mult;
unsigned long frac1 = frac;
while(frac1 /= 10) padding--;
while(padding--) Serial.print("0");
Serial.print(frac,DEC) ;
}
}
void setup() {
Serial.begin(115200);
}
#define ThermistorPIN 0 // Analog Pin 0
double temp;
void loop() {
temp=Thermistor(analogRead(ThermistorPIN)); // read ADC and convert it to Celsius
Serial.print(", Celsius: "); printDouble(temp,3); // display Celsius
temp = (temp * 9.0)/ 5.0 + 32.0; // converts to Fahrenheit
Serial.print(", Fahrenheit: "); printDouble(temp,3); // display Fahrenheit
Serial.println(""); // End of Line
delay(100); // Delay a bit... for fun, and to not Serial.print faster than the serial connection can output
}
A Digital
Thermistor looks similar to a three legged transistor. Its three pins correspond to GND, signal and 5V. It is really easy to use. The way it works is that it puts out 10 millivolts per degree centigrade on the signal pin. To allow measuring temperatures below freezing there is a 500mV offset
25°C=750mV
0°C=500mV
To convert this from the digital value to degrees you use Arduino's ability to do some math. To display the temperature, you'll use the
Serial Monitor.
Here is the schematic
After you wire the circuit, complete and upload the code:
/*
* A simple program to output the current temperature
* to the IDE's debug window
* For more details on this circuit:
* http://tinyurl.com/c89tvd */
int temperaturePin = 0;
float temperatureVoltage;
void setup(){
Serial.begin(9600);
}
void loop(){
float temperature = getVoltage(temperaturePin);
//converting from 10 mv
//per degree with 500 mV offset to
//degrees ((volatge - 500mV) times 100)
temperatureVoltage=temperature;
temperature = (temperature - .5) * 100;
//print the temperature and temperatureVoltage to the Serial Monitor
__________________
Serial.println(" degrees in centigrade");
__________________
Serial.println(" output in voltage");
//wait a second
__________________
}
/*
* getVoltage() - returns the voltage on the analog input
* defined by pin
*/
float getVoltage(int pin){
/*converting from a 0 to 1024 digital range
* to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
*/
return (analogRead(pin) * .004882814);
}
This is the formula to convert Centigrade to fahrenheit:
Modify the code above to print the temperature in fahrenheit.
/ /Exercise 3
- The relay that you will be working with can handle a lot of power - 30A at 220VAC.
Like with capacitors, you under-rate the relay so that you mitigate the risk of relay failure. If you need 10A@120VAC, don't use a relay rated for 10A@120VAC, instead use a bigger one (such as 30A@120VAC). power = current * voltage so a 30A@220V relay can handle up to a 6,000W device.
- Build up the Power Control board.


This board contains the relay, transistor, and activation LED. The board requires 5V and GND to operate. A control pin controls whether the relay is 'closed' (allows high power to flow) or 'open' (paddle's default state of disconnected).
The coil within the relay requires up to 80mA. This is more than a GPIO pin can handle (20mA by default) so use NPN transistor as a controllable connection to ground. The NPN transistor can handle up to a 200mA which is more than the coil (80mA) and the LED (20mA) combined.
When the 'RELAY' pin (aka CTRL) goes high, the NPN transistor connects to ground sending current through the coil (activating the relay) and through the LED (turning the activation LED on). R1 pulls the 'RELAY' pin to ground so if anything goes haywire the relay will remain in the safe, off position.
The 1N4148 diode is connected in a odd fashion for a reason. This is placed between power and ground in a reverse fashion. When the coil of the relay is de-activated, it acts like an inductor, trying to suppress current change. This can cause some havoc on the 5V power rail. When this happens, the 1N4148 will forward bias causing the current stored in the coil to flow back to the 5V rail protecting the power supply and the near-by parts.
- Look at your extension cord’s plug: one prong is larger than the other. Split the 2 wires of the cord apart, then cut the wire that runs to the smaller prong (this is also the wire without a ridge), and strip 1" off each side.

TIP: The correct wire is the one without ridges running along its length. Don’t worry if you cut both wires; you can just splice the other wire back together.
-
Solder a wire to each side of the split cord wire.
- Wrap each individual solder joint with electrical tape, and then wrap the set of
wires with another piece of tape.
Switching
#define RELAY_PIN 2
static int relayVal = 0;
void setup(){
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600); // open serial
Serial.println("Press 1 or 0 to toggle relay on/off");
}
void loop(){
int cmd;
while (Serial.available() > 0){
cmd = Serial.read();
switch (cmd){
case '0':
relayVal=0;
report();
break;
case '1':
relayVal=1;
report();
break;
default:
Serial.println("Press 1 or 0 to toggle relay on/off");
}
if (relayVal){
digitalWrite(RELAY_PIN, HIGH);
}
else{
digitalWrite(RELAY_PIN, LOW);
}
}
}
void report(){
if (relayVal){
Serial.println("On");
}
else{
Serial.println("Off");
}
}
- Rewrite the code so that if a certain temperature is reached, the relay is tripped
/ /Exercise 4
The relay that you will be working with can handle a lot of power - 30A at 220VAC.
Like with capacitors, you under-rate the relay so that you mitigate the risk of relay failure. If you need 10A@120VAC, don't use a relay rated for 10A@120VAC, instead use a bigger one (such as 30A@120VAC).
power = current * voltage so a 30A@220V relay can handle up to a 6,000W device.
- Build up the Power Control board.


This board contains the relay, transistor, and activation LED. The board requires 5V and GND to operate. A control pin controls whether the relay is 'closed' (allows high power to flow) or 'open' (paddle's default state of disconnected).
The coil within the relay requires up to 80mA. This is more than a GPIO pin can handle (20mA by default) so use NPN transistor as a controllable connection to ground. The NPN transistor can handle up to a 200mA which is more than the coil (80mA) and the LED (20mA) combined.
When the 'RELAY' pin (aka CTRL) goes high, the NPN transistor connects to ground sending current through the coil (activating the relay) and through the LED (turning the activation LED on). R1 pulls the 'RELAY' pin to ground so if anything goes haywire the relay will remain in the safe, off position.
The 1N4148 diode is connected in a odd fashion for a reason. This is placed between power and ground in a reverse fashion. When the coil of the relay is de-activated, it acts like an inductor, trying to suppress current change. This can cause some havoc on the 5V power rail. When this happens, the 1N4148 will forward bias causing the current stored in the coil to flow happily back to the 5V rail protecting the power supply and the near-by parts.
- Take the extension cord and cut off the female connector about 6" from the female end.

This should leave a few feet of extension cord between the part that plugs into the wall (male end) and the bare, exposed, recently cut-off end of the extension cord. Don't plug it in!
Note: A two-wire extension cord will not work correctly. In the three-wire cord, the extra wire is the ground return and allows the GFCI to operate correctly.
Using a meter set to continuity, check that the ground pin (the round one) is indeed connected to the green ground wire.
- Use a wire stripper or an exacto-knife to strip the three wires individually about 1". Twist the ends of the wires to combine the strands of the wires together in preparation for soldering.

- Tin the ends

- Be sure to thread the extension cord through the housing (shown above) before soldering to the control board.

- With a good 6" of wire exposed, cut the black wire about 5" down from the end. The is where the relay will live.


Here you have the black wire cut and soldered to the control board. The relay is a NO (Normally Open) type relay. When power is off, there is no connection between the two thick black strands that you just cut and soldered. This is a safety feature - if all things go wrong and the power goes out of the coil, the relay will kick off and the outlet will shut down.
When you send 5V to the coil, the paddle flips from the 'off' state to the 'on' state, connecting the two pieces of black wire (on the left side of the picture above), power is delivered to the outlet and your project is powered.

- The Black and white wires connect to the two side terminals of the GFCI - the green wire (ground) connects to the end of the outlet.
Notice how the hooks of the tinned wires are arranged so that they are clock-wise. If you align the hooks of the wires under the screws correctly, as you tighten the screws, the hook of the wire will be 'sucked' into the tightening screw. This creates a very compact connection.

- Now lower the relay into the enclosure and feed the control wires (red, yellow, and black) out one corner of the housing.

- Once you have everything lowered into place, screw the outlet onto the enclosure, and the face plate onto the enclosure.
DO NOT plug the extension cord into the wall yet.
- Attach the three control wires (5V, GND, and CTRL) to the Arduino
Tying the CTRL line to 5V I heard a very friendly click as the relay kicked over. This indicated (along with the LED on the control board) that the relay was actuated to the 'on' position. Removing CTRL from the 5V rail (called floating because the CTRL line is neither connected to 5V or GND), the relay released. This is good! If CTRL is left floating or tied to ground, the outlet is turned off.
You can also use a meter in continuity mode to check that the relay is working properly before you connect to 120VAC.
When the relay's open, one of the fins of the plug and one of the rectangular holes of the outlet will not have continuity, and when it's closed, they will. The other fin and rectangular hole will always have continuity, as will the ground pin and the funny hole. Always do this check before plugging into the 120VAC.
-
The next step is to plug the extension cord into the wall and test again. If anything goes wrong the GFCI should activate and cut off. Be sure to unplug the outlet anytime you are working on it.
- You should now have an outlet that is fully controllable over 5V logic. When you plug a device into the outlet, it will by default be off. When you expose 5V to the CTRL line, the relay will activate turning on power to the device plugged into the outlet.