12.Arduino learning notebook--ADXL345 acceleration sensor experiment

12.Arduino learning notebook--ADXL345 acceleration sensor experiment



               Juvtmall ( a company supply PCB PrototypingPCBA service and sell kinds of components, modules and so on) ( Catduino has the same function of Arduino)


Today we are going to study another sensoracceleration sensor.


What is the acceleration sensor?

The acceleration sensor is used to measure the force produced during the acceleration.The most basic is acceleration of gravity, which is 1g.

Where is the acceleration sensor normally used?

By measuring the acceleration caused by gravity, you can calculate the tilt Angle of the device relative to the horizontal plane. By analyzing the dynamic acceleration, you can get how does the device move. In the self-balancing vehicle, the acceleration sensor and gyroscope are used to correct the Kalman filter and the attitude correction.

The ADXL345 digital sensor used in this experiment, directly outputs digital signals through the I2C or SPI interface. At 1g acceleration, the output value is 256.




Here is the hardware connection diagram







Below is the code of 002X IDE

ARDUINO code
1. # include < Wire. H > / / call the I2C library brought by arduino
2. # include < LiquidCrystal. H > / / call the LiquidCrystal library of arduino
3. 
4. # define Register_ID 0
5. # define Register_2D 0 x2d
6. # define Register_X0 0 x32
7. # define Register_X1 0 x33
8. # define Register_Y0 0 x34
9. # define Register_Y1 0 x35
10. # define Register_Z0 0 x36
11. # define Register_Z1 0 x37
12. 
13. LiquidCrystal LCD (12, 11, 10, 9, 8, 7);/ / setup interface
14. 
15. Int ADXAddress xa7 = 0 > > 1;/ / convert to 7-bit address
16. Int reading = 0;
17. Int val = 0;
18. Int X0, X1, X_out;
19. Int Y0, Y1, Y_out;
20. Int Z1, Z0 Z_out;
21. Double Xg, Yg, Zg;
22. 
23. Void setup ()
24. {
25. LCD. The begin (16, 2);/ / initialize LCD
26. Delay (100);
27. Wire. The begin ();/ / initialize I2C
28. Delay (100);
29. Wire. BeginTransmission (ADXAddress);
30. Wire. Send (Register_2D);
31. Wire. Send (8);
32. Wire. EndTransmission ();
33. }
34. 
35. Void loop ()
36. {
37. Wire. BeginTransmission (ADXAddress);
38. Wire. Send (Register_X0);
39. Wire. Send (Register_X1);
40. Wire. EndTransmission ();
41. Wire. RequestFrom (ADXAddress, 2);
42. If (Wire. The available () < = 2);
43. {
44. X0 = wire.receive ();
45. X1 = wire.receive ();
46. X1 = X1 < < 8;
47. X_out = X0 + X1;
48. }
49. 
50. Wire. BeginTransmission (ADXAddress);
51. Wire. Send (Register_Y0);
52. Wire. Send (Register_Y1);
53. Wire. EndTransmission ();
54. Wire. RequestFrom (ADXAddress, 2);
55. If (Wire. The available () < = 2);
56. {
57. Y0 = wire.receive ();
58. Y1 = wire.receive ();
59. Y1 = Y1 < < 8;
60. Y_out = Y0 + Y1;
61. }
62. 
63. Wire. BeginTransmission (ADXAddress);
64. Wire. Send (Register_Z0);
65. Wire. Send (Register_Z1);
66. Wire. EndTransmission ();
67. Wire. RequestFrom (ADXAddress, 2);
68. If (Wire. The available () < = 2);
69. {
70. Z0 = wire.receive ();
71. Z1 = wire.receive ();
72. Z1 = Z1 < < 8;
73. Z_out = Z0 + Z1;
74. }
75. 
76. Xg = X_out / 256.00;/ / convert the output result to gravity acceleration g, accurate to 2 decimal places.
77. Yg = Y_out / 256.00;
78. Mid-december = Z_out / 256.00;
79. LCD. The clear ();/ / clear screen
80. LCD. Print (" X = ");/ / make the screen display text X =
81. LCD. Print (Xg);
82. LCD. SetCursor (8, 0);
83. LCD. Print (" Y = ");
84. LCD. Print (Yg);
85. LCD. SetCursor (0, 1);
86. LCD. Print (" Z = ");
87. LCD. Print (mid-december);
88. Delay (300);/ / delay 0.3 seconds, refresh frequency to adjust
89. 
90. }



After 1.0, the arduino syntax changes greatly, and the following code is the code for the IDE after 1.0.

ARDUINO code
1. # include < Wire. H > / / call the I2C library brought by arduino
2. # include < LiquidCrystal. H > / / call the LiquidCrystal library of arduino
3. 
4. # define Register_ID 0
5. # define Register_2D 0 x2d
6. # define Register_X0 0 x32
7. # define Register_X1 0 x33
8. # define Register_Y0 0 x34
9. # define Register_Y1 0 x35
10. # define Register_Z0 0 x36
11. # define Register_Z1 0 x37
12. 
13. LiquidCrystal LCD (12, 11, 10, 9, 8, 7);/ / setup interface
14. 
15. Int ADXAddress xa7 = 0 > > 1;/ / convert to 7-bit address
16. Int reading = 0;
17. Int val = 0;
18. Int X0, X1, X_out;
19. Int Y0, Y1, Y_out;
20. Int Z1, Z0 Z_out;
21. Double Xg, Yg, Zg;
22. 
23. Void setup ()
24. {
25. LCD. The begin (16, 2);/ / initialize LCD
26. Delay (100);
27. Wire. The begin ();/ / initialize I2C
28. Delay (100);
29. Wire. BeginTransmission (ADXAddress);
30. Wire. Write (Register_2D);
31. Wire. Write (8);
32. Wire. EndTransmission ();
33. }
34. 
35. Void loop ()
36. {
37. Wire. BeginTransmission (ADXAddress);
38. Wire. Write (Register_X0);
39. Wire. Write (Register_X1);
40. Wire. EndTransmission ();
41. Wire. RequestFrom (ADXAddress, 2);
42. If (Wire. The available () < = 2);
43. {
44. X0 = wire.read ();
45. X1 = wire.read ();
46. X1 = X1 < < 8;
47. X_out = X0 + X1;
48. }
49. 
50. Wire. BeginTransmission (ADXAddress);
51. Wire. Write (Register_Y0);
52. Wire. Write (Register_Y1);
53. Wire. EndTransmission ();
54. Wire. RequestFrom (ADXAddress, 2);
55. If (Wire. The available () < = 2);
56. {
57. Y0 = wire.read ();
58. Y1 = wire.read ();
59. Y1 = Y1 < < 8;
60. Y_out = Y0 + Y1;
61. }
62. 
63. Wire. BeginTransmission (ADXAddress);
64. Wire. Write (Register_Z0);
65. Wire. Write (Register_Z1);
66. Wire. EndTransmission ();
67. Wire. RequestFrom (ADXAddress, 2);
68. If (Wire. The available () < = 2);
69. {
70. Z0 = wire.read ();
71. Z1 = wire.read ();
72. Z1 = Z1 < < 8;
73. Z_out = Z0 + Z1;
74. }
75. 
76. Xg = X_out / 256.00;/ / convert the output result to gravity acceleration g, accurate to 2 decimal places.
77. Yg = Y_out / 256.00;
78. Mid-december = Z_out / 256.00;
79. LCD. The clear ();/ / clear screen
80. LCD. Print (" X = ");/ / make the screen display text X =
81. LCD. Print (Xg);
82. LCD. SetCursor (8, 0);
83. LCD. Print (" Y = ");
84. LCD. Print (Yg);
85. LCD. SetCursor (0, 1);
86. LCD. Print (" Z = ");
87. LCD. Print (mid-december);
88. Delay (300);/ / delay 0.3 seconds, refresh frequency to adjust
89. 
90. }


The other blog


评论

此博客中的热门博文

1. Arduino learning notebook -- Button experience (volume 2)

1. Arduino learning notebook -- Button experience (volume 1)

19.Arduino learning notebook--Atmega8 makes the minimum arduino system