📜 ⬆️ ⬇️

We catch the horizon with Arduino

In the comments on the post about creating a three-power platform based on the Arduino hardware platform, the issue of control was not only from the computer, so it was decided to deal with the operation of the MMA7260 accelerometer, which can be freely purchased in Hong Kong on Ebay .
image


Tools and materials


To implement our plans we need the following components:
Renamingamount
Accelerometer MMA72601 PC.
Three-power platform1 PC.
Bread board1 PC.
Screed1 PC.
Connecting wires8 pieces
Polar bear1 PC.


The most necessary detail for our installation is an accelerometer. In the open spaces of the network, there are many suggestions for Arduino in terms of accelerometers and the most affordable is the three-axis accelerometer MMA7260. It is already on sale on the board, where there is already a 3.3V 800mA stabilizer for power. At the outputs of the X, Y, Z axes, filters in the form of RC chains are decoupled, and it also has a low-pass filter (which is not very noticeable when we first met).
')
In accordance with the documentation on the device, it is possible to select the sensitivity (4 modes), as well as turn the sleep mode on and off. The sensitivity of the sensor is adjusted using the inputs G1 and G2. The maximum sensitivity level is 1.5g (00), the minimum is 6g (11).

Wiring diagram


Connecting the accelerometer to the Arduino board is carried out according to the scheme below:
image
In order for the accelerometer to always give out the values, it must be forced out of sleep mode, simply by applying power to the SL (Sleep) output. The sensitivity mode is selected using jumpers J1 and J2.

Initially, this scheme can be assembled on a breadboard, after which you can unsolder long wiring. The jumpers are placed on the contact pad 3x2, where the extreme left two contacts are 3.3V, the central G1 and G2, respectively, and the rightmost contacts are ground.

As a result, after assembly, the following construction is obtained:
image

Connecting the three-stage platform to the Arduino is carried out on three outputs with PWM support, and the inputs from the position sensor on the three analog inputs A0 ... A2. An example of connecting nodes to Arduino:
image

Programming


Testing and testing the accelerometer is performed using the following program:
int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  1. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  2. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  3. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  4. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  5. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  6. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  7. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  8. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  9. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  10. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  11. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  12. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  13. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  14. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  15. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  16. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  17. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  18. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  19. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  20. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  21. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  22. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  23. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  24. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
  25. int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .
int x, y, z; // int dx=357; int dy=385; int dz=178; void setup() { Serial.begin(9600); } void loop() { // x = analogRead(A0) - dx; y = analogRead(A1) - dy; z = analogRead(A2) - dz; // Serial monitor Serial.print( "X: " ); Serial.print(x); Serial.print( "Y:" ); Serial.print(y); Serial.print( "Z:" ); Serial.println(z); // delay(100); } * This source code was highlighted with Source Code Highlighter .


Testing


Before starting work, it is necessary to calibrate the sensor, according to the following procedure:
1) Set the calibration values ​​in the firmware of the controller to zero, write the firmware to the controller;
2) Fix the sensor in a horizontal position;
3) Take readings from the sensor for 3 seconds;
4) Calculate the average values ​​in a horizontal position and amend the calibration values.

These measurements must be done at or below the minimum sensitivity in order to exclude unnecessary readings on impacts or any other effects.

Prior to calibration, the sensor gave the following values:
image

The graph shows that withered low pass filters work quite strange, since the axles are noisy very much. To suppress noise, you must either write your own low-pass filter or normalize the measurement scale to less (in the case of servo drives, noise is partially suppressed by this method).

As a result of the measurements obtained, the following corrections were obtained:

After calibration, the sensor readings began to show the real picture:
image

In this form, you can already use the sensor readings in your projects.

Result




UPD. Software filtering of the signal is carried out according to the algorithm presented in Wikipedia :
function lowpass(real[0..n] x, real dt, real RC)
var real[0..n] y
var real α := dt / (RC + dt)
y[0] := x[0]
for i from 1 to n
y[i] := α * x[i] + (1-α) * y[i-1]
return y


* This source code was highlighted with Source Code Highlighter .
function lowpass(real[0..n] x, real dt, real RC)
var real[0..n] y
var real α := dt / (RC + dt)
y[0] := x[0]
for i from 1 to n
y[i] := α * x[i] + (1-α) * y[i-1]
return y


* This source code was highlighted with Source Code Highlighter .

although I like the second option more:
for i from 1 to n
y[i] := y[i-1] + α * (x[i] - y[i-1])


* This source code was highlighted with Source Code Highlighter .
for i from 1 to n
y[i] := y[i-1] + α * (x[i] - y[i-1])


* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/127165/


All Articles