B4R Code Snippet 5V 8-Channel relay module

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
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
IMG_20170511_010152.jpg


Enjoy...
 
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
Hi Peter,

I have the same 8 channel Relay and Wemos D1 8266. I connect relay and D1 as your picture. I run your code but nothing happen. Could you please help me? I'm jus a newbie in Arduino world.

Thank you so much.
 

Cableguy

Expert
Licensed User
Longtime User
Pin mapping in the Wemos is a bit different that in Arduino uno. I generally figure them out with trial and error but you can easily find relational tables for the pin mapping across different devices just by googling.
 

Peter Simpson

Expert
Licensed User
Longtime User
Our new manager @Cableguy is correct, but also the voltages are also different between an Arduino and an ESP8266/D1 mini, so you need to make sure that you get that correct too.

Also it depends on your application but if you only want to control those 8 relays with an ESP8266 then you could always use my code below and modify the ESP8266 code by first removing anything to do with MQTT, then adjust it to suit your needs, but please note that you will need to add a 74HC595 shift register in between your D1 Mini an 8 relays.

Controlling 8 relays via ESP8266 and shift rehiregi
https://www.b4x.com/android/forum/t...8-relays-wemos-d1-mini-wifi-android-4g.80909/

Learn about relays
https://www.b4x.com/android/forum/threads/relays-dont-get-caught-out.76771/

I may or may not have put code on here that controls relays using an ESP8266 via a shift converter, if I haven't I'll try to put code on in the next few days as I should be less busy then.
 
Top