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:

Each effect needs to be loaded into one of eight slots in the Waveform Sequencer:

You access the Waveform Sequencer by using the sequence property and providing an index for the slot location. To specify the effect, use the special Effect class provided in the library, giving it the effect number.

For example, here is how to load Effect ID No. 1 "Strong Click - 100%" into slot 0 of the sequence:

Download: file
drv.sequence[0] = adafruit_drv2605.Effect(1)

To play the sequence of effects 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 sequence. Right now, that's just the click effected loaded into slot 0.

Try selecting a different effect and playing it, like the "Buzz 1 - 100%", a strong buzz, which is Effect ID 47:

Download: file
drv.sequence[0] = adafruit_drv2605.Effect(47)
drv.play()

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

You can combine multiple effects to play back at once and create interesting compound effects. When you call play, the sequence is played until it either reaches a 0 or plays through all the slots. There's also a special Pause class you can use to generate a pause. You specify the pause time in seconds and it has to occupy a slot.

Let's play the two effects from above, separated by a half second pause:

Download: file
drv.sequence[0] = adafruit_drv2605.Effect(1)
drv.sequence[1] = adafruit_drv2605.Pause(0.5)
drv.sequence[2] = adafruit_drv2605.Effect(47)
drv.sequence[3] = 0 # stops the sequence here
drv.play()

The call to play is non-blocking, that is, it returns immediately and the rest of the code after that keeps running. The DRV2605 is doing the work of playing back the loaded sequence. If you ever want to stop the current playback, use the stop function:

Download: file
drv.stop()

Changing Motor Types

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!

Full Example Code

Here's a complete example of using the board to play the 123 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.

# Simple demo of the DRV2605 haptic feedback motor driver.
# Will play all 123 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-123).
# 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)  # for 0.5 seconds
    drv.stop()       # and then stop (if it's still running)
    # Increment effect ID and wrap back around to 1.
    effect_id += 1
    if effect_id > 123:
        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 Aug 22, 2019.