SAMD21 I2C Not Working - Arduino Zero based Board

Hey guys,

I'm currently working on project with SAMD21 MCU Weemos Board:

I've trying to get data from a MPU 6050, using the I2C protocol, but for some reason i get nothing in the serial monitor.
I'm using the pins :

PA22-SDA
PA23-SCL

So i tested the same code with an Arduino UNO and it worked perfectly.

I'm using 4k7 pull-up resistors in the SCL and SCA lines.

There you have my code:

// Codigo adaptado de: Usuário do Arduino JohnChi

#include<Wire.h>//Biblioteca para comunicação I2C

const int MPU_addr=0x68; //Endereço do sensor

int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ; //Variaveis para pegar os valores medidos

void setup(){
  Wire.begin(); //Inicia a comunicação I2C
  Wire.beginTransmission(MPU_addr); //Começa a transmissao de dados para o sensor
  Wire.write(0x6B); // registrador PWR_MGMT_1
  Wire.write(0); // Manda 0 e "acorda" o MPU 6050
  Wire.endTransmission(true);

  Serial.begin(9600); //Inicia a comunicaçao serial (para exibir os valores lidos)
}
void loop(){
  Wire.beginTransmission(MPU_addr); //Começa a transmissao de dados para o sensor
  Wire.write(0x3B); // registrador dos dados medidos (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true); // faz um "pedido" para ler 14 registradores, que serão os registrados com os dados medidos
  AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

  //Agora escreve os valores no monitor serial
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //Equação da temperatura em Cº de acordo com o datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  delay(333);
}

I hope someone can help figure out the problem with my SAMD21 board.

Best Regards

SM

does anything appear on the serial monitor?
have you tried using any of the libraries for the MPU 6050?
does the I2C scanner show anything?

1 Like

I've tried a bunch of MPU6050 libraries, but it didn't work.

I2C Scanner was also tested, but incredibly I got nothing in the serial monitor, not even the start of the scanner.

it should display something - did you have the correct baudrate
if I run

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

void setup() {
  // activate internal pullups for twi.
  digitalWrite(SDA, 1);
  digitalWrite(SCL, 1);
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 2; i < 120; i++)
  {
    Wire.beginTransmission (i);
      Serial.print (" 0x");
      Serial.print (i, HEX);

    if (Wire.endTransmission () == 0)
      {
      Serial.print ("\nFound address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (100);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

when run on an MKRFOX which also uses a SAMD21 processor I get

I2C scanner. Scanning ...
 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C
Found address: 28 (0x1C)
 0x1D 0x1E 0x1F 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2A 0x2B 0x2C 0x2D 0x2E 0x2F 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3A 0x3B 0x3C 0x3D 0x3E 0x3F 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A 0x5B 0x5C 0x5D 0x5E 0x5F 0x60 0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68 0x69 0x6A
Found address: 106 (0x6A)
 0x6B 0x6C 0x6D 0x6E 0x6F 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77Done.
Found 2 device(s).

the attached I2C device is a LSM9DS1 9-axis iNEMO Inertial Module

I did get a response in the serial monitor with the I2C Scanner.

There you have it:

But it's pretty strange, because it tells me that i have 118 devices in the I2C line, when i was supposed to see just one in the 0x68 address.

Futhermore, the I2C Scanner just worked when i pointed the board as a Arduino MKR FOX 1200.

When run my MPU 6050 code, adding the internal pull-up resistors. I get this in the serial monitor:

did the scanner show the address 0x68?

connected a LSM9DS1 9-axis iNEMO Inertial Module to MKRFOX and it displays

0xdb 0xff 0xc9 0xfe 0xb4 0x3f 0xf3 0x06 0x6b 0xfe 0xde 0xfd 0xe4 0xff 0x9e 0x00 0x13 0xff 
Accel 0xffdb 0xfec9 0x3fb4  Mag   0x06f3 0xfe6b 0xfdde  Gyro  0xffe4 0x009e 0xff13
Acceleration X: -0.02m/s^2 Y: -0.19m/s^2 Z: 9.76m/s^2

0x6b 0x38 0xfe 0x0f 0xcb 0x16 0xf3 0x06 0x6b 0xfe 0xde 0xfd 0x21 0xfd 0x7c 0x05 0xad 0x08 
Accel 0x386b 0x0ffe 0x16cb  Mag   0x06f3 0xfe6b 0xfdde  Gyro  0xfd21 0x057c 0x08ad
Acceleration X: 8.64m/s^2 Y: 2.45m/s^2 Z: 3.49m/s^2

0xc4 0x02 0x02 0x44 0x3d 0xf4 0xf3 0x06 0x6b 0xfe 0xde 0xfd 0xba 0x04 0xec 0x0b 0xcc 0x03 
Accel 0x02c4 0x4402 0xf43d  Mag   0x06f3 0xfe6b 0xfdde  Gyro  0x04ba 0x0bec 0x03cc
Acceleration X: 0.42m/s^2 Y: 10.41m/s^2 Z: -1.80m/s^2

0x93 0xe8 0x5b 0x30 0x8a 0xdf 0xf3 0x06 0x6b 0xfe 0xde 0xfd 0xfe 0x04 0x21 0xee 0xad 0x01 
Accel 0xe893 0x305b 0xdf8a  Mag   0x06f3 0xfe6b 0xfdde  Gyro  0x04fe 0xee21 0x01ad
Acceleration X: -3.59m/s^2 Y: 7.41m/s^2 Z: -4.97m/s^2

0x57 0xf6 0x94 0xdf 0x5d 0x36 0xf3 0x06 0x6b 0xfe 0xde 0xfd 0xbc 0x10 0xe7 0xf0 0xb4 0xf4 
Accel 0xf657 0xdf94 0x365d  Mag   0x06f3 0xfe6b 0xfdde  Gyro  0x10bc 0xf0e7 0xf4b4
Acceleration X: -1.48m/s^2 Y: -4.97m/s^2 Z: 8.33m/s^2

0xd8 0x3a 0x37 0x06 0xed 0x1f 0xf3 0x06 0x6b 0xfe 0xde 0xfd 0xfb 0x02 0x00 0xf7 0x77 0xf5 
Accel 0x3ad8 0x0637 0x1fed  Mag   0x06f3 0xfe6b 0xfdde  Gyro  0x02fb 0xf700 0xf577
Acceleration X: 9.01m/s^2 Y: 0.95m/s^2 Z: 4.89m/s^2

0x23 0xd4 0x5d 0xfa 0x36 0x1a 0xf3 0x06 0x6b 0xfe 0xde 0xfd 0x7d 0x00 0x59 0x40 0x62 0xfd 
Accel 0xd423 0xfa5d 0x1a36  Mag   0x06f3 0xfe6b 0xfdde  Gyro  0x007d 0x4059 0xfd62
Acceleration X: -6.72m/s^2 Y: -0.86m/s^2 Z: 4.01m/s^2

[/quote]

As i said it shows all the addresses from 1 to 118. Then, when i run the MPU 6050 code acceleration in the axis get stucked in -1, as well as the temperature.

certainly something is wrong
what particular MPU 6050 board do you have
how is it wired? powered? pull ups? etc

I'm currently using a GY-521 MPU6050 Board.

image

I'm using internal pull-ups and the wiring below:

I've also tried to use 4k7 resistors as external pull-ups, but the result is the same in the serial monitor.

try powering it from 3.3V pin

I already powered the MPU 6050 in 3.3V and I got the same result.

I've also tried to use the external pull-ups in the 3v3 and 5v potencial, but the values are the same in the serial monitor.

have you another arduino board you could test the MPY6050 on?

I've tested the MPU with another Samd21 board, but I got the same results.

When I test the code with and Arduino Uno, I'm getting the correct results.

are you using the same code on the UNO (which works) and the SAMD21 (which fails)?

What do you have the "board" set to?

Exactly. The code works perfectly with the Arduino UNO, it gets all the data from the MPU.

The SAMD21 board is set to be a MKR Fox 1200, just like @horace. I initially set it as a Arduino Zero board using the native port, but my serial monitor was freezed in that setup. When I changed to a MKR Fox I started to see life in the serial.Eventhough it's not useable data .

It looks to me like the MKR FOX1200 has SCL/SDA on PA8/PA9