mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

mLink serial I2C modules
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by admin » Thu Oct 07, 2021 3:48 pm

Image



The mLink relay modules are a range of 5V relay modules that have been designed to be controlled either manually or via a microcontroller. For manual control there is a set of digital pin(s) that can be toggled to directly control the relay. For controlling via a microcontroller there is a serial I2C interface minimising the amount of pins required to control multiple relays or relay modules.


For Arduino users you can use the mLink library (see below) to control any type of mLink module. Only one single instance of the library is needed to control multiple types of mLink modules resulting in very little resources overhead and therefore making it great for Arduinos with small amounts of memory and pin counts.

For Raspberry Pi users we have a Python module which can be installed via pip or downloaded and installed directly from our forum. Please see the mLink Python forum thread for requirements and download link here: viewtopic.php?f=131&t=3062&p=8592#p8592



ImageImageImage


Module specifications:
Module code: 							HCMODU0182 (1ch relay module)
									HCMODU0183 (2ch relay module)
									HCMODU0184 (4ch relay module)
Supply Voltage (VDD): 					4.5V to 5.5V
Current consumption (relay(s) off):			5.5mA
Current consumption (relay(s) on):			70mA (1ch), 125mA (2ch),  235mA (4ch)
Interfaces:							I2C, relay digital input(s), NO-COM-NC relay screw terminals.
I2C Interface speed: 						400kbits/s (fast mode)
I2C default address (HEX): 				0h52
Relay input (RL0 to RL3) low level voltage: 	0V to 1.5V 
Relay input (RL0 to RL3) high level voltage: 	3.5V to 5V
Relay contact rating (resistive load): 			5A max at 28VDC or 240VAC
Maximum number of modules: 				5 with pullups fitted, 112 with pullups removed*
Module dimensions (inc headers):			48.5mm x 38mm x 19.2mm (1ch relay module)
									63.5mm x 38mm x 19.2mm (2ch relay module)
									96.5mm x 38mm x 19.2mm (4ch relay module)

*Note the maximum number of connected modules will depend on cable lengths and power requirements of each module. Do not exceed 5 mLink modules connected in series with all fitted to all modules.





Standalone Control Example:

The mLink relay modules can be used standalone just like most other types of relay module. They provide a set of digital input pins (RL0 to RL3) to control each relay independently. Pulling a pin high (connecting it to 3.5V to 5V) will turn the relay on, pulling it low (0V/GND), or leaving it disconnected will turn the relay off.

Image





Arduino Connection Example:

When used with microcontollers such as Arduino the mLink relay modules can be controlled via a serial I2C interface.

Image

Image


Because the modules use an I2C interface this also means multiple modules can controlled from a single Arduinos I2C interface simply by daisy-chaining them together. Note to control multiple mLink modules of the same type requires changing the default I2C address of the additional modules. See mLink Library Quick Start Guide for how to do this.


Image




Toggle relay 0 example

Arduino Toggle Relay Example:

The following example will toggle (blink) relay 0 on and off every 10 seconds. If your module has more than one relay you can use SET_RLY1 for relay 1, SET_RLY2 for relay 2, or SET_RLY3 for relay 3 instead.

  1. #include "mLink.h"
  2.  
  3. mLink mLink;
  4.  
  5. #define I2C_ADD 0x52 // Default I2C address
  6.  
  7. void setup()
  8. {
  9.   mLink.init();
  10. }
  11.  
  12.  
  13. void loop()
  14. {  
  15.   mLink.SET_RLY0(I2C_ADD, HIGH);     // Turn relay 0 ON.
  16.   delay(10000);
  17.  
  18.   mLink.SET_RLY0(I2C_ADD, LOW);     // Turn relay 0 OFF
  19.   delay(10000);
  20. }

Read relays state example

Reading The State Of Relay 0 Example:

If you need to check the current state of a relay, for instance to check if its state has been changed via the external RL pin header, this can be done by reading the appropriate bit from the modules relay status register. As with the previous example the mLink library has macros to make this straightforward:

The following example will read the current state of relay 0 and will output the state to the serial monitor. If your module has more than one relay you can use READ_RLY1 for relay 1, READ_RLY2 for relay 2, or READ_RLY3 for relay 3 instead.

  1. #include "mLink.h"
  2.  
  3. mLink mLink;
  4.  
  5.  
  6. #define I2C_ADD 0x52 // Default I2C address
  7.  
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(9600);
  12.  
  13.   mLink.init();
  14.  
  15.   boolean state = mLink.READ_RLY0(I2C_ADD); // Get the current state of relay 0
  16.  
  17.   Serial.print("Relay 0 state = ");
  18.  
  19.   if(state == 1)
  20.     Serial.println("ON");
  21.   else
  22.     Serial.println("OFF");
  23. }
  24.  
  25.  
  26. void loop()
  27. {
  28.  
  29. }
  30.  

Relay timer mode

Setting Relay 0 On Time Example:

Putting a relay into timer mode allows you to specify the minimum amount of time a relay will stay energised for when triggered via either the I2C interface or its external RL trigger pin. To put a relay into timer mode its on time in seconds needs to be set by writing to the appropriate modules on time registers. Using the mLink library this can be easily done with one of its predefined macros:


The following example sets the on time for relay 0 to 10 seconds. The maximum on time for a relay is 65535 second. Setting the on time to 0 will disable timer mode and the relay will operate as normal. If your module has more than one relay you can use RLY1_SetOnTime for relay 1, RLY2_SetOnTime for relay 2, or RLY3_SetOnTime for relay 3 instead.

  1. #include "mLink.h"                                
  2.  
  3. mLink mLink;        
  4.  
  5. #define I2C_ADD 0x52 // Default I2C address
  6.  
  7. void setup()
  8. {
  9.   mLink.init();    
  10.  
  11.   mLink.RLY0_SetOnTime(I2C_ADD, 10); // Set the on time for relay 0 to 10 seconds
  12. }
  13.  
  14.  
  15. void loop()
  16. {
  17.  
  18. }




Raspberry Pi Connection Diagram

Image

Please note: when connecting to a Raspberry Pi the mLink modules I2C pullup resistors should be removed. See the 'Removing the modules I2C pullup resistors' section below for more information.


Relay toggle example

Raspberry Pi relay 0 toggle example:
mLink Raspberry Pi relay 0 toggle example.

This program requires the mLink Python library module to be installed. Please see the following forum thread for more information on how to install the Python module and requirements:

viewtopic.php?f=131&t=3062

  1. from mLink import mLink                     # Import the mLink module
  2. import time
  3.  
  4. ml = mLink.mLink(1)                         # Create an instance of the module
  5.  
  6. I2C_ADD = 0x52                              # Default I2C address is 0x52
  7.  
  8.  
  9. while 1:
  10.     ml.Relay0_On(I2C_ADD)                   # Turn relay 0 on
  11.     time.sleep(1)
  12.     ml.Relay0_Off(I2C_ADD)                  # Trun relay 0 off
  13.     time.sleep(1)



Removing the modules I2C pullup resistors

Please note that the modules I2C pullup resistors should be removed if you intend to use it as follows:

1) You wish to connect more than 5 modules to the same I2C bus.

2) You have a 3.3V development board that does not have a 5V tolerant I2C interface.

3) You have a 3.3V development board that has its own I2C pullup resistors which are connected to its 3.3V supply.


To disconnect the modules two 10K pullup resistors from the I2C bus, locate the 3 jumper pads labelled ‘END DEV’ and cut the two tracks linking them together as follows:

Image





Image

mLink Arduino library
viewtopic.php?f=58&t=3001


mLink Raspberry Pi Python module
The mLink python module can be installed with the following terminal command:

  1. pip install hc-mlink

Alternatively the library can be manually installed by downloading it from the forum and unzipping it to your project folder. See the Python module forum thread for more information and download link:
viewtopic.php?f=131&t=3062&p=8592#p8592

Please note that in some cases there may be additional configuration required. If you have issues getting your Raspberry Pi to communicate with the mLink module then please see the Python module forum thread here: viewtopic.php?f=131&t=3062


mLink Library Quick Start Guide For Arduino Users
https://hobbycomponents.com/downloads/m ... _Guide.pdf


mLink Library Reference Guide For The Relay Modules
https://hobbycomponents.com/downloads/m ... _Guide.pdf


mLink Specifications and Register Map For The Relay Modules
https://hobbycomponents.com/downloads/m ... er_Map.pdf


Customer contributions:

Tony Bond has kindly modelled the 2 channel version of the mLink relay in 3D. You can download it as a step file here:

https://hobbycomponents.com/downloads/m ... U0183.step




Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do no provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.

ChrisSharp
Posts: 30
Joined: Thu Dec 31, 2020 1:22 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by ChrisSharp » Mon Jun 19, 2023 3:50 pm

Hi,

I've got five 4ch modules plugged directly into each other. I'm thinking about adding another two.

What would I have to do to get round the five module limit?

I'm happy to remove the pull-up resistors. Just need to know which ones I need to remove. The additional two will be controlling things remote form the other five. So they can be wired independently with separate wires from the A4 and A5 pins on the nano.

Cheers
Chris

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by andrew » Tue Jun 20, 2023 8:08 am

I've got five 4ch modules plugged directly into each other. I'm thinking about adding another two.

What would I have to do to get round the five module limit?

I'm happy to remove the pull-up resistors. Just need to know which ones I need to remove. The additional two will be controlling things remote form the other five. So they can be wired independently with separate wires from the A4 and A5 pins on the nano.

As suggested you can either remove the pullup resistors (R3 & R4) from the additional boards located here...

mLink_Relay_I2C_Pullup_Resistors_V1_1.png


...or you can disconnect them from the I2C interface by locating the set of 3 jumper pads labelled 'END DEV' and cutting the tracks between them....
mLink_Relay_I2C_Pullup_Removal_V1_1.png

Ideally you'll want to arrange you modules so the last module on the bus still has the pullups fitted but this isn't critical.
You do not have the required permissions to view the files attached to this post.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.


MrGamecase
Posts: 2
Joined: Fri Sep 22, 2023 2:25 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by MrGamecase » Fri Sep 22, 2023 2:52 pm

Hi All,
Basic rundown of what im trying to acheive,

Id like to use the 4CH relay module without the MLINK library as i use ESPHome on all my controllers, What commands do i need to send to get the relay board mcu to switch over ic2 lines?.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by andrew » Fri Sep 22, 2023 3:31 pm

Just for reference, we have just published the mLink library to the PlatformIO registry:

https://registry.platformio.org/librari ... ents/mlink

So that should make using the mLink library with ESPHome a lot easier. To include the mLink library in your project you would just need to add it under libraries in your yaml file like this:

Code: Select all

libraries:
    - "Wire"
    - "mlink"

However, if you would prefer not to use it then below is an example Arduino sketch that just uses the wire library directly to write to the mLink relays I2C registers. The sketch just will blink the relays on and off in pairs.

The register that sets the state of the relays is at address 10 (decimal not hex) with the first 4 bits of the register controlling the state of each relay. Hopefully this will give you the information you need.


  1. #include <Wire.h>
  2.  
  3. #define I2C_ADD 0x52
  4. #define RELAY_STATE_REG 10
  5.  
  6. void setup()
  7. {
  8.   Wire.begin();
  9. }
  10.  
  11.  
  12. void loop()
  13. {
  14.   Wire.beginTransmission(I2C_ADD);
  15.   Wire.write(RELAY_STATE_REG);  
  16.   Wire.write(0b1010);
  17.   Wire.endTransmission(true);
  18.  
  19.   delay(1000);
  20.  
  21.   Wire.beginTransmission(I2C_ADD);
  22.   Wire.write(RELAY_STATE_REG);  
  23.   Wire.write(0b0101);
  24.   Wire.endTransmission(true);
  25.  
  26.   delay(1000);
  27. }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

MrGamecase
Posts: 2
Joined: Fri Sep 22, 2023 2:25 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by MrGamecase » Fri Sep 22, 2023 4:38 pm

andrew wrote:
Fri Sep 22, 2023 3:31 pm
Just for reference, we have just published the mLink library to the PlatformIO registry:

https://registry.platformio.org/librari ... ents/mlink

So that should make using the mLink library with ESPHome a lot easier. To include the mLink library in your project you would just need to add it under libraries in your yaml file like this:

Code: Select all

libraries:
    - "Wire"
    - "mlink"

However, if you would prefer not to use it then below is an example Arduino sketch that just uses the wire library directly to write to the mLink relays I2C registers. The sketch just will blink the relays on and off in pairs.

The register that sets the state of the relays is at address 10 (decimal not hex) with the first 4 bits of the register controlling the state of each relay. Hopefully this will give you the information you need.


  1. #include <Wire.h>
  2.  
  3. #define I2C_ADD 0x52
  4. #define RELAY_STATE_REG 10
  5.  
  6. void setup()
  7. {
  8.   Wire.begin();
  9. }
  10.  
  11.  
  12. void loop()
  13. {
  14.   Wire.beginTransmission(I2C_ADD);
  15.   Wire.write(RELAY_STATE_REG);  
  16.   Wire.write(0b1010);
  17.   Wire.endTransmission(true);
  18.  
  19.   delay(1000);
  20.  
  21.   Wire.beginTransmission(I2C_ADD);
  22.   Wire.write(RELAY_STATE_REG);  
  23.   Wire.write(0b0101);
  24.   Wire.endTransmission(true);
  25.  
  26.   delay(1000);
  27. }
Hi Andrew,

Many thanks for the rapid reply & supplied info..... ill have a tinker tonight with the library and ESPHome.


Kind regards
Mr.S.J.Dorrington

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by andrew » Sun Sep 24, 2023 8:32 am

If you've not already figured out a solution here is an example based on the esphome custom switch example:


Yaml file:
  1. esphome:
  2.   name: mLinkRelayExample
  3.   friendly_name: mLink Relay Example
  4.  
  5.   includes:
  6.    - mLinkRelay.h
  7.  
  8.   libraries:
  9.    - "Wire"
  10.     - "mlink"
  11.  
  12. esp8266:
  13.   board: d1_mini_lite
  14.  
  15. # Enable logging
  16. logger:
  17. # Enable Home Assistant API
  18. api:
  19.   encryption:
  20.     key: "your api key"
  21.  
  22. ota:
  23.   password: "ota password"
  24.  
  25. wifi:
  26.   ssid: !secret wifi_ssid
  27.   password: !secret wifi_password
  28.  
  29.   # Enable fallback hotspot (captive portal) in case wifi connection fails
  30.   ap:
  31.     ssid: "mLink Fallback Hotspot"
  32.     password: "fallback password"
  33.  
  34. captive_portal:
  35. switch:
  36. - platform: custom
  37.   lambda: |-
  38.     auto mLinkRelay0 = new mLinkRelay(0);
  39.     App.register_component(mLinkRelay0);
  40.  
  41.     auto mLinkRelay1 = new mLinkRelay(1);
  42.     App.register_component(mLinkRelay1);
  43.  
  44.     auto mLinkRelay2 = new mLinkRelay(2);
  45.     App.register_component(mLinkRelay2);
  46.  
  47.     auto mLinkRelay3 = new mLinkRelay(3);
  48.     App.register_component(mLinkRelay3);
  49.  
  50.     return {mLinkRelay0, mLinkRelay1, mLinkRelay2, mLinkRelay3};
  51.  
  52.   switches:
  53.     - name: "mLink Relay 0"
  54.       id: mLinkRelay0
  55.     - name: "mLink Relay 1"
  56.       id: mLinkRelay1
  57.     - name: "mLink Relay 2"
  58.       id: mLinkRelay2
  59.     - name: "mLink Relay 3"
  60.       id: mLinkRelay3



Then save the following to a file called mLinkRelay.h in your configuration directory:

  1. #include "esphome.h"
  2. #include "mLink.h"
  3.  
  4. #define I2C_ADD 0x52        // Default I2C address
  5.  
  6. class mLinkRelay : public Component, public Switch
  7. {
  8.   private:
  9.     int relay;
  10.  
  11.   public:
  12.     mLink ml;
  13.     mLinkRelay(int i)
  14.     {
  15.       relay = i;
  16.     }
  17.  
  18.   void setup() override
  19.   {
  20.     ml.init();
  21.   }
  22.  
  23.  
  24. void write_state(bool state) override
  25. {
  26.   // This will be called every time the user requests a state change.
  27.   switch(relay)
  28.   {
  29.     case 0:
  30.       state ? ml.SET_RLY0(I2C_ADD, 1) : ml.SET_RLY0(I2C_ADD, 0);
  31.       break;
  32.    
  33.     case 1:
  34.       state ? ml.SET_RLY1(I2C_ADD, 1) : ml.SET_RLY1(I2C_ADD, 0);
  35.       break;
  36.    
  37.     case 2:
  38.       state ? ml.SET_RLY2(I2C_ADD, 1) : ml.SET_RLY2(I2C_ADD, 0);
  39.       break;
  40.    
  41.     case 3:
  42.       state ? ml.SET_RLY3(I2C_ADD, 1) : ml.SET_RLY3(I2C_ADD, 0);
  43.       break;
  44.     }
  45.  
  46.     // Acknowledge new state by publishing it
  47.     publish_state(state);
  48.   }
  49. };
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

lewsut
Posts: 8
Joined: Tue Jan 09, 2024 9:08 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by lewsut » Tue Jan 16, 2024 8:54 pm

A useful addition would be a required "pulsed command", and if that pulsed command isn't received to fail closed.

Currently I can set my relays for a what could be semi critical system (in my case filling a water butt), my MCU fails or the ability to send the close command is lost, the relay stays open forever until commanded to shut. It's not very fail safe. With the MCU on board the relay module it would be nice to have the ability to do some watchdogging and the like.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: mLink 1ch (HCMODU00182), 2ch (HCMODU0183), And 4ch (HCMODU0184) I2C / Parallel Relay Modules

Post by andrew » Wed Jan 17, 2024 11:40 am

A useful addition would be a required "pulsed command", and if that pulsed command isn't received to fail closed.
It sounds like the timer mode will do what you require? By setting the on time in seconds for one of the relays will put it into timer mode. When in timer mode, when the relay is triggered it will automatically de-energise after that amount of seconds. So you would then only ever have to send a trigger command. Here is the relevant section from the quick start guide:

Relay timer mode Putting a relay into timer mode allows you to specify the minimum amount of time a relay will stay energised for when triggered via either the I2C interface or its external RL trigger pin. To put a relay into timer mode its on time in seconds needs to be set by writing to the appropriate modules on time registers. Using the mLink library this can be easily done with one of its predefined macros: The following example sets the on time for relay 0 to 10 seconds. The maximum on time for a relay is 65535 second. Setting the on time to 0 will disable timer mode and the relay will operate as normal. If your module has more than one relay you can use RLY1_SetOnTime for relay 1, RLY2_SetOnTime for relay 2, or RLY3_SetOnTime for relay 3 instead.

  1. #include "mLink.h" // Include the library
  2. mLink mLink; // Create an instance of the library
  3. #define I2C_ADD 0x52 // Default I2C address
  4. void setup()
  5. {
  6. mLink.init(); // Initialise the library
  7. mLink.RLY0_SetOnTime(I2C_ADD, 10); // Set the on time for relay 0 to 10 seconds
  8. }
  9. void loop()
  10. {
  11. }

Note that when you set an on time it is stored in the modules non-volatile memory so unless you need to change it you will only ever have to set it once.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “mLink”