yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
STM32-BoardCar📖
0. Get started quickly
  • 0.ReadMe
  • 1. Installation Instructions
  • 2. Version configuration
  • 3. Smart car installation video
  • 4. Precautions for use
1. Introduction to STM32 development board
  • 1. Learn about STM32
  • 2. Introduction to STM32 development board
  • 3. Development board pin assignment
  • 4. Introduction to HAL library and LL library
2. Setting up and using the development environment
  • 1. Common development tools
  • 2. STM32CubeMX Install - Use
  • 3. STM32CubeIDE Install - Use
  • 4. Common Driver Installation
  • 5. Program download and simulation
  • 6. Engineering experience and migration
3. Development board basic tutorial
  • 1. RGB searchlight (GPIO)
  • 2. Buzzer control (GPIO)
  • 3. Button control (EXTI)
  • 4. Serial communication (UASRT)
  • 5. Voltage detection (ADC)
  • 6. Internal temperature sensor(ADC)
  • 7. Independent watchdog(IWDG)
  • 8. Window watchdog(WWDG)
  • 9. Basic timer (TIM6)
  • 10. Universal Timer (TIM3)
  • 11. Advanced timers (TIM1)
  • 12. I2C Communication (IIC)
  • 13. Off-chip Flash (SPI)
  • 14. On-chip Flash (Flash)
  • 15. DMA:USART
  • 16. DMA:SPI
  • 17. DMA:I2C
  • 18. USB communication
  • 19. USB analog U disk
  • 20. FreeRTOS
4. Car expansion tutorial
  • 1. Servo control
  • 2. Basic use of ultrasonic steering gear gimbal
  • 3. Motor control
  • 4. OLED display light sensitivity value
  • 5. Infrared sensor
  • 6. Four-way tracking module
  • 7. Infrared remote control
  • 8. Ultrasonic ranging
  • 9. PS2 controller test
  • 10.K230 Vision Module
  • 11.AI voice interaction integration module
5. Robot car tutorials
  • 1. Car line patrol
  • 2. Ultrasonic follow
  • 3. Ultrasonic obstacle avoidance
  • 4. Ultrasonic obstacle avoidance (servo gimbal version)
  • 5. Infrared sensor obstacle avoidance
  • 6. Infrared remote control
  • 7. PS2 handle remote control
  • 8.K230 face tracking on PTZ
  • 9.K230 color tracking
  • 10.K230 Color Line Patrol
  • 11.K230 QR code command
  • 12.K230 machine code tracking
  • 13.K230 road sign indication action (voice)
  • 14.K230 color response (voice)
  • 15.K230 gesture control (voice)
  • 16.K230 Rock-Paper-Scissors Game (Voice)
6. Operation video
  • 1-How to install STM32CubeIDE software
  • 2-How to create Demo project
  • 3-How to download code into STM32 robot car
  • 4-How to open and compile a code file for STM32
  • 5-About IR control and Charge battery
Download
  • Code
  • SCH
  • Software File
  • STM32 Chip DataSheet
  • Installation manual
Download ZIP

Welcome to STM32-BoardCar repository

5. Voltage detection (ADC)

Voltage detection

Voltage detection1. Software-Hardware2. Brief principle1. Hardware schematic diagram2. Physical connection diagram3. Control principle3. Project configuration1. Description2. Pin configuration4. Main functions1. User function2. LL library function analysis5. Experimental phenomena

This tutorial demonstrates: obtaining the real-time voltage of the battery pack through ADC (ADC3_IN5), and printing the battery voltage information through serial port (UASRT1).

1. Software-Hardware

  • STM32F103CubeIDE

  • STM32 Robot Development Board

    ADC, USART: chip internal peripherals

    7.4V battery pack: external

  • Type-C data cable or ST-Link

    Download programs or simulate the development board

2. Brief principle

1. Hardware schematic diagram

image-20231029221831369

image-20231029222808919

2. Physical connection diagram

When conducting this experiment, an external 7.4V battery pack is required.

image-20231029222123262

image-20231102183406266

3. Control principle

Obtain the converted value of the battery BAT pin through a single ADC, convert the value into actual voltage information and print it out through the serial port.

  • ADC

STM32F103ZET6 has three built-in 12-bit analog-to-digital converters. Each ADC controller has up to 18 channels and can measure 16 external and 2 internal signal sources.

  • ADC Channel

    Only the ADC channels used in this tutorial are selected here.

image-20231029230442660

  • ADC conversion value

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

ADC value range=0−212=0−4095

The ADC converted value can be stored in the 16-bit data register in a left-justified or right-justified manner.

  • ADC Conversion Mode

A/D conversion of each channel of the ADC can be performed in single, continuous, scan or discontinuous modes.

modelfunction
Single conversion modeADC only performs one conversion
continuous conversion modeAs soon as the previous ADC conversion is completed, another conversion is started.
Scan modeUsed to scan a group of ADC channels
Intermittent modeGroup conversion of multiple ADC channels until the entire sequence is converted
Rule 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. After the injection channel is completed, the regular channel conversion can be continued.
  • ADC conversion time

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

Total conversion time=TCONV=sampling time+12.5 clock cycles

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

Total conversion time=TCONV=sampling time+12.5clock cycles=(239.5+12.5)∗112000000=21us
  • Actual voltage conversion: ADC internal reference voltage 3.3V
VBAT=ValueADC converted value∗(3.3)4096

Referring to the hardware schematic diagram, we can see: Principle of equal current

ICurrent=VBATR7=VMR3+R7⟹VBAT3.3=VM10+3.3⟹VM=VBAT∗(10+3.3)3.3
Thatis,actualvoltage=VM=VBAT∗(10+3.3)3.3

3. Project configuration

Project configuration: Prompt configuration options during STM32CubeIDE project configuration

1. Description

Omitted project configuration part: New project, chip selection, project configuration, SYS of pin configuration, RCC configuration, clock configuration and project configuration content

The project configuration part that is not omitted is the key point that needs to be configured in this tutorial.

xxxxxxxxxx
Please refer to [2. Development environment construction and use: STM32CubeIDE installation and use] to understand how to configure the omitted parts of the project.

2. Pin configuration

  • Configure specified pin function

You can directly select the corresponding pin number in the pin view, and the corresponding options will appear when you left-click the mouse.

image-20231030003052008

  • ADC

image-20231030003348482

  • USART

image-20231029202911507

  • Clock Configuration

image-20231030003603356

  • Advanced Settings

image-20231030003649993

  • Generate code

image-20231011102854602

4. Main functions

It mainly introduces the functional code written by the user. For detailed code, you can open the project file provided by us yourself and enter the Bsp folder to view the source code.

1. User function

Function: Redirect print

​x
#include "stdio.h"

#ifdef __GNUC__
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE {
  HAL_UART_Transmit(&USART1, (uint8_t *)&ch, 1, 0xFFFF);
  return ch;
}

Function: Get_BAT

Function prototypeuint16_t Get_BAT(uint32_t ch)
Function descriptionObtain battery ADC conversion raw data
Input parametersch: channel
Return valueADC converted value

Function: Adc_Get_Average

Function prototypeuint16_t Adc_Get_Average(uint32_t ch, uint8_t times)
Function descriptionAverage value of multiple ADC conversion values
Input parameter 1ch: channel
Input parameter 2times: average times
Return valueADC converted value

Function: Adc_Get_Measure_Volotage

Function prototypefloat Adc_Get_Measure_Volotage(void)
Function descriptionGet original voltage value
Input parametersNone
Return valueOriginal voltage value

Function: Adc_Get_Battery_Volotage

Function prototypefloat Adc_Get_Battery_Volotage(void)
Function descriptionObtain the actual battery voltage before voltage division
Input parametersNone
Return valueActual battery voltage

2. LL library function analysis

The HAL library functions that have been introduced in the previous tutorial will not be introduced again in the tutorial!

xxxxxxxxxx
If you want to find the HAL library and LL library function analysis involved in the entire tutorial, you can view the documents in the folder [8. STM32 Manual: STM32F1_HAL Library and LL Library_User Manual]

Function: HAL_ADC_Init

Function prototypeHAL_StatusTypeDef HAL_ADC_Init(ADC_HandleTypeDef* hadc)
Function descriptionInitialize ADC parameters
Input parametershadc: ADC handle address
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

Function: HAL_ADC_MspInit

Function prototypevoid HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
Function descriptionInitialize the peripheral clock, GPIO and NVIC of the ADC
Input parametershadc: ADC handle address
Return valueNone

Function: HAL_ADC_MspDeInit

Function prototypevoid HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
Function descriptionCancel the initialization of ADC peripheral clock, GPIO and NVIC
Input parametershadc: ADC handle address
Return valueNone

Function: HAL_ADC_ConfigChannel

Function prototypeHAL_StatusTypeDef HAL_ADC_ConfigChannel
(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)
Function descriptionConfigure ADC specific channels
Input parameter 1hadc: ADC handle address
Input parameter 2sConfig: Configure ADC channel parameters: conversion channel, conversion sequence, sampling period
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

Function: HAL_ADC_Start

Function prototypeHAL_StatusTypeDef HAL_ADC_Start(ADC_HandleTypeDef* hadc)
Function descriptionEnable ADC rule group conversion
Input parametershadc: ADC handle address
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

Function: HAL_ADC_PollForConversion

Function prototypeHAL_StatusTypeDef HAL_ADC_PollForConversion(ADC_HandleTypeDef* hadc, uint32_t Timeout)
Function descriptionWait for ADC rule group conversion to complete
Input parameter 1hadc: ADC handle address
Input parameter 2Timeout: timeout time
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

Function: HAL_ADC_GetValue

Function prototypeuint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
Function descriptionGet ADC conversion value
Input parametershadc: ADC handle address
Return valueADC conversion value

Function: HAL_UART_Init

Function prototypeHAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
Function descriptionInitialize serial port parameters
Input parametershuart: serial port handle address
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

Function: HAL_UART_MspInit

Function prototypevoid HAL_UART_MspInit(UART_HandleTypeDef *huart)
Function descriptionInitialize the peripheral clock, GPIO and NVIC of the serial port
Input parametershuart: serial port handle address
Return valueNone

Function: HAL_UART_MspDeInit

Function prototypevoid HAL_UART_MspInit(UART_HandleTypeDef *huart)
Function descriptionCancel the initialization of serial port peripheral clock, GPIO and NVIC
Input parametershuart: serial port handle address
Return valueNone

Function: HAL_UART_Transmit

Function prototypeHAL_StatusTypeDef HAL_UART_Transmit
(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
Function descriptionSend data in polling mode
Input parameter 1huart: serial port handle address
Input parameter 2pData: The first address of the data buffer to be sent
Input parameter 3Size: Number of data bytes sent
Input parameter 4Timeout: timeout time, in milliseconds
Return valueHAL status value: HAL_OK, HAL_ERROR, HAL_BUSY, HAL_TIMEOUT

5. Experimental phenomena

After successfully downloading the program, press the RESET button on the development board to open the serial port debugging assistant to observe the phenomenon!

xxxxxxxxxx
For program download, please refer to [2. Development environment construction and use: program download and simulation]

Phenomenon:

The serial port debugging assistant will print the real-time voltage information of the battery pack (the maximum voltage of the 7.4V battery pack can reach 8.4V).

image-20231102183842112

Serial communication (UASRT)
Internal temperature sensor(ADC)

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/中文