cancel
Showing results for 
Search instead for 
Did you mean: 

I have a STM32WB55CGU6 where I have all my ADC pins used up and im trying to obtain the battery level using a pin which does not have a ADC (PB6) but unable to do so. Can someone check the code if its correct?

JGoh.1
Associate

I have a STM32WB55CGU6 where I have all my ADC pins used up and im trying to obtain the battery level using a pin which does not have a ADC (PB6) which is branched as shown in the picture attached.

This follows the principle where it will charge and discharge following this link : link

This is my current program but somehow it doesnt work and im pretty sure im doing something wronf but what I am not too sure.

while(1){

 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);

 HAL_Delay(1);

 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6);

 while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)==1){

 time++;

 }

 if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6)==0){value = time;}

}

Im programming with STMCubeIDE where I have set the pin to GPIO_Output

3 REPLIES 3
TDK
Guru

If the pin

If you feel a post has answered your question, please click "Accept as Solution".
TDK
Guru

You never set the pin as input. You just set it as output and wait for its value to change. That's not going to happen. Re-read what the link you posted says you should do:

  • Set the GPIO pin as an output and set it Low. This discharges any charge in the capacitor and ensures that both sides of the capacitor are 0V.
  • Set the GPIO pin as an input. This starts a flow of current through the resistors and through the capacitor to ground. The voltage across the capacitor starts to rise. The time it takes is proportional to the resistance of the LDR.
  • Monitor the GPIO pin and read its value. Increment a counter while we wait.
  • At some point the capacitor voltage will increase enough to be considered as a High by the GPIO pin (approx 2v). The time taken is proportional to the light level seen by the LDR.
  • Set the GPIO pin as an output and repeat the process as required.

If you feel a post has answered your question, please click "Accept as Solution".
BNist.1
Associate II

Hello,

I am working with the writer of the post in the same team. We have already tried the following code as well:

	  /*Configure GPIO pin : PB6 */
	    	    GPIO_InitStruct.Pin = GPIO_PIN_6;
		    	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;  
	    	    GPIO_InitStruct.Pull = GPIO_NOPULL;
	    	    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	    	    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
	  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); 
	  HAL_Delay(5);
	  
	  /*Configure GPIO pin : PB6 */
	    	    GPIO_InitStruct.Pin = GPIO_PIN_6;
		    	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;  // analog Input
	    	    GPIO_InitStruct.Pull = GPIO_NOPULL;
	    	    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	    	    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
	    		 
	  
	  test = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6); // it gives us 0 or 1 -

Where we get lost is how do we increment the counter while we wait? We have tried various options, nothing works. Would you mind explaining this to us, or even maybe showing us the code?

Thank you,

Bogdan