Firmware Basics (Servo Control)
Flash a first sketch: smooth joint moves, channel map, and serial commands.
Learning objectives
- Install Arduino IDE (or CLI) and required libraries
- Drive servos via PCA9685 or Servo.h
- Implement joint limits and eased motion
- Add a simple serial protocol for joint angles
Lesson
Install Adafruit PWM Servo Driver library if using PCA9685, or use Arduino Servo for direct pins. Create a Joint struct: channel, minUs, maxUs, minDeg, maxDeg, currentDeg.
Always clamp commanded angles to mechanical limits you measured. Move with interpolation (e.g., 1° steps with a short delay, or time-based easing) instead of teleporting from 0° to 180° — teleports slam gears and yank the PSU.
Example serial protocol (simple and extensible):
J,<id>,<deg>\n → move joint
G,<percent>\n → gripper
H\n → go home
Echo acknowledgements so a host PC or Python script can synchronize.
Keep a HOME[] array matching the mechanical home photograph from Chapter 3.
Components for this chapter
Arduino Uno / Nano / compatible MCU
×1ElectronicsRuns control firmware
USB cable for MCU programming
×1ElectronicsUpload sketches
| Component | Qty | Category | Purpose | Notes |
|---|---|---|---|---|
| Arduino Uno / Nano / compatible MCU | 1 | Electronics | Runs control firmware | — |
| USB cable for MCU programming | 1 | Electronics | Upload sketches | — |
Step-by-step
- 1
Install toolchain and libraries
Install Arduino IDE 2.x (or arduino-cli). Add Adafruit PWM Servo Driver (+ Adafruit BusIO) via Library Manager if using PCA9685. Select the correct board and port.
- 2
Bring up a single channel sweep
Write a sketch that slowly sweeps CH0 between safe pulse widths. Confirm the base joint moves as expected. Power servo rail before expecting motion.
- 3
Encode the joint map and limits
Create arrays for channel numbers and min/max degrees per joint. Upload and verify each joint one at a time with the arm supported.
- 4
Add eased multi-joint move and home
Implement a function `moveJoint(id, target, durationMs)` that interpolates. Implement `goHome()`. Test with small displacements first.
- 5
Expose serial commands
Parse lines from Serial at 115200 baud. Test from the Serial Monitor, then optionally from a short Python `pyserial` script on your PC.
Tips, safety & pitfalls
Finished this chapter?
Sign in to save progress to your account and pick up on any device.