Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: I2C Examples not working on Teensy 3.6

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Toronto Canada
    Posts
    24

    I2C Examples not working on Teensy 3.6

    I am at a loss as to why I can't get any communication between 2 teensy 3.6's. I am using a pull-up resistor (tried 4.7k and 2.2k). I've seen a variety of posts with similar issues. I had it working with an Uno to 3.2.

    Trying a "Scanner" example didn't show any broadcaster/master, yet the master seems to be running properly as it is very basic code and pretty sure wired properly. Here's an image of the basic wiring ... both are wired the same.

    Ultimately I need to send values of a couple variables to teensy's that control stepper motors (via TeensyStep). I have posted my previous code at the bottom which I need to expand so that I can retrieve several integer variables (motorSpeed, acceleration, enable, etc). Really hoping someone can help resolve my issues. Thanks!

    MASTER EXAMPLE:
    Code:
    // Wire Master Writer
    #include <Wire.h>
    
    int led = LED_BUILTIN;
    
    void setup()
    {
      pinMode(led, OUTPUT);
      Wire.begin(); // join i2c bus (address optional for master)
    }
    
    byte x = 0;
    
    void loop()
    {
      digitalWrite(led, HIGH);    // briefly flash the LED
      Wire.beginTransmission(9);  // transmit to device #9
      Wire.write("x is ");        // sends five bytes
      Wire.write(x);              // sends one byte
      Wire.endTransmission();     // stop transmitting
      digitalWrite(led, LOW);
    
      x++;
      delay(500);
    }
    SLAVE EXAMPLE:
    Code:
    // Wire Slave Receiver
    #include <Wire.h>
    
    int led = LED_BUILTIN;
    
    void setup()
    {
      pinMode(led, OUTPUT);
      Wire.begin(9);                // join i2c bus with address #9
      Wire.onReceive(receiveEvent); // register event
      Serial.begin(9600);           // start serial for output
    }
    
    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)
    {
      digitalWrite(led, HIGH);       // briefly flash the LED
      while(Wire.available() > 1) {  // loop through all but the last
        char c = Wire.read();        // receive byte as a character
        Serial.print(c);             // print the character
      }
      int x = Wire.read();           // receive byte as an integer
      Serial.println(x);             // print the integer
      digitalWrite(led, LOW);
    }

    PREVIOUS WORKING CODE:
    Code:
    #include <Wire.h>
    #include <StepControl.h>
    
    unsigned long targetInterval = 5000; // read speed from master
    unsigned long pulsePerSec = 1000000/targetInterval;
    
    //Step, Dir
    Stepper M1(23, 22), M2(21, 20), M3(17, 16), M4(15, 14), M5(12, 11), M6(10, 9), M7(8, 7), M8(6, 5), M9(4, 3);
    StepControl<> controller;   // Use default settings 
    
    constexpr int spr = 16*200; // 3200 steps per revolution
    int acceleration = 1500;
    int pullInSpeed = 100;
    const byte numMotors = 9;
    int microSteps = 4; 
    int PPR = 5000; //pulses per rotation wo microstepping
    
    int targetDist[numMotors] = {100*spr}; //50x decent
    //int S1MaxSpeed[numMotors] = {50000};
    //int S1MaxSpeed[numMotors] = {1*PPR*microSteps};
    int S1MaxSpeed[numMotors] = {8000};
    
    int scene = 0;
    
    // SCL/SDA Stuff
    byte b[3];  //byte array
    bool flag1 = LOW;
    
    void setup() {
      delay(2000);
      Wire.begin(8);                // join i2c bus with address #8
      Wire.onReceive(receiveEvent); // register event
      Serial.begin(9600);           // start serial for output
      delay(4000);
    
      Stepper* motorSet_A[] = {&M1, &M2, &M3, &M4, &M5, &M6, &M7, &M8, &M9};
      for (int i = 0; i < numMotors; i++) {
        motorSet_A[i]->setPullInSpeed(pullInSpeed);
        motorSet_A[i]->setAcceleration(acceleration);
      }
      
    }
    
    void loop() {
      Stepper* motorSet_A[] = {&M1, &M2, &M3, &M4, &M5, &M6, &M7, &M8, &M9};
      
      scene = 0;
    //  if (scene != lastScene){  // only go through if scene changes
        switch (scene){
          case 0:                 
            M1.setMaxSpeed(S1MaxSpeed[0]);
            M2.setMaxSpeed(S1MaxSpeed[0]);
            M3.setMaxSpeed(S1MaxSpeed[0]);
            M4.setMaxSpeed(S1MaxSpeed[0]);
            M5.setMaxSpeed(S1MaxSpeed[0]);
            M6.setMaxSpeed(S1MaxSpeed[0]);
            M7.setMaxSpeed(S1MaxSpeed[0]);
            M8.setMaxSpeed(S1MaxSpeed[0]);
            M9.setMaxSpeed(S1MaxSpeed[0]);
            M1.setTargetRel(targetDist[0]);
            M2.setTargetRel(targetDist[0]);
            M3.setTargetRel(targetDist[0]);
            M4.setTargetRel(targetDist[0]);
            M5.setTargetRel(targetDist[0]);
            M6.setTargetRel(targetDist[0]);
            M7.setTargetRel(targetDist[0]);
            M8.setTargetRel(targetDist[0]);
            M9.setTargetRel(targetDist[0]);
            
            Stepper* motorSet_A[] = {&M1, &M2, &M3, &M4, &M5, &M6, &M7, &M8, &M9};
    
    //        for (int i = 0; i < numMotors; i++) {                    // loop through all motors in motorSet_A...
    //          motorSet_A[i]->setTargetRel(targetDist[i]);                // ... set targets to 0...
    //          motorSet_A[i]->setMaxSpeed(S1MaxSpeed[i]);
    //          Serial.print(i); Serial.print(": MaxSpeed "); Serial.println(S1MaxSpeed[i]);
    //          Serial.print(i); Serial.print(": targetDist "); Serial.println(targetDist[i]);
    //        }
            Serial.print("M1: MaxSpeed "); Serial.println(S1MaxSpeed[0]);
            Serial.print("M1: TargetDist "); Serial.println(targetDist[0]);
    
            break;
    //      default:
    //        Serial.println("error");
    //        break;
        }
    //  lastScene = scene; 
    //  }
      
        if (flag1 == HIGH){
          if (b[0] == '*')
          {
            targetInterval = (b[1] << 8) | b[2]; 
            pulsePerSec = 1000000/targetInterval; //OLD SPEED STUFF
    //        Serial.print("Slave Speed: ");
    //        Serial.println(targetInterval, DEC); //target speed
            flag1 = LOW;
          }
        }
    
      controller.move(motorSet_A);
      delay(5000);
    }
    
    void receiveEvent(int howMany){
      for (int i = 0; i < howMany; i++){
        b[i] = Wire.read();
      }
      flag1 = HIGH;
    }

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    3,154
    Quote Originally Posted by jingleby View Post
    I am at a loss as to why I can't get any communication between 2 teensy 3.6's. I am using a pull-up resistor (tried 4.7k and 2.2k). I've seen a variety of posts with similar issues. I had it working with an Uno to 3.2.
    as teensy is very fast, put a delay(100) after wire.begin to let I2C settle before reading/writing. maybe this helps.

  3. #3
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,623
    Your example master and slave work for me. I have 2.2K pullups to 3v3 and a common ground. your photo looked a little ambiguous ?, I presume both edges of the T3.6 were not pushed into same conductive plane of black breadboard.
    I did add a delay(3000) after Wire.begin() in master, just to give slave a chance to fire up

  4. #4
    Senior Member
    Join Date
    Nov 2015
    Location
    Cold hollow VT
    Posts
    188
    Quote Originally Posted by manitou View Post
    Your example master and slave work for me. I have 2.2K pullups to 3v3 and a common ground. your photo looked a little ambiguous ?, I presume both edges of the T3.6 were not pushed into same conductive plane of black breadboard.
    I did add a delay(3000) after Wire.begin() in master, just to give slave a chance to fire up
    Absolutely, there must be a common ground connection between the two Teensys.

  5. #5
    Member
    Join Date
    Dec 2018
    Location
    Toronto Canada
    Posts
    24
    Thanks for testing this out. I did forget about the common ground and will give that a try.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •