These days, you can find accelerometers in smartphones. They are used in applications like games. They are also used to detect when your screen should switch from vertical to horizontal.
Besides smartphones, accelerometers are used in a variety of everyday products. Depending on your ideas, you can find many ways to use them.
Expected time to complete: 90 minutes
Parts needed:
Arduino (Arduino Uno R3) https://www.adafruit.com/products/50
Breadboard https://www.adafruit.com/products/64
3-axis accelerometer module (KXR94-2050) http://www.digikey.com/product-detail/en/KXR94-2050-FR/1191-1012-1-ND/3137358
Micro-servo 9G (SG90 http://www.amazon.com/SMAKN%C2%AE-Micro-Small-Helicopter-Arduino/dp/B00SMICV86/)
Photo 1 The 3-axis accelerometer KXR94-2050
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 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.
Accelerometer Test
*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.
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.
Refer to the KXR94-2050 data sheet
This sensor has 8 pins. The assignments of those pins are shown below in figure 2.
Figure 2 Accelerometer sensor pins
Figure 3 Circuit for connecting the accelerometer sensor to Arduino
Picture 2 Arduino and the accelerometer sensor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//******************************** //Program to obtain values from an accelerometer //******************************** void setup() { //Initialize serial monitor Serial.begin(9600) ; } void loop() { long x , y , z ; x = y = z = 0 ; x = analogRead(3) ; // Xaxis y = analogRead(4) ; // Yaxis z = analogRead(5) ; // Zaxis Serial.print("X:") ; Serial.print(x) ; Serial.print(" Y:") ; Serial.print(y) ; Serial.print(" Z:") ; Serial.println(z) ; delay(50) ; } |
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.
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//******************************** //Second program to obtain values from accelerometer //******************************** void setup() { //initialize serial monitor Serial.begin(9600) ; } void loop() { int i ; long x,y,z; //read value 50 times and output the average x=y=z=0; for (i=0 ; i < 50 ; i++) { x = x + analogRead(3) ; // X軸 y = y + analogRead(4) ; // Y軸 z = z + analogRead(5) ; // Z軸 } x = x / 50 ; y = y / 50 ; z = z / 50 ; int rotateX = (x-277)/2.48 - 90; //formula to determine angle Serial.print("X:") ; Serial.print(x) ; Serial.print(" ") ; Serial.print(rotateX) ; int rotateY = (y-277)/2.48 - 90; Serial.print(" Y:") ; Serial.print(y) ; Serial.print(" ") ; Serial.print(rotateY) ; delay(50) ; } |
Figure 7 Displaying angle from the accelerometer
Here, we have displayed the angle that was retrieved from the accelerometer.
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?
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.