Copyright ©

Mongoose OS Forum


New to i2c, looking for some very basic help reading value from gy-521 sensor

shoelessoneshoelessone Kashihara, Japan

Hello! Thanks in advance for the help.

Describe your goal

My basic goal is to learn how to use i2c with Mongoose OS + ESP32, because i'm going to need to use it to communicate with a sensor I'm using for my current project. Just a note, I'm using gy-521 / MPU6050 here because I happen to have one available to me, the important thing is to learn to use i2c, not so much the actual sensor itself or it's functionality. More specifically, I want to get a basic sketch setup that will allow me to read the temperature from the gy-521.

I guess a bonus question is what is mgos_i2c_read vs mgos_i2c_read_reg_b... my understanding is that mgos_i2c_read_reg_b reads a specific byte from a specific register, but what does mgos_i2c_read do then? Please keep in mind this is not something I have a good "feel" for. Currently I'm looking at "registers" as buckets lined up. read_b grabs a byte from a specific bucket by address. but not sure what the other command does. Where does it start reading from?

I started by reading this blog post, which is based on Arduino, but goes over the basics of using i2c with this sensor and and the ESP8266. - note in this example the way that data is read seems different... it reads 8 bits at a time I think? but there is this concept of "end transmission" and such, I'm not sure if I need to worry about that with the Mongoose OS implementation?

FYI, this is the register map for this thing, and here is the datasheet.

Show what you do to achieve the goal - the code, commands, etc

Here is what I've written so far. Keep in mind this is just a PoC, just tring to get the sensor readings working!


author: mongoose-os
description: A Mongoose OS app skeleton
version: 1.0

libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

  - c

  - src

  - fs

  - ["i2c.enable", true]
  - ["i2c.sda_gpio", 21]
  - ["i2c.scl_gpio", 22]

  - origin:
  - origin:
  - origin:
  - origin:
  - origin:
  - origin:

# Used by the mos tool to catch mos binaries incompatible with this file format
manifest_version: 2017-09-29

and here is my code

#include "mgos.h" #include "mgos_i2c.h" const int i_mpu_addr=0x68; static void expansion_io_setup() { struct mgos_i2c *i2c = mgos_i2c_get_global(); if(i2c == NULL) { LOG(LL_INFO, ("I2C is not enabled!")); } else { // wake up MPU-6050 w/ 0x6B power management register writing 0 - this is based on the Arduino example mgos_i2c_write_reg_b(i2c, i_mpu_addr, 0x6B, 0x00); } } static void timer_cb(void *user_data) { LOG(LL_INFO, ("reading..")); //I don't know if I need this delay, but I saw it in another library so included it just to test mgos_usleep(15000); struct mgos_i2c *i2c = mgos_i2c_get_global(); //According to the datasheet this should be the address for part of the temperature data... "H" int temp = mgos_i2c_read_reg_b(i2c, i_mpu_addr, 0x41); LOG(LL_INFO, ("TEMP_OUT_H: %d", temp)); //and another part of the temperature data "L" temp = mgos_i2c_read_reg_b(i2c, i_mpu_addr, 0x42); LOG(LL_INFO, ("TEMP_OUT_L: %d", temp)); (void) user_data; } enum mgos_app_init_result mgos_app_init(void) { LOG(LL_INFO, ("Hi there")); expansion_io_setup(); LOG(LL_INFO, ("Expansion setup.")); mgos_set_timer(5000, true, timer_cb, NULL); return MGOS_APP_INIT_SUCCESS; }

Show the result you see - logs, output, observed behavior

[Feb 20 22:23:52.434] mgos_app_init        Expansion setup.
[Feb 20 22:23:52.442] mgos_init            Init done, RAM: 289988 total, 225760 free, 225620 min free
[Feb 20 22:23:57.438] timer_cb             reading..
[Feb 20 22:23:57.459] timer_cb             TEMP_OUT_H: -1
[Feb 20 22:23:57.465] timer_cb             TEMP_OUT_L: -1
[Feb 20 22:24:02.438] timer_cb             reading..
[Feb 20 22:24:02.459] timer_cb             TEMP_OUT_H: -1
[Feb 20 22:24:02.465] timer_cb             TEMP_OUT_L: -1

I also tried, just for fun, to switch the GPIO 21 and GPIO22 just because I wasn't totally sure I had them connected correctly, and now I see

[Feb 20 22:25:47.438] timer_cb             reading..
[Feb 20 22:25:47.458] timer_cb             TEMP_OUT_H: 0
[Feb 20 22:25:47.462] timer_cb             TEMP_OUT_L: 0
[Feb 20 22:25:52.439] timer_cb             reading..
[Feb 20 22:25:52.458] timer_cb             TEMP_OUT_H: 0
[Feb 20 22:25:52.462] timer_cb             TEMP_OUT_L: 0

Describe the result you expect

I hope to get ANY values other than 1 or 0 in the console... ideally some integer that I can take and actually massage into a temperature reading of some sort!

THANK YOU, I'll take any help I can get!


Sign In or Register to comment.