SubName: Set relay 1-8 from 'NO' (Normally Open) to 'NC' (Normally Closed) and back again.
Description: Here is some basic code for switching an 8 channel relay from 'NO' (Normally Open) to 'NC' (Normally Closed). The code is initialising each pin (eight pins in total) in a loop and then runs Loopey. In Loopey each relay is being switched from 'NO' to 'NC', this is done one relay at a time with a 100 milliseconds delay until all the relays are closed, then the routine is ran again but in reverse switching the state of each relay one at a time until all the relays are 'NO' again.
Tags: 8, Relay, Module, Arduino, State
8 channel relay module connected to an UNO based board
Enjoy...
Description: Here is some basic code for switching an 8 channel relay from 'NO' (Normally Open) to 'NC' (Normally Closed). The code is initialising each pin (eight pins in total) in a loop and then runs Loopey. In Loopey each relay is being switched from 'NO' to 'NC', this is done one relay at a time with a 100 milliseconds delay until all the relays are closed, then the routine is ran again but in reverse switching the state of each relay one at a time until all the relays are 'NO' again.
AppStart
Initialize Pin 2
Initialize Pin 3
Initialize Pin 4
Initialize Pin 5
Initialize Pin 6
Initialize Pin 7
Initialize Pin 8
Initialize Pin 9
Relay 2 is Closed
Relay 3 is Closed
Relay 4 is Closed
Relay 5 is Closed
Relay 6 is Closed
Relay 7 is Closed
Relay 8 is Closed
Relay 9 is Closed
Relay 2 is Open
Relay 3 is Open
Relay 4 is Open
Relay 5 is Open
Relay 6 is Open
Relay 7 is Open
Relay 8 is Open
Relay 9 is Open
Relay 2 is Closed
Relay 3 is Closed
Relay 4 is Closed
Relay 5 is Closed
Relay 6 is Closed
Relay 7 is Closed
Relay 8 is Closed
Relay 9 is Closed
Relay 2 is Open
Relay 3 is Open
Relay 4 is Open
Relay 5 is Open
Relay 6 is Open
Relay 7 is Open
Relay 8 is Open
Relay 9 is Open
Initialize Pin 2
Initialize Pin 3
Initialize Pin 4
Initialize Pin 5
Initialize Pin 6
Initialize Pin 7
Initialize Pin 8
Initialize Pin 9
Relay 2 is Closed
Relay 3 is Closed
Relay 4 is Closed
Relay 5 is Closed
Relay 6 is Closed
Relay 7 is Closed
Relay 8 is Closed
Relay 9 is Closed
Relay 2 is Open
Relay 3 is Open
Relay 4 is Open
Relay 5 is Open
Relay 6 is Open
Relay 7 is Open
Relay 8 is Open
Relay 9 is Open
Relay 2 is Closed
Relay 3 is Closed
Relay 4 is Closed
Relay 5 is Closed
Relay 6 is Closed
Relay 7 is Closed
Relay 8 is Closed
Relay 9 is Closed
Relay 2 is Open
Relay 3 is Open
Relay 4 is Open
Relay 5 is Open
Relay 6 is Open
Relay 7 is Open
Relay 8 is Open
Relay 9 is Open
B4X:
'WIRE LEGEND for 8 channel relay module
'VCC = 5V
'In1 = D2
'In2 = D3
'In3 = D4
'In4 = D5
'In5 = D6
'In6 = D7
'In7 = D8
'In8 = D9
'GND = GND
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private RelayState = True As Boolean
Private Pin(10) As Pin
Private FirstRelay = 2, RelayCount = 8 As Int
End Sub
Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
Dim i As Int = FirstRelay 'First relay pin number (GPIO number)
Do While i < FirstRelay + RelayCount
Log("Initialize Pin ", i)
Pin(i).Initialize(i, Pin(i).MODE_OUTPUT) 'Initialise each pin and set the pin mode
Pin(i).DigitalWrite(True) 'Set the relay to NO (Normally Open), it's default state.
i = i + 1
Loop
'Loopey is for example purposes only
AddLooper("Loopey")
End Sub
Sub Loopey
RelayState = Not(RelayState)
Dim State As String 'State is for logging purposes only
If RelayState Then State = "Open" Else State = "Closed"
Dim i As Int = FirstRelay
Do While i < FirstRelay + RelayCount
Log("Relay ", i, " is ", State)
Pin(i).DigitalWrite(RelayState) 'Open or Close each relay (1-8) depending on its previous set state
Delay(100) 'Wait 1/10 of a second
i = i + 1
Loop
End Sub
Tags: 8, Relay, Module, Arduino, State
8 channel relay module connected to an UNO based board
Enjoy...
Last edited: