yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Self-balancing Robot Car📖
00.Get started quickly
  • 01.Balance car manual
  • 02.Balancing car assembly
  • 03.Get started quickly
  • 04.Balance car interface description
  • 05.Common Problem Analysis
  • 06.Balance Car V2 Library Function Development Manual
01.Environment development
  • 01.MDK-ARM installation
  • 02.STM32CubeIDE installation
  • 03.Common driver installation
  • 04.Download the program
  • 05.MDK-ARM project usage
  • 06.Program simulation
  • 07. VSCode Installation
02.STM32 getting started course
  • 01.Project creation
  • 02.LED Control(GPIO)
  • 03.Buzzer control(GPIO)
  • 04.Button control(EXTI)
  • 05.Serial communication(USART)
  • 06.Voltage detection(ADC)
  • 07.Basic timer(TIM)
  • 08.Universal timer(TIM)
  • 09.Advanced Timer(TIM)
  • 10.I2C Communication(I2C)-OLED
  • 11.SPI communication(SPI)-2.4G handle
  • 12.Independent Watchdog (IWDG)
  • 13.Window Watchdog (WWDG)
  • 14.DMA-USART
  • 15.DMA-I2C
03.STM32 expansion course
  • 01.Battery voltage detection (ADC)
  • 02.Ultrasonic module-measuring distance (TIM)
  • 03.Motor drive+encoder (TIM)
  • 04.OLED Data-Display(I2C)
  • 05.MPU6050-Data Read (I2C)
  • 06.Bluetooth module-Data reading (USART)
  • 07.2.4G handle-Data reading (SPI)
  • 08.Tracking module-Read status (GPIO)
  • 09.CCD module-Reading data (ADC)
  • 10.Electromagnetic module-Reading data (ADC)
  • 11.K210 module-Serial communication (USART)
  • 12.Tmini-Plus lidar-Reading data (USART)
04.Robot car PID control course
  • 01.PID basic concept
  • 02.PID example analysis
  • 03.P, PI, PD controller theory
  • 04.Position PID
  • 05.Incremental PID
  • 06.Cascade PID
  • 07.Balance principle of car
  • 08.Car upright control (PD)
  • 09.Car speed ​​control (PI)
  • 10.Car steering control (PD)
  • 11.Got angle and angular velocity (DMP algorithm)
  • 12.Got angle and angular velocity (Kalman filter algorithm)
  • 13.Got angle and angular velocity(Complementary filter algorithm)
05.Robot car basic course
  • 01.Car parameter adjustment
  • 02.Ultrasonic obstacle avoidance
  • 03.Ultrasonic follow
  • 04.Bluetooth remote control
  • 05.Load balance
06.Robot car advanced course
  • 01.4-channel tracking
  • 02.4-channel tracking avoid
  • 03.2.4G handle control
  • 04.CCD tracking
  • 05.CCD tracking avoid
  • 06.Electromagnetic tracking
  • 07.K210-QR code recognition
  • 08.K210-Color line tracking
  • 09.K210-Color follow
  • 10.K210-Self learning
  • 11.K210-Number recognition
  • 12.Lidar avoid
  • 13.Lidar guard
  • 14.Lidar follow
  • 15.Lidar patrol
  • 16.Lidar wall tracking-straight line
  • 17.Lidar wall tracking-multiple walls
  • 18. DIY Automatic driving car
07.Robot car LQR control
  • Robot car LQR control
08.Circuit design
  • 01.Overall circuit design
  • 02.STM32 minimum system
  • 03.Serial port circuit design
  • 04.Power circuit design
  • 05.MPU6050 Circuit Design
  • 06.Motor drive circuit design
09.Mathematical model
  • Mathematical model
10.Expand course
  • Autonomous Driving
Download
  • APP(Android)
  • Codes
  • Hardware-Info
  • SCH
  • Software Tools
  • STM32_Chip_DataSheet
  • 3D_Model_File
  • Video_PPT
Download ZIP

Welcome to Self-balancing Robot Car repository

06.Voltage detection(ADC)

Voltage detection

Voltage detection

Voltage detectionHardware connectionControl principlePin definitionSoftware codeControl functionExperimental phenomenon

The tutorial demonstrates ADC (ADC12_IN5) to obtain the real-time voltage of the battery pack and print the voltage value through the serial port.

The tutorial only introduces the standard library project code

Hardware connection

image-20240825204131635

PeripheralsDevelopment boardDescription
Battery packPA5The battery pack DC interface needs to be connected to the development board

Control principle

The converted value of the battery BAT pin is obtained through a single ADC, and the value is converted into actual voltage information and printed out through USART1.

  • ADC

STM32F103RCT6 has three 12-bit analog-to-digital converters embedded, and each ADC controller has up to 16 channels.

  • ADC channel

Only introduce the ADC channel corresponding to the battery pack interface.

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PA5PA5SPI1_SCK/DAC_OUT2/ADC12_IN5 
  • ADC conversion value

The ADC of STM32F103RCT6 is a 12-bit successive approximation analog-to-digital converter with 12-bit resolution;

ADCvaluerange=0−212=0−4095

The converted value of ADC can be stored in 16-bit data register in left-aligned or right-aligned mode.

  • ADC conversion mode

The A/D conversion of each ADC channel can be performed in single, continuous, scan or intermittent mode.

ModeFunction
Single conversion modeADC performs only one conversion
Continuous conversion modeStarts another conversion as soon as the previous ADC conversion is completed
Scan modeUsed to scan a group of ADC channels
Intermittent modeConverts multiple ADC channels in groups until the entire sequence is converted
xxxxxxxxxx
Regular channel: Perform channel conversion in a certain order
Injection channel: The injection channel can interrupt the conversion of the regular channel to execute the injection channel, and then continue to execute the regular channel conversion after the injection channel is completed
  • ADC conversion time

The input clock of the ADC must not exceed 14MHz, which is generated by the frequency division of PCLK2.

Totalconversiontime=TCONV=samplingtime+12.5clockcycles

Conversion time in this tutorial: ADC clock frequency 12MHz, sampling time 239.5 clock cycles

Totalconversiontime=TCONV=samplingtime+12.5clockcycles=(239.5+12.5)∗112000000=21us
  • Actual voltage conversion: ADC internal reference voltage 3.3V

()VBAT=ValueADCconvertedvalue∗(3.3)4096

Referring to the hardware schematic diagram, we can see: Current equality principle

()Icurrent=VBATR16=VMR16+R14⟹VBAT3.3=VM10+3.3⟹VM=VBAT∗(10+3.3)3.3
()Thatis,theactualvoltage=VM=VBAT∗(10+3.3)3.3

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PA5PA5SPI1_SCK/DAC_OUT2/ADC12_IN5 

Software code

Since the default function of the PA5 pin is a normal IO pin function, we need to use the multiplexing function.

xxxxxxxxxx
Product supporting data source code path: Attachment → Source code summary → 1.Base_Course → 6.ADC

Control function

The tutorial only briefly introduces the code, and you can open the project source code to read it in detail.

Battery_init

​x
//Battery power detection initialization
void Battery_init(void)
{
    ADC_InitTypeDef ADC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(BAT_GPIO_CLK | BAT_ADC_CLK, ENABLE); //Enable BAT_ADC channel clock
    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //Set ADC division factor 6

    //72M/6=12, ADC maximum input clock cannot exceed 14M
    //PA5 as analog channel input pin
    GPIO_InitStructure.GPIO_Pin = BAT_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Analog input
    GPIO_Init(BAT_GPIO_PORT, &GPIO_InitStructure); //Initialization GPIOA.5

    ADC_DeInit(BAT_ADC); //Reset BAT_ADC and reset all registers of the peripheral BAT_ADC to default values
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC independent mode
    ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Single channel mode
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Single conversion mode
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //Conversion is started by software instead of external trigger
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC data right alignment
    ADC_InitStructure.ADC_NbrOfChannel = 1; //Number of ADC channels for regular conversion in sequence
    ADC_Init(BAT_ADC, &ADC_InitStructure); //Initialize peripheral ADCx according to specified parameters
    ADC_Cmd(BAT_ADC, ENABLE); //Enable specified BAT_ADC
    ADC_ResetCalibration(BAT_ADC); //Start reset calibration
    while (ADC_GetResetCalibrationStatus(BAT_ADC)); //Wait for reset calibration to end
    ADC_StartCalibration(BAT_ADC); //Start AD calibration
    while (ADC_GetCalibrationStatus(BAT_ADC)); //Wait for calibration to end
}

Battery_Get

xxxxxxxxxx
// Get ADC value, ch: channel value
static uint16_t Battery_Get(uint8_t ch)
{
    uint16_t timeout = 1000;
    // Set the specified ADC rule group channel, set their conversion order and sampling time
    ADC_RegularChannelConfig(BAT_ADC, ch, 1, ADC_SampleTime_239Cycles5);
    // Channel 1, the regular sampling order value is 1, and the sampling time is 239.5 cycles
    ADC_SoftwareStartConvCmd(BAT_ADC, ENABLE); // Enable software conversion function
    while (!ADC_GetFlagStatus(BAT_ADC, ADC_FLAG_EOC) && timeout--); // Wait for the conversion to end
    return ADC_GetConversionValue(BAT_ADC); // Return the most recent conversion result of the BAT_ADC rule group
}

Battery_Get_Average

xxxxxxxxxx
uint16_t Battery_Get_Average(uint8_t ch, uint8_t times)
{
    uint16_t temp_val = 0;
    uint8_t t;
    for (t = 0; t < times; t++)
    {
        temp_val += Battery_Get(ch);
    }
    if (times == 4)
    {
        temp_val = temp_val >> 2;
    }
    else
    {
        temp_val = temp_val / times;
    }
    return temp_val;
}

Get_Measure_Volotage

xxxxxxxxxx
// Get the measured raw voltage value
float Get_Measure_Volotage(void)
{
    uint16_t adcx;
    float temp;
    adcx = Battery_Get(BAT_ADC_CH); // Battery channel 5 is measured once
    temp = (float)adcx * (3.30f / 4096);
    return temp;
}

Get_Battery_Volotage

xxxxxxxxxx
// Get the actual battery voltage before voltage division
float Get_Battery_Volotage(void)
Serial communication(USART)
Basic timer(TIM)

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文