Control a Relay Over the Internet Via Arduino With Teleduino

75,122

61

15

About: Developer of the telecontrol and telemonitoring system Teleduino, using the Arduino platform.

Intro: Control a Relay Over the Internet Via Arduino With Teleduino

Relays are an oldie but a goodie. They allow you to switch high current/voltage loads using a digital output from something like the Arduino.

What would make a relay even better? How about controlling a relay over the internet from anywhere in the world? Yea!

This process is made really simple using the Teleduino sketch for your ethernet enabled Arduino. Haven't heard of Teleduino? That's cool, you may want to take a look at Arduino Control via a Web Service with Teleduino to get yourself started.

This tutorial will guide you through the process of attaching a 5V relay to your Arduino, and provide some example Teleduino API calls to:
  • Define the pin mode of a pin
  • Turn on the relay
  • Turn off the relay
  • Toggle the relay
If you get stuck on any of the steps, or if you feel that there is a step that could be explained better, please leave a comment so that we can improve things.

Let's get started!

Step 1: Parts Needed and Circuit Diagram

Parts Needed
You need the following parts to complete this tutorial:
  • Breadboard (optional, but makes prototyping much easier)
  • 5V Relay (the relay in the image has had legs added so that it can be connected to a breadboard)
  • 1K Resistor
  • 1N4001 Diode
  • 2N2222 Transistor
  • Some hookup wire

Circuit Diagram
Take a peak at the attached circuit diagram. It shows how the components needs to be connected. You may need to look at the datasheet for your relay to know which pins are used for the coil.
The additional image illustrates the pin layout of your 2N2222 transistor. This will help you understand what pin needs to connect to which part of the circuit.

Time to get this hooked up!

Step 2: Put Circuit Together

Image 1
On this particular relay the coil pins are right next to each other, which makes things pretty easy. Ensure that you check the datasheet for your particular relay to work out which pins are used for the coil.

Image 2
The Diode and Transistor has been added. Note the direction of the Diode and the Transistor - this is very important! The circuit will not work if the direction is incorrect (and may cause irreversible damage to your Arduino!). Check your component layout against the circuit diagram in Step 1.

Image 3
Add the Resistor and Hookup Wire. There are three wires that need to be connected to your Arduino (5V, GND and Pin 4).

Image 4
Shows the correct way to connect the relay circuit to your Arduino board.

Let's make the relay click!

Step 3: Control Relay Via the Teleduino API

Here are some example API calls that you can use to control your relay. You need to replace {key} with the unique API key you obtained when you completed the instructable Arduino Control via a Web Service with Teleduino. Just chuck the URLs into your browser's address bar and you should see some magic happen on that relay.

Define the pin mode of digital pin 4 (Must be done once per boot prior to setting digital outputs. 'pin=4' mean pin 4, 'mode=1' means OUTPUT):
http://us01.proxy.teleduino.org/api/1.0/328.php?k={key}&r=definePinMode&pin=4&mode=1

Set the output of digital pin 4 to HIGH ('pin=4' means pin 4, 'output=1' means HIGH):
http://us01.proxy.teleduino.org/api/1.0/328.php?k={key}&r=setDigitalOutput&pin=4&output=1

Set the output of digital pin 4 to LOW ('pin=4' means pin 4, 'output=0' means LOW):
http://us01.proxy.teleduino.org/api/1.0/328.php?k={key}&r=setDigitalOutput&pin=4&output=0

Toggle the output of digital pin 4 ('pin=4' means pin 4, 'output=2' means toggle):
http://us01.proxy.teleduino.org/api/1.0/328.php?k={key}&r=setDigitalOutput&pin=4&output=2

Make it click!

Step 4: Further Reading

Controlling a relay is just a very simple example of what can be achieved by using the Teleduino Arduino library/sketch.

To view the full API reference manual, take a look at http://www.teleduino.org/rtfm/api/328.php.

Have fun!

Share

    Recommendations

    • Fix It! Contest

      Fix It! Contest
    • Metalworking Contest

      Metalworking Contest
    • Tiny Home Contest

      Tiny Home Contest

    15 Discussions

    0
    None
    abunn1

    4 years ago on Introduction

    when I power on arduino my relay goes HIGH. Tried to include a PinMode(4,LOW) with no luck. Any suggestions welcomed fairly new to arduino

    1 reply
    0
    None
    SirCubesabunn1

    Reply 2 years ago

    I am also having same problem....

    0
    None
    Waylon111

    2 years ago on Introduction

    Hello: I have tried this project about 10 times now and cannot get it to work. When I enter any of the API's I receive the following message: {"status":200,"message":"OK","response":{"result":0,"time":0.19250297546387,"values":[]}}

    Can someone help me? Am I suppose to change the Arduino Sketch? I am New to Arduino but not new to electronics or electrical.
    My second question is, if i ever do get this Remote Relay control to work, is it possible to run it from my own server? Hey... Could use some help. Thank you.

    0
    None
    dstech

    3 years ago on Introduction

    nice ible! It looks like there are a lot of possibilities for that API

    0
    None
    heavyweather

    3 years ago on Introduction

    Hi Nathan,
    Can you say what latency you have switching the relay? I was wondering if realtime control of faster moving objects would be possible (maximum of 4,16mm/ms).
    Is "Autobahn" comparable to teleduino?

    0
    None
    uchup26

    3 years ago on Introduction

    whether to use Arduino uno r3 with ethernet shield?

    0
    None
    nathanknzkareem1

    Reply 4 years ago on Introduction

    Hi kareem1,

    There is a link on step 1 to the Instructable about the Teleduino service. There are download links available there.

    Thanks!

    0
    None
    MJ93

    4 years ago on Introduction

    Hi there...first of all hats off to u fr u efforts and thanks fr being generous for sharing this information.
    is there a way i can control more than 1 relay (4 in my case) ..i've got ethernet sheild up running and till nw i am able to control a relay(thanks to the tutorial)

    0
    None
    Seduction

    5 years ago on Introduction

    Hello,
    Thank you both, nathanknz and Prodigal_Son. I relaxed when you said to me you can control arduino with your android app via teleduino.
    Now time to make something good with these.
    thanks again..

    0
    None
    Prodigal_Son

    5 years ago on Introduction

    Hello Seduction.

    I´m the one that Nathan points at.
    I´we created an Android App that let´s you control your Teleduino device.

    Short description for v1.0:
    6 buttons for Units on (turn green when pushed = status), can control both serial and Digital out.
    6 buttons for Units off (puts the On button black again when pushed), same as above.
    4 Toggle-buttons, controls only Digital Pins.

    Long press on any buttons opens the Button Settings. Choose label and what it shoud control.
    The Settings button on main screen is used to set the Teleduino Key, Define Serial and Digital Pins. Also as Debug where the last URL sent to Teleduino is shown.

    I´m about to release the app on Google Play any day now and I´ll inform you in this thread when It´s available for the public.

    0
    None
    Seduction

    5 years ago on Step 4

    Hello,
    Firstly i have to say that it's nice work. I want to do that project with some diffrences. In addition to this project, i wanna add android interface to control it. So, i want to control my house's lights with my android phone. With your project i learned control relay with teleduino. But i dont know, can i send my commands(like turn on the lights) from my android application to arduino via teleduino? If it is possible can you give me a lead?
    I am new with arduino and teleduino. But I am willing to do this project. please inform me about my question.
    thanks..

    1 reply
    0
    None
    nathanknzSeduction

    Reply 5 years ago on Step 4

    Hi Seduction,

    Thanks for your message.

    Yes it is certainly possible to make API calls from an Android application, however I haven't done any Android development (yet!) so I can't be of much help there.

    I have been contacted recently by someone who has written an Android app for basic control via Teleduino. I'll let him know that you have been in touch and point him to this conversation.

    Thanks,
    Nathan.