Friday, November 15, 2013

Controlling Your Test and Measurement Instruments Remotely with Arduino

In this post we will look at how to use the new Arduino Yun to connect to, control, and get data from test and measurement instruments remotely using WiFi. The Arduino Yún is a open source development board based on the ATmega32u4 microcontroller and the Atheros AR9331 processor. The Atheros processor is equipped with Linux distribution based on OpenWRT named Linino. The microcontroller side of the board delivers capabilities such as digital in / out, ADC, DAC, PWM, and more. The processor side of the board offers Ethernet, micro-SD card storage, and WiFi support. What is really cool about  the Yun is how the designers abstracted the communication between the two chips, which allows the microcontroller to easily communicate across WiFi with other devices, such as test and measurement instrumentation, by writing code only for the microcontroller! To learn more about the Arduino Yun click here.

A Yun can act like a WiFi access point which allows it to connect to a WiFi network created by an Ethernet based router (you probably have one of these at home). If an instrument has remote LAN capability, we can use a LAN cable to connect it to the same Ethernet network that the Yun connected to allowing a connection to be established between the two devices. With this connection the Yun can communicate and control the instrument to have it make measurements, output signal, etc. Why would this be useful? Well for instance you could use this to setup a low cost self contained data logging system. The Yun microcontroller could make basic measurements such as temperature and even offer basic digital control of the device you are testing. Then for more complex or high precision measurements you could bring in an instrument, such as function generator or spectrum analyzer and have the Arduino Yun control it. You can then store and retrieve the results from an SD card on the Yun, a Dropbox account that the Yun logs into, or the Yun could connect to a remote computer and send it the results.

Now let's look at a simple example of an Arduino Yun connecting to an instrument remotely making a measurement and then allowing a user to fetch the resulting measurement remotely from the Yun. For this example we will use an Agilent N6705B DC Power Analyzer as the instrument the Yun will communicate with. We will actually use the N6705B to power the Yun and then have the Yun connect to it and get a measurement of its own current draw. Below is the setup with channel one of the N6705B connected to the power input of the Yun.

The N6705B channel one is set for 5 V to power the Yun. The arduino sketch with the code written to implement this example can be seen below with comments to help explain each code line. Refer to the the "#include" libraries in the code, the first one is the Bridge library. This is the library that handles the communication between the microcontroller and the processor. This library allows you to control both the microcontroller and a lot of the processor functions by writing code only for the microcontroller, it takes a lot of complexity out of networking and storage functions in the Yun. The other two libraries, YunClient and YunServer, enable the network communication between the Yun, instrument, and the end user. Right below the libraries we define two network ports and an IP address. The IP address is the network address of the N6705B. It was obtained via the front panel of the N6705B. If you use this code be sure to change the address to the address of the instrument you want to control. The address is used to tell the network who the Yun is trying to communicate with. The port is like the channel that the communication will take place on, test and measurement instruments like the N6705B typically use port 5025. The second port "6666" is the port we will use to fetch the measurement data from the Yun remotely. The object "iClient" will be used to communicate with the N6705B and the object "server" we will use to communicate with a PC that the Yun will send the measurement data to. Notice that in the function setup() we start the Bridge which will establish communication between the microcontroller and the processor. This needs to be done for us to do network communication.

/*In this example Arduino Yun will remotely connect to the N6705B power supply (which is powering 
the YUN). The N6705B is on the same local network as the YUN. The YUN will then remotely via WiFi
connect to the N6705B and measure its current and store the value. A computer then connects to the YUN
over WiFi via terminal to retrieve the measurements */
#include Bridge.h>
#include YunClient.h>
#include YunServer.h>

#define IPORT 5025 //This is the port most if not all agilent instrument use to communicate over LAN
#define CPORT 6666 //This is the port we will connect with a PC to get measurement data
IPAddress ip(192,168,0,18); //create variable with IP address (you will want to plug your own IP address in here)
YunClient iClient; //create client object, the instrument acts as a server so the YUN is the client
YunServer server(CPORT); //create server object to connect to computer
int getData = 1; //bool to track when computer connects

void setup() {
  pinMode(13, OUTPUT); //set LED pin to output
  digitalWrite(13, LOW); //turn LED off, we will turn it on later after we get the measurement data
  Bridge.begin();//start bridge between ucontroller and processor. This can take 2 sec

void loop() {
  iClient.connect(ip,IPORT); //connect to the N6705B
  while(!iClient.connected()) { delay(100); } //loop until connected
  String question = "*IDN?\n"; //SCPI command to get instrument info
  iClient.write((uint8_t*)&question[0], question.length()); //send *IDN? command to N6705B
  String response; //create string to store response
  while(iClient.connected()) { //loop until get the entire response
    if(iClient.available()) {  //if available read response
      char cmd =; //read response from N6705B one character at a time
      if(cmd == '\n') { break; } //If at newline character got message so break out of loop
          else { response += String(cmd); } //add next character to response string
   response = "Connected to: " + response + "\n"; //add heading and endline to string
  question = "MEAS:CURR? (@1)\n"; //SCPI command for measure current at channel 1
  iClient.write((uint8_t*)&question[0], question.length()); //send measure command to N6705B
  String measurement; //create string to store the measurement
  while(iClient.connected()) { //loop until get the entire response
    if(iClient.available()) {  //if available read response
      char cmd =; //read response from N6705B one character at a time
      if(cmd == '\n') { break; } //If at newline character got message so break out of loop
          else { measurement += String(cmd); } //add next character to response string
   measurement = "The Yun current is: " + measurement +" A\n";
   iClient.stop(); //Disconnect from the N6705B
   digitalWrite(13, HIGH);//turn LED on to signal we connected to the N6705B and made a measurement

//************** Now we wait for computer to connect and retrieve data

   server.noListenOnLocalhost(); //Tell Yun to wait look for connection (not just local host connection)
   server.begin(); //Start server
   //Loop until computer connects. I used the terminal on my mac to connect using the "nc" command.
  //Just type "nc 6666" into the command line, of course substitute your YUN's IP address in
   while(getData) { //Loop until computer connects
   YunClient client = server.accept(); //accept a connection and create a client object for it

  if(client.connected()){ //if we are connected to a computer send N6705B and measurement info
    client.write((uint8_t*)&response[0], response.length()); //send instrument information
    client.write((uint8_t*)&measurement[0], measurement.length()); //send current measurement
    client.stop(); //disconnect from computer
    getData = 0; //we are done so set variable to break out of loop

 while(1) { //never ending loop since we are done
  delay(1000); }

In the top section of the main loop we start a connection with the N6705B and send it two commands. In network jargon the N6705B is the server and the Yun is the client. The first command "*IDN?" gets basic information on the instrument we are connected to such as model and serial number. The second command "MEAS:CURR? (@1)" tells the instrument to measure the current on channel one, this is the channel that is powering the Yun. These commands are known as SCPI commands and can be obtained from an instrument's manual. Notice that after each command is sent the Yun reads the response and stores it in a string. Once the exchange is complete the Yun ends the connection with the N6705B and turns on the LED on the board connected to pin 13. This tells the user that the measurement is complete.

Now we need to get the data from the Yun by remotely connecting to it with a PC, in this case the Yun now acts as a server and the PC as a client. This interaction takes place in the bottom section of the code in the main loop. In this section a while loop is used to continuously check if a remote PC is trying to connect. Once a connection is made the Yun sends the data it obtained from the instrument and closes the connection. From there it enters an infinite loop and preforms no other actions.

There are multiple ways for us to collect the data from the Yun at this point using a PC. One way would be to write a simple program in MATLAB or LabView and use their networking libraries to connect to the Yun and retrieve the data. For this example we will use the Terminal on a Mac laptop. Terminal's are found on Linux and Mac based operating systems. For this example we will use the "netcat" or "nc" command which allows us to create a network connection with another network device and read from or write data to that device. To connect with the Yun to get the N6705B data we need its IP address, which is for the Yun used in this example, and the port number, which is 6666 as mentioned earlier. The full command for the Terminal is "nc 6666." Below is a screen shot from a terminal that connected to the Yun and retrieved the N6705B data, which includes the basic N6705B information and the measurement of the Yun's current consumption.

We can see from the fetched data that the Yun's current was ~ 266 mA.

In this blog we looked at how the Arduino Yun can be used to connect to, control, and fetch measurement data from test and measurement instrumentation with remote LAN capability. We stepped through an example using a Yun, an N6705B DC Power Analyzer, and a Mac computer. If you have any comments related to this post use the comments section below and if you have any questions feel free to email me.

Click here for more information on Arduino

Click here more information on the N6705B DC Power Analyzer


  1. This comment has been removed by the author.

  2. I have never seen this type of thing before? Can it be used to do home wiring?
    Harlin |