Relay Switch


Relay switches are great for controlling electrical components/appliances that require more power than what the Arduino digital output pins can provide.

Be sure to check the rating of your relay switch. Not all relay switches are made the same. Some are capable of handling higher voltages and currents, some are capable of handling only direct current, while some are capable of handling both direct current and alternating current.

2013-12-19 16.56.39.jpg

If you are using the relay switch that comes with the Christmas Development Kit (Relay Model Number: Songle SRD-05VDC-SL-0), you will be happy to know that this relay is capable of handling up to 10A, 250VAC and 10A, 30VDC.

Please be careful if you intend to use your relays for AC-powered appliances! You could severely injure or kill yourself if you are not careful.

Wire Connections

The wiring connections here apply only to the relay switch that comes with the Christmas Development Kit.

2013-12-19 17.01.39.jpg

When the Arduino output pin (on the right of the photo) sends a HIGH (5V) signal, the relay switch will close the circuit on the left (of the photo), thus switching on the electrical component/appliance.

When the Arduino output pin sends a LOW (0V) signal, the relay switch will open the circuit on the left (of the photo), thus switching off the electrical component/appliance.

The Code

Here is a simple code demonstrating how you can draw a button on your smartphone to control the relay.

This sample code was taken from Lesson 7 of the Andee Library examples. You can find this lesson and other lessons in the Arduino IDE, under File -> Examples -> Andee -> Lesson 7 Using Buttons to Control LED or Relays

// Always include these libraries. Annikken Andee needs them
// to work with the Arduino!
#include <SPI.h>
#include <Andee.h>
// We'll just create one button to toggle AndeeHelper displayState; AndeeHelper togglebutton;
// In this example, we'll connect the relay to pin 2. // Do not use Pins 8, 11, 12, 13 (Arduino Uno layout) as Andee // is using them. Strange things will happen if you do. const int outputPin = 2;
int state; // This variable will store the current state
// The setup() function is meant to tell Arduino what to do // only when it starts up. void setup() { Andee.begin(); // Setup communication between Annikken Andee and Arduino Andee.clear(); // Clear the screen of any previous displays setInitialData(); // Define object types and their appearance
state = 0; // Initialise your state to zero
pinMode(outputPin, OUTPUT); // Configures outputPin for output. }
// This is the function meant to define the types and the apperance of // all the objects on your smartphone void setInitialData() { // We're gonna create two objects. The first object, displayState, will // read the current state of the pin and report it to your smartphone. // The second object, togglebutton, will be used to turn the LED/relay // on or off.
displayState.setId(0);// Don't forget to assign a unique ID number displayState.setType(DATA_OUT); displayState.setLocation(0,0,FULL); displayState.setTitle("Current Output State");
togglebutton.setId(1); // Don't forget to assign a unique ID number togglebutton.setType(BUTTON_IN); // Defines object as a button togglebutton.setLocation(1,0,FULL); togglebutton.setTitle("Switch to HIGH"); // Sets the initial words for button // You can't use setData() and setUnit() for buttons. }
// Arduino will run instructions here repeatedly until you power it off. void loop() { state = digitalRead(outputPin); // Read current state of the output pin
// Change text in the display box to indicate the current state of the pin if(state == 0) // LOW state { displayState.setData("LOW"); } else // HIGH state { displayState.setData("HIGH"); }
// Here's how you code the button action if( togglebutton.isPressed() ) { // Acknowledge button press. You must put this or your phone will be waiting togglebutton.ack(); if(state == 0) // If in a LOW state { togglebutton.setTitle("Switch to LOW"); digitalWrite(outputPin, HIGH); } else // If in a HIGH state { togglebutton.setTitle("Switch to HIGH"); digitalWrite(outputPin, LOW); } }
displayState.update(); // Update new info onto smartphone screen togglebutton.update();
delay(500); // Always leave a short delay for Bluetooth communication }
comments powered by Disqus