Accelerometer Phidget

The Spatial Accelerometer 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

Accelerometers measure vibrations in audio applications (noise monitoring), mechanical (car tires), and buildings (seismic activity monitoring). Accelerometers monitor speed and g-force (for example, on a roller coaster). Accelerometers are also used in fall detection systems or in your Phone to rotate the screen from portrait to landscape view.

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
Troubleshoot
  1. Make sure everything is plugged in properly and your VINT Hub is connected to your computer
  2. Stop running other programs that are using your Phidgets before running a program.

If these do not solve your issue visit the Advanced Troubleshooting Page.

What are Phidgets?

Phidgets are programmable, modular USB devices, either sensors or controllers that you can connect together. Simply write code in your favorite language and solve real-world problems.

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