The Adafruit CRICKIT library lets Python programs control and read components connected to an Adafruit CRICKIT HAT on a Raspberry Pi. It exposes the board’s onboard NeoPixel LED, capacitive touch inputs, DC motors, and servo connections as Python objects and attributes, so physical behavior can be driven directly from scripts or explored interactively in a REPL. section 2.2 section 2.7 section 2.1.2
Import the shared `crickit` object with `from adafruit_crickit import crickit`. Set output attributes to act on hardware: fill the onboard pixel with an RGB hexadecimal value, assign a DC motor’s `throttle`, or assign a servo’s `angle`. Read a touch sensor’s `value` attribute to obtain `True` when it detects touch and `False` otherwise. section 2.3 section 2.4 section 2.7 section 8.4.1
Common control patterns include:
- Set `crickit.onboard_pixel.brightness` and call `fill()` to display an RGB color; a dictionary can map readable names to hexadecimal values. section 2.3
- Poll `crickit.touch_1.value` in a loop, then choose output values with conditional expressions. section 2.4 section 2.6
- Set `crickit.dc_motor_1.throttle` to start or stop a DC motor, pairing motor state with LED feedback when useful. section 2.6
- Set `crickit.servo_1.angle` or `crickit.servo_2.angle` to position a servo; configure `actuation_range` when the physical travel differs from the default range. section 8.4.1
Touch-controlled motor with LED status
A short control loop can make touch immediately visible and physical. Read `touch_1.value`, set motor throttle to `1` when touched or `0` otherwise, and set the pixel red while the motor runs and blue while it stops. A 0.1-second sleep keeps the interaction responsive without continuously hammering the loop. This combines input polling, conditional expressions, LED output, motor output, and timing in one runnable behavior. section 2.6 section 2.7
The library hides the CRICKIT HAT’s I2C communication with its onboard microcontroller, allowing Python code to work through hardware-focused attributes instead of sending I2C commands directly. It fits into a stack where Python runs on Linux on the Raspberry Pi, while applications use the library to control robot hardware. section 2.3 section 3.3 section 1.3.5
Servo calibration for a pan-and-tilt camera
A servo can be tested from the REPL by assigning 0, 90, and 180 to `crickit.servo_1.angle`. If its measured mechanical travel is only 142 degrees, set `actuation_range = 142`; subsequent angle values then represent that real-world range. Requests beyond the configured range raise `ValueError`, preventing an invalid angle assignment. section 8.4.1
Library calls also scale from direct experiments to reusable robot functions. A snack-pushing routine uses `crickit.servo_2.angle` to raise and lower an arm after QR-code-guided movement locates a target, showing how a small hardware operation can be wrapped inside higher-level task logic. section 11.3.2
Listing 2.2 touch.py: Starting the motor when the touch sensor is pressed
#!/usr/bin/env python3
import time
from adafruit_crickit import crickit
RGB = dict(red=0xFF0000, green=0x00FF00, blue=0x0000FF)
POLL_DELAY = 0.1
crickit.onboard_pixel.brightness = 0.01
while True:
throttle = 1 if crickit.touch_1.value else 0
color = RGB['red'] if crickit.touch_1.value else RGB['blue']
crickit.onboard_pixel.fill(color)
crickit.dc_motor_1.throttle = throttle
time.sleep(POLL_DELAY)
Use the complete touch-control script to see imports, named color settings, polling delay, conditional output selection, and hardware assignments together. section 2.6
