yahboom
HomeTutorialsProduct
Solution
Blog
Online Store
About Us
EN/中文
Store
STM32 Self-balancing 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 STM32 Self-balancing Car repository

10.Electromagnetic module-Reading data (ADC)

Electromagnetic module-Reading data (ADC)

Electromagnetic module-Reading data (ADC)Hardware connectionControl principleSoftware configurationPin definitionSoftware codeControl functionExperimental phenomenon

The tutorial combines OLED and serial port to display the data output by the electromagnetic line patrol module.

The tutorial only introduces the standard library project code

Hardware connection

image-20240825214206771

Since we have configured a special connection line, we only need to install it to the corresponding interface:

PeripheralsDevelopment board
Electromagnetic line patrol: ADC1-L1PC0
Electromagnetic line patrol: ADC2-L2PC1
Electromagnetic line patrol: ADC3-L3PC2
Electromagnetic line patrol: ADC4-L4PC3
Electromagnetic line patrol: ADC5-L5PC4
Electromagnetic line patrol: ADC6-L6PC5
Electromagnetic line patrol: ADC7-L7PB0
Electromagnetic line patrol: NCNot connected
Electromagnetic line patrol: GNDGND
Electromagnetic line patrol: GNDGND

Control principle

The electromagnetic line patrol module consists of an electromagnetic line patrol probe module and an operational amplifier.

  • Electromagnetic line patrol probe module

img

PeripheralsDescription
Electromagnetic line patrol: L1-L7Induction signal output pin: need to connect to operational amplifier
Electromagnetic line patrol: GNDPower supply pin: GND
Electromagnetic line patrol: NCDo not connect
  • Operational amplifier

image-20240808150332909

PeripheralsDescription
Operational amplifier: VCCPower supply pin: 3.3-5V
Operational amplifier: GNDPower supply pin: GND
Operational amplifier: L1-L4Sensing signal input pin: electromagnetic line patrol probe module
Operational amplifier: ADC1-ADC4Gained signal: connected to MCU's IO port
Operational amplifier: 1-4Gain potentiometer: gain adjustment

image-20240808145609751

The gain potentiometer needs to be adjusted before use: Place each inductor of the electromagnetic line patrol probe module vertically in the same position directly above the enameled wire of the electromagnetic signal generator, and adjust the gain potentiometer so that the output voltage after the operational amplifier is basically consistent (the voltage is determined by the ADC data converted by the MCU).

Software configuration

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PC0PC0ADC123_IN10 
STM32F103RCT6PC1PC1ADC123_IN11 
STM32F103RCT6PC2PC2ADC123_IN12 
STM32F103RCT6PC3PC3ADC123_IN13 
STM32F103RCT6PC4PC4ADC12_IN14 
STM32F103RCT6PC5PC5ADC12_IN15 
STM32F103RCT6PB0PB0ADC12_IN8/TIM3_CH3/TIM8_CH2NTIM1_CH2N

Software code

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

xxxxxxxxxx
Product supporting materials source code path: Attachment → Source code summary → 2.Extended_Course → 10.ElE

Control function

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

ele_Init

​x
void  ele_Init(void)
{    
    ADC_InitTypeDef ADC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | ELE_ADC_CLK , ENABLE );
    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //Set ADC division factor 6 72M/6=12, ADC maximum time cannot exceed 14M
    //Set analog channel input pins

    //Left ADC 10,11,12
    GPIO_InitStructure.GPIO_Pin = ELE_L1_Pin | ELE_L2_Pin |ELE_L3_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //Analog input pins If it is not 0 when it is floating, it can be changed to pull-down
    GPIO_Init(ELE_L1_Port, &GPIO_InitStructure); //All are GPIOC, just choose one

    //Middle ADC 13
    GPIO_InitStructure.GPIO_Pin = ELE_MID_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //Analog input pin If it is not 0 when it is floating, it can be changed to pull-down
    GPIO_Init(ELE_MID_Port, &GPIO_InitStructure);

    //Right ADC 14,15
    GPIO_InitStructure.GPIO_Pin = ELE_R1_Pin |ELE_R2_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //Analog input pin If it is not 0 when it is floating, it can be changed to pull-down
    GPIO_Init(ELE_R1_Port, &GPIO_InitStructure); //All are GPIOC, just choose one

    //ADC 8 on the right
    GPIO_InitStructure.GPIO_Pin = ELE_R3_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //Analog input pin is not 0 when it is floating, it can be changed to pull-down
    GPIO_Init(ELE_R3_Port, &GPIO_InitStructure);

    ADC_DeInit(ELE_ADC); //Reset ADC and reset all registers of peripheral ADC to default values
    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC working mode: ADC1 and ADC2 work in independent mode
    ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Analog-to-digital conversion works in single-channel mode
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Analog-to-digital conversion works in 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-aligned
    ADC_InitStructure.ADC_NbrOfChannel = 1; //Number of ADC channels for regular conversion in sequence
    ADC_Init(ELE_ADC, &ADC_InitStructure); //Initialize the registers of the peripheral ADCx according to the parameters specified in ADC_InitStruct
    ADC_Cmd(ELE_ADC, ENABLE);
    ADC_ResetCalibration(ELE_ADC); //Enable reset calibration
    while(ADC_GetResetCalibrationStatus(ELE_ADC)); //Wait for reset calibration to end
    ADC_StartCalibration(ELE_ADC); //Start AD calibration
    while(ADC_GetCalibrationStatus(ELE_ADC)); //Wait for calibration to end
}

Get_Adc_ele

xxxxxxxxxx
u16 Get_Adc_ele(u8 ch)   
{
    //Set the specified ADC rule group channel, a sequence, sampling time
    ADC_RegularChannelConfig(ELE_ADC, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1, ADC channel, sampling time is 239.5 cycles
    ADC_SoftwareStartConvCmd(ELE_ADC, ENABLE); //Enable the software conversion start function of the specified ADC1
    while(!ADC_GetFlagStatus(ELE_ADC, ADC_FLAG_EOC ));//Wait for the conversion to end
    return ADC_GetConversionValue(ELE_ADC); //Return the most recent conversion result of the ADC1 rule group
}

guiyi_way

xxxxxxxxxx
int guiyi_way(void)
{
    int sum , Sensor;
    int Sensor_Left,Sensor_Right;

    // Normalized processing
    sum=(Sensor_Left_1*1+Sensor_Left_3*100)
    + Sensor_Middle *200
    +(Sensor_Right_1*300+Sensor_Right_3*399);
    Sensor_Left =  Sensor_Left_1+Sensor_Left_3;
    Sensor_Right = Sensor_Right_1+Sensor_Right_3;

    //  sum=(Sensor_Left_3*1)
    //          + Sensor_Middle *100
    //          +(Sensor_Right_1*199);

    Sensor_Left =  Sensor_Left_3+ Sensor_Left_1 + Sensor_Left_2;
    Sensor_Right = Sensor_Right_1+ Sensor_Right_3 + Sensor_Right_2;

    Sensor=sum/(Sensor_Left+Sensor_Middle+Sensor_Right);    // Find the deviation
    return Sensor; // Return the current position in the magnetic field
}

getEleData

xxxxxxxxxx
void getEleData(void)
{
    // With the rear of the car facing you, count from left to right
    Sensor_Left_1=Get_Adc_ele(ELE_L1_CH)>>4;                 // Collect the data of the left inductor
    Sensor_Left_2=Get_Adc_ele(ELE_L2_CH)>>4;
    Sensor_Left_3=Get_Adc_ele(ELE_L3_CH)>>4;                 // Collect the data of the left inductor

    Sensor_Middle=Get_Adc_ele(ELE_M1_CH)>>4;               // Collect intermediate inductance data

    Sensor_Right_1=Get_Adc_ele(ELE_R1_CH)>>4;                // Collect the data of the right inductor
    Sensor_Right_2=Get_Adc_ele(ELE_R2_CH)>>4;
    Sensor_Right_3=Get_Adc_ele(ELE_R3_CH)>>4;                // Collect the data of the right inductor

    ele_seat = guiyi_way();
}

Experimental phenomenon

The ELE.hex file generated by the project compilation is located in the OBJ folder of the ELE project. Find the ELE.hex file corresponding to the project and use the FlyMcu software to download the program to the development board.

After the program is successfully downloaded: OLED and serial port display the data output by the electromagnetic patrol module.

xxxxxxxxxx
OLED displays L3 L2 L1 R1 R2 R3 sensor data
Serial port displays threshold and intermediate sensor data

image-20240827185737047

CCD module-Reading data (ADC)
K210 module-Serial communication (USART)

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