Objective: Designed and implemented a closed-loop motor control system using a PIC32 microcontroller, with a Python interface for real-time interaction and data visualization.
Requirements: Develop current and position control using nested PID loops, communicate with a brushed DC motor via UART, and achieve reliable motion control with tunable gains and trajectory tracking.
Top Skills: embedded C (PIC32), Python UI, control systems (PID), UART communication, real-time motor feedback, system integration
This three-week Intro to Mechatronics project focused on creating a closed-loop motor control system from the ground up. I programmed the PIC32 microcontroller in C to control a brushed DC motor through a DRV8835 H-bridge, with sensor feedback from an encoder and a MAX9918 current amplifier. A Python client communicated with the board via UART, allowing real-time interaction, gain tuning, and trajectory execution. This project gave me hands-on experience with low-level embedded systems, real-time control, and hardware-software integration.
Microcontroller: PIC32MX170F256B via NU32 board
Motor: Brushed DC motor with quadrature encoder
Current Sensing: MAX9918 amplifier with 15 mΩ shunt resistor
Actuation: DRV8835 H-bridge circuit controlled via PWM
Control Loops:
Inner current loop (PI) @ 5 kHz
Outer position loop (PID) @ 200 Hz
Communication: UART over USB to Python interface
Power: 6V battery pack
System Components
Simplified System Layout Sketch
✅ Encoder interface for count and angle (degrees) with reset option
✅ Current sensor reading in both ADC counts and calibrated mA
✅ PWM control with user-specified duty cycle
✅ Set/Get PID gains for both current and position control
✅ Interactive UI (Python) with a menu of 20+ real-time commands
✅ Current loop test (ITEST) with error calculation and plotting
✅ Angle hold mode for precise position control
✅ Trajectory loading (step or cubic) using genRef()
✅ Trajectory execution and plotting of actual vs. reference angles
Python UI in Windows PowerShell
Position Control Demonstration (90°)
Current Control ITEST Example:
±200 mA square wave reference
Achieved avg. error: ~32 mA with tuned PI gains
Step Trajectory Tracking:
0° → 180° → −90° → 0° → 45°
Held within ±5° of target
Cubic Trajectory:
Smooth reference transitions
Minimal overshoot with PD tuning
The videos for these demonstrations are shown below:
Step Trajectory Tracking Accuracy Example
Step Trajectory Tracking Example Video
Cubic Trajectory Tracking Accuracy Example
Cubic Trajectory Tracking Example Video
This was my first time building a fully functional embedded motor control system with real-time feedback. I gained hands-on experience writing ISR-based control loops, developing UART communication protocols, and integrating hardware with software. Tuning nested PID loops gave me a deeper understanding of control dynamics, and writing modular C code for embedded systems taught me good design practices for scalability and debugging.