Design of a Baby Height and Weight Measurement Device Based on STM32 Microcontroller

1. Overall System Design

This design uses the STM32F103C8T6 microcontroller as the control core, integrating weight measurement module, height measurement module, motor drive module, display module, and power supply module to achieve automated measurement of a baby’s height and weight. The system employs a resistive strain gauge pressure sensor combined with the HX711 A/D converter for weight detection, utilizes the HC-SR04 ultrasonic sensor for non-contact height measurement, and employs the L298N driver module to control a stepper motor for automatic positioning of the measurement slider, ultimately displaying the measurement results in real-time on an LCD1602 display.

2. Hardware System Design

2.1 Core Control Module

The STM32F103C8T6 is selected as the main control chip, which is based on the ARM Cortex-M3 core, operates at a frequency of 72MHz, and has rich peripheral resources and strong data processing capabilities, meeting the needs for multi-sensor data acquisition and motor control. The minimum system circuit includes a reset circuit, crystal oscillator circuit, and power filtering circuit to ensure stable operation of the microcontroller.

2.2 Weight Measurement Module

A resistive strain gauge weight sensor with a range of 50kg is used, featuring a 1000-ohm half-bridge strain gauge structure. The sensor’s output signal is processed by the HX711 24-bit high-precision A/D converter and connected to the microcontroller’s SPI interface. The HX711 integrates a voltage regulator and clock oscillator, characterized by strong anti-interference and fast response, capable of amplifying the sensor’s weak signal and converting it into a digital quantity. In the circuit design, an RC filter circuit (100nF capacitor in parallel with a 10k resistor) is added to the sensor’s power supply to effectively suppress high-frequency interference.

2.3 Height Measurement Module

The HC-SR04 ultrasonic sensor is used for non-contact measurement, operating at a voltage of 5V, with a measurement range of 2cm-400cm and an accuracy of ±0.5cm. The sensor is fixed above the measurement platform, and the microcontroller triggers the distance measurement via the PB5 pin (providing at least 10us high level), while the PB6 pin receives the echo signal. The measurement principle is: the speed of sound in air is 340m/s, and the formula distance(cm) = high level time(us)/58 is used to calculate the vertical distance from the baby’s head to the sensor, then the actual height is obtained by subtracting this distance from the preset sensor height.

2.4 Motor Drive Module

The L298N dual-channel motor driver module is selected, with a driving voltage range of 7-12V and a continuous output current of 2A. The module’s IN1 (PA5), IN2 (PA6) pins are connected to the microcontroller’s PWM output to control the stepper motor’s forward and reverse rotation; the ENA pin is connected to PA4 for speed adjustment. The circuit is powered by an external 9V power supply, and the onboard 78M05 voltage regulator chip provides 5V voltage for the logic circuit. The microcontroller and module GND must share a common ground to ensure consistent logic level reference. The 28BYJ-48 stepper motor is used, with a step angle of 5.625°/64, achieving precise angle control through the ULN2003 driver.

2.5 Display Module

The LCD1602 character LCD is connected in parallel to the microcontroller’s PC0-PC7 data ports, with PD0 (RS), PD1 (RW), PD2 (E) serving as control signals. The display can simultaneously show weight (in kg), height (in cm), and BMI index, meeting the need for quick readings.

2.6 Overall System Circuit Diagram

The system hardware structure is shown in Figure 1, mainly consisting of the STM32 minimum system, weight measurement circuit (sensor + HX711), height measurement circuit (HC-SR04), L298N motor drive circuit, and LCD display circuit. In the circuit design, each module is powered independently, with twisted pair wiring for analog signal paths, and digital ground and analog ground connected at a single point to effectively reduce electromagnetic interference.

For the circuit schematic of the baby height and weight measurement system, please follow my public account and send me a private message to receive it.

3. Software System Design

3.1 Main Program Flow

Upon powering on the system, initialization (GPIO, UART, I2C, timers, etc.) is performed first, followed by entering standby mode. When a baby placement signal is detected (weight sensor reading exceeds the threshold of 500g), the measurement process is initiated: first, the L298N drives the motor to reset the measurement slider to the initial position, then the ultrasonic sensor begins continuous distance measurement. The microcontroller controls the motor to lower the slider to the baby’s head position using a PID algorithm, while the weight module performs A/D sampling (10 times per second, averaging the values). After the measurement is complete, the motor resets, and the LCD displays the results.

3.2 Key Module Program Design

3.2.1 L298N Motor Drive Program

C

/** * @brief L298N motor driver initialization * @param None * @retval None */void L298N_Init(void)

{ GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; // Enable peripheral clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // PA5(IN1), PA6(IN2), PA4(ENA) push-pull output GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexed push-pull output GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // Timer 3 initialization (PWM output) TIM_TimeBaseStructure.TIM_Period = 1999; // Period 20ms TIM_TimeBaseStructure.TIM_Prescaler = 71; // Prescale 72, counting frequency 1MHz TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // PWM mode configuration TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; // Initial duty cycle 0 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM3, &TIM_OCInitStructure); // PA6(IN2) corresponds to TIM3_CH1 TIM_OC2Init(TIM3, &TIM_OCInitStructure); // PA5(IN1) corresponds to TIM3_CH2 TIM_Cmd(TIM3, ENABLE); // Enable timer}/** * @brief Control motor forward * @param speed: PWM duty cycle (0-2000) * @retval None */void Motor_Forward(uint16_t speed)

{ TIM_SetCompare2(TIM3, speed); // IN1=PA5=TIM3_CH2 TIM_SetCompare1(TIM3, 0); // IN2=PA6=TIM3_CH1}/** * @brief Control motor backward * @param speed: PWM duty cycle (0-2000) * @retval None */void Motor_Backward(uint16_t speed)

{ TIM_SetCompare2(TIM3, 0); TIM_SetCompare1(TIM3, speed);}/** * @brief Stop motor * @param None * @retval None */void Motor_Stop(void)

{ TIM_SetCompare2(TIM3, 0); TIM_SetCompare1(TIM3, 0);}

3.2.2 HC-SR04 Ultrasonic Distance Measurement Program

C

/** * @brief Ultrasonic module initialization * @param None * @retval None */void HC_SR04_Init(void)

{ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // PB5(TRIG) push-pull output, PB6(ECHO) floating input GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); // Initialize Timer 2 (for calculating echo time) TIM2_Init(0xFFFF, 71); // 1us counting}/** * @brief Get distance measurement value * @param None * @retval Distance(cm), -1 indicates measurement failure */float HC_SR04_GetDistance(void)

{ uint32_t t = 0; // Send 10us trigger pulse TRIG_HIGH; delay_us(15); TRIG_LOW; // Wait for echo signal while(ECHO_READ == 0); TIM_SetCounter(TIM2, 0); // Clear counter TIM_Cmd(TIM2, ENABLE); // Start timer // Wait for echo end while(ECHO_READ == 1)

{ if(TIM_GetCounter(TIM2) > 30000) { // Timeout (>30ms) TIM_Cmd(TIM2, DISABLE); return -1; } } TIM_Cmd(TIM2, DISABLE); t = TIM_GetCounter(TIM2); // Calculate distance: s = 340m/s * t/2 = 0.034cm/us * t/2 = t/58 return (float)t / 58.0;}

3.2.3 HX711 Weight Measurement Program

C

/** * @brief HX711 read AD value * @param None * @retval AD conversion result (24 bits) */uint32_t HX711_ReadData(void)

{ uint32_t data = 0; uint8_t i; HX711_SCK_LOW; delay_us(10); while(HX711_DOUT_READ); // Wait for data to be ready // Read 24-bit data for(i = 0; i < 24; i++)

{ HX711_SCK_HIGH; data <<= 1; HX711_SCK_LOW; if(HX711_DOUT_READ) data++; delay_us(1); } // Send a pulse to select gain (128) HX711_SCK_HIGH; delay_us(1); HX711_SCK_LOW; // Convert to signed number if(data & 0x800000)

{ data |= 0xFF000000; } return data;}/** * @brief Get weight measurement value * @param None * @retval Weight (kg) */float Get_Weight(void)

{ uint32_t sum = 0; uint8_t i; float weight; // Continuous sampling 10 times for averaging for(i = 0; i < 10; i++)

{ sum += HX711_ReadData(); delay_ms(10); } // Calculate weight (requires calibration coefficient) weight = (float)(sum / 10 – OFFSET) / SCALE; return weight > 0 ? weight : 0; // Negative weight is considered as 0}

3.3 Main Function Design

C

int main(void)

{ float height = 0, weight = 0, bmi = 0; uint8_t measure_flag = 0; // System initialization SystemInit(); delay_init(); LCD1602_Init(); HC_SR04_Init(); HX711_Init(); L298N_Init(); KEY_Init(); LCD1602_ShowString(0, 0, “Baby Measurement”); LCD1602_ShowString(1, 0, “Wait…”); while(1)

{ // Check if a baby is placed weight = Get_Weight(); if(weight > 0.5)

{

// Start measurement if weight exceeds 0.5kg if(!measure_flag)

{ measure_flag = 1; LCD1602_Clear(); LCD1602_ShowString(0, 0, “Measuring…”); // Drive motor to lower slider Motor_Forward(800); // 50% duty cycle delay_ms(500); // Continuous distance measurement 5 times for averaging height = 0; for(uint8_t i = 0; i < 5; i++)

{ height += HC_SR04_GetDistance(); delay_ms(50); } height = (30.0 – height/5) * 10; // 30cm is the sensor height // Motor reset Motor_Backward(800); delay_ms(500); Motor_Stop(); // Calculate BMI index (weight kg/height m²) bmi = weight / ((height/100) * (height/100)); // Display results LCD1602_Clear(); LCD1602_ShowString(0, 0, “W:”); LCD1602_ShowFloat(0, 2, weight, 1); LCD1602_ShowString(0, 6, “kg”); LCD1602_ShowString(0, 9, “H:”); LCD1602_ShowNum(0, 12, (uint16_t)height, 3); LCD1602_ShowString(0, 15, “cm”); LCD1602_ShowString(1, 0, “BMI:”); LCD1602_ShowNum(1, 4, (uint16_t)bmi, 2); LCD1602_ShowChar(1, 6, ‘.’); LCD1602_ShowNum(1, 7, (uint16_t)((bmi-(uint16_t)bmi)*10), 1); } }

else

{ measure_flag = 0; } delay_ms(100); }}

4. System Debugging and Performance Testing

4.1 Hardware Debugging

  • Power Debugging: Use a multimeter to measure the supply voltage of each module, ensuring that the STM32 core is at 3.3V, the sensor at 5V, and the motor drive at 9V, all functioning normally without short circuits.
  • Sensor Calibration: The weight module is calibrated using standard weights, adjusting the HX711 reference voltage to achieve a measurement accuracy of ±0.1kg; the ultrasonic module is calibrated at different distances using a tape measure, correcting the measurement formula coefficient.
  • Motor Control: Observe the PWM output waveform with an oscilloscope, adjusting the duty cycle to ensure smooth motor operation without jitter, controlling the slider positioning error within ±0.5cm.

4.2 Software Debugging

Online debugging is performed using the J-Link emulator with the MDK5 development environment, focusing on:

  • Timer interrupt response time, ensuring accurate capture of ultrasonic echo;
  • Stability of HX711 readings, reducing fluctuations through a sliding average filter algorithm;
  • Smoothness of motor movement trajectory, optimizing PID control parameters (P=800, I=50, D=20).

4.3 Performance Indicators

  • Measurement Range: Weight 0.5-50kg, Height 30-100cm
  • Measurement Accuracy: Weight ±0.1kg, Height ±0.5cm
  • Response Time: Time to complete a single measurement < 3 seconds
  • Operating Environment: Temperature 0-40℃, Humidity ≤85%RH
  • Power Supply Method: DC 12V/1A adapter or lithium battery pack (11.1V/2000mAh)

5. Conclusion and Outlook

This design integrates multi-sensor technology through the STM32 microcontroller, achieving automated, non-contact measurement of a baby’s height and weight, which offers advantages over traditional mechanical measurement methods, such as ease of operation, high accuracy, and good safety. The system’s hardware structure is compact, and the software algorithm is reasonably optimized, with potential for further expansion of Bluetooth data upload functionality, enabling historical data recording and growth curve plotting via a mobile app, meeting the multi-scenario application needs of families and medical institutions.

Leave a Comment