How to Use Accelerometers on the Arduino - Ultimate Guide to the Arduino #42
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
🛠️ 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.
📏 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.
🔍 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.
📊 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
💡Sensors
💡IoT (Internet of Things)
💡ADXL345
💡SPI (Serial Peripheral Interface)
💡I2C (Inter-Integrated Circuit)
💡Acceleration
💡Two's Complement
💡Calibration
💡SunFounder
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
the three in one smart car and iot
learning kit from Sun founder is a
Hands-On all included Electronics kit
that is perfect for anyone who wants to
learn how to master the Arduino
the kit comes with an Arduino 22
different sensors and modules
thread boards jumper wires and
everything else you need to build a
bunch of fun and interesting projects
learn about robotics by building a
remote controlled smart car that can be
controlled with an infrared remote
controller
or drive on its own and avoid obstacles
or Follow The Line
learn about the internet of things with
a project that lets you monitor the
temperature humidity and light level of
a room from an app on your smartphone
[Music]
and build a plant monitor that tracks
the temperature humidity light intensity
and soil moisture
and displays it on your smartphone so
you can keep your plants water remotely
it's a super cool kit and I had lots of
fun building all the projects in it so
click the link in the description below
to order the kit from some founder
foreign
[Music]
we're going to learn about
accelerometers
accelerometers are used to detect motion
and orientation
you can use an accelerometer to wake up
an Arduino when it's tapped or picked up
by someone
they can also detect when a device is
falling
computers have accelerometers to turn
off the hard drive if it's dropped
they're also used in smartphones to turn
off the display after it hasn't been
moved in a while
the accelerometer we're going to look at
is the adxl 345
mine is a breakout module from Adafruit
this is the sensor chip right here
these are directional arrows that show
you where each axis points
the x-axis points in this direction
the y-axis points in this direction
and the z-axis points up
the adxl 345 can communicate with the
Arduino over SPI and i2c
in this video we're going to use i2c but
SPI is an option if you want to use it
up here at the top we have the VN pin
where the 5 volt power connects
one down from that is the 3.3 volt pin
you can power the adxl345 with either
3.3 volts or 5 volts
this is where the 3.3 volt power source
would connect
here's the ground pin
this is the Cs pin or chip select pin
for connecting it with SPI
the adxl 345 has two internet pins
N1 and N2
these can be used to trigger Hardware
interrupts on the Arduino
this is the sdo or slave data output pin
it functions the same as a miso pin
which transmits data from the sensor to
the Arduino over SPI
thank you now we have the two i2c pins
SDA and scl
the SDA pin doubles as the most C pin
for SPI
and the scl pin doubles as the scrk PIN
for SPI
acceleration is the rate of increase or
decrease of velocity
when you're driving in your car and you
step on the gas
your car has a positive acceleration
when you step on the brakes it has a
negative acceleration
accelerometers measure both static
acceleration and dynamic acceleration
static acceleration is caused by forces
like gravity
Dynamic acceleration is caused by forces
of motion like the acceleration from a
car speeding up or slowing down
two common units of acceleration are
meters per second squared and G's
1G is defined as the rate of
acceleration of gravity
which is 9.8 meters per second squared
the adxl 345 output separate
acceleration measurements for each axis
x y and z
the adxl345 measures acceleration by
detecting changes in capacitance
along each axis there's a tiny plate
suspended between two micro Springs they
can move back and forth
the mobile plate and the fixed plate are
charged
so an electric field is formed between
them
when the accelerometer is at rest the
electrical field between the plates is
constant
when the sensor accelerates the mobile
plate moves and the distance between the
plates changes
capacitance is a function of the
distance between two charge plates
so when the distance between the plates
changes
the electrical field between the plates
also changes
the sensor measures this changing
capacitance and calculates an
acceleration value
there are a few different kinds of
accelerometers you can get for the
Arduino
the main difference between them is
sensitivity
an accelerometer sensitivity is usually
provided in G's
some accelerometers can measure
acceleration between plus or minus 1G
other accelerometers can measure
acceleration up to plus or minus 250 G's
the smaller the acceleration range the
more sensitive the sensor
to detect small taps or vibrations you
would probably want an accelerometer
with a range of about 2gs
to detect something much stronger like a
rocket launcher a collision
you'd probably want to range closer to
250 G's
the adxl 345 has four different ranges
that you can select
plus or minus two G's plus or minus 4
G's plus or minus 8 G's and plus or
minus 16 G's
in the sketch we're going to use in a
minute you'll be able to set the
sensitivity
now let's connect the accelerometer to
the Arduino and take a look at the raw
values it provides
we're going to connect the accelerometer
with i2c so the wiring is pretty simple
ground connects to ground
and VCC connects to 5 volts
the SDA pin of the accelerometer
connects to analog pin A4 on the Arduino
and the scl pin in the accelerometer
connects to analog pin A5 on the Arduino
to program it we're going to need a
library
SparkFun has a great one with a lot of
useful functions
you can download it from this link
once you get that installed we can take
a look at the sketch
this sketch will output The Rock
accelerometer readings to the serial
monitor
the first thing we do is include the
SparkFun adxl 345 library with a hash
include
sparkphone underscore adxl345.h
next we create an object called adxl
which is a member of the adxl 345 class
we set that object equal to the function
adxl345.
the adxl 345 function configures the
communication mode the sensor we use to
talk to the Arduino
when there are no arguments passed to
the function as in this case
the sensor will use ITC to communicate
with the Arduino
if you're using SPI though put the
number 10 in here
we're using i2c so we can leave this
empty
now we declare a variable called range
this will store the sensitivity range we
want the adxl 345 to have
it can be either 2 4 8 or 16 G's
in the setup section we initialize the
serial monitor with serial begin 9600
now we initialize the adxl 345 with the
function power on
this function is applied to the adxl
object which we created up here
next we call the set range setting
function to set the sensitivity range of
the sensor
we pass out the range variable which
stores the range setting we defined when
we declared it
that's all the setup we needed to do
in the loop section the first thing we
have to do is declare some variables
that will hold the sensor readings
the adxl345 outputs separate
acceleration measurements for each axis
so we need a unique variable for each
axis
here I've declared three int variables x
y and z
next we get the sensor readings from the
accelerometer with the read Excel
function
the arguments are the variables that
hold the axis measurements
it doesn't matter what you name these
variables
the only thing that matters is the order
that they're entered into the function
the x-axis values will be stored in the
variable you put in the first parameter
the y-axis values will be stored in the
variable in the second parameter
and the z-axis values will be stored in
the variable you put as the third
parameter
now all we have to do is print the
variables
it'll be easier to see if we print all
the axes on one line
so I print in x equals with serial print
then I print the X variable
same thing for the Y and Z variables
print a y
then print the Y variable
and a z then print the Z variable
the Z variable is the last thing we
print on the line so we use serial print
line
then we delay 250 milliseconds to slow
down the output a bit
okay let's check this out
I've got my adxl 345 connected to my
Arduino
I also attached the sensor to a block of
wood so it's easier to see which axis is
which
you can see the sensor is outputting
values close to zero for the X and Y
axes
but the z-axis is outputting a value
around 230.
the force of gravity is creating an
acceleration vector along the z-axis
if I turn the sensor so that the y-axis
is perpendicular to the ground it'll
measure the acceleration from Gravity
along the y-axis
of course you can also create
acceleration yourself
watch the values of the X and Y axes
when I move the sensor around
these values aren't like other raw ADC
values we've seen when we read voltages
those values were always between 0 and
10 23.
the numbers you see here are in two's
complement format
two's complement is a way to represent
signed or negative numbers in binary
we can convert these two's complement
values to G's
with the conversion factors in the
datasheet
in the specifications table on page 3
there's a section called sensitivity
this section has the scale factors for
each sensitivity range
to get the acceleration readings in G's
we need to multiply the sensors two's
complement output by one of these scale
factors
each sensitivity range has a different
scale factor
this is the scale factor for the plus or
minus 2G sensitivity range
here's the scale factor when the range
is plus or minus 4 G's
eight G's
and 16 G's
now let's look at a sketch that outputs
the accelerometer readings in G's
foreign
actually this sketch is going to Output
the sensor readings in milligs
if you want the output in G's though
divide the millig's reading by 1000.
the first half of the sketch is the same
as the raw data sketch
we include the smartphone adxl 345
Library
create the adxl object
and configure i2c communication
Define the range variable
initialize the serial monitor
initialize the sensor
and set the range
in the loop we still declare X Y and Z
variables
and get the readings for each axis with
the read Excel function
now we convert the raw sensor readings
to G's
by multiplying the raw sensor readings
by the scale factor
the scale factor is going to be
different depending on which range
setting we choose
so this is a good place for a switch
case statement
we can use the switch statement and put
the range variable as the condition
case statement will Define what happens
when the range is set to 2 4 8 or 16 G's
when the range variable is equal to 2
the code in the first case will be
executed
the scale factor for 2G sensitivity is
3.9
so we have to multiply the output from
each axis by 3.9
then we have the break keyword so the
program exits the case
now we have case statements for all the
other possible values of the range
variable
when the range is set to 4Gs
the scale factor is 7.8
so we multiply each axis output by 7.8
when the range is set to AGS
the scale factor is 15.6
so we multiply all three axis variables
by 15.6
and when the range is set to 16 G's
the scale factor is 31.2 so we have to
multiply everything by 31.2
then we have a default statement at the
end
in case the range variable is set equal
to something different than 2 4 8 or 16.
the default code prints a line that says
please specify a range of 2 4 8 or 16
G's
now we print the X Y and Z variables to
the serial monitor
we print some text that says x equals
then print the X variable
we print some text that says y equals
and print the Y variable
same thing for Z
and print line the Z variable
then we delay for 250 milliseconds
let's take a look at how this works
so now the acceleration readings from
each axis are in milligs instead of
two's complement
the z-axis value is around 910 milligs
that's pretty close to 1G which is the
acceleration due to gravity
if your sensor readings seem a bit off
your sensor might need to be calibrated
there's a good SparkFun tutorial that
shows you how to calibrate an adxl345
visit this link to check it out
accelerometers measure acceleration
along the X Y and Z axes
but in the next video we're going to
look at gyroscopes which measure
rotation around the X Y and Z axes
[Music]
Sun founder is my go-to source for
sensors modules and other parts for the
Arduino and Raspberry Pi
they have a huge selection of stem
Robotics and iot kits and lots of useful
sensors and modules
every product has an online tutorial
with wiring diagrams and example code
they also offer free shipping on all
orders with no minimum give them a try
at
www.sunfounder.com next time you need to
order some parts
تصفح المزيد من مقاطع الفيديو ذات الصلة
5.0 / 5 (0 votes)