Arduino Example Gball
In this example we are demonstrating how to use the Azande Library together with the accelerometer ADXL345.
The ADXL senses acceleration force in 3 directions X, Y and Z..
Following functions are demonstrated in this example:
- Azande
- Connect Wifi to Azande
- ADXL345, 3-Axis Digital Accelerometer
- Timer Interrupt with help from Timer5 library
Hardware
In this example we have used:
- one MKR1000
- one ADXL345
- one Battery
The Ball
By putting the hardware inside a 6'' Foam Ball, we can drop, bounce and throw it without breaking it.
With a knife we cut a hole big enough to tightly fit the hardware. The hardware is positioned with the ADXL345 senor in center of the ball.
- The hardware is inserted
- The lid is taped
ADXL345
The ADXL senses acceleration force in 3 directions X, Y and Z.
Note that earth gravity always affects the sensors with an acceleration of approximately 9.81 m/s²
More details about the ADXL345 at SparkFun.
Dependencies
Following libraries are used and need to be installed in your Arduino IDE:
Arduinos with WINC1500 Wi-Fi Module
This example is designed for Arduino systems using the WINC1500 Wi-Fi Module.
Example devices:
- MKR1000
- Arduino WiFi Shield 101
WiFi101 Library
This example requires that you have the WiFi101 library installed.
This WiFi101 Library allows you to use the Arduino WiFi Shield 101 and the MKR1000 board.
More info about WiFi101 Library
Your Configuration
To be able to run the example you need to enter your own configurations in some places:
- Configure Wi-Fi Network
- Configure Socket Server Connection
- Calibration parameters of your ADXL345 module
Configure Wi-Fi Network
In the file arduino_secrets.h you need to specify following credentials for your Wi-Fi Network:
SECRET_SSID |
Name of your Wi-Fi-Network. |
SECRET_PASS |
Password to your Wi-Fi-Network. |
Configure Socket Server Connection
Start Azande Studio and start the socket server. Right click the server and you will see...
In the example code enter Host Name (or IP) and Port Number.
char myPcNetworkName[] = "ZEIJLON-SYSTEMS"; // Host Name unsigned int azandeStudioServerPortNr = 41414; // Port number |
More info about Socket/LAN
Calibration parameters of your ADXL345 module
When you have performed a calibration of your ADXL345 you can use the values as default and you don't need to do a calibration every time you restart your system.
For each axis there is an Offset and a Gain that is used to convert the raw sensor output to m/s²
#define DefaultOffsetX -21 #define DefaultOffsetZ 142 #define DefaultOffsetY -4 #define DefaultGainMs2X 3.924567 #define DefaultGainMs2Y 3.822630 #define DefaultGainMs2Z 3.160041 |
The circuit
Check carefully that you have compatible Vcc voltage.
ADXl345 |
Arduino Board |
|
Pin |
Pin |
Type |
SCL |
SCL |
I²C Serial Clock |
SDA |
SDA |
I²C Serial Data |
Vcc |
VCC |
Supply |
Gnd |
GND |
Supply |
Azande Studio View
Main Menu
In Main Menu you can see the acceleration on each of the axises X, Y and Z.
By clicking a button you can select one of the 4 modes:
- Falling
- Spherical
- Setup
- Sleep
Falling
When the ball is not moving the sensors will sense the standard acceleration (~9.81 m/s²) due to earth gravity g0.
When we drop the ball and it starts free falling, the ball will now accelerate with the acceleration g0 towards center of earth, and the sensors sense 0 m/s² acceleration.
This is the same effect as when astronauts practice Zero Gravity in a diving (free falling) airplane.
This makes it possible to detect when the ball is free falling. (see when we drop the ball in Spherical Mode)
The falling distance is calculated by:
d = g0t2/2
d = distance
t = time
Falling Chart Example
In this chart you can see that we drop the ball 4 times from different heights.
Spherical
In the Spherical mode we transform the 3 acceleration values X, Y and Z to the Spherical Coordinate System.
The Spherical Coordinate System has only one acceleration 'Radial Acceleration' that is"the sum of all accelerations".
The direction of the acceleration is specified with 2 angles φ (PHI) and θ (THETA).
The 'Radial Acceleration' alone gives us a more intuitive view of the acceleration of the ball.
More information about Spherical coordinate systems on WIKIPEDIA.
Dropping the ball in Spherical
This chart shows the Radial Acceleration when we drop the ball to the floor.
- Holding the ball still in the hand.
- Falling with acceleration close to 0
- Some short bounces
- The ball comes to rest on the floor.
Throw the ball in Spherical
In this chart you can see the Radial Acceleration when the ball is thrown into a wall and bounce back.
The peek at ~70 m/s² is the throw.
It is hard to tell why the bounce do not show much acceleration maybe:
- Because the ball is quite soft, and absorbs the chock.
- The bounce duration is very short and we do not catch the acceleration. (we sample the sensors once every 5 ms)
Setup
In Setup Mode you can see the Raw output from the X,Y and Z sensors.
Following setups can be done in this mode:
- Perform a calibration
- Select to use your new calibration or the Default/Hard-Coded one.
- Select sensor range. (default is +/- 16G)
Perform a calibration
The calibration is using gravity as reference. The intent is to find minimum and maximum raw value for each axis.
- Minimum is when the axis points straight down towards center of earth. The acceleration is now -g0 (-9.81 m/s²)
- Maximum is when the axis points straight up away from center of earth. The acceleration is now +g0 (+9.81 m/s²)
- Click "Calibration: START"
- Roll carefully the ball around. Make sure to turn all 3 axises straight up and straight down. Use the chart as help.
- Click "Calibration: END":
- New calibration parameters are calculated and saved
- The new parameters are sent to "X/Y/Z offset" and "X/Y/Z Gain"
More details about calibration at SparkFun.
Select Calibration Parameters
- click "Use Default" to use the default/hard-coded parameters. The range will also be set to the default one +/- 16G.
- click "Use Saved Calibration" if you want to use the parameters from your most recent calibration.
Select Range
Select one of the predefined ranges.
Note that you have to perform a new calibration if the range is changed.
Sleep Mode
When you click "Sleep" button in Main Menu the Arduino will go to Sleep Mode.
It is actually not a real sleep mode, we only turn off the Wi--Fi module to save battery time.
Current measurement on battery supply:
- Normal mode with Wi-Fi ON: ~130mA (~9 hours with a 1200mAh battery)
- Sleep mode with Wi-Fi OFF: ~12mA (~100 hours with a 1200mAh battery)
Wakeup from Sleep
To leave Sleep Mode, just throw the ball up in the air and catch it.
While in Sleep Mode the Arduino is detecting free falling.
If a free fall is detected the Arduino will do the same we do in setup():
- Try to connect to Wi-Fi network
- Try to connect to Azande Studio Socket Server.
- Go to Main Menu
Downloads
The example is included in the Arduino Library.