본문 바로가기

ESP32 Dev. Board (English)

I2C Test with my ESP32 Do-IT DevKit using Arduino IDE

I2C Test with my ESP32 Do-IT DevKit using Arduino IDE

(Testing with MPU-6050  3-axis Gyro, Accels, Temperature.

I have used GY-88, but you can not do this with separate MPU-6050 or GY-521.

Original MPU-6050 or GY-521 has INT pin that needs to be connected also.)


(*) It took me weeks to succeed in programming I2C for ESP32.   Just until I succeed, I thought ESP32 is piece of junk made only for wifi since it wasn't any useful at all. Now I can do some serious work with this module.  




I have used GY-88 (MPU6050, HMC5338L, BMP085) module in this experiment.


* I can't find my ESP32 Do-it DevKit and GY-88 Fritzing module, so I have used similar ones to draw.

Here, I use only VCC, GND, SCL, SDA on GY-88, and VIN, GND, 21(SDA) 22(SCL) from ESP32. 



(*) The most difficult issue in programming I2C using ESP32 is probably the POWER issue.   I2C programming in Arduino Uno or Nano is simple, easy and straight forward.   However, it is not the same case for ESP32.   I tried it for many WEEKS in my spare time, and, I have finally succeeded.   

It was mostly the power problem due to the the fact that ESP32 module takes a whole lot more power than Arduino counterparts.  


POWER PROBLEM :

   I learned that the power seems to be the most important issue in programming ESP32 by long suffering.    Unlike connecting to the Arduino Nano or NodeMCU type, many of the USB cables that connects from PC to ESP32 board did not work, did not even detect the boards.   I have only one cable that easily detects ESP32 board even when ESP32's wifi function is enabled.    I have one other cable that detects it, but lost connection when Wifi function is enabled.   Wifi function in ESP32 seems to be sucking up power, and many USB cables are unable to provide such a power.


Eliminate all other possible problems :

   I have copied my Arduino Folder to my safe backup location, and deleted all files from Arduino Folder before I install Arduino IDE for the latest one, version 1.8.5.   

  I have downloaded Arduino IDE 1.8.5, the latest one, because my previous one 1.6 version was not successful programming I2C for ESP32 no matter what I did. 

  Also, I updated ESP32 library to the latest one "https://github.com/espressif/arduino-esp32"

  I used no other library except wire.h since the other library may be made for Arduino, not ESP32.

  I have failed so many times before change to Arduino IDE Version 1.8.5.   


Scan First to see if it works 

(You might have press reset button since it comes out very fast, and only once)


Scan Code : (this code is not mine.  I got it from web.)

// I2C Scanner

// Written by Nick Gammon

// Date: 20th April 2011


#include <Wire.h>


void setup() {

  Serial.begin (115200);


  // Leonardo: wait for serial port to connect

  while (!Serial) 

    {

    }


  Serial.println ();

  Serial.println ("I2C scanner. Scanning ...");

  byte count = 0;

  

  Wire.begin(21, 22);

  for (byte i = 8; i < 120; i++)

  {

    Wire.beginTransmission (i);

    if (Wire.endTransmission () == 0)

      {

      Serial.print ("Found address: ");

      Serial.print (i, DEC);

      Serial.print (" (0x");

      Serial.print (i, HEX);

      Serial.println (")");

      count++;

      delay (1);  // 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() {}



If scanning works, now try for MPU-6050 3-Axis accels, gyro and temperature.



MPU-6050 Data Reading Result (Output) :



MPU-6050 Codes : (this code is not mine.  I got it from web.)

// MPU-6050 Short Example Sketch

// By Arduino User JohnChi

// August 17, 2014

// Public Domain


#include<Wire.h>


const int MPU_addr=0x68; // I2C address of the MPU-6050

int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;


void setup(){

  Wire.begin();

  Wire.beginTransmission(MPU_addr);

  Wire.write(0x6B); // PWR_MGMT_1 register

  Wire.write(0); // set to zero (wakes up the MPU-6050)

  Wire.endTransmission(true);

  Serial.begin(115200);

}


void loop(){

  Wire.beginTransmission(MPU_addr);

  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)

  Wire.endTransmission(false);

  Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers

  

  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)

  

  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); //equation for temperature in degrees C from datasheet

  Serial.print(" | GyX = "); Serial.print(GyX);

  Serial.print(" | GyY = "); Serial.print(GyY);

  Serial.print(" | GyZ = "); Serial.println(GyZ);

  delay(333);

}



I wish you get the same result.   Have fun programming.  

'ESP32 Dev. Board (English)' 카테고리의 다른 글

More I2C on ESP32 DO-IT DevKit1. 2X16 LCD Control  (0) 2017.12.04