cancel
Showing results for 
Search instead for 
Did you mean: 

I cant get continuous Data from Sensors with I2C

Broos
Associate II

The MCU I use is stm32f103c8t6 and the sensor is mpu-9255 on the board, there are 5 sensors and I show these sensor values on the LCD screen. I connected a button and when I press the button it moves to the next sensor. The problem is that I want to continuously receive the data of the sensors and display them on the LCD continuously. Instead, I was able to display one-time data with a delay, but after connecting the button toggle, I could not receive any data. It saves a fixed data and shows only that. As I said, what changes do I need to make to continuously receive and print data from a sensor every time I press the button?
By the way, if I take the value of only one sensor in "while" and print it on the LCD at once, I can get continuous data, but this does not work for more than one sensor.(I assume there is no hardware problem with this method)

This is the main.c file. If you need the library file to understand it, I can send it too.

 

 

while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

	  if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)==GPIO_PIN_SET)
	  {
	  	  lcd_print(2, 15, "1");
	  	  sensorindex++;
	  	  if(sensorindex>4)
	  	  {
	  		  sensorindex=0;
	  	  }
	  	  lcd_clear();
	  	  HAL_Delay(500);
	  	  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET);
	  }
	  else if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)==GPIO_PIN_RESET)
	  {
		  lcd_print(2, 15, "2");
	  }
	  else
	  {
		  lcd_print(2, 15, "3");
	  }
		MPU9255_t sensorData;
		MPU9255_Read_All(&hi2c1, &sensorData);
		  char lcdBuffer[64];
		  if(sensorindex==0)
		  {
			  int32_t pressure = BMP180_GetPressure();
			  sprintf(lcdBuffer, "B: %d", (int)pressure);
			  lcd_print(1, 1, lcdBuffer);
		  }
		  else if(sensorindex==1)
		  {
			  sprintf(lcdBuffer, "T:%.2f", sensorData.Temperature);
			  lcd_print(1, 1, lcdBuffer);
		  }
		  else if(sensorindex==2)
		  {
			  sprintf(lcdBuffer, "Gx:%.2f", sensorData.Gx);
			  lcd_print(1,1,lcdBuffer);

			  sprintf(lcdBuffer, "Gy:%.2f", sensorData.Gy);
			  lcd_print(1,7,lcdBuffer);

			  sprintf(lcdBuffer, "Gz:%.2f", sensorData.Gz);
			  lcd_print(2,1,lcdBuffer);
		  }
		  else if(sensorindex==3)
		  {
			  sprintf(lcdBuffer, "Ax:%.2f", sensorData.Ax);
			  lcd_print(1,1,lcdBuffer);

			  sprintf(lcdBuffer, "Ay:%.2f", sensorData.Ay);
			  lcd_print(1,7,lcdBuffer);

			  sprintf(lcdBuffer, "Az:%.2f", sensorData.Az);
			  lcd_print(2,1,lcdBuffer);
		  }
		  else if(sensorindex==4)
		  {
			  sprintf(lcdBuffer, "Mx:%d", (int)sensorData.Mx);
			  lcd_print(1,1,lcdBuffer);

			  sprintf(lcdBuffer, "My:%d", (int)sensorData.My);
			  lcd_print(1,7,lcdBuffer);

			  sprintf(lcdBuffer, "Mz:%d", (int)sensorData.Mz);
			  lcd_print(2,1,lcdBuffer);

		  }
  }

 

 

 

 

 

5 REPLIES 5

So continuous or when you press the button, seem like counter-directives.. Or whilst you press the button?

Is PC13 the button? Why are you setting it rather than just reading?

Use the </> code pasting icon when in-lining code to the posts.

The I2C should be able to continuously/repetitively interact with I2C device(s), at rates exceeding your ability to display and visually process the data.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

If you want it to keep reading the I2C device the code needs to flow, and not be blocked waiting on the state of the pin.

Watch also debouncing, the MCU can loop multiple times in the time you press and release the button, and the period where the contacts bounce/oscillate

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I am not an expert in this field. I'm trying to learn. Yes Pin 13 is the Button pin. What do you mean by reading?

I didn't think the problem was with I2C anyway. I'm wondering about the flaw in my own code.

What's happening now is that when I press the button and pull my hand back, it switches between fixed values (as I mentioned in my question, I think it returns the values saved to the MCU, not the ones coming from the Sensor?) But I think it should happen with this code: when I press the button and pull my hand back, it will switch from the Gyro sensor to the Accel sensor and If I move the sensor, I will be able to see the acceleration increase on the LCD. I don't think the problem is with the button.

Can you re-paste the code from the original source, so the lines and tabs/indents are formatted cleanly and not just a wall of text.

 

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET); // Write an input pin ??

}

else if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)==GPIO_PIN_RESET) // Reading input state

 

Presumably the printing is going to reflect whatever you last read from the sensor. I2C is a query-response type interface, you have to drive the bus, data is not going to stream over the interface.

MPU9255_t sensorData;

MPU9255_Read_All(&hi2c1, &sensorData);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

When I press the button, the status of pin 13 remains SET and the sensorindex constantly increases. I used the writepin command for this, I don't know if it's wrong, but it works.

"you have to drive the bus, data is not going to stream over the interfac." I don't understand what you mean by this, can you be a little more clear? What do you suggest for the solution?