I2C Communication

ddtav478

New member
Hello,

I have a joystick and gimbal combination where a teensy 4.0 board is attached to the joystick for button inputs while the gimbal has a arduino nano (similar) board attached to the gimbal. The teensy has 23 buttons attached to the joystick and I'm trying to figure out how I can get the teensy to talk to the arduino over I2C to take button inputs as well as gimbal inputs from a magnet based x and y sensor. Here is the code for the teensy

Code:
#include <i2c_device.h>
#include <i2c_driver.h>
#include <i2c_driver_wire.h>
#include "i2c_register_slave.h"

int pins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 21, 22, 23, 24, 26}; 


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 for (int i = 0; i < 23; i++){
 pinMode(pins[i], INPUT_PULLUP);
 Wire.begin(1);
}
}

void loop() {
  // put your main code here, to run repeatedly:

  for (int i = 0; i < 23; i++){
    Serial.print(digitalRead(pins[i]));
    Serial.print("\t");
    
}

Serial.println();
delay(5);


}

I'm trying to get the teensy (slave) to talk to the arduino (master) to hopefully take those inputs and read as a joystick. I haven't really figured out the master code for the arduino as I have been using examples to get going and haven't really gotten far. The code for the teensy basically takes all the inputs from the buttons and prints them as serial data but I haven't been able to get it across to the arduino over i2C.

I posted this question in the aruduino forums and they were pretty much no help.
 
Code:
#include <i2c_device.h>
#include "i2c_driver.h"
#include <i2c_driver_wire.h>
#include <i2c_register_slave.h>

int pins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 20, 21, 22, 23, 24, 26}; 


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
 for (int i = 0; i < 23; i++){
 pinMode(pins[i], INPUT_PULLUP);
 Wire.begin();
}
}

void loop() {
  // put your main code here, to run repeatedly:

  for (int i = 0; i < 23; i++){
    Serial.print(digitalRead(pins[i]));
    Serial.print("\t");
    Wire.beginTransmission(1);
    Wire.write(digitalRead(pins[i]));
    Wire.endTransmission(false);
   
    
}

Serial.println();
delay(5);


}

So I fixed the code a little. Had to add a wire.endtranmission in there.
This is the slave device, it is very generic but I got it to communicate.
Code:
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(1);                // join I2C bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
  delay(5);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print("\t");
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
   Serial.print("\t");
}

Now I can see the serial printout. I'm trying to get it in the same format as the master device.
 
Back
Top