![]() Next we have a series of Serial.print() functions to print the values stored in the x, y, and z variables to the serial monitor. The y axis measurements will be stored in the variable in the second parameter, and the z axis measurements will be stored in the variable placed in the third parameter. The sensor’s x axis measurement will be stored in the variable that is placed in the first parameter. The arguments of the readAccel() function are the variables that will hold the sensor readings from each axis. Next, we get the sensor readings from the accelerometer with the readAccel() function. Therefore we declare three int variables – x, y, and z. The ADX元45 outputs separate acceleration measurements for each axis, so we need a unique variable for each axis. In the loop() section, the first thing we do is declare variables to hold the sensor readings. We pass it the range variable, which stores the range setting we defined when we declared it. Next, we call the setRangeSetting() function to set the sensitivity range of the sensor. This function is called through the adxl object we created earlier. Then we initialize the ADX元45 with the powerOn() function. In the setup() section we initialize the serial monitor. Next we declare a variable called range, which will store the sensitivity range we want the ADX元45 to have. When there are no arguments passed to the function, as in this case, the sensor will use I2C to communicate with the Arduino. The ADX元45() function configures the communication mode that the sensor will use to talk to the Arduino. We set the adxl object equal to the function ADX元45(). Next we create an object called adxl, which is a member of the ADX元45 class. The first thing we do is include the Sparkfun ADX元45 library. The sketch will output the raw accelerometer readings to the serial monitor: #include Once you get the library installed, upload the code below to the Arduino. To program the ADX元45 accelerometer we will use the Sparkfun ADX元45 library. We will connect the accelerometer with I2C, so the wiring is pretty simple.Ĭonnect the accelerometer to the Arduino like this: How to Program the ADX元45 Accelerometer Let’s connect the accelerometer to the Arduino and take a look at the raw values it provides. How to Connect the ADX元45 Accelerometer to the Arduino The sensor measures this change in capacitance and calculates an acceleration value. When the sensor accelerates, the mobile plate moves, and the distance between the plates changes.Ĭapacitance is a function of the distance between two charged plates, so when the distance between the plates changes, the electric field between the plates also changes. When the accelerometer is at rest, the electric field between the plates is constant. The mobile plate and the fixed plate are charged, so an electric field is formed between them: Along each axis there is a tiny plate suspended between two micro-springs that can move back and forth: The ADX元45 measures acceleration by detecting changes in capacitance. The ADX元45 outputs separate acceleration measurements for each axis, x, y, and z: One g is defined as the rate of acceleration of gravity, which is 9.8 m/s 2. Two common units of acceleration are meters per second squared (m/s 2) and g’s. Accelerometers measure both static acceleration and dynamic acceleration. Static acceleration is caused by forces like gravity. If you step on the brakes, the car has a negative acceleration. If you drive in a car and step on the gas, the car has a positive acceleration. Doubles as the SCLK pin for SPI.Īcceleration is the rate of increase or decrease of velocity: ![]() ![]() Doubles as the MOSI pin for SPI communication. Doubles as the MISO pin for SPI communication. CS – Chip select pin for SPI communication.This pin is where the 3.3 volt power source would connect. 3V3 – The ADX元45 can be powered with 3.3 volts or 5 volts.Vin – Connects to a 5 volt power source.In this article we will use I2C, but SPI is an option if you want to use it. The ADX元45 can communicate with the Arduino over SPI and I2C. The z axis extends up and down, perpendicular to the x and y axes. The directional arrows indicate the direction of each sensor axis (x, y, and z) in relation to the physical board. ![]() The accelerometer we will use in this tutorial is the ADX元45 accelerometer from Adafruit: It includes all of the parts, wiring diagrams, code, and step-by-step instructions for 58 different robotics and internet of things projects that are super fun to build! Introduction to the ADX元45 Accelerometer The 3-in-1 Smart Car and IOT Learning Kit from SunFounder has everything you need to learn how to master the Arduino.
0 Comments
Leave a Reply. |