ADS1115 with STM32 CubeMx

ADS1115 with STM32 CubeMx

Hello everyone, In this article I will talk about How can we use ADS1115 with STM32 and Cubemx. Firstly I want to talk about what ADS1115 is and what it does.

ADS1115

ADS1115 is an analog to digital converter (ADC). It is ultra small sized and has I2C communication. It has 4 channels to read analog voltages and transmit these voltage values via I2C communication bus. For more details you can check datasheet of ADS1115.

Now lets start with a cubemx project and configure board for ADS1115 and I2C communication. My processor is STM32F051R8T6 and I have created project with this microprocessor.


STM32 CubeMX I2C Configuration

After creating new cubemx project we need to acticate I2C under Connectivity section. PB7 as SDA and PB6 as SCL pin will be activated. On the left panel in parameter Above image have all my I2C parameter settings. I did not activated NVIC because I will do send and receive. This means I will send data and according to this ADS1115 will send requested datas. So just activate I2C and set parameter like above. Do not activate NVIC and DMA for this process.

If we are ready to start we must generate code now.

First we need to declare necessary variables.


//Connect ADDR pin to GND and I2C slave adress will be 0X48 .
#define ADS1115_ADDRESS 0x48
unsigned char ADSwrite[6];
int16_t reading;
float voltage[4];
const float voltageConv = 6.114 / 32768.0;

We are gonna use these variables to read voltage values from ADS1115.

Now we need to insert below code block into while section.


for(int i=0; i< 4; i++){
			ADSwrite[0] = 0x01;
			
			switch(i){
				case(0):
					ADSwrite[1] = 0xC1; //11000001
				break;
				case(1):
					ADSwrite[1] = 0xD1; //11010001
				break;
				case(2):
					ADSwrite[1] = 0xE1;
				break;
				case(3):
					ADSwrite[1] = 0xF1;
				break;
			}
			
			ADSwrite[2] = 0x83; //10000011 LSB
			
			HAL_I2C_Master_Transmit(&hi2c1, ADS1115_ADDRESS << 1, ADSwrite, 3, 100);
			ADSwrite[0] = 0x00;
			HAL_I2C_Master_Transmit(&hi2c1, ADS1115_ADDRESS << 1 , ADSwrite, 1 ,100);
			HAL_Delay(20);
			
			HAL_I2C_Master_Receive(&hi2c1, ADS1115_ADDRESS <<1, ADSwrite, 2, 100);
			reading = (ADSwrite[0] << 8 | ADSwrite[1] );
			if(reading < 0) {
				reading = 0;
			}
			voltage[i] = reading * voltageConv;
			
		}

As you can see there is a for loop top of code. This for loop looking for 4 values. We are choosing which channel to read in this for loop. 0xC0, 0xD0, 0xE0 and 0xF0 values provided from datasheet. These values are for selecting to channel. Last value of the sended variable is for choosing LSB or MSB. If we want get value as reversed. After specifing the read configurations we need to transmit this variable into I2C data bus and ADS1115 will read this values. After this ADS1115 will answer to our microprocessor. Then receive the answer.

Then multiply the value with voltage conversation constant and get the between 0 - 5 voltage value.

You are ready for reading ADS1115 now. We can make this with all series stm32. If you want to do this STM32F4 processors you are free to do this.

That is all for this post now.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • Thecodeprogram User
    misafir hesabı

    gayet güzel lakin benim stm studio programımda değerler olması gereken değerler çıkmıyor kopyala yapıştırda bile ads bağlantısını addr pini gndye seri yaptıım 0x48den dolayı ama v1 yani a0 gözüküyor sadece ve oda her zaman 4.90'da sıkıntı sizce nedir?

    2020/02/13 05:37:23
    • Thecodeprogram User
      Burak Hamdi TUFAN

      Merhabalar, Ben bu yazıyı yazarken denediğim ve çalıştığı şekilde yazdım. Birkaç farklı sebebi olabilir, kontrol etmenizde fayda var: Öncelikle, -Bağlantılar doğrumu. analog değer gönderen elemanın 5v pini ile yanlışlıkla ads1115 in analog input pini bağlanmış olabilir. -Haberleşme yapılandırması olduğu gibimi, -analog veriyi gönderen cihazda bir problem olabilirmi

      2020/02/14 13:42:39