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.

close

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
Artificial Intelligence TechC++Introduction to C++Learn C++

How To Make A Simple But Powerful Chat Bot In C++

C++C++17Introduction to C++Language FeatureLearn C++

How To Operate On The Words In A String In C++

C++Introduction to C++Language FeatureLearn C++

What Is The Best C++ Compiler for Windows 10?

C++Language FeatureLearn C++

How To Make An MSIX Windows Installer Package

en_USEnglish