element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Embedded Forum I2C Communications
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 14 replies
  • Answers 7 answers
  • Subscribers 474 subscribers
  • Views 1799 views
  • Users 0 members are here
  • embedded c
  • arduino programing
  • stm-32
  • ewarm
  • how to i2c
  • 3-axis magnetometer
  • iar
Related

I2C Communications

rbevy101
rbevy101 over 8 years ago

Hi guys! I have a sensor module that I am trying to use. I can't seem to get it working with I2C.

Does anyone have any tips or can anyone point me to any useful materials to help me make progress with this sensor?

 

The sensor is a 3 axis magnetometer.

 

Thanks!

  • Sign in to reply
  • Cancel

Top Replies

  • koudelad
    koudelad over 8 years ago +3 suggested
    If you prefer a video tutorial, Ben Hack made one: www.youtube.com/watch Depending on how serious you are with electronics, I can recommend buying a logic analyzer (some cheap cost a few dollars). Or,…
  • clem57
    clem57 over 8 years ago +2 suggested
    You do not show any details, so a general tutorial may help https://learn.sparkfun.com/tutorials/i2c
  • koudelad
    koudelad over 8 years ago in reply to rbevy101 +1 suggested
    I've quickly gone through the linked datasheet and I think you only have to properly set MR_REG_M register in order to start measuring. By default (after reset), it is in sleep-mode.
  • clem57
    0 clem57 over 8 years ago

    You do not show any details, so a general tutorial may help https://learn.sparkfun.com/tutorials/i2c

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • koudelad
    0 koudelad over 8 years ago

    If you prefer a video tutorial, Ben Hack made one:

     

    You don't have permission to edit metadata of this video.
    Edit media
    x
    image
    Upload Preview
    image

     

    Depending on how serious you are with electronics, I can recommend buying a logic analyzer (some cheap cost a few dollars). Or, some microcontroller programmers have support for various protocols analysis (UART, I2C, SPI).

    This allows you to test any new sensor/device with a piece of HW and SW that is proven to work and later makes it easier to debug your own HW and firmware. Almost all microcontroller families now have an integrated I2C module and most development environments have ready made libraries for it.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • rbevy101
    0 rbevy101 over 8 years ago in reply to clem57

    https://www.adafruit.com/product/1120  This is the board itself

    https://cdn-shop.adafruit.com/datasheets/LSM303DLHC.PDF This is the datasheet

     

    I am trying to read the magnetometer.

     

    I have tried to use PSoC 4 kit in I2C to read the x axis values. When I am in debug mode in the PSoC creator I can't get any values returned when the device is being read. Every time I keep getting 0. The registers CRA_REG_M, CRB_REG_M, and MR_REG_M I beleive all need set for the calibration of the device. Am I correct in assuming this? Also is there a chance I have made a rookie mistake?

     

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • koudelad
    0 koudelad over 8 years ago in reply to rbevy101

    I've quickly gone through the linked datasheet and I think you only have to properly set MR_REG_M register in order to start measuring. By default (after reset), it is in sleep-mode.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • koudelad
    0 koudelad over 8 years ago in reply to rbevy101

    To be honest, I am also struggling with debugging PSoCs in PSOC Creator. Compared to debugging PICs, it is quite limited. (Must go deeper to make sure I am not using it wrong.)

    That's why I recommended a logic analyser. You can easily read/write the entire memory of the sensor and quickly check all values against the datasheet.

    If you are interested, I can insert some working I2C code for PSoC 5LP (it is probably the same for PSOC 4) I used for a real time clock.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • clem57
    0 clem57 over 8 years ago in reply to koudelad

    Here is example http://www.psocdeveloper.com/project/psoc4-i2c-communication/

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • rbevy101
    0 rbevy101 over 8 years ago in reply to koudelad

    Yea I ordered a logic analyser yesterday so i'm waiting on it coming. If you have code I would love to compare it to what I have!

     

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • koudelad
    0 koudelad over 8 years ago in reply to rbevy101

    The code below is the minimum I extracted from my project and should be general enough. I've worked with DS3232M RTC. The component name (placed in the design) is I2C and microcontroller used is some PSOC 5LP series.

     

    I am not sure about the original source, but I think this code comes from a Cypress application note (I've just opened a few, but couldn't find it). This code doesn't check for I2C component availability etc. and as provided is useless (nothing is done with the data). I only pasted the functions to perform a single read from the I2C slave device.

     

    #define I2C_WRITE 0u
    #define I2C_READ 1u
    #define RTC_I2C_ADDRESS 0x68 // I2C address of DS3232M (and some other RTCs)
    #define RTC_RAM_TD_LOCATION 0u // RAM location of current time and date
    #define RTC_NO_OF_BYTES_FULL 255u // number of bytes to read full RTC RAM
    
    void RTC_I2C_Read_RAM (unsigned char ramLocation, unsigned char bytesCount, unsigned char *i2cBuffer) {
        I2C_MasterSendStart(RTC_I2C_ADDRESS, I2C_WRITE);
        I2C_MasterWriteByte(ramLocation);
        I2C_MasterReadBuf(RTC_I2C_ADDRESS, i2cBuffer, bytesCount, (I2C_MODE_REPEAT_START | I2C_MODE_COMPLETE_XFER));    
    }
    
    uint8 i2cRTCReadBufferFull[256]; // buffer big enough to store full RAM of RTC clock
    
    int main(void)
    {
        CyGlobalIntEnable; /* Enable global interrupts. */
    
        I2C_Start(); // start I2C component
    
        RTC_I2C_Read_RAM(RTC_RAM_TD_LOCATION, RTC_NO_OF_BYTES_FULL, i2cRTCReadBufferFull); // read the whole RAM of DS3232M
    
     for(;;) {;}
    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • rbevy101
    0 rbevy101 over 8 years ago in reply to koudelad

    I have a funny feeling I am reading the device wrong or else not sending the correct device data.

     

    #define MAG_ADDR (0x1E) // magnetometer 7 bit address

    #define MAG_ADDR_Read (0x1E | 0x01) // magnetometer 8 bit address

    #define MAG_ADDR_Write (0x1E | 0x00) // magnetometer 8 bit address

    #define CRA_ADDR (0x00) // Control Register A

    #define CRB_ADDR (0x01) // Control Register B

    #define MR_ADDR (0x02) // Mode Register

    #define X_AXIS_H (0x03)

    #define X_AXIS_L (0x04)

     

     

    /////////////////////////////////////////////////////////////////// i2c.c containing i2c functions //////////////////////////////////////////////////////////////

    uint8 mag_ReadReg(uint8 Reg){

       

        uint8 Write_Buf[1] = {0}; //buffer that contains the register we want to read from

        Write_Buf[0] = Reg;

     

     

        uint8 Read_Buf[1] = {0};

       

        // step 1

        I2C_1_I2CMasterWriteBuf(MAG_ADDR, (uint8 *)Write_Buf, 1, I2C_1_I2C_MODE_NO_STOP);

        while((I2C_1_I2CMasterStatus() & I2C_1_I2C_MSTAT_WR_CMPLT) == 0){}

       

        // step 2

        I2C_1_I2CMasterReadBuf(MAG_ADDR, (uint8 *)Read_Buf, 1, I2C_1_I2C_MODE_REPEAT_START);

        while((I2C_1_I2CMasterStatus() & I2C_1_I2C_MSTAT_RD_CMPLT) == 0){}

       

        return Read_Buf[0];

    }

     

     

    // step(1) the register address that you write to

    //     (2) the value you wish to write to the register

     

     

    void mag_WriteReg(uint8 Reg, uint8 value){

       

        uint8 Write_Buf[2]={0};

        Write_Buf[0] = Reg; // register to write to

        Write_Buf[1] = value; // value to write to register

       

        I2C_1_I2CMasterWriteBuf(MAG_ADDR, (uint8 *)Write_Buf, 2, I2C_1_I2C_MODE_COMPLETE_XFER);

        while((I2C_1_I2CMasterStatus() & I2C_1_I2C_MSTAT_WR_CMPLT) == 0){}

       

        return;

    }

     

     

    ///////////////////////////////////////////////////////////////main.c//////////////////////////////////////////////////////

    int main(void)

    {

        I2C_1_Start();

       

        CyGlobalIntEnable; /* Enable global interrupts. */

       

        uint8 deviceID[1]={0};

        uint8 CRA_Before, CRA_After, CRB_Before;

       

        CRA_Before = mag_ReadReg(CRA_ADDR); //register before written to

        mag_WriteReg(MR_ADDR, 0x00); // register written to

        CRA_After = mag_ReadReg(CRA_ADDR); // register after written to

       

        for(;;)

        {

            deviceID[0] = mag_ReadReg(MR_ADDR);

        }

    }

     

    I keep trying to watch the variables CRA_BEFORE and CRA_AFTER to see is the register has changed. I keep getting <optimized out> in the watch window where there should be values.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • rbevy101
    0 rbevy101 over 8 years ago in reply to koudelad

    I found out my problem. All is working now! Thank Goodness

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube