>Use Cases>
/N32G031_SERVO_SMOOTH

Sweep a servo smoothly

N32G031_SERVO_SMOOTH

Introduction

An introductory project designed to teach servo motor control using the N32G031 microcontroller, with a specific focus on achieving seamless, jerk-free movement (Smooth Sweep). This project serves as an excellent foundational learning tool for robotics and for loop applications tailored for kids and beginners. This project is fully optimized for cross-platform workflows using UnityMbed.

Target MCUNations N32G031 (Cortex-M0)
Active Signal PinPA1 (Bit-Banged PWM)
Motion ProfileSmooth Sweep (0°–90°)

Video Demonstration

Wiring & Hardware Connection

DevicePinN32G031 Board PinNotes & Safety
Servo Motor (e.g., SG90)Brown / BlackGNDCommon System Ground
Servo Motor (e.g., SG90)Red (VCC)5VMotor Power Supply
Servo Motor (e.g., SG90)Orange / YellowPA1Control Signal Pin (PWM Bit-Banging)

Behaviour & Execution Logic

Once powered on, the robotic arm will smoothly sweep through the following loop continuously:

Phase 1 : 0° ➔ 45°

Smooth Sweep 0° to 45°

Smoothly sweeps from 0 to 45 Degrees (PULSE_0_DEG ➔ PULSE_45_DEG), then holds position.

Phase 2 : 45° ➔ 90°

Smooth Sweep 45° to 90°

Smoothly sweeps from 45 to 90 Degrees (PULSE_45_DEG ➔ PULSE_90_DEG), then holds position.

Phase 3 : 90° ➔ 45°

Smooth Sweep 90° to 45°

Smoothly sweeps back from 90 to 45 Degrees (PULSE_90_DEG ➔ PULSE_45_DEG), then holds position.

Phase 4 : 45° ➔ 0°

Smooth Sweep 45° to 0°

Smoothly sweeps back from 45 to 0 Degrees (PULSE_45_DEG ➔ PULSE_0_DEG), holds position, then restarts cycle.

main.c
// UnityMbed — Smooth Servo Sweep Control @ N32G031
#include "n32g031.h"
#include "n32g031_gpio.h"
#include "n32g031_rcc.h"

/* ====================================================================
 * [Hardware Calibration Zone]
 * Pulse width parameters for calibrated angular positions
 * ==================================================================== */
#define PULSE_0_DEG     2250   // 0 degrees (Far Left)
#define PULSE_45_DEG    3325   // 45 degrees (Mid-Left)
#define PULSE_90_DEG    4400   // 90 degrees (Perpendicular / Center)

/* Simple software delay loop */
void Delay_Loop(volatile uint32_t count) {
  while(count--) { __NOP(); }
}

/* ====================================================================
 * Sub-routine: Single-step pulse delivery (PWM bit-banging)
 * ==================================================================== */
void Servo_Step(uint32_t pulse_width) {
  GPIO_SetBits(GPIOA, GPIO_PIN_1);
  Delay_Loop(pulse_width);
  GPIO_ResetBits(GPIOA, GPIO_PIN_1);
  Delay_Loop(100000);
}

/* ====================================================================
 * Core Motion Routine: Smooth sweep transition between pulse angles
 * ==================================================================== */
void Smooth_Move(uint32_t start_pulse, uint32_t end_pulse) {
  // If target angle is greater, increment pulse width smoothly
  if (start_pulse < end_pulse) {
      for (uint32_t p = start_pulse; p <= end_pulse; p += 20) {
          Servo_Step(p);
      }
  }
  // If target angle is lower, decrement pulse width smoothly
  else {
      for (uint32_t p = start_pulse; p >= end_pulse; p -= 20) {
          Servo_Step(p);
      }
  }
}

/* ====================================================================
 * Main Entry Point: Periodic Smooth Sweep Loop
 * ==================================================================== */
int main(void) {
  /* Initialize GPIOA Peripheral Clock and Pin 1 as Push-Pull Output */
  RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  GPIO_InitType GPIO_InitStructure;
  GPIO_InitStruct(&GPIO_InitStructure);
  GPIO_InitStructure.Pin = GPIO_PIN_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);

  while(1) {
      /* 1. Smoothly sweep arm from 0 to 45 degrees */
      Smooth_Move(PULSE_0_DEG, PULSE_45_DEG);
      Delay_Loop(5000000); // Hold position

      /* 2. Smoothly sweep arm from 45 to 90 degrees */
      Smooth_Move(PULSE_45_DEG, PULSE_90_DEG);
      Delay_Loop(5000000);

      /* 3. Smoothly sweep arm back from 90 to 45 degrees */
      Smooth_Move(PULSE_90_DEG, PULSE_45_DEG);
      Delay_Loop(5000000);

      /* 4. Smoothly sweep arm back from 45 to 0 degrees to restart cycle */
      Smooth_Move(PULSE_45_DEG, PULSE_0_DEG);
      Delay_Loop(5000000);
  }
}

Learning & AI Extension Ideas

Students can use the built-in AI Assistant in UnityMbed IDE to explore, debug, and modify this project:

1

Smooth Movement

Understand the difference between making a robot "teleport" instantly versus "taking incremental steps" using the Smooth_Move function to create life-like, fluid animations.

2

Hardware Calibration

Gain hands-on experience tuning real hardware parameters (Pulse Width) to accurately calibrate 0, 90, and 180-degree angles.

3

For Loop Application

Visualize how a for loop works by watching the robot's arm move sequentially as numerical values increment or decrement.

4

Change the sweep step size

Encourage students to modify the step increments inside the Smooth_Move function (p += 20 vs p += 50 or p += 5) to observe speed changes and slow-motion effects.

Build and Flash Guide

1

Open Project: Open this project folder directly in the IDE.

2

Build & Flash: Simply click the Build and Flash buttons on the interface.