Single Servo Control

Outline

  • Servos will control the joints of the robotic arm. In this post, we’ll look at controlling a single servo using a HAT (hardware attached on top).

Preview

Materials

  • servo HAT
  • HAT power source
  • soldering kit
  • small test servos
  • total cost: $57 + cost of previous materials
    • cost so far: $147 + cost of common materials

Background

How do servos work?

Why do we need the servo HAT?

  • The pi has the ability to send PWM signals, but there are two problems
    1. servos draw enough power that operating them can interfere with the pi operations
    2. the pi only has a single PWM pin, which means it can only control one servo
  • The servo HAT allows for controlling many servos at once, and also uses an external power supply to avoid interfering with the pi

Walkthrough

Servo HAT hardware setup

  • Solder the pins into the servo HAT then attach the HAT to the pi as in the following picture A connected HAT
  • Attach the servo to the first set of 3 pins
  • Power on the pi and servo HAT

Servo HAT software setup

  • Download the software library that comes with the servo HAT as described in this tutorial
  • Add the downloaded library to PYTHONPATH in your raspberry pi bash_profile
    • export PYTHONPATH="/home/pi/software/Adafruit-Raspberry-Pi-Python-Code/Adafruit_PWM_Servo_Driver:$PYTHONPATH"
      • where software is the directory where I downloaded the software
  • Since these programs are run with the super user, and running with super user erases the PYTHONPATH by default, we need to explicitly keep the PYTHONPATH when running as super user
    • sudo visudo -f /etc/sudoers.d/custom
      • this creates a custom file rather than directly editing sudoers
      • by default this uses nano to edit the file
    • add Defaults env_keep += "PYTHONPATH" to the file and exit

Running the tutorial demo

  • Download the additional code used to control the servo
    • this is in the same github repo as the code from the previous tutorial
    • it contains a utility function that allows for controlling the servo based on the desired angle of the servo arm rather than with pulse widths
  • Run the tutorial demo
    • sudo python 2_single_servo.py

Result

Blake Wulfe
Blake Wulfe
Research Engineer