## **讀取ADXL3xx加速度計**
> 本例向你展示如何讀取ADXL3xx(例如:ADXL320, ADXL321, ADXL322,DXL330)系列加速度計。并且通過Arduino IDE或其他串口數據接收器輸出加速度數據。本例使用的板子是Sparkfun的**breakout**板。使用**adafruit accelerometer breakout板**也可以不過連線會有所不同。
>
> 譯者注:可以用**KXR94-2283**來代替**ADXL3xx**系列,不過需要注意連線
ADXL3xx系列加速度計將角度轉化為0-5v的電壓。如果要讀取數據,只需要使用analogRead()函數。
### **所需硬件**
* Arduino或Genuino板子
* ADXL3xx系列加速度計
* 連接線
### **電路**
加速度計只會消耗很少的電流,因此可以直接用輸出引腳驅動。?

* * *
如果你用了普通Arduino板+加速度計
| 引腳 | 模擬引腳 |
| --- | :-: |
| 自測引腳 | 孔雀 |
| Z軸引腳 | A1 |
| Y軸引腳 | A2 |
| X軸引腳 | A3 |
| GND | GND |
| VDD | 5V |
請特別注意,有些加速度計最大支持3.3V,如果用5V會損壞加速度計。請參閱加速度計的數據手冊來確定合適電壓范圍。
* * *
如果你使用里的教程的板子,為了讓breakout能直接連接到Arduino你需要使用3個模擬引腳來當作數字IO引腳(在基礎部分中的數字有提到)來提供電源和GND。然后用其他三個模擬引腳讀取加速度計的模擬信號 。按照下列方式連接引腳:
| 引腳 | 模擬引腳 |
| --- | :-: |
| 自測引腳 | A0 |
| Z軸引腳 | A1 |
| Y軸引腳 | A2 |
| X軸引腳 | A3 |
| GND | A4 |
| VDD | A5 |
### **原理圖**

### **代碼**
加速度計引腳在代碼開頭以常量方式定義,本代碼使用A4(19)和A5(18)作為電源和GND。如果你沒有用教程用的板子,請將19改為14,18改為15。
~~~
const int groundpin = 18;
const int powerpin = 19;
~~~
設置A5(19)為高電平,A4(18)為低電平。讓加速度計工作起來。
~~~
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
~~~
Sparkfun的breakout板可以直接連接到Arduino或Genuino板上。但有些板可能需要鏈接到標準5V或3.3V以及GND接口,你需要將setup上方的代碼改為注釋中的內容。
~~~
/*
讀取ADXL3xx加速度計
讀取ADXL3xx加速度計并且將數據發送到電腦。本例使用Sparkfun的breakout板子最好,下面是購買鏈接:
http://www.sparkfun.com/commerce/categories.php?c=80
代碼公開
*/
// 常量引腳定義:
const int groundpin = 18; // A4--GND
const int powerpin = 19; // A5--+5V
const int xpin = A3; // x軸
const int ypin = A2; // y軸
const int zpin = A1; // z軸
void setup() {
// 初始化串口連接:
Serial.begin(9600);
// 下列代碼這樣設置是為了將breakout板直接連接到Arduino上。如果你沒有用breakout板子,直接將傳感器連上了+5v和GND請去掉下面四行代碼
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop() {
// 輸出傳感器值:
Serial.print(analogRead(xpin));
// 輸出一個table制表符:
Serial.print("\t");
Serial.print(analogRead(ypin));
// 輸出一個table制表符:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
// 延遲100毫秒
delay(100);
}
~~~
### **數據**
下列輸出的是y軸的數據。但是你的數據可能由于傳感器型號的不同而與下列數據有所差異。
| 角度 | 加速度 |
| --- | :-: |
| -90 | 662 |
| -80 | 660 |
| -70 | 654 |
| -60 | 642 |
| -50 | 628 |
| -40 | 610 |
| -30 | 589 |
| -20 | 563 |
| -10 | 537 |
| 0 | 510 |
| 角度 | 加速度 |
| --- | :-: |
| 0 | 510 |
| 10 | 485 |
| 20 | 455 |
| 30 | 433 |
| 40 | 408 |
| 50 | 390 |
| 60 | 374 |
| 70 | 363 |
| 80 | 357 |
| 90 | 355 |
### **相關資料**
[pinMode()](https://www.arduino.cc/en/Reference/PinMode)?
[digitalWrite()](https://www.arduino.cc/en/Reference/DigitalWrite)?
[analogRead()](https://www.arduino.cc/en/Reference/AnalogRead)?
[serial.begin()](https://www.arduino.cc/en/Serial/Begin)?
[serial.print()](https://www.arduino.cc/en/Serial/Print)?
[原文鏈接](http://www.arduino.cc/en/Tutorial/ADXL3xx)
- 說明
- 系統示例文件目錄結構及說明
- 01.Basics
- AnalogReadSerial
- BareMinimum
- Blink
- DigitalReadSerial
- Fade
- ReadAnalogVoltage
- 02.Digital
- BlinkWithoutDelay
- Button
- Debounce
- DigitalInputPullup
- StateChangeDetection
- toneKeyboard
- toneMelody
- toneMultiple
- tonePitchFollower
- 03.Analog
- AnalogInOutSerial
- AnalogInput
- AnalogWriteMega
- Calibration
- Fading
- Smoothing
- 04.Communication
- ASCIITable
- Dimmer
- Graph
- Midi
- MultiSerial
- PhysicalPixel
- ReadASCIIString
- SerialCallResponse
- SerialCallResponseASCII
- SerialEvent
- SerialPassthrough
- VirtualColorMixer
- 05.Control
- Arrays
- ForLoopIteration
- IfStatementConditional
- switchCase
- switchCase2
- WhileStatementConditional
- 06.Sensors
- ADXL3xx
- Knock
- Memsic2125
- Ping
- 07.Display
- barGraph
- RowColumnScanning
- 08.Strings
- CharacterAnalysis
- StringAdditionOperator
- StringAppendOperator
- StringCaseChanges
- StringCharacters
- StringComparisonOperators
- StringConstructors
- StringIndexOf
- StringLength
- StringLengthTrim
- StringReplace
- StringStartsWithEndsWith
- StringSubstring
- StringToInt
- 09.USB
- Keyboard
- KeyboardLogout
- KeyboardMessage
- KeyboardReprogram
- KeyboardSerial
- KeyboardAndMouseControl
- Mouse
- ButtonMouseControl
- JoystickMouseControl
- 10.StarterKit_BasicKit (與特定硬件相關,暫無)
- p02_SpaceshipInterface
- p03_LoveOMeter
- p04_ColorMixingLamp
- p05_ServoMoodIndicator
- p06_LightTheremin
- p07_Keyboard
- p08_DigitalHourglass
- p09_MotorizedPinwheel
- p10_Zoetrope
- p11_CrystalBall
- p12_KnockLock
- p13_TouchSensorLamp
- p14_TweakTheArduinoLogo
- p15_HackingButtons
- 11.ArduinoISP(暫無)
- ArduinoISP