Accelerometer Phidget

The Accelerometer Phidget contains a 3-axis accelerometer. The accelerometer measures acceleration in units of g-force. A stationary object experiences 1 g of acceleration due to the Earth's gravitational field. A high-speed roller coaster typically develops 4 g to 6 g.

See tutorial

Setup

Before you do any coding you will have to attach your Accelerometer Phidget to your VINT Hub as shown:

Code (Java)

Create a file called Accel and insert the following code. Run your code. Move your Accelerometer Phidget to see the output change.

Not your programming language? Set my language and IDE.

  
package accelerometer;

//Add Phidgets Library
import com.phidget22.*;

public class Accel{
    public static void main(String[] args) throws Exception {
        
        //Create
        Accelerometer accelerometer = new Accelerometer();
        
        //Open
        accelerometer.open(1000);
        
        //Use your Phidgets
        while (true) {
            System.out.println("Acceleration: x = " + accelerometer.getAcceleration()[0] + " g | y = " + accelerometer.getAcceleration()[1] + " g | z = " + accelerometer.getAcceleration()[2]+ " g");
            Thread.sleep(250);
        }
    }
}
  
  
//Add Phidgets Library
import com.phidget22.*;

public class Accel{
    public static void main(String[] args) throws Exception {
        
        //Create
        Accelerometer accelerometer = new Accelerometer();
        
        //Open
        accelerometer.open(1000);
        
        //Use your Phidgets
        while (true) {
            System.out.println("Acceleration: x = " + accelerometer.getAcceleration()[0] + " g | y = " + accelerometer.getAcceleration()[1] + " g | z = " + accelerometer.getAcceleration()[2]+ " g");
            Thread.sleep(250);
        }
    }
}
  
  
//Add Phidgets Library
import com.phidget22.*;

//Define
Accelerometer accelerometer;

void setup(){
  try{
    
    //Create 
     accelerometer = new Accelerometer();
    
    //Open
    accelerometer.open(1000);
    
  }catch(Exception e){
    e.printStackTrace();
  }
}

void draw(){
  try{
    
    //Use your Phidgets
    println("Acceleration: x = " + accelerometer.getAcceleration()[0] + " g | y = " + accelerometer.getAcceleration()[1] + " g | z = " + accelerometer.getAcceleration()[2]+ " g");
    delay(250);
    
  }catch(Exception e){
    e.printStackTrace();
  }
}
  

Code (Python)

Create a file called Accelerometer and insert the following code. Run your code. Move your AccelerometerPhidget to see the output change.

Not your programming language? Set my language and IDE.

  
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.Accelerometer import *
#Required for sleep statement
import time

#Create
accelerometer = Accelerometer()

#Open
accelerometer.openWaitForAttachment(1000)

#Use your Phidgets
while (True):
    print("Acceleration: x = " + str(accelerometer.getAcceleration()[0]) + " g | y = " + str(accelerometer.getAcceleration()[1]) +" g | z = " + str(accelerometer.getAcceleration()[2]))
    time.sleep(0.25)
  

Code (C#)

Create a file called Accelerometer and insert the following code. Run your code. Move your AccelerometerPhidget to see the output change.

Not your programming language? Set my language and IDE.

  
//Add Phidgets Library
using Phidget22;

namespace Accel{
    class Program{
        static void Main(string[] args){

            //Create
            Accelerometer accelerometer = new Accelerometer();

            //Open
            accelerometer.Open(1000);

            //Use your Phidgets
            while (true)
            {
                System.Console.WriteLine("Acceleration: x = " + accelerometer.Acceleration[0] + "g | y = " + accelerometer.Acceleration[1] + "g | z = " + accelerometer.Acceleration[2] + "g");
                System.Threading.Thread.Sleep(250);
            }

        }
    }
}
  

Code (Swift)

Create a file called Accelerometer and insert the following code. Run your code. Move your Accelerometer Phidget to see the output change.

Not your programming language? Set my language and IDE.

You will need to add three Labels.

  
import Cocoa
//Add Phidget Library
import Phidget22Swift

class ViewController: NSViewController {

    @IBOutlet weak var xLabel: NSTextField!
    @IBOutlet weak var yLabel: NSTextField!
    @IBOutlet weak var zLabel: NSTextField!
    
    //Create
    let accelerometer = Accelerometer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        do{
            //Subscribe to event
            let _ = accelerometer.accelerationChange.addHandler(onAccelerationChange)
            
            //Open
            try accelerometer.open()
        }catch{
            print(error)
        }
    }
    
    func onAccelerationChange(sender:Accelerometer, data: (acceleration:[Double], timestamp: Double)){
        DispatchQueue.main.async {
	    //Use information from your Phidget to change label
            self.xLabel.stringValue = "X-axis: " + String(data.acceleration[0]) + "g"
            self.yLabel.stringValue = "Y-axis: " + String(data.acceleration[1]) + "g"
            self.zLabel.stringValue = "Z-axis: " + String(data.acceleration[2]) + "g"
        }
    }
}
  

Applications

If you have been using a phone or tablet, you may have noticed that the screen contents will rotate when you rotate your phone. That is an accelerometer in action!

  • Accelerometers are used to detect eartquakes and measure the impact.
  • Modern vehicles use accelerometers as "crash sensors" to detect when an accident happens and deploy the airbags.
  • Sensitive production lines (e.g. hard drive manufacturing) may use accelerometers in order to monitor and maintain quality.

Practice

Create the beginning fall detection system using your Spatial Phidget and Getting Started Kit.

  1. When the Phidget is sitting flat on your desk, turn on the green LED.
  2. When the Spatial Phidget moves, turn on the Red LED.

Check out the advanced lesson Using the Sensor API before you use the API for the first time.

API

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