Velleman K8056 Arduino library

I own a Velleman RS232 8-channel Relay card for some years now. I used to use it for switching on and of the lights and heaters of my reptile enclosures. When I was still a student I used a PC and a VB6 program to sent the RS232 commands. Ever since I left my student apartment I was collecting dust on a shelf. After I was introduced to the Arduino board I realized that an Arduino could be used to sent the needed RS232 commands. Due to chronically lack of time, 2 small kids, I didn’t try to get it to work. Well, enough chit chat. Lets talk RS232, Arduino, Velleman Relaiscard and libraries.

RS232 commands

The card handles several RS232 commands of which Clear, Set and Toggle are the most important ones.

  • ‘E’ : Emergency stop all cards, regardless of address. (Carefull, relays turned on by open collector inputs will not be turned off by this command).
  • ‘D’ : Display address. All cards show their current address in a binary fashion. (LD1: MSB, LD8: LSB)
  • ‘S’ : Set a relay. ‘S’-instruction should be followed by relay #’1′ (0x31) to ‘9’ (0x39) (‘9′ sets all relays at once)
  • ‘C’ : Clear a relay. ‘C’-instruction should be followed by relay #’1′ (0x31) to ‘9’ (0x39) (‘9′ clears all relays at once)
  • ‘T’ : Toggle a relay. ‘T’-instruction should be followed by relay #’1′ (0x31) to ‘8’ (0x38)
  • ‘A’ : Change the current address of a card. ‘A’-instruction should be followed by the new address (1..255)
  • ‘F’ : Force all cards to address 1 (default)
  • ‘B’ : Send a byte. Allows to control the status of all relays in one instruction, by sending a byte containing the relay status for each relay. (MSB: relay1, LSB: relay8)

The sequence is:

  1. CHR$(13)
  2. card address (1..255)
  3. instruction
  4. address (1..255) or relay# (‘1′..’9′ ASCII)
  5. checksum (2-complement of the sum of the 4 previous bytes + 1)

The sequence should be sent at least twice for proper operation of the card. Add a pause of at least 200ms between 2 instructions. In my example the delay between the toggle, if set too low, will fail to do something. Before I forget: Baudrate is 2400.

The wiring to Arduino

The arduino boards have 1 or more serial ports available, the library is designed with my Arduino MEGA so it will accept up to 4 serial ports if compiled to something different than a MEGA 2560 it will default to Serial0 even if 3 is set during initialize(). The Arduino Serial ports output logic (TTL) level which is incompatible with RS232. By using a TTL to RS232 converter chip/breakout the logic levels are easily converted.

found this image on the internet some time ago, unfortunately I forgot the original source. Feel free to inform me so I can mention it here.

I used the MAX3232 breakout board from SparkFun to do my testing with.

ArduinoMAX3232 breakoutK8056 relay card
TxT1IN
Rx (optional)T1OUT
T1OUTIN
GndGnd0
+5V3V-5.5V

Library

To make implementation a lot easier I created a library for use with the K8056. Usage is fairly easy. I only implemented the most commonly used commands, the rest is on my TODO list.

https://github.com/AlbanT/VellemanK8056library

 #include <K8056Velleman.h> 
K8056Velleman Relay; 
void setup()
{ 
   Relay.begin(3); 
   Relay.EmergencyStop(); 
   for (int i = 0; i <= 8; i++) 
   { 
      Relay.ON(1,i); delay(500); 
   } 
   delay(500); 
   Relay.OFF(1,4); 
   delay(2000); 
   Relay.EmergencyStop(); 
   delay(2000); 
   Relay.Toggle(1,3); 
   delay(1500); 
   Relay.Toggle(1,3); 
   delay(1500); 
   Relay.Toggle(1,3); 
   delay(1500); 
   Relay.Toggle(1,3); 
   delay(2000); 
   Relay.digitalWrite(1,6,HIGH); // same as Relay.ON(1,6);
 delay(2000); 
   Relay.digitalWrite(1,6,LOW); // same as Relay.OFF(1,6);
} 

void loop()
{ 

} 

begin(byte SerialAddress)

Initializes the Serialport and sets the 2400 Baud. SerialAddress (optional) : the Serial port number on the Arduino, if omitted Serial0 is automagicly used. If set to for example 3 (=Serial3.begin(2400)) and compiled for a board other than the Arduino MEGA 2560 it will default back to Serial0 (=Serial.begin(2400)). Feel free to add more multi serial port boards to the check in the library, please update me.

ON(byte CardAddress, byteRelay )

Turns a single, or all, relay(s) ON. Same as the ‘S’-instruction. CardAddress : the address of the relay card. Up to 255 Relay cards can be connected. Relay : an integer between 1 and 9. 1 to 8 are the individual relays and 9 is all together.

OFF(byte CardAddress, byte Relay )

Turns a single, or all, relay(s) OFF. Same as the ‘C’-instruction. CardAddress : the address of the relay card. Up to 255 Relay cards can be connected. Relay : an integer between 1 and 9. 1 to 8 are the individual relays and 9 is all together.

Toggle(byte CardAddress, byte Relay )

Switches the current state of the given relay. If currently ON then Toggle will turn it OFF and vice versa. Same as the ‘T’-instruction. CardAddress : the address of the relay card. Up to 255 Relay cards can be connected. Relay : an integer between 1 and 8.

digitalWrite(byte CardAddress, byte Relay , byte Status)

Turns a single, or all, relay(s) ON or OFF depending on the value of Status. CardAddress : the address of the relay card. Up to 255 Relay cards can be connected. Relay : an integer between 1 and 9. 1 to 8 are the individual relays and 9 is all together. Status : HIGH to turn ON and LOW to turn OFF.

EmergencyStop()

Emergency stop all cards, regardless of address. (Careful, relays turned on by open collector inputs will not be turned off by this command). Same as the ‘E’-instruction.