Python & CircuitPython

It's easy to use the DRV2605 controller with Python or CircuitPython, and the Adafruit CircuitPython DRV2605 module.  This module allows you to easily write Python code that controls the vibration of the motor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

CircuitPython Microcontroller Wiring

First wire up a DRV2605 to your board exactly as shown on the previous pages for Arduino using an I2C connection.  Here's an example of wiring a Feather M0 to the controller with I2C:

  • Board 3V to controller VIN
  • Board GND to controller GND
  • Board SCL to controller SCL
  • Board SDA to controller SDA
  • Controller Motor - to motor negative / blue wire.
  • Controller Motor + to motor positive / red wire.

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3V3 to sensor VIN
  • Pi GND to sensor GND
  • Pi SCL to sensor SCL
  • Pi SDA to sensor SDA

CircuitPython Installation of DRV2605 Library

You'll need to install the Adafruit CircuitPython DRV2605 library on your CircuitPython board.

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_drv2605.mpy
  • adafruit_bus_device

Before continuing make sure your board's lib folder or root filesystem has the adafruit_drv2605.mpy, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Python Installation of DRV2605 Library

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-drv2605

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

To demonstrate the usage of the board we'll initialize it and vibrate the motor using effects built-in to the DRV2605 chip.  First run the following code to import the necessary modules and initialize the I2C connection with the controller:

Download: file
import board
import busio
import adafruit_drv2605
i2c = busio.I2C(board.SCL, board.SDA)
drv = adafruit_drv2605.DRV2605(i2c)

Now you're ready to start vibrating the motor with different built-in effects.  First you need to choose an effect to play based on its ID number.  See table 11.2 in the datasheet for a list of all the effects, but for example a strong click with effect ID #1 would be:

Download: file
drv.set_waveform(1)

Simply call the set_waveform function and pass the effect ID number of the desired effect (a value from 1 to 123).  For reference here's a quick snapshot of the effects from table 11.2 of the datasheet:

Now to play the effect call the play function:

Download: file
drv.play()

You should feel the motor vibrate in a sharp click!  Every time you call the play function the motor will play back the sharp click effect.

Try selecting a different effect and playing it, like the strong buzz with effect ID 47:

Download: file
drv.set_waveform(47)
drv.play()

Try other effects to see which ones are interesting and useful in your project!

There are a few other handy features to be aware of with the DRV2605 module.  One is that you can stop playback of effects with the stop function:

Download: file
drv.stop()

You can also combine multiple effects to play back at once and create interesting compound effects.  Read the datasheet for more details but there are 7 slots which can each be independently loaded with an effect to play back when the play function is called.  Simply provide the slot number as an optional keyword argument to set_waveform (the default if not specified is slot 0). 

For example try running:

Download: file
drv.set_waveform(84)         # Effect 84 in slot 0
drv.set_waveform(1, slot=1)  # Effect 1 in slot 1
drv.set_waveform(0, slot=2)  # End effects after slot 1 (set slot 2 to 0)
drv.play()

This will build a compound effect from two waveforms:

  • Effect #84 (ramp up, medium smooth) in slot 0, the default slot.
  • Effect #1 (strong click) in slot 1

Notice how slot #2 is set to a value of zero.  This tells the chip to stop adding compound effects after slot 1.  You can add more effects in other slots up to a value of slot #6.

Finally it's uncommon but you might want to switch between using a linear resonance actuator motor or eccentric rotating mass motor.  The small flat pancake motors like sold in the Adafruit shop are ERM motors and the library defaults to their usage.  However you can call the use_LRM function to switch to configure the chip to use a LRM style motor:

Download: file
drv.use_LRM()

You can also switch back to using an ERM style motor (the default) with the use_ERM function:

Download: file
drv.use_ERM()

That's all there is to using the DRV2605 with CircuitPython!  Happy motoring!

Here's a complete example of using the board to play the 117 first effects for a half second each.  Save this as main.py on your board and watch the REPL output to see the effect ID printed as it plays on the motor.

Full Example Code

# Simple demo of the DRV2605 haptic feedback motor driver.
# Will play all 117 effects in order for about a half second each.
# Author: Tony DiCola
import time

import board
import busio

import adafruit_drv2605


# Initialize I2C bus and DRV2605 module.
i2c = busio.I2C(board.SCL, board.SDA)
drv = adafruit_drv2605.DRV2605(i2c)

# Main loop runs forever trying each effect (1-117).
# See table 11.2 in the datasheet for a list of all the effect names and IDs.
#   http://www.ti.com/lit/ds/symlink/drv2605.pdf
effect_id = 1
while True:
    print('Playing effect #{0}'.format(effect_id))
    drv.sequence[0] = adafruit_drv2605.Effect(effect_id)  # Set the effect on slot 0.
    # You can assign effects to up to 7 different slots to combine
    # them in interesting ways. Index the sequence property with a
    # slot number 0 to 6.
    # Optionally, you can assign a pause to a slot. E.g.
    # drv.sequence[1] = adafruit_drv2605.Pause(0.5)  # Pause for half a second
    drv.play()  # Play the effect.
    time.sleep(0.5)
    # Increment effect ID and wrap back around to 1.
    effect_id += 1
    if effect_id > 117:
        effect_id = 1
This guide was first published on Dec 17, 2014. It was last updated on Dec 17, 2014. This page (Python & CircuitPython) was last updated on Dec 12, 2018.