React Native course · Module 11: Device and sensors

expo-sensors - Device sensors

1 min read ·
In this lesson3

Access the accelerometer, gyroscope, and other sensors!

npx expo install expo-sensors

Accelerometer

import { Accelerometer } from 'expo-sensors';
import { useState, useEffect } from 'react';

function AccelerometerDemo() {
  const [data, setData] = useState({ x: 0, y: 0, z: 0 });

  useEffect(() => {
    const subscription = Accelerometer.addListener(setData);
    Accelerometer.setUpdateInterval(100); // ms

    return () => subscription.remove();
  }, []);

  return (
    <View>
      <Text>X: {data.x.toFixed(2)}</Text>
      <Text>Y: {data.y.toFixed(2)}</Text>
      <Text>Z: {data.z.toFixed(2)}</Text>
    </View>
  );
}

Gyroscope

import { Gyroscope } from 'expo-sensors';

function useGyroscope() {
  const [rotation, setRotation] = useState({ x: 0, y: 0, z: 0 });

  useEffect(() => {
    const sub = Gyroscope.addListener(setRotation);
    return () => sub.remove();
  }, []);

  return rotation;
}

Barometer (pressure)

import { Barometer } from 'expo-sensors';

function BarometerDemo() {
  const [pressure, setPressure] = useState(0);

  useEffect(() => {
    const sub = Barometer.addListener(({ pressure }) => {
      setPressure(pressure);
    });
    return () => sub.remove();
  }, []);

  return <Text>Pressure: {pressure.toFixed(2)} hPa</Text>;
}
Code for this lesson: App.tsx
import React, { useState, useEffect } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { Accelerometer } from 'expo-sensors';

export default function App() {
  const [data, setData] = useState({ x: 0, y: 0, z: 0 });

  useEffect(() => {
    // TODO: set the update frequency to 100 ms
    //   use Accelerometer.setUpdateInterval(100)

    // TODO: subscribe to the sensor with Accelerometer.addListener(setData)
    //   and store the result in a subscription variable
    const subscription = undefined;

    // TODO: in the cleanup function call subscription.remove()
    return () => {
      // clean up the subscription here
    };
  }, []);

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Atlantis Accelerometer</Text>
      <Text style={styles.value}>X: {data.x.toFixed(2)}</Text>
      <Text style={styles.value}>Y: {data.y.toFixed(2)}</Text>
      <Text style={styles.value}>Z: {data.z.toFixed(2)}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#0e3d59',
  },
  title: {
    fontSize: 20,
    color: '#fff',
    marginBottom: 16,
  },
  value: {
    fontSize: 18,
    color: '#aed6f1',
    marginBottom: 6,
  },
});

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do you install expo-sensors?

  2. 2. What values does the Accelerometer return?

These are 2 of 5 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Horizontal ordering

    Arrange the Accelerometer subscription:

  • Horizontal ordering

    Arrange the interval setup:

  • Code editor

    Complete the code using Accelerometer.

  1. QuestionInstalling sensors
  2. QuestionAccelerometer data
  3. Put in orderaddListener
  4. Put in ordersetUpdateInterval
  5. QuestionGyroscope
  6. QuestionBarometer
  7. Questionsubscription.remove
  8. Coding taskSensor demo

Useful articles