Home Blog Blog Details

Make a desktop pet puppy based on STM32

March 18 2025
Ampheo 13

Inquiry

Global electronic component supplier AMPHEO PTY LTD: Rich inventory for one-stop shopping. Inquire easily, and receive fast, customized solutions and quotes.

QUICK RFQ
ADD TO RFQ LIST
Creating a desktop pet puppy based on an STM32 microcontroller is a fun and educational project! Below is a step-by-step guide to help you build a simple interactive desktop pet puppy using an STM32 development board, sensors, and actuators.

Creating a desktop pet puppy based on an STM32 microcontroller is a fun and educational project! Below is a step-by-step guide to help you build a simple interactive desktop pet puppy using an STM32 development board, sensors, and actuators.


Components Required

  1. STM32 Development Board (e.g., STM32F103C8T6, STM32F4 Discovery, or any STM32 board with GPIO, PWM, and ADC capabilities).

  2. Servo Motor (for moving the puppy's head or tail).

  3. LEDs (for eyes or other visual feedback).

  4. Buzzer (for sound effects like barking).

  5. Ultrasonic Sensor (to detect proximity or interaction).

  6. Push Buttons (for user input).

  7. Resistors, Wires, and Breadboard.

  8. Power Supply (e.g., USB or battery).

  9. 3D-Printed or Crafted Puppy Body (optional, for aesthetics).


Steps to Build the Desktop Pet Puppy

1. Hardware Setup

  • Servo Motor: Connect the servo motor to the STM32 PWM pin (e.g., TIM2_CH1) to control the puppy's head or tail movement.

  • LEDs: Connect LEDs to GPIO pins (with current-limiting resistors) for the puppy's eyes or other visual feedback.

  • Buzzer: Connect the buzzer to a GPIO pin for sound effects.

  • Ultrasonic Sensor: Connect the ultrasonic sensor (e.g., HC-SR04) to GPIO pins for distance measurement (trigger and echo pins).

  • Push Buttons: Connect push buttons to GPIO pins for user interaction (e.g., feeding or petting the puppy).

2. Software Development

Use STM32CubeIDE or any compatible IDE to write and upload the code to the STM32 board.

  • Initialize Peripherals:

    • Configure GPIO pins for LEDs, buttons, and buzzer.

    • Set up PWM for the servo motor.

    • Configure ADC if using analog sensors.

    • Initialize timers for precise control.

  • Main Logic:

    • Use the ultrasonic sensor to detect when a user is nearby. If the distance is below a threshold, activate the puppy (e.g., move the head or tail).

    • Use push buttons to trigger specific actions (e.g., feeding the puppy or making it bark).

    • Use LEDs to indicate the puppy's mood (e.g., happy, sad, or hungry).

    • Use the buzzer to play sound effects (e.g., barking, whining).

  • Example Code Snippet:

c
 
#include "stm32f1xx_hal.h"

// Define pins for servo, LEDs, buzzer, and ultrasonic sensor
#define SERVO_PIN GPIO_PIN_0
#define LED_PIN GPIO_PIN_1
#define BUZZER_PIN GPIO_PIN_2
#define TRIGGER_PIN GPIO_PIN_3
#define ECHO_PIN GPIO_PIN_4

void Servo_Move(uint16_t angle) {
    // Convert angle to PWM duty cycle and set PWM
    uint16_t pulse_width = (angle * 10) + 500; // Example calculation
    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pulse_width);
}

void Bark() {
    HAL_GPIO_WritePin(BUZZER_PIN, GPIO_PIN_SET);
    HAL_Delay(200);
    HAL_GPIO_WritePin(BUZZER_PIN, GPIO_PIN_RESET);
}

uint16_t Read_Ultrasonic() {
    // Send trigger pulse
    HAL_GPIO_WritePin(TRIGGER_PIN, GPIO_PIN_SET);
    HAL_Delay(10);
    HAL_GPIO_WritePin(TRIGGER_PIN, GPIO_PIN_RESET);

    // Measure echo pulse width
    uint32_t start_time = HAL_GetTick();
    while (HAL_GPIO_ReadPin(ECHO_PIN) {}
    uint32_t end_time = HAL_GetTick();

    return (end_time - start_time) * 0.034 / 2; // Calculate distance in cm
}

int main(void) {
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_TIM2_Init();

    while (1) {
        uint16_t distance = Read_Ultrasonic();
        if (distance < 20) { // If user is within 20 cm
            Servo_Move(90); // Move servo to 90 degrees
            Bark(); // Make the puppy bark
            HAL_GPIO_TogglePin(LED_PIN); // Blink LED
        }
        HAL_Delay(100);
    }
}

3. Mechanical Assembly

  • Attach the servo motor to a 3D-printed or crafted puppy body to create movement (e.g., wagging tail or nodding head).

  • Place the ultrasonic sensor on the puppy's face to detect user interaction.

  • Position the LEDs as eyes or other visual indicators.

4. Testing and Calibration

  • Test each component individually (e.g., servo movement, ultrasonic sensor, buzzer).

  • Calibrate the ultrasonic sensor for accurate distance measurement.

  • Adjust the servo angles and delays for smooth movement.

5. Enhancements

  • Add more sensors (e.g., temperature sensor to detect if the puppy is "cold").

  • Implement a state machine to simulate different moods (e.g., happy, sad, hungry).

  • Use an OLED display to show the puppy's status or animations.


Conclusion

This project combines hardware and software to create an interactive desktop pet puppy. It’s a great way to learn about STM32 microcontrollers, sensors, and actuators while building something fun and engaging! Let me know if you need further assistance with any part of the project. 🐶

Ampheo