How to Use Accelerometers on the Arduino - Ultimate Guide to the Arduino #42

Circuit Basics
11 Jul 202319:12

Summary

TLDRThe video introduces the 'Three in One Smart Car and IoT, Learning Kit' from SunFounder, an all-inclusive electronics kit designed for mastering Arduino. It includes an Arduino, sensors, modules, and tools for building various projects, such as a remote-controlled smart car, IoT temperature and humidity monitoring, and a plant monitor. The script also covers the ADXL345 accelerometer, detailing its functionality, wiring, and programming with the Arduino, showcasing raw data and G's conversion, and highlighting SunFounder's extensive support for STEM and IoT projects.

Takeaways

  • πŸš— The 'Three in One Smart Car and IoT Learning Kit' from SunFounder is an all-inclusive electronics kit designed for mastering Arduino.
  • πŸ”§ The kit includes an Arduino, 22 different sensors and modules, breadboards, jumper wires, and other necessary components for building various projects.
  • πŸ€– Learn robotics by constructing a remote-controlled smart car that can be operated with an infrared remote or programmed to drive autonomously and avoid obstacles.
  • 🌑️ Explore IoT with a project that monitors temperature, humidity, and light levels in a room via a smartphone app.
  • 🌱 Build a plant monitor to track temperature, humidity, light intensity, and soil moisture, displaying this information on a smartphone for remote plant care.
  • πŸ“ˆ Accelerometers, like the ADXL345, are used to detect motion and orientation and can be used in various applications, including waking up an Arduino or detecting device falls.
  • πŸ”’ The ADXL345 can measure both static (from gravity) and dynamic (from motion) acceleration and communicates with the Arduino over SPI or I2C.
  • πŸ“Š Accelerometers measure acceleration in units of meters per second squared or G's, with 1G being equivalent to 9.8 m/sΒ², the acceleration due to gravity.
  • πŸ” The ADXL345 outputs separate acceleration measurements for each axis (x, y, z) and uses changes in capacitance to calculate acceleration values.
  • πŸ› οΈ Different accelerometers have varying sensitivities, measured in G's, with ranges from Β±1G to Β±250G, allowing for detection of subtle to significant forces.
  • πŸ”§ The ADXL345 has four selectable sensitivity ranges: Β±2G, Β±4G, Β±8G, and Β±16G, and requires a specific library for programming and interpreting its data.

Q & A

  • What is the 'Three in One Smart Car and IoT Learning Kit' from SunFounder?

    -The 'Three in One Smart Car and IoT Learning Kit' from SunFounder is a comprehensive electronics kit designed for learning to master the Arduino platform. It includes an Arduino, 22 different sensors and modules, breadboards, jumper wires, and other necessary components to build various fun and educational projects.

  • What can you learn from the kit about robotics?

    -With the kit, you can learn about robotics by building a remote-controlled smart car that can be operated with an infrared remote controller, drive autonomously, avoid obstacles, or follow a line.

  • How does the kit help in learning about the Internet of Things (IoT)?

    -The kit includes an IoT project that allows you to monitor temperature, humidity, and light levels of a room from an app on your smartphone, and build a plant monitor that tracks and displays temperature, humidity, light intensity, and soil moisture.

  • What is an accelerometer and how is it used in the context of the ADXL 345?

    -An accelerometer is a sensor used to detect motion and orientation. The ADXL 345, which is featured in the script, can be used to wake up an Arduino when tapped or picked up, detect falling devices, and is used in smartphones to turn off the display after a period of inactivity.

  • How does the ADXL 345 communicate with the Arduino?

    -The ADXL 345 can communicate with the Arduino over SPI (Serial Peripheral Interface) or I2C (Inter-Integrated Circuit). The script demonstrates using I2C for communication.

  • What are the two common units of acceleration measurement mentioned in the script?

    -The two common units of acceleration measurement mentioned are meters per second squared and G's, with 1G being equivalent to the acceleration due to gravity, which is 9.8 meters per second squared.

  • How does the ADXL 345 measure acceleration?

    -The ADXL 345 measures acceleration by detecting changes in capacitance along each axis. It has a tiny plate suspended between two micro springs that can move back and forth. When the sensor accelerates, the distance between the plates changes, altering the capacitance, which the sensor measures to calculate the acceleration value.

  • What are the different sensitivity ranges of the ADXL 345?

    -The ADXL 345 has four different sensitivity ranges: plus or minus 2G, plus or minus 4G, plus or minus 8G, and plus or minus 16G.

  • How do you convert the raw sensor readings from the ADXL 345 to G's?

    -To convert the raw sensor readings to G's, you multiply the two's complement output by the scale factor provided in the datasheet for the selected sensitivity range.

  • What is the purpose of the SparkFun ADXL 345 library mentioned in the script?

    -The SparkFun ADXL 345 library is used to interface with the ADXL 345 sensor from within an Arduino sketch. It provides useful functions to initialize the sensor, set its range, and read acceleration data from the sensor.

  • How can you calibrate the ADXL 345 sensor if the readings seem off?

    -If the ADXL 345 sensor readings seem off, you can calibrate it by following a tutorial provided by SparkFun, which is mentioned in the script and can be found on their website.

  • What additional components does SunFounder provide for Arduino and Raspberry Pi projects?

    -SunFounder provides a wide selection of STEM, robotics, and IoT kits, as well as various useful sensors and modules for Arduino and Raspberry Pi projects. They also offer online tutorials with wiring diagrams and example code for every product.

Outlines

00:00

πŸ› οΈ Arduino Smart Car and IoT Kit Overview

The script introduces the 'three in one' smart car and IoT learning kit from SunFounder, an all-inclusive electronics kit designed for mastering the Arduino platform. It includes an Arduino UNO, a variety of sensors and modules, solderless breadboards, jumper wires, and other necessary components to create engaging projects. The kit facilitates learning about robotics through building a remotely controlled smart car with obstacle avoidance capabilities and line-following features. It also covers IoT by enabling the creation of projects that monitor environmental conditions and plant health through smartphone apps. The video encourages viewers to order the kit via a provided link and mentions the exploration of accelerometers, specifically the ADXL 345, in subsequent content.

05:01

πŸ“ Understanding Accelerometers and ADXL 345

This paragraph delves into the functionality of accelerometers, which are sensors used to detect motion and orientation. The ADXL 345 is highlighted as an example, with a focus on its ability to be woken up by tapping or picking up and to detect falling motions, similar to its applications in computers and smartphones. The ADXL 345's breakout module from Adafruit is described, detailing its pin functions, including power supply options (3.3 volts or 5 volts), chip select for SPI, and interrupt pins. The communication protocols available for the ADXL 345, such as SPI and I2C, are mentioned, with I2C being the chosen method for the video. The concept of acceleration, both static due to gravity and dynamic due to motion, is explained, along with the units used to measure it, such as meters per second squared and G's.

10:02

πŸ” Connecting and Programming the ADXL 345

The script provides a step-by-step guide on connecting the ADXL 345 accelerometer to an Arduino using I2C communication, with specific pin connections detailed. It also outlines the process of programming the Arduino to interact with the accelerometer, including the installation of the SparkFun ADXL 345 library. The provided sketch demonstrates how to initialize the sensor, set its sensitivity range, and read raw acceleration values from each axis. The script explains the two's complement format of the raw data and how to convert these values to G's using scale factors from the datasheet. Additionally, it discusses the sensitivity ranges of accelerometers and how to select the appropriate range for different applications.

15:03

πŸ“Š Interpreting Accelerometer Data in G's

This section of the script focuses on converting the raw accelerometer readings into meaningful measurements in G's. It explains the use of a switch-case statement to select the appropriate scale factor based on the chosen sensitivity range of the ADXL 345. The sketch provided in the script demonstrates how to perform this conversion and output the results in milligs, with an option to adjust for G's by dividing by 1000. The script also touches on the potential need for sensor calibration if readings are inconsistent and references a SparkFun tutorial for calibrating the ADXL 345. Finally, it teases the topic of gyroscopes for future videos and promotes SunFounder as a reliable source for Arduino and Raspberry Pi components, kits, and tutorials.

Mindmap

Keywords

πŸ’‘Arduino

Arduino is an open-source electronics platform based on easy-to-use hardware and software. It is the central theme of the video as the kit is designed to help users master the Arduino. The Arduino board is used to build various projects, including a smart car and IoT devices, demonstrating its versatility and importance in the script.

πŸ’‘Sensors

Sensors are devices that detect and respond to some type of input from the environment. In the video, different sensors are included in the kit to help users learn about various aspects of electronics, such as motion, orientation, temperature, humidity, and light. They are integral to the projects described, like the accelerometer and the plant monitor.

πŸ’‘IoT (Internet of Things)

IoT refers to the network of physical devices, vehicles, and other items embedded with sensors, software, and connectivity that enables these objects to collect and exchange data. The video discusses an IoT project that allows users to monitor environmental conditions via a smartphone app, illustrating the practical application of IoT in everyday life.

πŸ’‘ADXL345

The ADXL345 is a specific type of accelerometer mentioned in the script, used to detect motion and orientation. It is an example of a sensor that can be integrated with the Arduino to create interactive projects. The video explains how this sensor works and how it can be used in projects to measure acceleration along different axes.

πŸ’‘SPI (Serial Peripheral Interface)

SPI is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. The script mentions SPI as a communication protocol that the ADXL345 can use to interface with the Arduino, although I2C is used in the video's example.

πŸ’‘I2C (Inter-Integrated Circuit)

I2C is a multi-master, single-bus, serial computer bus invented by Philips Semiconductor. It is one of the most popular methods for connecting the ADXL345 to the Arduino in the video. It allows for communication between the microcontroller and the sensor with only two wires, simplifying the connection process.

πŸ’‘Acceleration

Acceleration is the rate of change of velocity of an object. The video explains that accelerometers, like the ADXL345, measure both static and dynamic acceleration. The script uses the concept of acceleration to describe how the sensor can detect motion and the effects of gravity on the sensor's readings.

πŸ’‘Two's Complement

Two's complement is a mathematical representation of signed numbers in binary form. The video explains that the raw sensor readings from the ADXL345 are in two's complement format, which is necessary to understand when converting the raw data into meaningful acceleration measurements in G's or milligs.

πŸ’‘Calibration

Calibration is the process of adjusting a system or instrument to ensure that its output is accurate and reliable. The script mentions that if sensor readings seem off, the ADXL345 might need to be calibrated, indicating the importance of this process in achieving accurate measurements.

πŸ’‘SunFounder

SunFounder is a company mentioned in the script as a source for sensors, modules, and other parts for Arduino and Raspberry Pi projects. The video promotes their learning kit and highlights their offerings, including STEM, robotics, and IoT kits, as well as their online tutorials and free shipping policy.

Highlights

The 'Three in One Smart Car and IoT, Learning Kit' from SunFounder is an all-inclusive electronics kit designed for mastering Arduino.

The kit includes an Arduino, 22 different sensors and modules, breadboards, jumper wires, and other necessary components for project building.

Learn robotics by constructing a remote-controlled smart car that can be operated with an infrared remote or autonomously to avoid obstacles or follow a line.

Explore IoT through a project that monitors room temperature, humidity, and light levels using a smartphone app.

Build a plant monitor that tracks temperature, humidity, light intensity, and soil moisture, displaying this information on a smartphone for remote plant care.

The kit is highly rated for its engaging projects and comprehensive learning experience in electronics and programming.

Accelerometers, such as the ADXL 345, are used to detect motion and orientation and can be utilized to trigger actions in response to movement or changes in position.

The ADXL 345 can be interfaced with the Arduino over SPI or I2C, offering flexibility in project design.

The ADXL 345 has multiple pins for power, interrupts, and data output, supporting both SPI and I2C communication.

Acceleration is measured in meters per second squared or G's, with 1G equal to the acceleration due to gravity at 9.8 m/sΒ².

The ADXL 345 outputs separate acceleration measurements for the X, Y, and Z axes, detecting both static and dynamic acceleration.

ADXL 345 measures acceleration by detecting changes in capacitance between suspended plates, translating movement into electrical signals.

Different accelerometers offer varying sensitivity ranges, from Β±1G to Β±250G, suitable for detecting subtle vibrations or significant impacts.

The ADXL 345 has four selectable sensitivity ranges: Β±2G, Β±4G, Β±8G, and Β±16G, allowing for customization based on the application's needs.

Programming the ADXL 345 involves using a library, such as the SparkFun ADXL 345 library, which simplifies communication and data retrieval.

Raw accelerometer data is in two's complement format, requiring conversion to G's using scale factors provided in the datasheet.

SunFounder is a recommended source for Arduino and Raspberry Pi components, offering a wide selection of STEM, robotics, and IoT kits, along with detailed tutorials and free shipping.

Transcripts

play00:00

the three in one smart car and iot

play00:02

learning kit from Sun founder is a

play00:04

Hands-On all included Electronics kit

play00:07

that is perfect for anyone who wants to

play00:09

learn how to master the Arduino

play00:11

the kit comes with an Arduino 22

play00:13

different sensors and modules

play00:15

thread boards jumper wires and

play00:18

everything else you need to build a

play00:19

bunch of fun and interesting projects

play00:22

learn about robotics by building a

play00:24

remote controlled smart car that can be

play00:26

controlled with an infrared remote

play00:27

controller

play00:28

or drive on its own and avoid obstacles

play00:31

or Follow The Line

play00:33

learn about the internet of things with

play00:35

a project that lets you monitor the

play00:37

temperature humidity and light level of

play00:40

a room from an app on your smartphone

play00:42

[Music]

play00:44

and build a plant monitor that tracks

play00:46

the temperature humidity light intensity

play00:48

and soil moisture

play00:50

and displays it on your smartphone so

play00:52

you can keep your plants water remotely

play00:56

it's a super cool kit and I had lots of

play00:59

fun building all the projects in it so

play01:01

click the link in the description below

play01:02

to order the kit from some founder

play01:05

foreign

play01:07

[Music]

play01:18

we're going to learn about

play01:19

accelerometers

play01:21

accelerometers are used to detect motion

play01:24

and orientation

play01:26

you can use an accelerometer to wake up

play01:28

an Arduino when it's tapped or picked up

play01:30

by someone

play01:31

they can also detect when a device is

play01:33

falling

play01:34

computers have accelerometers to turn

play01:36

off the hard drive if it's dropped

play01:38

they're also used in smartphones to turn

play01:41

off the display after it hasn't been

play01:42

moved in a while

play01:46

the accelerometer we're going to look at

play01:48

is the adxl 345

play01:51

mine is a breakout module from Adafruit

play01:55

this is the sensor chip right here

play01:59

these are directional arrows that show

play02:00

you where each axis points

play02:05

the x-axis points in this direction

play02:10

the y-axis points in this direction

play02:14

and the z-axis points up

play02:19

the adxl 345 can communicate with the

play02:21

Arduino over SPI and i2c

play02:24

in this video we're going to use i2c but

play02:26

SPI is an option if you want to use it

play02:32

up here at the top we have the VN pin

play02:34

where the 5 volt power connects

play02:38

one down from that is the 3.3 volt pin

play02:41

you can power the adxl345 with either

play02:44

3.3 volts or 5 volts

play02:46

this is where the 3.3 volt power source

play02:48

would connect

play02:51

here's the ground pin

play02:54

this is the Cs pin or chip select pin

play02:57

for connecting it with SPI

play03:01

the adxl 345 has two internet pins

play03:04

N1 and N2

play03:06

these can be used to trigger Hardware

play03:08

interrupts on the Arduino

play03:12

this is the sdo or slave data output pin

play03:16

it functions the same as a miso pin

play03:18

which transmits data from the sensor to

play03:20

the Arduino over SPI

play03:24

thank you now we have the two i2c pins

play03:27

SDA and scl

play03:31

the SDA pin doubles as the most C pin

play03:33

for SPI

play03:37

and the scl pin doubles as the scrk PIN

play03:40

for SPI

play03:45

acceleration is the rate of increase or

play03:47

decrease of velocity

play03:52

when you're driving in your car and you

play03:54

step on the gas

play03:55

your car has a positive acceleration

play04:00

when you step on the brakes it has a

play04:02

negative acceleration

play04:06

accelerometers measure both static

play04:07

acceleration and dynamic acceleration

play04:11

static acceleration is caused by forces

play04:13

like gravity

play04:16

Dynamic acceleration is caused by forces

play04:19

of motion like the acceleration from a

play04:21

car speeding up or slowing down

play04:25

two common units of acceleration are

play04:28

meters per second squared and G's

play04:31

1G is defined as the rate of

play04:33

acceleration of gravity

play04:35

which is 9.8 meters per second squared

play04:40

the adxl 345 output separate

play04:43

acceleration measurements for each axis

play04:45

x y and z

play04:50

the adxl345 measures acceleration by

play04:53

detecting changes in capacitance

play04:58

along each axis there's a tiny plate

play05:00

suspended between two micro Springs they

play05:03

can move back and forth

play05:07

the mobile plate and the fixed plate are

play05:09

charged

play05:09

so an electric field is formed between

play05:11

them

play05:15

when the accelerometer is at rest the

play05:17

electrical field between the plates is

play05:18

constant

play05:22

when the sensor accelerates the mobile

play05:24

plate moves and the distance between the

play05:26

plates changes

play05:29

capacitance is a function of the

play05:31

distance between two charge plates

play05:33

so when the distance between the plates

play05:35

changes

play05:36

the electrical field between the plates

play05:38

also changes

play05:40

the sensor measures this changing

play05:41

capacitance and calculates an

play05:43

acceleration value

play05:47

there are a few different kinds of

play05:48

accelerometers you can get for the

play05:49

Arduino

play05:51

the main difference between them is

play05:53

sensitivity

play05:55

an accelerometer sensitivity is usually

play05:57

provided in G's

play05:59

some accelerometers can measure

play06:01

acceleration between plus or minus 1G

play06:04

other accelerometers can measure

play06:06

acceleration up to plus or minus 250 G's

play06:13

the smaller the acceleration range the

play06:15

more sensitive the sensor

play06:19

to detect small taps or vibrations you

play06:21

would probably want an accelerometer

play06:22

with a range of about 2gs

play06:26

to detect something much stronger like a

play06:29

rocket launcher a collision

play06:30

you'd probably want to range closer to

play06:32

250 G's

play06:36

the adxl 345 has four different ranges

play06:39

that you can select

play06:40

plus or minus two G's plus or minus 4

play06:43

G's plus or minus 8 G's and plus or

play06:46

minus 16 G's

play06:48

in the sketch we're going to use in a

play06:50

minute you'll be able to set the

play06:51

sensitivity

play06:55

now let's connect the accelerometer to

play06:57

the Arduino and take a look at the raw

play06:59

values it provides

play07:01

we're going to connect the accelerometer

play07:03

with i2c so the wiring is pretty simple

play07:06

ground connects to ground

play07:10

and VCC connects to 5 volts

play07:13

the SDA pin of the accelerometer

play07:15

connects to analog pin A4 on the Arduino

play07:19

and the scl pin in the accelerometer

play07:21

connects to analog pin A5 on the Arduino

play07:27

to program it we're going to need a

play07:28

library

play07:30

SparkFun has a great one with a lot of

play07:32

useful functions

play07:34

you can download it from this link

play07:37

once you get that installed we can take

play07:39

a look at the sketch

play07:46

this sketch will output The Rock

play07:47

accelerometer readings to the serial

play07:49

monitor

play07:52

the first thing we do is include the

play07:53

SparkFun adxl 345 library with a hash

play07:56

include

play07:57

sparkphone underscore adxl345.h

play08:04

next we create an object called adxl

play08:07

which is a member of the adxl 345 class

play08:11

we set that object equal to the function

play08:13

adxl345.

play08:16

the adxl 345 function configures the

play08:19

communication mode the sensor we use to

play08:21

talk to the Arduino

play08:24

when there are no arguments passed to

play08:25

the function as in this case

play08:27

the sensor will use ITC to communicate

play08:30

with the Arduino

play08:33

if you're using SPI though put the

play08:34

number 10 in here

play08:37

we're using i2c so we can leave this

play08:38

empty

play08:43

now we declare a variable called range

play08:46

this will store the sensitivity range we

play08:48

want the adxl 345 to have

play08:51

it can be either 2 4 8 or 16 G's

play09:01

in the setup section we initialize the

play09:03

serial monitor with serial begin 9600

play09:07

now we initialize the adxl 345 with the

play09:11

function power on

play09:13

this function is applied to the adxl

play09:15

object which we created up here

play09:26

next we call the set range setting

play09:28

function to set the sensitivity range of

play09:29

the sensor

play09:32

we pass out the range variable which

play09:34

stores the range setting we defined when

play09:36

we declared it

play09:38

that's all the setup we needed to do

play09:46

in the loop section the first thing we

play09:48

have to do is declare some variables

play09:50

that will hold the sensor readings

play09:52

the adxl345 outputs separate

play09:55

acceleration measurements for each axis

play09:57

so we need a unique variable for each

play09:59

axis

play10:01

here I've declared three int variables x

play10:04

y and z

play10:06

next we get the sensor readings from the

play10:09

accelerometer with the read Excel

play10:10

function

play10:12

the arguments are the variables that

play10:13

hold the axis measurements

play10:16

it doesn't matter what you name these

play10:17

variables

play10:18

the only thing that matters is the order

play10:20

that they're entered into the function

play10:23

the x-axis values will be stored in the

play10:25

variable you put in the first parameter

play10:28

the y-axis values will be stored in the

play10:30

variable in the second parameter

play10:33

and the z-axis values will be stored in

play10:35

the variable you put as the third

play10:36

parameter

play10:39

now all we have to do is print the

play10:40

variables

play10:46

it'll be easier to see if we print all

play10:47

the axes on one line

play10:50

so I print in x equals with serial print

play10:53

then I print the X variable

play10:56

same thing for the Y and Z variables

play10:59

print a y

play11:00

then print the Y variable

play11:03

and a z then print the Z variable

play11:07

the Z variable is the last thing we

play11:08

print on the line so we use serial print

play11:10

line

play11:12

then we delay 250 milliseconds to slow

play11:14

down the output a bit

play11:18

okay let's check this out

play11:33

I've got my adxl 345 connected to my

play11:35

Arduino

play11:37

I also attached the sensor to a block of

play11:39

wood so it's easier to see which axis is

play11:40

which

play11:45

you can see the sensor is outputting

play11:47

values close to zero for the X and Y

play11:49

axes

play11:50

but the z-axis is outputting a value

play11:52

around 230.

play11:55

the force of gravity is creating an

play11:56

acceleration vector along the z-axis

play11:59

if I turn the sensor so that the y-axis

play12:01

is perpendicular to the ground it'll

play12:03

measure the acceleration from Gravity

play12:04

along the y-axis

play12:09

of course you can also create

play12:11

acceleration yourself

play12:15

watch the values of the X and Y axes

play12:17

when I move the sensor around

play12:30

these values aren't like other raw ADC

play12:32

values we've seen when we read voltages

play12:35

those values were always between 0 and

play12:37

10 23.

play12:39

the numbers you see here are in two's

play12:41

complement format

play12:43

two's complement is a way to represent

play12:45

signed or negative numbers in binary

play12:48

we can convert these two's complement

play12:50

values to G's

play12:51

with the conversion factors in the

play12:53

datasheet

play13:00

in the specifications table on page 3

play13:02

there's a section called sensitivity

play13:18

this section has the scale factors for

play13:20

each sensitivity range

play13:23

to get the acceleration readings in G's

play13:26

we need to multiply the sensors two's

play13:28

complement output by one of these scale

play13:30

factors

play13:32

each sensitivity range has a different

play13:33

scale factor

play13:35

this is the scale factor for the plus or

play13:37

minus 2G sensitivity range

play13:41

here's the scale factor when the range

play13:42

is plus or minus 4 G's

play13:45

eight G's

play13:46

and 16 G's

play13:50

now let's look at a sketch that outputs

play13:52

the accelerometer readings in G's

play13:57

foreign

play14:04

actually this sketch is going to Output

play14:05

the sensor readings in milligs

play14:08

if you want the output in G's though

play14:10

divide the millig's reading by 1000.

play14:15

the first half of the sketch is the same

play14:16

as the raw data sketch

play14:19

we include the smartphone adxl 345

play14:22

Library

play14:24

create the adxl object

play14:27

and configure i2c communication

play14:30

Define the range variable

play14:37

initialize the serial monitor

play14:40

initialize the sensor

play14:42

and set the range

play14:49

in the loop we still declare X Y and Z

play14:51

variables

play14:53

and get the readings for each axis with

play14:55

the read Excel function

play14:58

now we convert the raw sensor readings

play14:59

to G's

play15:00

by multiplying the raw sensor readings

play15:02

by the scale factor

play15:04

the scale factor is going to be

play15:06

different depending on which range

play15:07

setting we choose

play15:09

so this is a good place for a switch

play15:10

case statement

play15:16

we can use the switch statement and put

play15:19

the range variable as the condition

play15:22

case statement will Define what happens

play15:24

when the range is set to 2 4 8 or 16 G's

play15:32

when the range variable is equal to 2

play15:34

the code in the first case will be

play15:35

executed

play15:37

the scale factor for 2G sensitivity is

play15:40

3.9

play15:41

so we have to multiply the output from

play15:43

each axis by 3.9

play15:46

then we have the break keyword so the

play15:48

program exits the case

play15:52

now we have case statements for all the

play15:54

other possible values of the range

play15:55

variable

play16:00

when the range is set to 4Gs

play16:02

the scale factor is 7.8

play16:04

so we multiply each axis output by 7.8

play16:13

when the range is set to AGS

play16:15

the scale factor is 15.6

play16:18

so we multiply all three axis variables

play16:20

by 15.6

play16:28

and when the range is set to 16 G's

play16:31

the scale factor is 31.2 so we have to

play16:34

multiply everything by 31.2

play16:38

then we have a default statement at the

play16:40

end

play16:41

in case the range variable is set equal

play16:43

to something different than 2 4 8 or 16.

play16:47

the default code prints a line that says

play16:49

please specify a range of 2 4 8 or 16

play16:52

G's

play17:02

now we print the X Y and Z variables to

play17:04

the serial monitor

play17:06

we print some text that says x equals

play17:09

then print the X variable

play17:11

we print some text that says y equals

play17:14

and print the Y variable

play17:16

same thing for Z

play17:18

and print line the Z variable

play17:23

then we delay for 250 milliseconds

play17:28

let's take a look at how this works

play17:42

so now the acceleration readings from

play17:43

each axis are in milligs instead of

play17:45

two's complement

play17:53

the z-axis value is around 910 milligs

play17:57

that's pretty close to 1G which is the

play17:59

acceleration due to gravity

play18:05

if your sensor readings seem a bit off

play18:06

your sensor might need to be calibrated

play18:10

there's a good SparkFun tutorial that

play18:12

shows you how to calibrate an adxl345

play18:15

visit this link to check it out

play18:18

accelerometers measure acceleration

play18:20

along the X Y and Z axes

play18:23

but in the next video we're going to

play18:24

look at gyroscopes which measure

play18:26

rotation around the X Y and Z axes

play18:33

[Music]

play18:39

Sun founder is my go-to source for

play18:42

sensors modules and other parts for the

play18:44

Arduino and Raspberry Pi

play18:47

they have a huge selection of stem

play18:49

Robotics and iot kits and lots of useful

play18:52

sensors and modules

play18:55

every product has an online tutorial

play18:57

with wiring diagrams and example code

play19:01

they also offer free shipping on all

play19:03

orders with no minimum give them a try

play19:06

at

play19:08

www.sunfounder.com next time you need to

play19:10

order some parts

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Arduino KitSmart CarIoT ProjectsSensor ModulesSTEM LearningDIY ElectronicsInfrared ControlObstacle AvoidanceLine FollowingSunFounder