An accelerometer is a sensor that measures inclination, shock, and vibration. The 3-axis accelerometer KXR94-2050 that we’ll be using monitors the state of the device on the XYZ planes and the acceleration in any given direction. The accelerometer by ROHM Group’s “Kionix” is soldered such that it can be inserted directly into a breadboard.
There are many methods for measuring acceleration, such as optical methods, vibration measurement using a spring, capacitive types that measure potential, and so on. For more details on the internal workings of these devices, please use Wikipedia to look up information about accelerometer.
Before Using the Accelerometer, Let’s Take a Look at the Smartphone
Before using the accelerometer with Arduino, let’s try to understand what an accelerometer is and how it works. If you have a smartphone with an internal accelerometer, then you’ll be able to see the state of the internal accelerometer by accessing the URL below.
*The feature that allows for detection of acceleration in HTML5 can be viewed by devices with Android 4.0 or above and iPhones with iOS 4.2.1 and above.
Figure 1 Testing the accelerometer with a browser
This sample shows us the XYZ coordinates returned by the accelerometer in your device. By interpreting these numbers, it is possible to determine which way your device is facing, and whether it is moving. It’s possible to use these values in games and applications.
Let’s Use an Accelerometer with Arduino
Next, let’s try it out with Arduino. As the sensor requires between 2.5V and 5.25V, we can connect it directly to the Arduino UNO’s Vcc, which is 5V.
This sensor has 8 pins. The assignments of those pins are shown below in figure 2.
Figure 2 Accelerometer sensor pins
VDD – Power supply
PSD – Connect to VDD to display sensor output
GND – ground
Parity – Will not be used this time
SelfTest – Will not be used this time
Figure 3 Circuit for connecting the accelerometer sensor to Arduino
Picture 2 Arduino and the accelerometer sensor
//Program to obtain values from an accelerometer
//Initialize serial monitor
Figure 4 Obtaining values
Running this program will display the default sensor values.
In this case, it is sitting on a flat desk (in other words, 0 degrees of tilt). While stable, the X axis shows 498, the Y axis shows 536, and the Z axis returns a value of 750.
Reading the Orientation
Using the values in figure 4 as an example, let’s determine the orientation of the sensor. To describe the orientation of the sensor as simply as possible, by determining the amount of gravity acting on each part of the sensor, it becomes possible to determine the orientation (for more details, please use online sources). We use the symbol “g” to represent gravity. If you want to determine the orientation, we can take the value of X from the XYZ axis display and determine the orientation as shown in figure 5.
*To briefly delve into physics, 1g describes the condition of all stationary objects on earth at sea level. This is known as the universal law of gravitation. To be honest, there are a variety of influences at play, including elevation and location on the Earth (due to the earth’s centrifugal force, things are lighter at the North and South Poles). So, the numbers may be slightly inaccurate.
Figure 5 When the X-axis is between -90 and 90 degrees
Picture 3 Tilting left on the X axis = -1g (Arduino value 277)
Picture 4 Tilting right on the X axis = -1g (Arduino value 724)
Figure 6 Arduino values from pictures 3 and 4
We can see values from 277 (-90 degrees) to 728 (90 degrees). The normal position is around 500, so we can see that the sensor values are 277 (-90 degrees), 500 (0 degrees), and 728 (90 degrees). We will use a proportional expression to determine the angle.
Determining the correlation coefficient between sensor values and angle
1 degree of sensor value = (largest value – smallest value)/180
(724 – 277)/180 = 2.48 ← approximate value of 1 degree
Angle = (current sensor value – smallest value)/2.48 – 90
So then, we can use the preceding formula to convert the values that we retrieve from Arduino into an angle. *Every sensor may have a different level of noise. You must calculate those values for every sensor to determine the accurate correlation coefficient. Also, you must be careful to make sure that the sensor is straight when inserting it into the breadboard.
Next, let’s improve our program to stabilize the values that we get from the accelerometer. Let’s get a value from the sensor inside of a “for” loop (50 times in this example) and output the average reading, just to stabilize the output. (Figure 7)
//Second program to obtain values from accelerometer
//initialize serial monitor
//read value 50 times and output the average
introtateX=(x-277)/2.48-90;//formula to determine angle
Figure 7 Displaying angle from the accelerometer
Here, we have displayed the angle that was retrieved from the accelerometer.
Let’s Create an Application for an Horizontal Camera Device
Let’s put this accelerometer to use. The first thing that comes to mind is creating something that will allow us to take pictures with the horizon intact. No matter how tilted the device is, the ability to take a good picture would be quite a benefit! Maybe it’s just that I’m not a good photographer, but I often take pictures of the landscape only to look at them later and notice that the horizon is not horizontal. At any rate, wouldn’t it be convenient to have a device that would keep a platform level at all times?
I Gave It a Try
Picture 5 Mock-up for the level camera platform
I went ahead and put it together using easy materials. Even when tilted, the platform stays horizontal, isn’t it? I’ll explain the details of this creation in the next episode.
This is extremely simple. By taking the XY readings from the sensor and using a servo motor to move in the exact opposite direction, we can ensure that the platform at the top is always level.
There are some cases where the speed of the servo motor or noise from the accelerometer lead to some differences, but these can be accounted for within the program or through the use of a hard, unbending material in the device housing. Please try to think up techniques for this yourself.
This time, we have learned about the basics of accelerometers and tested it with a simple creation. Next time, we’ll improve upon the device that we made for keeping a camera level.