Hackster will be offline on Monday, June 15 from 5pm to 7pm PDT to perform some scheduled maintenance.

RT-Spark ADC Voltage Monitor

A simple RT-spark-based voltage monitoring system was developed to measure battery and USB voltages using the ADC.

IntermediateFull instructions provided2 hours53
RT-Spark ADC Voltage Monitor

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Resistor 1M ohm
Resistor 1M ohm
×2
Jumper wires (generic)
Jumper wires (generic)
×10
Panasonic Coin Cell CR1220
optional brand
×1
Voltmeter, DC Voltage
Voltmeter, DC Voltage
you can also use digital multimeter
×1
RT-Thread RT-Spark Development Board
×1

Software apps and online services

RT-Thread STM32CubeIDE
version 1.19.0

Story

Read more

Custom parts and enclosures

My Repository

Schematics

Pin Diagram

Code

Main code

C/C++
  /* USER CODE BEGIN 2 */
  unsigned int value = 0;
  float halfVolts = 0;
  float sourceVolt = 3.3;
  float volt = 0;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  HAL_ADC_Start(&hadc1);
	  HAL_ADC_PollForConversion(&hadc1, 10);
	  value = HAL_ADC_GetValue(&hadc1);
	  halfVolts = (value * sourceVolt)/ 4095.0;
	  volt = halfVolts * 2;
  }
  /* USER CODE END 3 */
}

Enhanced Code

C/C++
 /* USER CODE BEGIN 2 */
  unsigned int value = 0;
  float halfVolts = 0;
  float sourceVolt = 3.3;
  float volt = 0;

  float average = 0;
  float summation = 0;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  summation = 0;   // Reset before taking 10 samples

	      for(int i = 0; i < 10; i++)
	      {
	          HAL_ADC_Start(&hadc1);
	          HAL_ADC_PollForConversion(&hadc1, 10);

	          value = HAL_ADC_GetValue(&hadc1);

	          halfVolts = (value * sourceVolt) / 4095.0;
	          volt = halfVolts * 2.0092;   // Fixed capitalization

	          summation += volt;
	      }

	      average = summation / 10.0;

	      HAL_Delay(100);
  }
  /* USER CODE END 3 */
}

Credits

MA REGINE BERMUDO
7 projects • 0 followers
Paul Rodolf P. Castor
57 projects • 9 followers

Comments