Skip to content

Commit cf7bcec

Browse files
authored
Merge pull request #15 from arduino/latch-relay
Add Latch relay support
2 parents a0e42eb + db1008a commit cf7bcec

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

examples/latch_relay.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
This example demonstrates how to use the Modulino Latch Relay module
3+
to turn a relay on and off repeatedly.
4+
5+
Initial author: Sebastian Romero (s.romero@arduino.cc)
6+
"""
7+
8+
from modulino import ModulinoLatchRelay
9+
from time import sleep_ms
10+
11+
relay = ModulinoLatchRelay()
12+
initial_state = relay.is_on
13+
14+
if initial_state is None:
15+
print("Relay state is unknown (last state before poweroff is maintained)")
16+
else:
17+
print(f"Relay is currently {'on' if initial_state else 'off'}")
18+
19+
while True:
20+
print("Turning relay on")
21+
relay.on()
22+
sleep_ms(150) # Wait for the relay to settle
23+
print(f"Relay is currently {'on' if relay.is_on else 'off'}")
24+
sleep_ms(1000) # Keep the relay on for 1 second
25+
print("Turning relay off")
26+
relay.off()
27+
sleep_ms(150) # Wait for the relay to settle
28+
print(f"Relay is currently {'on' if relay.is_on else 'off'}")
29+
sleep_ms(1000) # Keep the relay off for 1 second

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
["modulino/movement.py", "github:arduino/modulino-mpy/src/modulino/movement.py"],
1212
["modulino/pixels.py", "github:arduino/modulino-mpy/src/modulino/pixels.py"],
1313
["modulino/thermo.py", "github:arduino/modulino-mpy/src/modulino/thermo.py"],
14-
["modulino/joystick.py", "github:arduino/modulino-mpy/src/modulino/joystick.py"]
14+
["modulino/joystick.py", "github:arduino/modulino-mpy/src/modulino/joystick.py"],
15+
["modulino/latch_relay.py", "github:arduino/modulino-mpy/src/modulino/latch_relay.py"]
1516
],
1617
"deps": [
1718
["lsm6dsox", "latest"],

src/modulino/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
from .knob import ModulinoKnob
1414
from .movement import ModulinoMovement
1515
from .distance import ModulinoDistance
16-
from .joystick import ModulinoJoystick
16+
from .joystick import ModulinoJoystick
17+
from .latch_relay import ModulinoLatchRelay

src/modulino/latch_relay.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from .modulino import Modulino
2+
3+
class ModulinoLatchRelay(Modulino):
4+
"""
5+
Class to control the relay module of the Modulino.
6+
"""
7+
8+
default_addresses = [0x4]
9+
10+
def __init__(self, i2c_bus=None, address=None):
11+
"""
12+
Initializes the Modulino Buzzer.
13+
14+
Parameters:
15+
i2c_bus (I2C): The I2C bus to use. If not provided, the default I2C bus will be used.
16+
address (int): The I2C address of the module. If not provided, the default address will be used.
17+
"""
18+
super().__init__(i2c_bus, address, "Latch Relay")
19+
self.data = bytearray(3)
20+
21+
def on(self) -> None:
22+
"""
23+
Turns on the relay.
24+
"""
25+
self.data[0] = 1
26+
self.write(self.data)
27+
28+
def off(self) -> None:
29+
"""
30+
Turns off the relay.
31+
"""
32+
self.data[0] = 0
33+
self.write(self.data)
34+
35+
@property
36+
def is_on(self) -> bool:
37+
"""
38+
Checks if the relay is currently on.
39+
"""
40+
status = self.read(3)
41+
if status[0] == 0 and status[1] == 0:
42+
return None # last status before poweroff is maintained
43+
44+
if status[0] == 1:
45+
return False
46+
47+
return True
48+

src/modulino/modulino.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
0x76: "Knob",
2222
0x74: "Knob",
2323
0x6C: "Pixels",
24-
0x58: "Joystick"
24+
0x58: "Joystick",
25+
0x4: "Latch Relay"
2526
}
2627

2728
class _I2CHelper:

0 commit comments

Comments
 (0)