Twin Relay (BV4601)
Contents
- Serial Interface Windows
- I2C interface Windows
- Serial RPi
- I2C Rpi
- Arduino
- SWS Utility
- Mini Project
- Purchase <Byvac Shop> <eBay shop>
Data Sheet - This gives the commands and technical details
The BV4601 is a twin relay that can be controlled with either a serial or I2C input. The connectors on the side allow it to be stacked without the need of additional wiring.
The relays can be timed on or off from 1mS to 48 days. In addition there are three 10 bit ADC inputs and one digital output.
This text describes how to use the device, the full technical details are in the data sheet. The sideways connector is used throughout the range and so rather than including it here, it has its own page. For details of the connector see the sideways stackable connector page.
Windows Serial
This is the easiest method of control as it is much easier to debug than I2C. Even if using with I2C it may be worth running through some serial commands first.
For the relays to operate correctly +V must be at leas 5V and this can be provided by the USB interface. +L can be anywhere between 2.5V and 5.5V so in this case these are connected together. For devices that operate on 3.3V logic see the sideways stackable connector page.
Once connected use BV_COM, this is free and does not need installing, just run the exe.
Set echo on, set the correct COM port.
- type fH (that is lower case f, upper case H followed by enter). The device will respond with ACK ([06]).
- type fD to get the device number and the result will be 4601[06]
- type fa1,1000 This will turn on relay 1 in 1 second
- type f0 to turn it off (this will turn all the relays off)
Above is what you should expect using BV_COM
Windows I2C
There is no output on a Windows PC that will give I2C and so some other device is required. For this a BV4221_V2 device is used that allows an I2C device to be connected via the USB. The Python code 'BV4221_i2c.py' is given in the link as the BV4221_V2 does require setting up for this it is best to follow the instructions on that link. If you have the dialog box running above then you can run the 'swsi2c.py' code and obtain the dialog box as above but this time via the I2C interface.
Raspberry Pi Serial
The connection details are as shown. Observe that TX goes to RX and RX goes to TX. The RPi will operate two or even four relays from its 5V supply but more than that is pushing it. The Rpi needs to be prepared a bit to enable the serial port to be used as it is normally used by the system. Instructions for this are here but can also be found on many websites. Install and open minicom (instructions are in the previous link) just to get a feel of the device.
- type ctrl-A e (this tuns on local echo)
- type fH (this returns ACK but spoils the minicom screen)
- type fD (returns 4601)
- type fa1,1000 (turns on relay 'a' in 1 second)
- type fo (turns relay off)
This is what the session looks like.
Raspberry Pi I2C
Special Note: By default this device is serial. I2C is activated by the pull up resistors on the I2C bus. The implication is that when connecting to the Rpi, the SDA and SCL lines must be connected BEFORE connecting the power to this board. This is only necessary if the board is connected to the RPi that is already switched on.
Connection to the RPi via the I2C lines. Here the logic voltage is not important as the PRi will control this with its own internal pull up resistors.
If using the 5V supply then the +L and +V can be connected together
Quick test
Assuming i2c-tools are installed then i2cdetect -y 1 should reveal the I2C address of 0x33.
- Turn on relay 1 now: i2cset -y 1 0x33 1 1 0 1 i
- Turn off relay 1 now: i2cset -y 1 0x33 1 0 0 1 i
- Turn on relay 1 after a delay of 1000: i2cset -y 1 0x33 1 1 3 0xe8 i
- Turn on relay 2 now: i2cset -y 1 0x33 2 1 0 1 i
- Turn off relay 2 now: i2cset -y 1 0x33 2 0 0 1 i
- Turn on relay 2 after a delay of 1000: i2cset -y 2 0x33 1 1 3 0xe8 i
NOTE: When using the RPI power supply there is a surge of current when the relay turns on. This can upset the I2C bus and stop it working. For devices like this it is recommended that a capacitor is used across the relay +V supply.
Before using I2C install notsmb. This makes interfacing with I2C far easier than using smbus. The easiest way now is to use the sws software but it is possible to use Python interactively without the overhead of the GUI.
Arduino
There are two libraries for the Arduino depending on the interface used. Both have been created using release 1.01.
The library consists of the following functions.
- constructor: RELAY rly(0x33) // supply default 7 bit address of relay board
- rly.on(relay, on_time) // relay is a number 0 or 1 and the time is a 16 bit value in mS(aprox.)
- rly.off(relay, off_time)
- rly.alloff() // turns all of the relays off
- rly.vref(ref) // sets voltage reference ref is a value 1 to 4
- rly.digital(n) // This pin is always an output set value on n=0 for low, n<>0 for high
- rly.gettimer(relay) // returns 16bit value of relay timer, relay is 0 or not 0
- rly.adc(chan) // returns value on channel where channel is 1 to 3
Relay 'a' is 0 and relay 'b' is not 0, any value will do, choose 1. The functions follow the commands in the data sheet and the relay designation of 0 or 1 is consistent with both serial and I2C.
The example uses the serial monitor to show the timer values and also the ADC values.
SWS Utility
Instructions for both Windows and RPi for obtaining and running this utility in the software section. Assuming the BV4601 is connected then pressing "open the Selected Device" button will produce this screen.
From here you can set the relays and check the ADC values. The screen shot is from the Raspberry Pi
Mini Project
The purpose of this project is to get an idea of how to use the device in a real situation, it may not be practical but gives a staring point.
This shows an LDR (Light Dependent Resistor) connected to the ADC1 input and a 12V battery with a car bulb connected to the relay. An LDR will increase in resistance the darker it gets and so the idea is to turn a light on at a certain point. The readings I got were about 200 in reasonable light and 500 in darkish conditions. You can try this using the SWS Utility and updating the ADC values.
The program is in Python (obviously) and the complete code is here:
#!/usr/bin/env python # BV4601 twin relay mini project # import sys import serial from sv3common import sv3 TURNOFF = 300 TURNON = 400 deviceAdr = 'f' # ****************************************************************************** # Mini Project for BV4601 - relay controller # use python bv4601mp.py "/dev/ttyAMA0" # or python bv4601mp.py "COM5" # ****************************************************************************** def main(): # input com port on start sp = None if len(sys.argv) < 2: print "Com port needed as part of input" else: print "use ctrl-C to stop" while 1: try: sp = serial.Serial(sys.argv[1], 9600, timeout=.05, stopbits=1, parity='N' ) except: print "Cant open port ",sys.argv[1] break # get adc 1 value v = int(sv3(sp,deviceAdr+"g1\r")) if v < TURNOFF: sv3(sp,deviceAdr+"a0,0\r") if v > TURNON: sv3(sp,deviceAdr+"a1,0\r") if __name__ == "__main__": main()
It does import "sv3common.py" so the easiest thing to do is to copy the above text into a file and run it from the sws folder.