Using Arduino with Parts and Sensors – Accelerometer Part 1

Level camera

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.

Today Electronic’s Recipe

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/)

What Is an Accelerometer?

Arduino Accelerometer part
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, 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.

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.

Mobile Accelerometer Test
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.

Refer to the KXR94-2050 data sheet

This sensor has 8 pins. The assignments of those pins are shown below in figure 2.

Accelerometer pins
Figure 2 Accelerometer sensor pins

  1. VDD – Power supply
  2. PSD – Connect to VDD to display sensor output
  3. GND – ground
  4. Parity – Will not be used this time
  5. SelfTest – Will not be used this time
  6. X-axis data
  7. Y-axis data
  8. Z-axis data

Circuit for connecting accelerometer to Arduino
Figure 3 Circuit for connecting the accelerometer sensor to Arduino

Arduino and accelerometer
Picture 2 Arduino and the accelerometer sensor

Values from the accelerometer
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.

X-axis is between -90 and 90 degrees
Figure 5 When the X-axis is between -90 and 90 degrees

Accelerometer X axis
Picture 3 Tilting left on the X axis = -1g (Arduino value 277)

Accelerometer X axis
Picture 4 Tilting right on the X axis = -1g (Arduino value 724)

Accelerometer values on the Arduino
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)

Displaying angle from the acceleromoter
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

Level camera
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.

Summary

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.