Friday, March 7, 2014

Arduino controlled BK Precision 1787B power supply

Intro

For one of my projects at work, I needed to control a BK Precision 1787B DC power supply with an Arduino.  I couldn't find much related to the setup and code on the internet, and I wrote my own.  I believe this code should also work for similar BK Precision power supplies.

Below, I explain the hardware setup, and provide the Arduino code I wrote for the occasion.

Hardware Setup

First and foremost, do not use the IT-E131 Communication Cable.  This cable serves to convert between TTL and RS232 logic.  Because both the power supply and the Arduino both use TTL for communication, you don't need the converter [4].

Second, I used an Arduino Mega 2560 because it has 3 additional serial ports.  It may be possible to use a standard Arduino along with the SoftwareSerial library [6], but I haven't checked this.

The figure below shows the setup.  Solder/connect the wires as shown below.  I used the second serial communication port (RX1 and TX1) on the Arduino Mega.

Figure 1.  Wiring setup.

The next figure shows the pinout of the DB9 interface on the power supply.

Figure 2.  DB9 pinout [5].

Quick and easy.  

Arduino Code

Because I only needed to talk to the power supply, I only implemented the write features.  If I find time, I plan on implementing the remaining features and converting the final product into a library for easier use.  If you need to read features sooner than that, email me with the request or implement it using my pre-written code as a template.

void setup(){
  //// BK1785 initialization
  BK1785write("RCon", 0);   // turn on remote control
  BKStatus();
  delay(100);
  BK1785write("SetV", 5000); // set voltage.  output still off. units in mV
  BKStatus();
  delay(100);
  BK1785write("SetI", 500); // set current.  output still off.  units in mA
  BKStatus();
  delay(100);
  BK1785write("OutputOn", 0);  // turn on voltage and current output
  BKStatus();
  delay(1000);
}

void loop(){
}

void BKStatus()
// check supply status after sending it a command
  {
  byte input[26];
  for(int i=0;i<26;i++){
    input[i]=Serial1.read();
  }
  if (input[3]==0x90){
    Serial.print("Checksum incorrect");
  }
  else if(input[3]==0xA0){
    Serial.print("Parameter incorrect");
  }
  else if(input[3]==0xB0){
    Serial.print("Unrecognized command");
  }
  else if(input[3]==0xC0){
    Serial.print("Invalid command");
  }
  else if(input[3]==0x80){
    Serial.print("Command was successful");
  }
  else{
    Serial.print("Status packet not recognized");
  }
  delay(50);
Serial.println("");
}

void BK1785write(String command, long input2)
// writes commands to BK power supply.  Note: voltages and currents are in mV and mA.
  {
  byte output[26];
  byte byteArray[4];

  // max voltage
  if (command.equals("SetV")) {
    if (input2>36000){
      input2=36000;
    }
  }

  // Convert input2 from a double to 4 bytes
  byteArray[3] = (byte)((input2 >> 24) & 0xFF) ;
  byteArray[2] = (byte)((input2 >> 16) & 0xFF) ;
  byteArray[1] = (byte)((input2 >> 8) & 0XFF);
  byteArray[0] = (byte)((input2 & 0XFF));

  // Construct output byte array
  // bytes 0 and 1
  output[0]=0xAA;
  output[1]=0x00;

  // bytes 2 to 7
  if (command.equals("RCon")){
    Serial.print("RC turned on");
    output[2]=0x20;
    output[3]=0x1;
    for (int i=4; i<7; i++){
      output[i]=0;
    }
  }
  else if (command.equals("RCoff")){
    Serial.print("RC turned off");
    output[2]=0x20;
    output[3]=0x0;
    for (int i=4; i<7; i++){
      output[i]=0;
    }
  }
  else if (command.equals("OutputOn")){
    Serial.print("Output turned on");
    output[2]=0x21;
    output[3]=0x1;
    for (int i=4; i<7; i++){
      output[i]=0;
    }
  }
  else if (command.equals("OutputOff")){
    Serial.print("Output turned off");
    output[2]=0x21;
    output[3]=0x0;
    for (int i=4; i<7; i++){
      output[i]=0;
    }
  }
  else if (command.equals("SetV")){
    Serial.print("Voltage set to: ");
    Serial.print(input2/1000.0);
    output[2]=0x23;
    for (int i=3; i<7; i++){
      //      Serial.println(output[i],HEX);
      output[i]=byteArray[i-3];
      //      Serial.print(output[i]);
      //      Serial.print(" ");
    }
  }
  else if (command.equals("SetI")){
    Serial.print("Current set to: ");
    Serial.print(input2/1000.0);
    output[2]=0x24;
    for (int i=3; i<7; i++){
      //      Serial.println(output[i],HEX);
      output[i]=byteArray[i-3];
    }
  }
  else{
    Serial.println("Invalid command");
  }

  // set bytes 8 - 26 to zero.
  for (int i=7; i<26; i++){
    output[i]=0;
  }

  // checksum.  byte 26
  for (int i=0; i<25; i++){
    output[25]=output[25]+output[i];
  }

  // write command to BK power supply (which is only Serial1)
  for (int i=0; i<26; i++){
    Serial1.write(output[i]);
  }
}


References

1 comment:

  1. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think. c627n

    ReplyDelete