Trouble Connecting Roborio to Arduino via I2C

Hi, I’m trying to connect a sparkfun redboard to roboRio via I2C and it doesn’t work. Everything seems to be wired correctly.`The roboRio is the Master and the arduino is the slave.

Java code:

public class LED implements Subsystem {
      I2C i2c;
      byte[] toSend = new byte[] {1};
public void init()

   {
      resetState();
      i2c = new I2C(I2C.Port.kOnboard, 168);
      m_ledOutput = (WsI2COutput) Core.getOutputManager().getOutput(TridentOutputs.LED.getName());
   }

public void update()

   {
      m_ledOutput.setValue(toSend);
}

Arduino Code:

#include <Wire.h>

int bytes[4];

void setup()
{
  Wire.begin(0x10);                
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  Serial.print("Serial works");
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while (0 < Wire.available()) // loop through all but the last
  {
    Serial.print("first byte is ");
    bytes[0] = Wire.read(); // receive byte as a character
    Serial.println(bytes[0]);         // print the character
    Serial.print("second byte is ");
    bytes[1] = Wire.read(); // receive byte as a character
    Serial.println(bytes[1]);         // print the character
    Serial.print("first byte is ");
    bytes[2] = Wire.read(); // receive byte as a character
    Serial.println(bytes[2]);         // print the character
    Serial.print("first byte is ");
    bytes[3] = Wire.read(); // receive byte as a character
    Serial.println(bytes[3]);         // print the character
  }
}

The Arduino slave code is designed to receive 4 bytes, but the java code is only programmed to send one byte. This will be changed later, but I don’t think it’s a problem right now.

1 Like

Is the Java code supposed to be writing to the I2C object? It doesn’t seem to be, and the write call seems like it’s happening to another object.

On a slightly different note, it looks like you want to control WS2812 LEDs with that class. For 2020, the roboRIO can natively control WS2812 LEDs, even while disabled, bypassing the need for an Arduino. If you want to try it, check out the AddressableLED class in wpilib

We created our own LED class, it’s not meant to program the LEDs directly. I’m planning on sending a byte to the redboard via I2C and having the program read the byte and choose a pattern to play. I’m having trouble communicating to the I2C. We’re using Adafruit Dotstar LEDs. I’m not sure what the I2C object is. Sorry, I’m new to Java.

@Thad_House Does the AddressableLED use the FPGA to offload writing to LED strip, or is it all on the RIO CPU?

The Java code is above is not complete - it is writing to an output that wraps the I2C object that is configured elsewhere, and that has worked in the past.

Side question (and it’s probably in the docs somewhere): What is the clock speed of I2C on the RIO? I remember the cRIO was fixed at 38400.

It’s entirely in the FPGA, with a 5460 LED buffer.

I2C runs at 400KHz according to the spec docs.

Thanks for the quick reply. i’ll work with him tomorrow to convert to AddressableLED.

Cool. Note it hooks up to one of the PWM Header pins, not one of the DIO pins. It is documented, but its easy to skip over, so figured its best to just mention it.

Thanks. I probably would have run fast enough to trip over that

I tried using the AddressableLED method to control the LEDs, but it isn’t working and we don’t know why. Here’s my code:

public void init()
   {
      ledStrip = new AddressableLED(4);

      buffer = new AddressableLEDBuffer(4);
      ledStrip.setLength(buffer.getLength());
      ledStrip.setData(buffer);
      ledStrip.start();

      resetState();
}

public void update()
   {
      for (var i = 0; i < buffer.getLength(); i++) {
         buffer.setRGB(i, 255, 0, 0);
      }
      ledStrip.setData(buffer);
}

We’re using PWM port 4 with 4 NeoPixel LEDs. Here’s a link to our reference code:
https://docs.wpilib.org/en/latest/docs/software/actuators/addressable-leds.html#