Introduction

Discord Build Status Code Style: Black

CircuitPython library for the PMW3360 motion sensor. Port of Arduino PMW3360 Module Library by SunjunKim.

Tested working on KB2040 (RP2040), Adafruit Feather M4 Express (SAMD51) and Adafruit ESP32-S3 Feather with 4MB Flash 2MB PSRAM (ESP32-S3).

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle or individual libraries can be installed using circup.

Installing to a Connected CircuitPython Device with Circup

Make sure that you have circup installed in your Python environment. Install it with the following command if necessary:

pip3 install circup

With circup installed and your CircuitPython device connected use the following command to install:

circup install pmw3360

Or the following command to update an existing version:

circup update

Usage Example

import PMW3360
import board
from digitalio import DigitalInOut, Direction

# board.SCK may be board.CLK depending on the board
# board.D10 is the cs pin
sensor = PMW3360.PMW3360(board.SCK, board.MOSI, board.MISO, board.D10)

# Any pin. Goes LOW if motion is detected. More reliable.
mt_pin = DigitalInOut(board.A0)
mt_pin.direction = Direction.INPUT

# Initalizes the sensor
if sensor.begin():
    print("sensor ready")
else:
    print("firmware upload failed")

# Setting and getting CPI values. Default is 800.
sensor.set_CPI(1200)
print(sensor.get_CPI())

while True:
    # Captures a snapshot
    data = sensor.read_burst()

    # uncomment if mt_pin isn't used
    # if data["is_on_surface"] == True and data["is_motion"] == True:
    if mt_pin.value == 0:
        print(data)

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Table of Contents

Simple test

Ensure your device works with this simple test.

examples/pmw3360_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Jerico Tenmatay
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import board
 7import PMW3360
 8from digitalio import DigitalInOut, Direction
 9
10# board.SCK may be board.CLK depending on the board
11# board.D10 is the cs pin
12sensor = PMW3360.PMW3360(board.SCK, board.MOSI, board.MISO, board.D10)
13
14# Any pin. Goes LOW if motion is detected. More reliable.
15mt_pin = DigitalInOut(board.A0)
16mt_pin.direction = Direction.INPUT
17
18# Initalizes the sensor
19if sensor.begin():
20    print("sensor ready")
21else:
22    print("firmware upload failed")
23
24# Setting and getting CPI values. Default is 800.
25sensor.set_CPI(1200)
26print(sensor.get_CPI())
27
28while True:
29    # Captures a snapshot
30    data = sensor.read_burst()
31
32    # uncomment if mt_pin isn't used
33    # if data["is_on_surface"] == True and data["is_motion"] == True:
34    if mt_pin.value == 0:
35        print(data)

Mouse test

HID mouse sample

Indices and tables