Sending data from the cRIO to an Arduino

Sometimes it is useful to use a coprocessor to handle operations on some sensors, lights, etc. A popular processor is the Arduino. This article shows sample code to send some data between the cRIO and an Arduino. Although it only sends data in one direction (from the cRIO to the Arduino), it serves as an example of how to do it.

This program sends one of two values (either 72 or 76) from the cRIO to either turn the LED (pin 13 on the Arduino) either on or off. The value is arbitrary and was just part of a larger sample program.

The cRIO program

The cRIO program

The I2C protocol has a master and slave processors or devices. The master controls the bus and either sends data to a slave or requests data from a slave processor. Slaves cannot initiate transactions on their own. Each slave processor (or device) has a unique address that the master processor uses to select it. In this example, the Arduino slave processor recognizes address 84. The steps are:

  1. Initialize the I2C connection on address 84. Because of differences between the implementation of the library for the cRIO and Arduino (the lower bit of the address selects either read or write) the cRIO uses address 168. 168 is the address 84 shifted by 1 bit (the read/write bit).
  2. Use a byte array to fill with the data to send. In this case it's a single byte, either 76 or 72 that sets the light on or off on the Arduino.
  3. Send the data to the Arduino without receiving any data. The parameters "toSend" and "1" specify a single byte from the "toSend" array. The second set of parameters (null and 0) would be a byte array and length if the master was expecting the slave to respond with some data.

The Arduino program

The Arduino program

The Arduino program:

  1. Sets up an interrupt handler to receive inbound requests as address 84
  2. Reads the data turning the light on or off depending on the value.

The results

The results

A few observations:

  1. The timing using Timer.delay() on the cRIO wasn't very precise - likely due to randomness in the Java scheduler and getting the thread restarted. You can see that between transactions.
  2. The program didn't work reliably with an Arduino Uno and seems to work correctly with a Mega. You can see the output of each transaction with the trace from the logic analyzer.