Using the Accelerometer on the Arduino 101
by TechMartian in Circuits > Arduino
1828 Views, 19 Favorites, 0 Comments
Using the Accelerometer on the Arduino 101
![EvK4gAueSkq1jqs1bR1Lpg_thumb_219.jpg](/proxy/?url=https://content.instructables.com/FSV/LRKY/J52Q8RJN/FSVLRKYJ52Q8RJN.jpg&filename=EvK4gAueSkq1jqs1bR1Lpg_thumb_219.jpg)
This is the fifteenth module of the series of lesson about use cases of the Arduino 101. Another feature that differentiates the Arduino 101 from the Uno is that the 101 has includes an IMU which includes an accelerometer. In this project, the accelerometer will be used to control motor. As the accelerometer detects a sideways motion it will spin the motor clockwise and and is detects the opposite sideways motion it will spin counterclockwise -- in the opposite direction.
Tools and Materials
![s4e42%OIRRWEJgGNn3KoFw_thumb_220.jpg](/proxy/?url=https://content.instructables.com/FJ3/RTK5/J52Q8RIZ/FJ3RTK5J52Q8RIZ.jpg&filename=s4e42%OIRRWEJgGNn3KoFw_thumb_220.jpg)
- Arduino 101
- Breadboard
- SparkFun Motor Driver - https://www.sparkfun.com/products/9457
- DC Motor
- Jumper Wires
Circuitry
![Screen Shot 2017-07-14 at 10.14.53 PM.png](/proxy/?url=https://content.instructables.com/F5Q/E0DF/J52Q7HU5/F5QE0DFJ52Q7HU5.png&filename=Screen Shot 2017-07-14 at 10.14.53 PM.png)
![s%ithIteSQyOH%NGaOxJ7Q_thumb_21d.jpg](/proxy/?url=https://content.instructables.com/FV9/ODR3/J52Q8RJM/FV9ODR3J52Q8RJM.jpg&filename=s%ithIteSQyOH%NGaOxJ7Q_thumb_21d.jpg)
The circuitry is the same as connecting a motor to the Arduino. There are many ways of driving a motor using an Arduino, two of which have been covered in past modules, and this will be using a Sparkfun Motor Driver board. However, this project will not require the use of the previous Instructable.
Connecting power from the Arduino power to the breadboard
- We will need two sources, one with 5V and the other at 3.3V, so we will be using both sides of the ground rails of the breadboard in this project.
- As a convention and as an organizational tool, all 5V sources will be marked with red and black jumper wires. While, the 3.3V sources are marked with orange and brown jumper wires.
- Connect the 5V pin from the Arduino to one of the red power rails on the breadboard with a red jumper wire.
- Connect the 3.3V pin from the Arduino to the other red power rail on the breadboard using an orange jumper wire.
- Connect the GND from the Arduino to the 5V black ground rail on the bread board using a black jumper wire.
- Connect the 5V black ground rail to the 3.3V black ground rail using a brown jumper wire. Ground is ground so it doesn't make a difference which ground we use.
When wiring the SparkFun motor driver, it is easiest to follow the wiring diagram for the motor driver since the pin names are on the opposite side facing the breadboard.
- Connect all the power and ground for the board to 3.3V.
- Connect the power and ground designated the motor (on the left side) to 5V.
- Connects pins 4, 5, and 6 from the Arduino to the motor driver board on the breadboard.
Connecting the motor to the motor driver board.
- Connect the motor pins to the 2nd last and 3rd last pin on the left side of the board. The order does not matter. This will be supplied 5V by the motor driver board.
Code
![Screen Shot 2017-07-14 at 10.15.31 PM.png](/proxy/?url=https://content.instructables.com/F46/AEEW/J52Q7HUH/F46AEEWJ52Q7HUH.png&filename=Screen Shot 2017-07-14 at 10.15.31 PM.png)
#include "CurieIMU.h"
const int DIR_A = 4; const int DIR_B = 5; const int PWM = 6;
// previous orientation (for comparison). Start at (-1) to start with
void setup() { //set motor control pins to OUTPUT pinMode(DIR_A, OUTPUT); pinMode(DIR_B, OUTPUT); pinMode(PWM, OUTPUT);
// Start the acceleromter CurieIMU.begin();
// Set the accelerometer range to 2G CurieIMU.setAccelerometerRange(2); }
void loop() { // read accelerometer: int x = CurieIMU.readAccelerometer(X_AXIS); int y = CurieIMU.readAccelerometer(Y_AXIS); int z = CurieIMU.readAccelerometer(Z_AXIS);
// calculate the absolute values, to determine the largest int absX = abs(x); int absY = abs(y); int absZ = abs(z);
if ( (absZ > absX) && (absZ > absY)) { // base orientation on Z if (z > 0) { brake(); } }
//else if Y is greater than X and Z its on edge else if ( (absY > absX) && (absY > absZ)) { // if Y is positive orientation (digital pins up)and is set to 1 if (y > 0) { forward(); } //the Y is in the negative orientation (analog pins up) and is set to 2 else { reverse(); } } } //custom function for driving the motor forward void forward() { digitalWrite(DIR_A, HIGH); digitalWrite(DIR_B, LOW); digitalWrite(PWM, HIGH); }
//custom function for driving the motor in reverse void reverse() { digitalWrite(DIR_A, LOW); digitalWrite(DIR_B, HIGH); digitalWrite(PWM, HIGH); }
//custom function for braking the motor void brake() { digitalWrite(DIR_A, LOW); digitalWrite(DIR_B, LOW); digitalWrite(PWM, LOW); }
Demo
![Accelerometer with Arduino 101 Demo](/proxy/?url=https://content.instructables.com/FGW/U02M/J52Q7HPT/FGWU02MJ52Q7HPT.jpg&filename=Accelerometer with Arduino 101 Demo)
The motor will spin one way when the Arduino is tilted left, and spin the opposite direction when tilted in to the right.