SparkFun Auto pHAT Hookup Guide
Python Package Examples
Servo Controller
Documentation and examples for utilizing the servo controller is hosted on ReadtheDocs.
sparkfun-pi-servo-hat
Python package.
Pi Servo pHAT (v2) Hookup Guide
July 11, 2019
Basic Servo Control for Beginners
February 25, 2020
Setting Up the Pi Zero Wireless Pan-Tilt Camera
September 14, 2017
Demo Example
This example is pulled from the examples in the ReadtheDocs documentation. It should be used with a 90 degree (range of rotation) servo
on channel 0
of the Auto pHAT. It will swing the servo arm between 0° and 90°, in a loop.
language:python
#!/usr/bin/env python
#-----------------------------------------------------------------------
# Pi Servo Hat - Example 1
#-----------------------------------------------------------------------
#
# Written by SparkFun Electronics, June 2019
# Author: Wes Furuya
#
# Compatibility:
# * Original: https://www.sparkfun.com/products/14328
# * v2: https://www.sparkfun.com/products/15316
#
# Do you like this library? Help support SparkFun. Buy a board!
# For more information on Pi Servo Hat, check out the product page
# linked above.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http:www.gnu.org/licenses/>.
#
#=======================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#=======================================================================
"""
This example should be used with a 90 degree (range of rotation) servo
on channel 0 of the Pi Servo Hat.
The extended code (commented out), at the end of the example could be
used to test the full range of the servo motion. However, users should
be wary as they can damage their servo by giving it a position outside
the standard range of motion.
"""
import pi_servo_hat
import time
# Initialize Constructor
test = pi_servo_hat.PiServoHat()
# Restart Servo Hat (in case Hat is frozen/locked)
test.restart()
# Test Run
#########################################
# Moves servo position to 0 degrees (1ms), Channel 0
test.move_servo_position(0, 0)
# Pause 1 sec
time.sleep(1)
# Moves servo position to 90 degrees (2ms), Channel 0
test.move_servo_position(0, 90)
# Pause 1 sec
time.sleep(1)
# Sweep
#########################################
while True:
for i in range(0, 90):
print(i)
test.move_servo_position(0, i)
time.sleep(.001)
for i in range(90, 0, -1):
print(i)
test.move_servo_position(0, i)
time.sleep(.001)
#########################################
# Code below may damage servo, use with caution
# Test sweep for full range of servo (outside 0 to 90 degrees).
# while True:
# for i in range(-23, 100):
# print(i)
# test.move_servo_position(0, i)
# time.sleep(.001)
# for i in range(100, -23, -1):
# print(i)
# test.move_servo_position(0, i)
# time.sleep(.001)
Motor Driver
Documentation and examples for utilizing the motor driver is hosted on ReadtheDocs.
Demo Example
This example is pulled from the examples in the ReadtheDocs documentation. It should be used with a DC motor
on MTRA
of the Auto pHAT. It will increase and decrease the speed between the 20 and 254 levels, in a loop.
language:python
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# A simple test to speed up and slow down 1 motor.
#------------------------------------------------------------------------
#
# Written by Mark Lindemer
# SparkFun Electronics, April 2020
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 1
#
from __future__ import print_function
import time
import sys
import math
import qwiic_scmd
myMotor = qwiic_scmd.QwiicScmd()
def runExample():
print("Motor Test.")
R_MTR = 0
L_MTR = 1
FWD = 0
BWD = 1
if myMotor.connected == False:
print("Motor Driver not connected. Check connections.", \
file=sys.stderr)
return
myMotor.begin()
print("Motor initialized.")
time.sleep(.250)
# Zero Motor Speeds
myMotor.set_drive(0,0,0)
myMotor.set_drive(1,0,0)
myMotor.enable()
print("Motor enabled")
time.sleep(.250)
while True:
speed = 20
for speed in range(20,255):
print(speed)
myMotor.set_drive(R_MTR,FWD,speed)
time.sleep(.05)
for speed in range(254,20, -1):
print(speed)
myMotor.set_drive(R_MTR,FWD,speed)
time.sleep(.05)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("Ending example.")
myMotor.disable()
sys.exit(0)
Encoder Reader
Documentation and examples for utilizing the encoder reader is hosted on ReadtheDocs.
Demo Example
This example is pulled from the examples in the ReadtheDocs documentation. It should be used with quadrature encoders attached to both ENC1
and ENC2
on the Auto pHAT. It will read and print out the encoder position; the ticker counter range rolls up to 32767 ticks.
language:python
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# ex1_qwiic_dual_encoder_reader.py
#
# Simple Example demonstrating how to read encoder counts for the Qwiic Dual Encoder Reader (as part of the SparkFun Auto pHAT)
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 1
#
from __future__ import print_function
import qwiic_dual_encoder_reader
import time
import sys
def runExample():
print("\nSparkFun Qwiic Dual Encoder Reader Example 1\n")
myEncoders = qwiic_dual_encoder_reader.QwiicDualEncoderReader()
if myEncoders.connected == False:
print("The Qwiic Dual Encoder Reader device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
myEncoders.begin()
while True:
print("Count1: %d, Count2: %s" % (myEncoders.count1, \
myEncoders.count2, \
))
time.sleep(.3)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
9-DoF IMU
Documentation and an example for utilizing the IMU is hosted on ReadtheDocs.
Demo Example
This example is pulled from the examples in the ReadtheDocs documentation. It display the raw data in the following order: a_x
, a_y
, a_z
, g_x
, g_y
, g_z
, m_x
, m_y
, and m_z
.
language:python
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# ex1_qwiic_ICM20948.py
#
# Simple Example for the Qwiic ICM20948 Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, March 2020
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# More information on qwiic is at https://www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
# Example 1
#
from __future__ import print_function
import qwiic_icm20948
import time
import sys
def runExample():
print("\nSparkFun 9DoF ICM-20948 Sensor Example 1\n")
IMU = qwiic_icm20948.QwiicIcm20948()
if IMU.connected == False:
print("The Qwiic ICM20948 device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
IMU.begin()
while True:
if IMU.dataReady():
IMU.getAgmt() # read all axis and temp from sensor, note this also updates all instance variables
print(\
'{: 06d}'.format(IMU.axRaw)\
, '\t', '{: 06d}'.format(IMU.ayRaw)\
, '\t', '{: 06d}'.format(IMU.azRaw)\
, '\t', '{: 06d}'.format(IMU.gxRaw)\
, '\t', '{: 06d}'.format(IMU.gyRaw)\
, '\t', '{: 06d}'.format(IMU.gzRaw)\
, '\t', '{: 06d}'.format(IMU.mxRaw)\
, '\t', '{: 06d}'.format(IMU.myRaw)\
, '\t', '{: 06d}'.format(IMU.mzRaw)\
)
time.sleep(0.03)
else:
print("Waiting for data")
time.sleep(0.5)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)