Posts | Comments

Planet Arduino

Archive for the ‘relay8:’ Category

Sep
14

Internet-controlled relays with teleduino and Freetronics RELAY8:

arduino, ethernet, freetronics, i2c, internet, lesson, lessons, Relay, relay8:, shield, teleduino, tronixstuff, tutorial, tutorials Comments Off on Internet-controlled relays with teleduino and Freetronics RELAY8: 

This is chapter forty-seven of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Updated 24/11/2012

In this article we’re going to look at controlling relays over the Internet. In doing so you will then be able to turn almost anything on and off as long as you have http access on an Internet-enabled device. Why would you want to do this? Connect an outdoor light – and turn it on before arriving home. Control the power to your TV setup – then you can control childrens’ TV viewing at a whim. Control farm water pumps without getting out of the truck. We’ll break this down into two stages. First we’ll explain how the RELAY8: relay control shield works and control it locally, then control it remotely using the teleduino service. We will be using Arduino IDE v1.0.1.

This tutorial will assume you have an understanding from three other articles – so please have a quick read of I2C bus, the MCP23017 I/O expander and teleduino. But don’t panic – we’ll try and keep it simple here.

The RELAY8: shield

First – our relay shield. We’ll be using the Freetronics RELAY8: shield:

Using the RELAY8: you can control eight relays using the I2C bus and the MCP23017 I/O expander – which saves your digital outputs for other purposes. There are three hardware settings you need to consider when using the shield:

  1. Power – how will you power the relay coils?
    • You can directly connect between 5 and 24V DC using the terminal block on the right-hand side of the shield – great for stronger relay coils.
    • You can power the relay coils using power from the Arduino. So whatever power is going to the Arduino Vin can power the shield. To do this jumper the two pins next to the Vin shield connector. In doing so – you must check that the combined current draw of all your relays on at once will not exceed what is available to the Arduino. Usually OK when using solid-state relays, as most examples use around 15mA of current to activate. However double-check your relay specifications before doing so.
    • You can also power the Arduino board AND the shield by feeding in external power to the shield and jumpering the two pins described above
  2. Which I2C address to use for each shield? By default it is 0×20. However you can alter the last three bits of the address by changing the jumpers at the bottom-left of the shield. Each jumper represents one bit of the bus address – no jumper means zero, and a jumper means one. So if you jumper ADDR0, the address will be 0×21 – etc. Using this method you can then stack up to eight shields – and control 64 relays!
  3. Are you using an Arduino Leonardo board? If so – your shield I2C pins aren’t A4/A5 – they’re over near the top of the board:

However this isn’t a problem. Solder in some header pins to the shield’s SCL/SDA holes (next to AREF). Then turn over the RELAY8: board and you will see some solder pads as shown below. With a thin knife, cut the copper tracks shown with the blue lines:

Doing this will redirect the I2C bus from the microcontroller to the correct pins at the top-left.

Once you have decided on your power and I2C-bus options, it’s time to connect the relays. Doing so is simple, just connect the +  and – from the relay coil to the matching position on your RELAY8: shield, for example:

Today we’re just using prototyping wires, so when creating a permanent installation ensure the insulation reaches the terminal block. When working with relays you would use a diode across the coil to take care of back-EMF – however the shield has this circuitry, so you don’t need to worry about that at all. And if you’re wanting to control more than one shield – they stack nicely, with plenty of clearance between shields, for example:

Now to test the shield with a quick demonstration. Our sketch will turn on and off each relay in turn. We use the addressing format described in table 1.4 of the MCP23017 data sheet,  The relays 1 to 8 are controlled by “bank A” of the MCP23017 – so we need to set that to output in our sketch, as shown below (download sketch):

// Example 47.1
#include "Wire.h" // for I2C bus
#define I2C_ADDR 0x20 // 0x20 is the address with all jumpers removed
void setup()
{
 Wire.begin(); // Wake up I2C bus
 // Set I/O bank A to outputs
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // Set all of bank A to outputs
 Wire.endTransmission();
}
int period = 500;
void loop()
{
 byte relay = 1;
 for (int i=1; i<9; i++)
 {
 // turn on relay 
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x12); // Select bank A
 Wire.write(relay); // Send value to bank A
 Wire.endTransmission(); 
 delay(period);
// turn off all relays
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x12); // Select bank A
 Wire.write(0); // Send value to bank A
 Wire.endTransmission(); 
 delay(period);

 relay = relay * 2; // move to next relay
 }
}

The sketch simply sends the values of 1, 2, 4, 8, 16, 32, 64 and 128 to the shield – each value in turn represents relays 1 to 8. We send 0 to turn off all the relays. Here’s a quick video showing it in action – the LEDs on the shield show the relay coil power status:

Now there is one small caveat – every time you send a new command to the MCP23017, it overwrites the status of the whole bank of pins. For example if relay 3 is on, and we send the value 2 – this will turn on relay 2 and turn off 3. Why? Because the values are converted to binary when heading down to the relay shield. So if we send 1, in binary this is:

00000001

which turns on relay 1 – and turns off relays 2 to 7. But then if we send 4 to turn on relay 3, in binary this is:

00000100

which turns on relay 3, but turns off relays 1, 2, and 4 to 8. So how do we turn on or off all eight relays at once? Just do a little binary to decimal conversion. Let’s say you want relays 1, 3, 5 and 7 on – and 2, 4, 6 and 8 off. In binary our command value would be:

01010101

and in decimal this is 85. Want to turn them all on at once? Send 255. Then all off? Send zero.

Now let’s do it via the Internet…

You’re going to need an Ethernet-enabled Arduino board. This could involve adding an Ethernet shield to your existing board, or using an all-in-one board like the Freetronics EtherTen. We will now use the teleduino service created by Nathan Kennedy to send commands to our Arduino boards via the Internet. At this point, please review and understand the teleduino article – then, when you can successfully control a digital output pin – return here to continue.

First, get the hardware together. So ensure your relay shield is in the Arduino and you have uploaded the

TeleduinoEthernetClientProxy.ino

sketch. For the first couple of times, it’s good to still have the teleduino status LED connected – just to keep an eye on it. Plug your Arduino into your router and the power. After it connects to teleduino (four blinks of the status LED) we have to send three commands via http. The first tells teleduino that we’re sending I2C commands. You only do this once after every Arduino reset or power-up situation. It is:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999r=defineWire

Remember to replace 999999 with your teleduino key. Then we send:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%00%00

At this stage the relay shield is now ready to accept your bytes to turn on and off the outputs. Again, just like the sketch – we send two bytes. For example:

 https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%12%FF

turns on all the outputs – however with the URL we need to send the byte representing the outputs in hexadecimal. So 255 is FF, 0 is 0, etc. For example to turn them all off, use:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%12%00

or to turn on outputs 1, 2, 3 and 4 use:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%12%0F

Simple. You can simply bookmark your URLs for later use as well – and don’t forget to use a URL-shortener such as bit.ly to makes things simpler for you.

Conclusion

Now you have a way to control many relays either locally or remotely over the Internet. I hope you found this article useful or at least interesting. If you have any suggestions for further articles (and not thinly-veiled methods of asking me to do your work for you…) – email them to john at tronixstuff dot com. Thanks to Freetronics for the use of their hardware and Nathan Kennedy for teleduino, his support and advice.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.


Sep
14

Control relays over the Internet with Arduino in chapter forty-seven of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Updated 24/11/2012

In this article we’re going to look at controlling relays over the Internet. In doing so you will then be able to turn almost anything on and off as long as you have http access on an Internet-enabled device. Why would you want to do this? Connect an outdoor light – and turn it on before arriving home. Control the power to your TV setup – then you can control childrens’ TV viewing at a whim. Control farm water pumps without getting out of the truck. We’ll break this down into two stages. First we’ll explain how the RELAY8: relay control shield works and control it locally, then control it remotely using the teleduino service. We will be using Arduino IDE v1.0.1.

This tutorial will assume you have an understanding from three other articles – so please have a quick read of I2C bus, the MCP23017 I/O expander and teleduino. But don’t panic – we’ll try and keep it simple here.

The RELAY8: shield

First – our relay shield. We’ll be using the Freetronics RELAY8: shield:

Using the RELAY8: you can control eight relays using the I2C bus and the MCP23017 I/O expander – which saves your digital outputs for other purposes. There are three hardware settings you need to consider when using the shield:

  1. Power – how will you power the relay coils?
    • You can directly connect between 5 and 24V DC using the terminal block on the right-hand side of the shield – great for stronger relay coils.
    • You can power the relay coils using power from the Arduino. So whatever power is going to the Arduino Vin can power the shield. To do this jumper the two pins next to the Vin shield connector. In doing so – you must check that the combined current draw of all your relays on at once will not exceed what is available to the Arduino. Usually OK when using solid-state relays, as most examples use around 15mA of current to activate. However double-check your relay specifications before doing so.
    • You can also power the Arduino board AND the shield by feeding in external power to the shield and jumpering the two pins described above
  2. Which I2C address to use for each shield? By default it is 0×20. However you can alter the last three bits of the address by changing the jumpers at the bottom-left of the shield. Each jumper represents one bit of the bus address – no jumper means zero, and a jumper means one. So if you jumper ADDR0, the address will be 0×21 – etc. Using this method you can then stack up to eight shields – and control 64 relays!
  3. Are you using an Arduino Leonardo board? If so – your shield I2C pins aren’t A4/A5 – they’re over near the top of the board:

However this isn’t a problem. Solder in some header pins to the shield’s SCL/SDA holes (next to AREF). Then turn over the RELAY8: board and you will see some solder pads as shown below. With a thin knife, cut the copper tracks shown with the blue lines:

Doing this will redirect the I2C bus from the microcontroller to the correct pins at the top-left. Once you have decided on your power and I2C-bus options, it’s time to connect the relays. Doing so is simple, just connect the +  and – from the relay coil to the matching position on your RELAY8: shield, for example:

Today we’re just using prototyping wires, so when creating a permanent installation ensure the insulation reaches the terminal block. When working with relays you would use a diode across the coil to take care of back-EMF – however the shield has this circuitry, so you don’t need to worry about that at all. And if you’re wanting to control more than one shield – they stack nicely, with plenty of clearance between shields, for example:

Now to test the shield with a quick demonstration. Our sketch will turn on and off each relay in turn. We use the addressing format described in table 1.4 of the MCP23017 data sheet,  The relays 1 to 8 are controlled by “bank A” of the MCP23017 – so we need to set that to output in our sketch, as shown below:

// Example 47.1
#include "Wire.h" // for I2C bus
#define I2C_ADDR 0x20 // 0x20 is the address with all jumpers removed
void setup()
{
 Wire.begin(); // Wake up I2C bus
 // Set I/O bank A to outputs
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x00); // IODIRA register
 Wire.write(0x00); // Set all of bank A to outputs
 Wire.endTransmission();
}
int period = 500;
void loop()
{
 byte relay = 1;
 for (int i=1; i<9; i++)
 {
 // turn on relay 
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x12); // Select bank A
 Wire.write(relay); // Send value to bank A
 Wire.endTransmission(); 
 delay(period);
// turn off all relays
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x12); // Select bank A
 Wire.write(0); // Send value to bank A
 Wire.endTransmission(); 
 delay(period);

 relay = relay * 2; // move to next relay
 }
}

The sketch simply sends the values of 1, 2, 4, 8, 16, 32, 64 and 128 to the shield – each value in turn represents relays 1 to 8. We send 0 to turn off all the relays. Here’s a quick video showing it in action – the LEDs on the shield show the relay coil power status:

Now there is one small caveat – every time you send a new command to the MCP23017, it overwrites the status of the whole bank of pins. For example if relay 3 is on, and we send the value 2 – this will turn on relay 2 and turn off 3. Why? Because the values are converted to binary when heading down to the relay shield. So if we send 1, in binary this is:

00000001

which turns on relay 1 – and turns off relays 2 to 7. But then if we send 4 to turn on relay 3, in binary this is:

00000100

which turns on relay 3, but turns off relays 1, 2, and 4 to 8. So how do we turn on or off all eight relays at once? Just do a little binary to decimal conversion. Let’s say you want relays 1, 3, 5 and 7 on – and 2, 4, 6 and 8 off. In binary our command value would be:

01010101

and in decimal this is 85. Want to turn them all on at once? Send 255. Then all off? Send zero.

Now let’s do it via the Internet…

You’re going to need an Ethernet-enabled Arduino board. This could involve adding an Ethernet shield to your existing board, or using an all-in-one board like the Freetronics EtherTen. We will now use the teleduino service created by Nathan Kennedy to send commands to our Arduino boards via the Internet. At this point, please review and understand the teleduino article – then, when you can successfully control a digital output pin – return here to continue.

First, get the hardware together. So ensure your relay shield is in the Arduino and you have uploaded the

TeleduinoEthernetClientProxy.ino

sketch. For the first couple of times, it’s good to still have the teleduino status LED connected – just to keep an eye on it. Plug your Arduino into your router and the power. After it connects to teleduino (four blinks of the status LED) we have to send three commands via http. The first tells teleduino that we’re sending I2C commands. You only do this once after every Arduino reset or power-up situation. It is:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999r=defineWire

Remember to replace 999999 with your teleduino key. Then we send:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%00%00

At this stage the relay shield is now ready to accept your bytes to turn on and off the outputs. Again, just like the sketch – we send two bytes. For example:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%12%FF

turns on all the outputs – however with the URL we need to send the byte representing the outputs in hexadecimal. So 255 is FF, 0 is 0, etc. For example to turn them all off, use:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%12%00

or to turn on outputs 1, 2, 3 and 4 use:

https://us01.proxy.teleduino.org/api/1.0/328.php?k=999999&r=setWire&address=32&bytes=%12%0F

Simple. You can simply bookmark your URLs for later use as well – and don’t forget to use a URL-shortener such as bit.ly to makes things simpler for you.

Conclusion

Now you have a way to control many relays either locally or remotely over the Internet. I hope you found this article useful or at least interesting. If you have any suggestions for further articles (and not thinly-veiled methods of asking me to do your work for you…) – email them to john at tronixstuff dot com. Thanks to Freetronics for the use of their hardware and Nathan Kennedy for teleduino, his support and advice.

LEDborder

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The post Internet-controlled relays with teleduino and Freetronics RELAY8: appeared first on tronixstuff.



  • Newsletter

    Sign up for the PlanetArduino Newsletter, which delivers the most popular articles via e-mail to your inbox every week. Just fill in the information below and submit.

  • Like Us on Facebook