Accelerometer Phidget with CMU Graphics

Learn how to use the Accelerometer Phidget (or any Phidget with an accelerometer) with Desktop CMU Graphics!

Setup

Note: you can use any Phidget with an accelerometer for this project.

Accelerometer Phidget

Phidget cable

USB cable

Install CMU Graphics

In order to use CMU Graphics, you first have to install it. You do this in the same way you previously installed the Phidget22 library. Simply navigate to your package manager, search for cmu-graphics and press install!

Note: macOS users must install dependencies before moving forward. For more information, please visit this page.

Thonny

If you're using Thonny, select Tools > Manage Packages and search for cmu-graphics.

PyCharm

If you're using PyCharm, select File > Settings > Python Interpreter and use the + symbol to install cmu-graphics.

PyScripter

If you're using PyScripter, select Tools > Tools > Install Packages with pip and enter cmu-graphics.

Write Code (Python)

Copy the code below into a new Python project.

  
from cmu_graphics import *
from Phidget22.Devices.Accelerometer import *

app.background = gradient('white', 'aliceBlue')

# Create a circle
dot = Circle(200, 200, 20)

# Phidgets Code Start
def mapAcceleration(val):
    try:
        # for simple tilting, -1 to 1 is an appropriate range
        minA = -1
        maxA = 1
        output = ((val - minA) / (maxA - minA) * app.width)
        return output
    except:
        print("error")
        return dot.centerX

    # Create, Address, Subscribe to Events and Open

def onAccelerationChange(self, acceleration, timestamp):
    dot.centerX = mapAcceleration(acceleration[0])

accelerometer = Accelerometer()
accelerometer.setOnAccelerationChangeHandler(onAccelerationChange)
accelerometer.openWaitForAttachment(1000)
# Set data interval to minimum
accelerometer.setDataInterval(accelerometer.getMinDataInterval())
# Phidgets Code End

cmu_graphics.run()
  

Run Your Program

When you tilt your accelerometer, the dot will also move.

Code Review

When using the Accelerometer Phiget, it's important to set the data interval to the minimum as shown in the code above. This will ensure your sensor is as responsive as possible. In this program, you are controlling the position of the alien with the sensor, so you must map the accelerometer range to the window range. You can do this with the following formula:

Y = (X - A) / (B - A) * (D - C) + C

where:

  • A and B are the range of the Accelerometer when tilting (-1 to 1)
  • C and D are the range of values you want to map to (0 to WIDTH)
  • X is the current value that falls between A and B
  • Y is the value you are looking for that falls between C and D

This formula has been implemented in the mapAcceleration function in the code.

Practice

  1. Try commenting out the section of your code that sets the data interval. What is the result? The minimum data interval on the Accelerometer Phidget is 20ms meaning your program will get a new value 50 times per second. Try setting the data interval to something slightly higher, like 50, and see if the movement is smoother.
  
accelerometer.setDataInterval(50)
  

2. Try to identify when the Accelerometer Phidget is being shaken and place the dot in a random location when this occurs. Review the Dice Roll project for information.

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done