Share this article:
Embedded Computing and Mechatronics with the PIC32 Microcontroller
As my course on embedded systems and mechatronics at Northwestern University, ME 333, evolved over 15 years, four points became increasingly clear to me:
(1) Every student of mechatronics should have a professional foundation in microcontrollers. Given the importance of microcontrollers in modern embedded systems, students in a first mechatronics course should not be given a path where they can rely on teammates’ programming skills.
(2) Traditional labs, at scheduled times and scheduled places, are passé. Since every student has a laptop, and since inexpensive portable instrumentation is now available, students can complete labs and projects on their own schedule in their dorm rooms, at coffee shops, in makerspaces, etc. This is more convenient for the students, and reduces the demand for teaching lab time and space for the university.
(3) Traditional lectures are on their way out, and they are being replaced by online lectures and better use of class time, i.e., “flipped” classes. With high-quality video content increasingly available online, students can watch the bulk of the lecture material in advance of class, allowing more meaningful interaction with the professor during class. For example, students can use their portable equipment to apply the lecture material to a project, with the professor available if they realize they didn’t understand something. In other words, students’ brains are fully turned “on” while the professor is around, unlike a traditional lecture class.
(4) In light of points (1)-(3), there was no suitable mechatronics textbook on the market. Mechatronics is inherently an integrative field, involving microcontrollers, programming, circuit analysis and design, mechanisms, system dynamics, sensors, actuators, and real-time control. The result is that many mechatronics textbooks are bloated and expensive, attempting to cover material that is usually covered in other engineering courses, or too broad and shallow. A more focused treatment, sensitive to the points above, was needed.
In response to points (1)-(4), my Northwestern colleagues Nick Marchuk (Lecturer in Mechatronics) and Matt Elwin (PhD student) and I developed a new textbook, Embedded Computing and Mechatronics with the PIC32 Microcontroller, and the surrounding ecosystem consisting of:
- Over 80 freely available YouTube videos covering much of the material in the book. The videos average approximately five minutes each, as studies have shown that shorter videos are more engaging than traditional long lectures. These videos take advantage of innovative educational technology developed at Northwestern, called the Lightboard. Students watch the online lectures in advance of class, allowing classroom time to be used for Q&A, projects, or other student-faculty interaction while the students are fully engaged.
A YouTube video lecture captured with the Lightboard
- An inexpensive portable function generator and oscilloscope called the nScope. The nScope works with the student’s laptop, and it replaces expensive stationary benchtop equipment. The nScope is appropriate for generation and analysis of signals up to hundreds of kHz, more than adequate for most mechatronics applications. This inexpensive portable equipment allows mechatronics to be taught without a fixed lab infrastructure.
- An inexpensive PIC32 microcontroller board called the NU32. The board acts like a 60-pin DIP chip and plugs into a standard breadboard, allowing easy prototyping of interface circuits.
Information on the book (including free sample chapters), extensive sample code, videos, the nScope, the NU32, and the entire mechatronics ecosystem can be found at http://nu32.org. In the rest of this article, I will focus on the philosophy of the book and its contents.
The book was written as a textbook for ME 333 and other courses in mechatronics, as well as for practicing embedded systems engineers. The book has two main focuses, corresponding to points (1) and (4) above: the Microchip PIC32 microcontroller and mechatronics.
The PIC32 Section
The PIC32 microcontroller is covered in chapters 1-20. The book adopts C as the programming language, and Appendix A provides an extensive practical introduction to programming in C. The choice of C strikes a balance: C is processor-independent, unlike assembly, but it is still relatively low-level with minimal abstractions, unlike C++. The low-level nature of C keeps the programmer close to the lowest-level assembly code, and the book accustoms the reader to examining the assembly code produced by the C compiler. The low-level programming model allows the programmer to better understand the connection between their code and the hardware, and to get the most out of the microcontroller.
The PIC32 was chosen because it is an inexpensive, powerful, modern, 32-bit architecture with good market penetration from a leading manufacturer of microcontrollers. Although Arduinos are a popular choice for beginners, we avoided them because Arduino software abstractions prevent the beginner from gaining a deep understanding of how their software connects to the hardware. Since many students in ME 333 go on to pursue further courses in mechatronics, and MS and PhD degrees in robotics, we decided a more professional introduction to microcontrollers was needed.
The PIC32 section is structured in three parts: a Quickstart, which allows the student to get their first programs running quickly; a Fundamentals part, which teaches the student about the PIC32MX’s MIPS32 M4K processor, special function registers, interrupts, the C compilation and build process, and the Microchip software distribution; and a Peripheral Reference part, which provides extensive freely downloadable sample code and clear explanations of the PIC32’s many peripherals, including digital I/O, counter-timers, pulse-width modulation, analog input, UART, SPI, I^2C, parallel master port, input capture, watchdog timer, CAN, and USB.
As one veteran PIC32 user and Microchip forum poster wrote about the book, “I highly recommend it. The section on peripherals is especially good. I thought I knew the PIC32 peripherals inside and out, but I learned a few new things from this book.”
The guiding philosophy of the PIC32 section is “no magic steps.” Too often students are given sample code and are told to modify it as necessary (e.g., using the MPLAB X Integrated Development Environment). Similar to using abstract Arduino-like libraries, this approach encourages the student to simply copy and modify code without truly understanding how it connects to the hardware. This is bad pedagogically, and leaves the student with little recourse if their code does not work. Until the student has a solid understanding of the process that translates the software they write to hardware actions, the use of pre-written software libraries should be minimized.
To provide the clearest introduction to the PIC32 of any book available, the Fundamentals part brings together key material from the PIC32 Reference Manual, the PIC32MX5xx/6xx/7xx Data Sheet, and the XC32 C/C++ Compiler User’s Guide, and organizes it in a logical linear fashion. In keeping with the “no magic steps” philosophy, the book encourages the use of a text editor to write programs, and “make” at the command-line to compile and load programs. The Makefile provided with the sample code clearly demonstrates how an executable is built from the source text files. This is in contrast to using the MPLAB X IDE, whose role in the build process can be mysterious to the beginner.
Once the student has mastered the material in the Fundamentals, MPLAB X and Microchip documentation is no longer mysterious. The Peripheral Reference also introduces the now-prepared student to MPLAB Harmony, Microchip’s latest software framework and library.
The Mechatronics Section
Mechatronics is covered in chapters 21-29, and Appendix B provides a brief review of circuit analysis that is considered a prerequisite for the book (linear RCL circuits, diodes, transistors, and op amps). No other prerequisites are assumed, other than freshman-level physics and math.
Instead of attempting to cover in detail topics such as circuit analysis, mechanisms, system dynamics, and control theory, which are covered in other books and university courses, the mechatronics section of the book focuses on topics that usually do not arise in other courses: common sensors in mechatronic systems, actuators, and their interfacing to a microcontroller. This section of the book also provides a practical, non-theoretical introduction to digital signal processing and real-time feedback control, as they play a crucial role in interfacing sensors and actuators to a microcontroller.
Coverage on actuators begins with a chapter focusing on the physics, governing equations, and data sheets of brushed permanent magnet DC motors. This is followed by a chapter on gearing and motor sizing, teaching the student how to choose an appropriate motor and transmission for their application. A chapter on motor control shows how feedback control theory, coupled with a motor encoder and an H-bridge circuit, allow a microcontroller to control a brushed motor. With the detailed example of the physics and control of a brushed motor worked out, another chapter efficiently covers other electric actuators, such as brushless DC motors, solenoids, voice coil actuators, RC servos, and stepper motors.
The book puts the student’s new knowledge to practice by extensive exercises at the end of each chapter, including actual experiments. For example, Exercises 3 and 4 of Chapter 25 have the student experimentally derive the data sheet of a mystery motor, such as one purchased on a surplus site, using nothing more than a multimeter, oscilloscope, and battery or power supply.
The Mechatronics section also leads the reader through two chapter-long projects that tie the mechatronics material to the PIC32 material. In the final project, students implement the electronics and software for a professional nested-loop brushed DC motor control system, where the outer-loop motion controller, running at hundreds of Hz, uses a motor reference trajectory and encoder feedback to generate a commanded torque (equivalently, motor current), and the inner-loop current controller, running at 5 kHz, controls the PWM signal to an H-bridge to achieve the commanded motor current. Sensors for the project include a motor current sensor and a motor encoder, and the software uses multiple interrupts and peripherals such as analog input, digital I/O, timers, pulse-width modulation, and SPI and UART communication, with optional extensions saving data to flash memory and using the parallel master port to drive an LCD display.
Students also create a MATLAB interface to the PIC32 via communication over a USB cable. The interface provides options that allow the user to set feedback gains, test the current-control loop in isolation, specify a motor trajectory, and plot the results of the controller’s trajectory-tracking performance. Knowing that commercial amplifiers with similar capabilities cost hundreds of dollars is good motivation for the students!
Students develop their PIC32 software from scratch based on concepts learned in the PIC32 section of the book.
About the Author
Kevin Lynch is Professor and Chair of the Department of Mechanical Engineering at Northwestern University, where he has been teaching robotics and mechatronics since 1997. He received his BSE in Electrical Engineering from Princeton University and his PhD in Robotics from Carnegie Mellon University. He has been awarded Northwestern’s highest teaching awards, including the Engineering Teacher of the Year Award and the Charles Deering McCormick Professorship of Teaching Excellence. He publishes and lectures widely on his research in robotics. He is a Fellow of the IEEE.
Want to read more? Save up to 25% on your own copy when you order it via the Elsevier store. Enter code STC215 at the checkout!
Electronics & Electrical Engineering
Electronics and electrical engineering have practically limitless applications. From power engineering, telecommunications, and consumer electronics to circuit design, computer engineering, and embedded systems, these disciplines form the backbone of our increasingly tech-dependent world. Elsevier’s collection of electronics and electrical engineering content — particularly our Newnes and Academic Press Imprints — encompasses these areas and more. Our books and journals provide fundamental knowledge and practical, up-to-date toolkits for professional engineers and technicians, undergraduate and postgraduate students, and electronics enthusiasts.