B4R Code Snippet 5V 8-Channel relay module

Discussion in 'B4R Code Snippets' started by Peter Simpson, May 11, 2017.

  1. Peter Simpson

    Peter Simpson Well-Known Member Licensed User

    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.

    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
    Code:
    '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(10As 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
    IMG_20170511_010152.jpg

    Enjoy...
     
    Last edited: May 17, 2017
    Beja, Jaames, Cableguy and 3 others like this.
Loading...