KennethPaul Rodolf P. Castor
Published

Simple Volt-Meter with RT-Spark (Spark-1)

A small range Volt-Meter with good accuracy, all done with a very minimal setup.

IntermediateProtip1 hour17
Simple Volt-Meter with RT-Spark (Spark-1)

Things used in this project

Hardware components

Jumper wires (generic)
Jumper wires (generic)
They usually come in a pack, and we only need 4 wires.
×1
Breadboard (generic)
Breadboard (generic)
×1
Resistor 1M ohm
Resistor 1M ohm
Used for the Voltage-Divider Circuit
×2
RT-Spark (Spark-1) RT-Thread Development Board
×1
Panasonic Coin Cell CR1220
Optional, used to prove that it is accurate and legit.
×1
Voltmeter, DC Voltage
Voltmeter, DC Voltage
Voltmeter or Multimeter, anything that can read voltage to again, proving the program that it works.
×1

Software apps and online services

RT-Thread STM32CubeIDE (V1.19.0)
Voltage Monitoring ADC (Program)

Story

Read more

Schematics

Pin Diagram

This pin diagram is a voltage divider, and it is used to avoid breaking the board when checking for a battery or any voltage outside the board's power rails

Code

Simple Code

C/C++
The simple version of the code, has cons such as reading accuracy, but enough to understand the fundamentals of how the board reads and converts the analog signal to digital signal.
int main(void)
{
  uint16_t rawValue = 0;
  float sourceVoltage = 3.3;
  float halvedVoltage = 0;
  float voltage = 0;

  while (1)
  {
	  HAL_ADC_Start(&hadc1);
	  HAL_ADC_PollForConversion(&hadc1, 10);
	  rawValue = HAL_ADC_GetValue(&hadc1);
	  halvedVoltage = (rawValue * sourceVoltage)/ 4095.0;
	  voltage = halvedVoltage * 2;
  }

}

Enhanced Code

C/C++
This is the enhanced code to account for the Resistance scale factor, improving the accuracy of the voltage reading.
int main(void)
{
  uint16_t rawValue = 0;
  float voltage = 0;
  float tempVal = 0;
  float avg = 0;

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

		  rawValue = HAL_ADC_GetValue(&hadc1);
		  voltage = (rawValue * "yourVoltageSourceValue ex. 3.337") / 4095.0;
		  voltage = voltage * "scalefactor";
		  tempVal = tempVal + voltage;
	  }

	  avg = tempVal / 10;
	  tempVal = 0;
  }
}

Credits

Kenneth
3 projects • 0 followers
Paul Rodolf P. Castor
18 projects • 9 followers

Comments