Digistump Forums

The Oak by Digistump => Oak Support => Topic started by: jdeon on March 10, 2016, 05:29:43 pm

Title: Oak <-> Digispark via I2C?
Post by: jdeon on March 10, 2016, 05:29:43 pm
I'd like to add USB HID to my Oak, and the logical thing to do is to pair it with one of my Digisparks.  :)

I'm having a heck of a time, though.  Perhaps someone can tell me if this is actually possible, and if so, what I'm doing wrong.

Ideally the Digispark would be the I2C slave, since it's receiving data from the Oak.  However, I think the included TinyWireM library only supports being a master.  No big, I'll just request data as a master:

Code: [Select]
#include <TinyWireM.h>
#include "DigiKeyboard.h"

#define I2C_ADDRESS_SPARK 8
#define I2C_ADDRESS_OAK 9

void setup() {
  DigiKeyboard.update();
  TinyWireM.begin();
}

unsigned long lastToggleTime = 0;

void loop() {
  DigiKeyboard.update();
 
  unsigned long curTime = millis();
  if(curTime > lastToggleTime+2000) {
    lastToggleTime = curTime;
    DigiKeyboard.sendKeyStroke(57);
  }

  TinyWireM.requestFrom(I2C_ADDRESS_OAK, 1);

  int bytesRead = 0;
  while(TinyWireM.available() != 0) {
    byte keyStroke = TinyWireM.receive();
    DigiKeyboard.sendKeyStroke(KEY_1+(bytesRead%10));
    bytesRead++;
    delay(500);
  }
}

This is sort of a minimal example.  I'm not doing anything with the data I read, I'm just requesting it and counting the bytes returned.

On the Oak side, we've got the full Arduino Wire library:

Code: [Select]
#include <Wire.h>

#define I2C_ADDRESS_SPARK 8
#define I2C_ADDRESS_OAK 9

void requestEvent() {
  Serial.println("requestEvent!");
  Wire.write("a");
}

void setup() {
  Serial.begin(115200);
  delay(500);

  Serial.println("startup");

  Wire.begin(I2C_ADDRESS_OAK);
  Wire.onRequest(requestEvent);

}

void loop() {
}

I've got the two boards wired up so that P0 on the Digispark is wired to P0 on the Oak, so SDA -> SDA.  Likewise, P2 on the Digispark is wired to P9 on the Oak, so SCK->SCK.  Both pins have 4.7k pullups to the Oak's VCC pin.

The expected result is that I'd see the Oak's serial print ("requestEvent!") over and over again.  This doesn't happen, however.  I end up printing "1" on the Digispark side (via the keyboard) over and over again, but it doesn't seem like the data is ever really requested/read.

Any help is appreciated :)  I'm pretty good at the digital side of things, but I'm pretty new to this stuff and a total newb at analog at all.
Title: Re: Oak <-> Digispark via I2C?
Post by: djflix on March 13, 2016, 09:02:08 am
Not sure if this matters, but I believe that the Oak runs @ 3.3v and the Digispark @ 5v. You might have an issue with I/O voltage levels.
Title: Re: Oak <-> Digispark via I2C?
Post by: jdeon on March 15, 2016, 11:14:11 am
Not sure if this matters, but I believe that the Oak runs @ 3.3v and the Digispark @ 5v. You might have an issue with I/O voltage levels.

Yeah, supposedly it's possible to make this work, assuming the 3.3v "HIGH" registers as a "HIGH" on the 5v side.  I'll need to test it a bit to figure out if that's actually the case, but it seems like the answer may be "no."  The easy way to test would be to set a digital pin to HIGH on the Oak and connect it to a digital input on the 'spark - if the 'spark reads it as HIGH, that's one hurdle overcome.

In the meantime, I ordered a couple of logic level converters, we'll see if that helps any.