Introduction: I2C Relay Board

Relays are one of the most common interfaces between a micro controller and the outside world. It provides electrical isolation between the low voltage circuits of the micro controller, and the loads.

So why this funny I2C Relay Board, you might ask....

With this instructable, I will try to give some of my reasoning behind the design of the I2C Relay Board.

Please enjoy, and any feedback is welcome.

Step 1: Normal Way to Drive a Single Relay

Normally, with a circuit driving only a minimum number of relays, the best way is to use a transistor driver circuit as shown. It is simple, low cost, and effective.

The resistors provide pull-down to ground, and transistor base current limitation.

The transistor is used to increase the current available to drive a relay. With only 1mA drawn from the micro controller pin, the transistor can switch a load of 100mA. More than enough for most types of relays.

The diode is a fly-back diode, protecting the circuit from high voltage spikes during relay switching.

The added advantage of using this circuit, is that the relay operating voltage can be different from the voltage of the micro controller. Thus, instead of using a 5V relay, one can use any DC voltage of up to 48V.

There is one drawback. Although the circuit only use 4 components, it all adds up quickly if more than about 4 relays are needed. So what to do if you want to drive 8 relays, but want to keep the PCB size as small as possible?

Step 2: Introducing the ULN2803

The more relays a project requires, the higher the component count. This will make the PCB design more difficult, and might use up valuable PCB space. But using a transistor array, like the ULN2803, will definitely assist in keeping the PCB size small.

The ULN2803 is ideally suited for 3.3V and 5V inputs from a micro controller, and can drive relays up to 48V DC.

This ULN2803 has 8 individual transistor circuits, each circuit fitted with all the components required to switch a relay.

Step 3: What We Have Achieved So Far

By using the ULN2803 driver, we reduced the component count to drive 8 relays from 32 components, down to a single component.

This will make the PCB design much more simpler, and reduce the overall PCB size.

Step 4: Connecting Relays to a Project Using ULN2803

This shows a project that drives 16 relays. As can be seen, the space taken by the ULN2803 drivers are minimal, and the pin layout of the ULN2803 made it very simple to incorporate on the PCB.

The headers to the right of the ULN2803 are used to connect to each relay. This makes the relay PCB simple, with only one supply connection, and one connection per relay.

So you may ask, is there any way to improve on this design...

Step 5: Intruducing the Microchip MCP23017 I/O Expander

The MCP23017 from MicroChip is an I/O Expaner IC, that adds 16 additional I/O ports to a micro controller. Each of the MCP23017 pins can be configured as an Input or as an Output. The MCP23017 is controlled via the I2C bus, and with programmable addressing on the MCP23017, up to 8 MCP23017 IC's can be connected to a project. This adds up to a total of 128 extra I/O pins.

Looking at the project where 16 relays are controlled, the number of wires between the main PCB and the relay board totals 17 wires - one relay positive supply, and one wire per relay.

Using the MCP23017, the wiring between the main PCB and relay board can be reduced to only 5 wires:

- relay positive supply

- +5V for MCP23017

- 0V

- SDA (I2C bus)

- SCL (I2C bus)

Best of all, to control the maximum of 128 extra I/O ports, it still only requires the same 5 wires from the main PCB. This makes for a simple and neat main PCB design, and reduces internal project wiring to a minimum.

Step 6: Designing of the I2C Relay Board

PCB Design software limitation

I am using the free version of Eagle for all my PCB designs. The software has only one limitation, and that is that the board size is restricted to 100mm x 80mm. For all my recent projects, this was acceptable.

Designing a single relay board with 16 relays and their screw terminals, was not possible on a 100mm x 80mm PCB. But it was possible to fit 8 relays with their terminal screws, a MCP23017 and ULN2803 on a single board. Instead of a single PCB, the design was changed to two identical PCBs. The only disadvantage with this option, was that instead of using all 16 I/O pins from a single MCP23017, each board had to be fitted with it's own MCP23017, and using only 8 of the 16 I/O ports.

Component selection

The design must be build using only standard through-hole components.

Connecting multiple I2C Relay Boards

The MCP23017 has three address lines (A0, A1 and A2), and each I2C Relay Board must have a unique I2C address. For easy addressing, header pins were added to set the address of each I2C Relay Board. With this option, up to 8 I2C Relay Boards can be connected to a project, to control a total of 64 relays.

Supplying power to the relays

With up to 64 relays that can be connected to a project, it is clear the power supply needed for the relays can no longer be taken directly from the main project PCB. Therefore, supply to each individual I2C Relay Board is done via screw terminals. To make it easy to loop the supply between I2C Relay Boards, two sets of screw terminals were

added.

Relay operating voltage

As there is a large selection of relays available, the design had to cater for different relay operating voltages. By using the ULN2803, the relay voltage can be between 5V and 48V DC. This makes selecting relays easier.

Connecting a load to the relays

A set of 3 screw terminals per relay allows easy connections the relay contacts (Common, Normally Open, Normally Closed). There is no common connections between the 8 relays, allowing maximum connection flexibility using the I2C Relay Board.

Connecting the I2C Bus

Connection of the four I2C Bus wires is done using headers. Again, two rows of headers were added for easy connection of multiple I2C Relay Boards.

Step 7: Cost and Design Breakdown

Using the project where I had to control 16 relays , the different designs can now be compared

Transistor design

32 Resistors, 16 Diodes (1N4007), 16 Transistors (BC109)

Total number of components excluding relays: 64

Total cost of components, excluding relays : ZAR116.96

Pros:

Standard everyday components

Cons:

Difficult PCB layout

17 wires between project and relays


ULN2803 design

2 x ULN2803

Total number of components excluding relays: 2
Total cost of components, excluding relays : ZAR18.24

Pros:
Minimum components

Easy PCB layout, and reduces overall PCB size

Cheapest option

Cons:

17 wires between project and relays

I2C Relay Board design

2 x ULN2803, 2 x MCP23017, 6 x 3-pin headers, 6 x mini-jumpers, 6 x resistors

Total number of components excluding relays: 22 (4 without the address selection headers)

Total cost of components, excluding relays : ZAR78.00

Pros:

Easy to connect multiple I2C Relay Boards to project

Only 6 wires between project and relays

Cons:

Not the cheapest option available.

Step 8: Connecting the I2C Relay Board

The project shown, uses 16 relays, so it contains two I2C Relay Boards. The address of the boards are set to 0 and 1 respectively. As can be seen, the connections between the different PC boards are now kept to a minimum.

Simply connect the +5v, 0V, SDA and SCL lines to an Arduino I2C Bus using ribbon cable.

Connect the required V-Relay to the board.

Step 9: Arduino and the I2C Relay Board

Connect the board to your Arduino as shown.

Below is a simple sketch for using the I2C Relay Boards.

In this sketch, only the "wire" library is used. Due to the easy comminucation on the I2C Bus, all comms to the MCP23017 is included in the sketch.

<p>#include   <Wire.h>   // needed for I2C operation<br></p><p>void setup() {
  //start I2C communications
  Wire.begin();</p><p>  // Setup relay boards
  SetupRelays();
}</p><p>void loop() {
  // put your main code here, to run repeatedly:
  
  for (byte relaydata = 0; relaydata <= 255; relaydata = relaydata + 1) {
    // write data to cards 0 .. 7 relays
    WriteRelays(0,relaydata);    // write data to relay card 0
    WriteRelays(1,relaydata);    // write data to relay card 1
    WriteRelays(2,relaydata);    // write data to relay card 2
    WriteRelays(3,relaydata);    // write data to relay card 3
    WriteRelays(4,relaydata);    // write data to relay card 4
    WriteRelays(5,relaydata);    // write data to relay card 5
    WriteRelays(6,relaydata);    // write data to relay card 6
    WriteRelays(7,relaydata);    // write data to relay card 7
    delay(1000);  
  }
}</p><p>//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Begin of MCP routines - no library needed
</p><p>//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p><p>void SetupRelays() {
  // Setup all possible relay cards (address 0 to 7)
  for (byte i = 0; i <= 7; i = i + 1) {
    MCP_Write(i, 0x00, 0b00000000);      //  set all pins to output
    MCP_Write(i, 0x12, 0b00000000);      //  set all outputs to off
  }
}</p><p>void WriteRelays(byte address, byte data) {
  // Write data to relays
  // --------------------
  MCP_Write(address, 0x12, data);
}</p><p>void MCP_Write(byte MCPaddress, byte MCPregister, byte MCPdata) {
  // I2C write routine 
  // -----------------
  MCPaddress = MCPaddress + 0x20;    // 0x20 is base address for MCP
  Wire.beginTransmission(MCPaddress);
  Wire.write(MCPregister);
  Wire.write(MCPdata);
  Wire.endTransmission();
}
</p><p>//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p><p>// End of MCP routines
</p><p>//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>

Enjoy !

Step 10: Building and Selling This Project

If there is a company interested in building this I2C Relay Board, please do so. It would be great if this can be made available for hobbyist as a ready-made add-on.

s.

Comments

author
mdchaara made it! (author)2017-04-15

Hi Eric. Wonderful work! Thanks for sharing this without copyrights!

implementing this will make automation with Blynk a very neat project, especially implementing an expansion board with a modular design.

I think you already know this, but can use fritzing to draw the boards. It will save you from the board size limitations of eagle ?

I have a question to you though: I am new to the field, with very very little experience. I see that the uln2903 is rated to a max output of 50v in the datasheet, which is ok to drive a 45v DC relay. I am assuming that this is the voltage of the output of the IC itself to the TTL control signal (and I might be wrong). There are relays available to drive 250v A/C with a current of 10A like KY-019. Would the current setup be sufficient to run such relays, or are the relays you have included are of a higher rating?

Thank you.

author
Eric Brouwer made it! (author)Eric Brouwer2017-04-15

Hi, it is a pleasure to share...
Personally, I will not drive relays with coil voltages above 24V DC. The relays that was used in this project is rated at 10A @125V AC, or 8A @220V AC. This unitcan be used to switch the AC load directly.

REgards

author
mdchaara made it! (author)mdchaara2017-04-15

Thank you for the quick response.

I think I was not clear in my question yet you answered me non the less.

I would not drive the relay coil with a voltage that high. most probably it will be the 5v supplied by the power source of the board. The relays I want to use once I start working on this will be used to run 240v AC, of course I need to figure out a way to have the whole thing running in parallel with the traditional switches and have an overide switch to change the whole thiny to manual should anything happen to the board.

So you recon the transistors should not have a problem with a relay even if it is rated to handle a higher ac current as long as the coil is being run with less than 50 v?

Thank you.

author
scotts88 made it! (author)2016-09-03

how do you use th i2c as i am new to it

author
Eric Brouwer made it! (author)Eric Brouwer2016-09-10

See https://learn.sparkfun.com/tutorials/i2c for some information about the protocol.

author
fritzstoop made it! (author)2016-07-25

Awsome instructable. where do you get the components so cheap?

author
Eric Brouwer made it! (author)Eric Brouwer2016-07-25

I think most of the components were purchased from a local dealer.
www.rabtron.co.za

author
Marcelotom made it! (author)2016-07-04

Guy! Very good your project. I will test and deploy. Congratulations.

author
Eric Brouwer made it! (author)Eric Brouwer2016-07-05

Thank you.

author
Zarris made it! (author)2016-04-05

If I had an AC signal(say a 1000hz audio signal) that i wanted to use to switch on a relay, is there a relay that can do that?

author
JoanM32 made it! (author)2016-01-13

If i want 16 relay output, I only need add another ULN2803 and the relay's circuit? I need reflash the MCP?
Help please... im new on this.

author
Eric Brouwer made it! (author)Eric Brouwer2016-01-21

The MCP23107 has 16 outputs, but on my PCB, I only had space for 8 relays. My PCB design makes use of GPA0..7 ports. If you want to add the other 8 relays, you can used the GPB0..7 ports as well, and just add a second ULN2803 and relays.

No programming is required on the MCP23107.

author
sspence made it! (author)2015-11-03

I was getting ready to follow up my MCP23017 LCD interface with a relay board when I came across this. Awesome job! Will be blogging about it. Take a look at my MCP23107 LCD interface at http://arduinotronics.blogspot.com/2015/11/diy-i2c-lcd.html

author
AlyssonR2 made it! (author)2015-09-15

A nice little 'ible, this - and one with a lot of possibilities.

The addition of a 9-way SIL header to link from the GPB pins of the MCP23017 to the GPA pins of a second board where the MCP23017 hasn't been installed (plus the supply ground) would allow the full 16 relays to be handled by a single MCP23017.

author
Eric Brouwer made it! (author)Eric Brouwer2015-09-16

Thank you.

Header pins was an option, but I could not get long enough headers locally for the second board to clear the relays on the first board.

author
AlyssonR2 made it! (author)AlyssonR22015-09-17

I was thinking more along the lines of sockets on the boards. Yes, extra wiring is ugly and prone to failure at the connectors, but cheap and simple.

The alternative would be DIL headers and IDC interconnect, allowing everything (including I2C and logic power) to be daisy-chained at the 'back' edge - and perhaps allowing status LEDs to be mounted on the front edge?

author
NeilRG made it! (author)2015-08-12

Excellent instructable and a good illustration of engineering practice, especially as a design moves from idea to test setup to final implementation, and the decision making process at each juncture. I was inspired .

author
Eric Brouwer made it! (author)Eric Brouwer2015-08-13

Thanks for the nice feedback. Glad you enjoyed it.

author
galen made it! (author)2015-08-02

Very nice write up. Thank you.

author
Eric Brouwer made it! (author)Eric Brouwer2015-08-03

Thank you

author
Dimitris Platis made it! (author)2015-08-01

Awesome!

author
Eric Brouwer made it! (author)Eric Brouwer2015-08-02

Thank you

author
K.Crankshaw made it! (author)2015-08-01

Really epic 'ible. Great to see some other South African's here :D

author
Eric Brouwer made it! (author)Eric Brouwer2015-08-02

Thank you

author
Modscientist made it! (author)2015-07-26

I'm an Audio video tech and we use relays a lot in A/V system to do several switching duties. This was before company like Crestron, AMX and the other popular companies that have switching thru wireless and macro sources. I found that putting a diode across the coil took care of the problem. This was also for same voltages. This circuit is great. One circuit for a multiple voltages. Great job!!

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

Thanks for the feedback. I did not install separate fly-back diodes across the relay coils, as these diodes are already part of the ULN2803 transistor array.

author
Modscientist made it! (author)Modscientist2015-07-30

Eric, yes this is true. The relays that I used were more for higher voltages relay banks. Primarily 70v-120v and were used to prevent relay chatter that would sometimes occurred when the relay would be activated under load. I would rather use relays then any solid state. Although solid state takes up less room there are some places good old fashion Electronics can not be replaced. It's great to see so many fellow techs and engineers coming together to talk shop with open minds and excitement to share. And Erick thank you for interacting and creating a discussion not a debate. Peace ModSceintist

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-30

If one want to go into a debate, there will be endless options for each and every design. One needs to look at the inputs of a design, decide what to do with the data, and control outputs with this data.
With the technology available, there will be endless options to choose from. But I am limited to a very small local electronic supplier, and must use what they have available.

For 12 - 220V loads, I have always preferred the good old trusty relay. Just because it provides electrical isolation.

Thanks again for your nice feedback.

author
AtypicalGeek made it! (author)2015-07-29

Great 'ible! Thank you for your time and attention to detail in putting this into an easy to understandable format. I understood most of it on the first read ;)

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-30

Thank you for the nice feedback.

author
BigShoe made it! (author)2015-07-27

hi friend,

since you made this wonderful instructable you can add the information regarding the very well-known processor AT328 or the AT168.
if you have one of those or other in the same series or even another proccessor that uses a serial output, you can easily expand the outputs by using a shifter - 74HC595

greets!

author
BigShoe made it! (author)BigShoe2015-07-27

+ awesome improvement for the relays array:

you can use a "moc" component (for example the moc3021).
it has the same use as the relay has, though you need to make a slight change for your scheme.

here are the advantages:

1. moc's current is much lower comparing to relay's.

2. no mechanic parts inside - meaning you can switch on and off with a high speed & accuracy and without physical deterioration.

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-28

I agree. It all depends on what you want to switch. The moc devices are much more expensive compared tothe relays when one wants to switch 5A loads.

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-28

See Step 9 for an example and source code to use the board with an Arduino.

As I wanted to keep the number of wires between the boards to a minimum, I opted for the I2C method (4 wires) instead of the 74HC595 (5 wires).

author
SPLatManOz made it! (author)2015-07-26

Nice project.

A word of caution, however: The board, as designed, is not "legally" safe to use to switch mains voltages. The reason is that you have coil traces too close to contact traces. The legal minimum, mandated by safety authorities, is 6.4mm in the US and 8mm in Europe. Your layout has roughly one quarter that spacing. This spacing is called creapage distance.

The problem is that dirt on the PCB can result in an arc-over and possible electric shock, or worse.

If anyone intends to use this board on 120/240V, it can be made "informally" safe by giving it a good clean with alcohol and then, when it's dry, coating the board. Almost any lacquer will do, though ideally proper conformal coating. I say "informally" because this measure is allowed in some jurisdictions, but only if the coating process is carried out in a quality audited facility.

The other thing that could be done is to mill slots in the PCB between the mains and low voltage parts of the circuit. Further information: http://forum.arduino.cc/index.php?topic=137049.0

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

Thanks for your good feedback. Although I did not mention it, I do clean all my boards with flux remover, then water, and afterwards cover all my PC boards with lacquer after testing.

But on the next batch of boards, I will keep this in mind, and use jumpers to connect the common supply rails to the relays.

author
SPLatManOz made it! (author)SPLatManOz2015-07-27

Eric, nIce to hear you clean and coat your boards.

If you have the choices in SA, and intend to revise the PCB, you might hunt around for relays that have the coil pins well separated from the contacts. For example Hong Fa JQX14FW or FF. I am not sure how the footprint would work for you. I am "retired and divested" now but "we" use(d) thousands of those.

BTW, Having to do it all myself, now, I designed my first PCB in 20 years last week. I shied away from Eagle because of the size limitation, and went with DesignSpark PCB (a whole new learning curve!). It has some rough edges, but it did the job, including auto-routing a passable first approximation.

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

Thanks for all your feedback. The relays I use, are the only once cheap enough for hobby used. When I revise the PCB, I will give DesignSpark a test.

author
marc.deloor.5 made it! (author)2015-07-26

A very well done instructable, very nice work!

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

Thank you

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

Thank you.

author
jwhitten made it! (author)2015-07-26

+1 for use of the MCP23017, well done!

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

Thanks

author
rusty0101 made it! (author)2015-07-26

Just as an idea for future improvements, if you can drag the extra 8 i/o pins from the MCP23017 so you could stack a daughter board with a second uln2803 and relays, you could be controlling all 128 relays that it would be possible if you could build larger boards. Another option would be to use smaller relays if what you are switching on and off can work with them. You might look at relays like the NTE R56S−5D.5−24D and use a 3pin header for co/no/nc connections instead of screw terminals. But yes, that would reduce the load you could control per relay.

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

I agree, and I did have a look at the daughter board option. Then I got lazy....

author
JaredE made it! (author)2015-07-26

Great Instructable. I will save this for a future project. You can get the MCP23017 and ULN2803 as free samples from the manufacturers' web sites.

http://www.microchip.com/wwwproducts/Devices.aspx?...
http://www.ti.com/product/ULN2803A/samplebuy

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

With the non-existing postal service in South Africa, the average waiting time for a parcel abroad can be up to 12 months.

Lucaly, these chips are available locally.

author
Alex_Calibur made it! (author)2015-07-26

another great improvement you can easilly made : use the non used pins of MCP23017 or 23018 as inputs ;)

author
Eric Brouwer made it! (author)Eric Brouwer2015-07-27

I agree. But for the original design, I needed all the relays, and due to PCB size limitations in Eagle, I did not have space for more functions.

author
KiwiMrDee made it! (author)2015-07-26

Loved step 10, I appreciate your generosity.

About This Instructable

76,632views

699favorites

More by Eric Brouwer:ESP8266-01 IoT Smart Timer for Home AutomationInstructables Hit Counter (ESP8266-01)Converting Camping Deep Freezer Into Fridge/Freezer Using AtTiny85
Add instructable to: