ARM I2C tutorial

Arm Development #2 – STM32 Cortex-M0 I2C Tutorial using ADS1115 Analog to Digital IC – Keil & CubeMX

The second ARM Tutorial is progressively more difficult as we tackle I2C. I’ve made a very detailed tutorial on how to get I2C working on Arduino in conjunction with the MPU-6050 not long ago. Based on the popularity of that tutorial I decided to do something with I2C on the Discovery board. That being said, I was interested in expanding my ADC signals, so I picked the ADS1115 Analog to Digital IC.

In this tutorial, we will be implementing I2C communication in order to read the data on the ADS1115. This data will be providing us with the information about 4 analog inputs. These inputs will be tied to four potentiometers. The potentiometers are all connected as voltage dividers simulating an analog input.

STM32 Discovery ADS1115 I2C Interface

Establishing I2C Communication

Through the CubeMX interface, one can easily implement I2C. There are two channels on the STM32F0 Controller. We will be using the first channel in this tutorial. In other words, PB6 and PB7 will be the SCL and SDA pins for the build. The ADS1115 we will use is already soldered on a breakout board. This makes it extremely easy to connect & communicate with the IC.

Hardware & Software for the Project

You are going to need a few things for the project; you can find them through the following links:

The full program we will be implementing in this project is available here: ADS1115 I2C Tutorial.

Implementing the I2C & ADC Settings of the ADS1115

Our first step is to understand how the data is being processed by the ADS1115 converter. It’s important to configure the right registers in order to be able to read the data we want.

Setting the registers

Our goal is to read individual channels and request the information sequentially. Therefore, if we refer to the ADS datasheet (Click Here), we need to set the following bits in the config register: 11000001. Keep in mind that this setting will allow us to read data from the first channel only. The subsequent reads will be done by setting the same register to 11010001, 11010010 & 11010011. It’s also important to note that in our software we will be passing the data in hex. It’s really easy to obtain through the “Scientific” function of the calculator on Windows or Mac OS.

Working with the LSB of the config register

The above explanation only covers 8 bits. The second byte of data will be the same for the 4 channels and is 10000011. Which converts to 0x83 in hex.

Leveraging the ARM HAL Library

The HAL library is extremely powerful. It allows one to send & receive I2C communication signals with few lines of code. In the code I wrote for the purpose of this tutorial, you can find several segments. The first one is used to set the above register to the specified values. The next command will set the data register which will request the information in it. The final read instruction is used to read the data back.

Stitching the data together

Once we’ve obtained the two bytes of data, it’s important to get a value we understand. In this case it will be the voltage on the potentiometer pin. Through the bit manipulation technique I’ve covered in another post (Click Here), we get the two bytes of data into a long. Followed by the multiplication by the constant, we are able to obtain the voltages we are looking for.

Conclusion – ARM I2C Communication

Reading I2C data on ARM is extremely straightforward & essential to master. Although your application may demand for a different IC, the ADS1115 is a great way to practice retrieving meaningful data.

Link to FULL SOFTWARE: Click Here

Thank you for reading & watching,
– EEEnthusiast

Leave a Reply

Your email address will not be published.

CommentLuv badge