C++Code SnippetComponentsLanguage FeatureLearn C++

What Is An Accelerometer And How To Use It in C++?

What is a motion sensor? What is an accelerometer? Which C++ IDE supports the use of motion sensors? How can I use a motion sensor in my C++ code? What is the difference between a motion detector and motion sensor? How can I detect the motion of a device? How can I measure the acceleration of device? Let’s answer these questions.

What is a motion sensor?

Strictly speaking, a motion detector, sometimes known as a motion sensor, is an electrical/electronic device that utilizes special hardware to detect motion of an object or objects. Generally it detects the motion nearby. Such a device is often integrated as a component of a system that automatically performs a task or alerts a user of motion in an area. These kinds of sensors are generally used for security and things like automated lighting control, home control, energy efficiency, and other useful systems.

A Motion Sensor – rather than detector – is generally used for the motion sensors included in devices such as mobile devices, IoT equipment and so on. It allows programs to detect motion of the device rather than motion near to the device. These sensors are generally use a type of hardware technology called an accelerometer. Because of this they are often called Acceloremeters. It is able to detect acceleration of the device in X Y Z directions. If you code can measure the acceleration in the X Y and Z directions, you can then detect the velocity and motion of the device.

What is TMotionSensor in C++?

In C++ Builder, Motion Sensor (TMotionSensor) is a class wrapper for the TCustomMotionSensor that receives data from the sensor (or sensors) of device like the acceleration in X,Y, and Z directions and angular acceleration in the X, Y and Z axes.

TMotionSensor or TCustomMotionSensor, offer information about the acceleration, angle, state, and speed of the device’s motion.

All TMotionSensor Properties can be found here and Methods can be found here

How to use TMotionSensor in C++ Builder?

Rad Studio, C++ Builder comes with Mobile Snippets in C++ that can be found in the “C:\Users\Public\Documents\Embarcadero\Studio\22.0\Samples\CPP\Mobile Snippets\Accelerometer” folder.

The Accelerometer sample demonstrates how to use the accelerometer in iOS or Android applications. The sample uses TMotionSensor component and TCustomMotionSensor class to offer information about the acceleration, angle, state, and speed of the device motion. Note that TCustomMotionSensor class supports Android, iOS and Windows platforms. However, each platform provides different motion sensors and each type of sensor supports a different set of properties (see TCustomMotionSensor for a complete list of platform, sensors and supported properties). Some functionalities of this sample could be disabled depending on the target platform. You can find more official detailed explanations here.

Let’s see how we can use TMotionSensor component in C++ Builder,

  1. Create a new Multi-Device C++ Builder FireMonkey application that allows us to develop Android or iOS applications, can be used with other IoTs too. Save all projects to a folder.
  2. Drag TMotionSensor from the component palette onto Form design.
  3. You can activate this MotionSensor1 component on design time or on run time. We highly recommend to activate on run time. You should use set its Active property to true as given example below,

Generally before reading sensor values we should check MotionSensor1->Sensor as below,

and we should check if the sensor of this device has the property that we want to measure. We should use AvailableProperties.Contains() method to check required property as below,

Finally we should put the property flag, for example for the X direction Acceleration we should use TCustomMotionSensor::TProperty::AccelerationX. Here is the full procedure that checks sensors, its property then gets the required value .

As you see we can easily read AccelerationX, AccelerationY, AccelerationZ and AngleAccelX, AngleAccelY, AngleAccelZ properties and more properties of TMotionSensor in C++ Builder. This example works on both Android, iOS and supported IoT devices.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?