Professional Documents
Culture Documents
Objective
The objective of this Interface is to show how to configure our sensors to read the MPU6050
sensor, HMC5883L and Barometric (BMP180). This sensor is an integrated Accelerometer
and Gyroscope, Barometric and Magnetometer with I2C communication. To demonstrate
its operation we will make an example where we will send the data read from the sensor to
the RS232 port to visualize them in the compiler terminal.
Outline
The first thing we will do is the corresponding configuration of the resources of our card
in STM32CubeMX as follows:
We created a new project (Image 1) and selected the STN32F407VG microcontroller, as
we already know, which is the one corresponding to our
The next step is to enable the I2C port number 1, 2, 3 respectively. The MMPU6050
Barometric and Hmc588L sensor is connected to this port. As you know this port uses two
pins or ports to perform the communication:
SDA: Bidirectional synchronous serial port, through which data is transmitted from
device to device.
SCL: Synchronous data serial port, this generates a clock signal with the speed of data
transfer to the sensor.
In the section "pin out" we enable the port I2C1 and put it in "I2C" mode, as shown in
Figure 4. Also in the image seen in the microcontroller pins ports are
enabled PB7 = I2C_SDA and PB6 = I2C_SCL . These ports are connected by default to
the sensor, therefore it is not necessary to move or rename them.
An additional device (an EEPROM memory) is also connected to port I2C1, as detailed in
table 1. Both devices have their ID or slave identification address configured as hardware,
as shown in the table. In the I2C protocol, the ID is always sent at the beginning of a
communication between master-slave devices. These addresses are used to control which
slave device the master device will communicate with. When a slave device recognizes its
ID, it responds to the master device through a recognition signal (acknowledgemt in
English), it is at that moment when communication is established between them.
Sensors Description ID
Mpu6050 Accelerometer/gyroscope 0X68
Barometric Bmp180 0xEE
HMC5883L magnetometer 0x1E
If we want to use another device with I2C protocol, it will not be possible to use
the I2C1 bus , since it is not mapped to any physical pin of the expansion ports CN1, CN2
and CN3; however the microcontroller has 2 additional I2C ports, which are possible to
enable (I2C2 or I2C3). We recommend reviewing the in section 5.14 Expansion ports to
identify in which connector we have an additional I2C port available.
Image 4. Enabling the I2C port and devices connected to the same port.
STM32F407vgtx Microprocessor
The STM32 F4-series is the first group of STM32 microcontrollers based on the ARM Cortex-M4 core.
It has Digital Signal Processing (DSP) and floating point Unit (FPU). ARM Cortex-M4 core runs at a
maximum clock rate of 84 / 168 / 180 MHz Most often controller work well with 168 MHZ. It supports
standard embedded memories (SRAM is up to 196 KB out of which 128KB on bus matrix and 64KB on
data bus dedicated for CPU usage. and 4KB for battery backup for RTC) Up to 1MB Flash.
The microprocessor chip STM32f407 based on high performance ARM 32-bit Cortex™-M4 RISC is
selected as the core chip of the hardware system. The external 8 MHz crystal oscillator is used, and the
operating frequency is up to 168 MHz which is sufficient for the high-precision real time data processing.
It has 1M bytes of flash memory and 192k bytes of SRAM. The chip has multiple general-purpose timers
and communication interfaces including three I2Cs, three SPIs and four USARTs. This is convenient to
communicate with the peripheral circuits.
The microprocessor real-timely collects and processes large amounts of data from the GPS receiver,
accelerometer, gyroscope, digital compass and barometer to determine the system.
It also features standard and advanced high performance communication interfaces,
❖ An USB OTG-HS (High Speed) and USB OTG – FS (Full Speed) with full speed compatibility.
❖ Two UARTs and four USARTs.
❖ Three I2C (Inter Integrated Circuits).
❖ Three full duplex SPI (Serial Peripheral Interface)
❖ Three ADCS.
1.2 Bm180 sensor interfacing
1.4. Digital Barometer Pressure Sensor (BMP180) using STM32F407VGTX microcontroller
1.4.1 Principles of Digital Barometer Pressure Sensor
The definition of pressure is a force “pressing” on an area. A common unit of pressure is pounds per
square inch (psi). One pound, pressing on one square inch, equals one psi. The SI unit is newton per
square meter, which are called Pascal’s (Pa).
There are lots of situations in which pressure can be measured (gravity, pull, etc.), but right now we’re
interested in atmospheric pressure, which is the force that the air around you is exerting on everything.
The weight of the gasses in the atmosphere creates atmospheric pressure. One doesn’t normally notice
that air weighs anything, but if you took a one inch wide column of air from sea level to the top of the
atmosphere, it would weigh about 14.7 pounds. (A 1 cm wide column of air would weigh about 1 kg.)
This weight, pressing down on the footprint of that column, creates the atmospheric pressure that we can
measure with sensors like the BMP180.
1.4.2 Temperature Effects
Because temperature affects the density of a gas, and density affects the mass of a gas, and mass affects
the pressure (whew), atmospheric pressure will change dramatically with temperature. Pilots know this as
“density altitude”, which makes it easier to take off on a cold day than a hot one because the air is denser
and has a greater aerodynamic effect. To compensate for temperature, the BMP180 includes a rather
good temperature sensor as well as a pressure sensor. To perform a pressure reading, you first take a
temperature reading, then combine that with a raw pressure reading to come up with a final temperature-
compensated pressure measurement.
This sensor is used to measure the pressure which will be used to measure the altitude. The
Temperature measurement can also be performed using this sensor. The sensor is designed to be
connected directly to a microcontroller of a mobile/computer device via the I2C bus. The pressure and
temperature data has to be compensated by the calibration data of the E2PROM of the sensor. The
pressure sensor consists of a piezo resistive Sensor, an analog to digital converter (ADC) and a control
unit with E2PROM and a serial I2C interface And USART.
It delivers the uncompensated value of pressure and temperature. The E2PROM has stored 176 bit of
individual calibration data. The 176 bit E2PROM is partitioned in 11 words of 16 bit each. These contain
11 calibration coefficients. Every sensor module has individual coefficients.
Before the first calculation of temperature and pressure, the master reads out the E2PROM data. The
BMP sensor will gives the Uncompensated Pressure (UP) and Uncompensated Temperature (UT).
Pressure data is a 16 to 19 bit and Temperature data is a 16 bit. The temperature data word UT, the
pressure data word UP and the calibration data read out from the sensor are used to calculate the true
pressure in steps of 1Pa (= 0.01hPa = 0.01mbar) and temperature in steps of 0.1°C.
I have also added possibility, that you calculate pressure right above the sea from known pressure at known
altitude.
❖ Sequence for reading useful data is:
❖ Start temperature measurement ✓ Wait 4.5ms at least
❖ Read temperature
❖ Start pressure measurement
❖ Delay depends on measurement mode you select
❖ Read pressure
❖ Use pressure data
1.1.4 Features of BMP180 SENSOR
❖ Read BMP180 temperature sensor
❖ Calculate sea level pressure at from given know pressure and altitude
❖ If you are at ex 1000meters and there is pressure 98000Pascals, then you can calculate pressure at sea level
from this values.
CHAPTER TWO
SENSOR INTERFACING AND MODULE CONFIGURATION
2.1 MPU6050 Digital 6- DOF with Stm32f4 Microcontroller
MPU6050 is the world’s first integrated 6 axis motion sensor, it combines one 3 axis accelerometer and
one 3 axis gyroscope, and it has its own digital motion processor (DMP) which can process the motion
data with its inside algorithm. It can output 6 axis raw data as well as 6 axis data which pass through the
Kalman filter or processed by the Quaternion algorithm. However, access to the filtered data as well as
the DMP need specific permission, so only the raw data is used in this project. This sensor can also attach
a 3 axis compass through the I2C bus which makes it a 9 axis inertial motion sensor. The chip itself has
an internal 16 bit analog to digital converter (ADC), so the output data are 16 bit digital values. There are
117 registers in total inside the chip and all of the registers are 8 bit, so it needs two registers to hold the
value for one axis’ data. The detection range of the accelerometer is +2g, 4g, 8g, 16g and that of the
gyroscope is +250, 500, 1000, 2000º/s, the range can be chosen by setting the corresponding registers.
MPU6050 is communicated with microcontroller through the I2C data bus at the clock frequency of 100
kHz. The InvenSense MPU6050 with embedded 3-axis MEMS accelerometer and gyroscope is chosen.
The acceleration along a particular axis induces a displacement on the corresponding proof mass which is
detected by the capacitive sensors and converted into a linear voltage signal. Figure shows the break out
board of MPU6050. The 16-bit sigma-delta ADC on each axis provides digital outputs. The full scale
range of the acceleration can be adjusted to ±2g, ±4g, ±8g, or ±16g. When the gyroscope is rotated
around the sense axis, the Coriolis force causes a vibration that is picked-up by the sensing capacitors and
converted into a voltage proportional to the angular rate by the signal conditioning circuit. This voltage is
digitized using on-chip 16-bit ADC. The full-scale range of the gyroscope can be programmed to ±250,
±500, ±1000, or ±2000 degrees per second
The output value of the chip is not the actual acceleration or angular velocity. It has to be calibrated
according to the calibration value in the datasheet. For example, if the range of the accelerometer is ±4g
(or 18 ±500 for the gyroscope) according to the datasheet, its units is 8192 LSB/g (65.5 LSB/ (°/sec) for
the gyroscope), which means that if the digital data we get from the chip is 9000, the actual value should
be 9000/8192=1.1g for the accelerometer and 9000/65.5=137.4°/sec for the gyroscope
2.1.1 3-Axis Gyroscope
The MPU6050 consist of 3-axis Gyroscope with Micro Electro Mechanical System (MEMS) technology.
It is used to detect rotational velocity along the X, Y, Z axes as shown in below figure. The MPU
includes a 3-Axis vibratory MEMS rate gyroscope, which detect rotations about the X-, Y-, and Z- Axes.
When the gyro is are rotated about any of the sense axes, the Coriolis Effect causes a vibration that is
detected by a capacitive pickoff. The resulting signal is amplified, demodulated, and filtered to produce a
voltage that is proportional to the angular rate. This voltage is digitized using individual on-chip 16-bit
Analog to- Digital Converters (ADCs) to sample each axis. The full-scale range of the gyro sensor may
be digitally programmed to ±250, ±500, ±1000, or ±2000 degrees per second (dps). The ADC sample rate
is programmable from 8,000 Samples per second, down to 3.9 samples per second, and user selectable
low pass filters enable a wide range of Cut-off frequencies.
Gyroscope Features
The triple-axis MEMS gyroscope in the MPU-6050 includes a wide range of features:
✓ Digital-output X-, Y-, and Z-Axis angular rate sensors (gyroscopes) with a user-programmable full-
scale range of ±250, ±500, ±1000, and ±2000°/sec
✓ Sensitivity: 131 LSB/dps (±250 dps), 65.5 LSB/dps (±500 dps), 32.8 LSB/dps (±1000 dps), and 16.4
LSB (±2000 dps).
✓ External sync signal connected to the FSYNC pin supports image, video and GPS synchronization
MPU6050 orientation and polarity of rotation
❖ When the gyros are rotated about any of the sense axes, the Coriolis Effect causes a vibration that is
detected by a MEM inside MPU6050.
❖ The resulting signal is amplified, demodulated, and filtered to produce a voltage that is proportional to
the angular rate.
❖ This voltage is digitized using 16-bit ADC to sample each axis.
❖ The full-scale range of output are +/- 250, +/- 500, +/- 1000, +/- 2000.
❖ It measures the angular velocity along each axis in degree per second unit.
Figure: 13 Acceleration along the axes deflects the movable mass.
This displacement of moving plate (mass) unbalances the differential capacitor which results in sensor output.
Output amplitude is proportional to acceleration
❖ 16-bit ADC is used to get digitized output.
❖ The full-scale range of acceleration are +/- 2g, +/- 4g, +/- 8g, +/- 16g.
❖ It measured in g (gravity force) unit.
❖ When device placed on flat surface it will measure 0g on X and Y axis and +1g on Z axis
Calculations
Note that gyroscope and accelerometer sensor data of MPU6050 module consists of 16-bit raw data in
2’s complement form.
Temperature sensor data of MPU6050 module consists of 16-bit data (not in 2’s complement form). Now
suppose we have selected,
- Accelerometer full scale range of +/- 2g with Sensitivity Scale Factor of 16,384 LSB (Count)/g.
- Gyroscope full scale range of +/- 250 °/s with Sensitivity Scale Factor of 131 LSB (Count)/°/s.
Then, to get sensor raw data, we need to first perform 2’s complement on sensor data of Accelerometer
and gyroscope.
After getting sensor raw data we can calculate acceleration and angular velocity by dividing sensor raw
data with their sensitivity scale factor as follows,
Accelerometer values into Gs (g force)
Raw Accelerometer value / 16384 (this is for +-2 G sensitivity
Acceleration along the X axis = (Accelerometer X axis raw data/16384) g.
Acceleration along the Y axis = (Accelerometer Y axis raw data/16384) g.
Acceleration along the Z axis = (Accelerometer Z axis raw data/16384) g.
Gyroscope values in °/s (degree per second)
In order to get degrees/sec form raw value;
Raw value of Gyro / 131 (this is for +-250 dps sensitivity)
Angular velocity along the X axis = (Gyroscope X axis raw data/131) °/s.
Angular velocity along the Y axis = (Gyroscope Y axis raw data/131) °/s.
Angular velocity along the Z axis = (Gyroscope Z axis raw data/131) °/s.
Temperature value in °/c (degree per Celsius)
Temperature in degrees C = ((temperature sensor data)/340 + 36.53) °/c.
For example, Suppose, after 2’ complement we get accelerometer X axes raw value = +15454
Then Ax = +15454/16384 = 0.94 g.
Registers 59 _ 64 – Accelerometer Measurements
ACCEL_XOUT_H, ACCEL_XOUT_L,
ACCEL_YOUT_H, ACCEL_YOUT_L,
ACCEL_ZOUT_H, and ACCEL_ZOUT_L
Type: Read Only
Register Register Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
(Hex (Decimal)
41 65 TEMP_OUT[15:8]
42 66 TEMP_OUT[15:8]
Register Register Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
(Hex) (Decimal)
43 67 GYRO_XOUT[15:8]
44 68 GYRO_XOUT[7:0]
45 69 GYRO_YOUT[15:8]
46 70 GYRO_YOUT[7:0]
47 71 GYRO_ZOUT[15:8]
48 72 GYRO_ZOUT[7:0]
Each 16-bit gyroscope measurement has a full scale defined in FS_SEL (Register 27). For each full scale
setting, the gyroscopes’ sensitivity per LSB in GYRO_XOUT is shown in the table below.
CHAPTER THREE
Digital Compass Module (HMC5883L) Using STM32F407vgtx Microcontroller
3.1 Principles of magnetometer (HMC5883L)
Magnetometer measures the magnetic field it is applied to. The magnetometer outputs
Three magnitudes: X, Y and Z. From these three values you can construct the magnetic field vector (magnitude
and direction) B= [X, Y, Z]
Magnetometers are devices that measures magnetic fields and it can sense where the strongest
Magnetic force is coming from. This sensor is a surface-mounted, multi-chip module designed for low-field
magnetic sensing with a digital interfaces for application such low-cost compassing and magnetometer. The
sensor include our state-of-the-art, high-resolution series magneto-resistive sensors plus an ASIC containing
amplification, automatic degaussing strap drivers, offset cancellation, and a 12-bit ADC that enables 1° to 2°
compass heading accuracy. The I2C serial bus allows for easy interface. This is a 3.0x3.0x0.9mm surface mount
16-pin leadless chip carrier (LCC). Includes on-board voltage regulator, allowing you to power the module with
voltages between 3.3V and 6V. The module includes on-board pull-up resistors on the I2C lines Applications
for this sensor include Mobile Phones, Notebooks, Consumer Electronics, Auto Navigation Systems, and
Personal Navigation Devices. These anisotropic, directional sensors feature precision in-axis sensitivity and
linearity. Magnetometer full scale range is ±8 gauss and has a resolution of up to 5 mG. A Magnetic compass
works by aligning itself to the earth magnetic field, because the compass needle is a ferrous material, it aligns
swings on its bearing in the center of the magnetic field of the earth pulls it into alignment. These magnetic
fields expand throughout the surface of the earth (and beyond) so we can use them to help us which direction
we facing. The Compass Module 3-Axis HMC5883L is a low-field magnetic sensing device with a digital
interface. The compass module converts any magnetic field to a differential voltage output on 3 axes (x, y and
z). This voltage shift is the raw digital output value, which can then be used to calculate headings or sense
magnetic fields coming from different directions. The module is designed for use with a large variety of
microcontrollers with different voltage requirements.
This device is controlled and configured via a number of on-chip registers, which are described in this section.
In the following descriptions, set implies a logic 1, and reset or clear implies a logic 0, unless stated
otherwise. All address locations are 8 bits.
Configuration Register A
The configuration register is used to configure the device for setting the data output rate and measurement
configuration. CRA0_CRA7 indicate bit locations, with CRA denoting the bits that are in the configuration
register A. CRA7 denotes the first bit of the data stream. The number indicates the default value of that bit.
DO0_DO7indicate Data Output Rate Bits. MS0_ MS1 indicate Measurement Configuration Bits.
Configuration Register B
The configuration register B for setting the device gain. CRB0_CRB7 indicate bit locations, with CRB
denoting the bits that are in the configuration register. CRB7 denotes the first bit of the data stream. The
number in parenthesis indicates the default value of that bit.CRB0_CRB4 indicate zero value.
Mode Register
The mode register is an 8-bit register from which data can be read or to which data can be written. This
register is used to select the operating mode of the device. MR0 through MR7 indicate bit locations, with MR
denoting the bits that are in the mode register. MR7 denotes the first bit of the data stream. The number in
parenthesis indicates the default value of that bit. MD1 to MD0 Mode Select Bits. These bits select the
operation mode of this device.
Status Register
The status register is an 8-bit read-only register. This register is used to indicate device status. SR0 through
SR7 indicate bit locations, with SR denoting the bits that are in the status register. SR7 denotes the first bit of
the data stream.
Identification Register A
The identification register A is used to identify the device. IRA0 through IRA7 indicate bit locations, with
IRA denoting the bits that are in the identification register A. IRA7 denotes the first bit of the data stream. The
number in parenthesis indicates the default value of that bit.
The identification value for this device is stored in this register. This is a read-only register. Register values.
ASCII value H.
Identification Register B
The identification register B is used to identify the device. IRB0 through IRB7 indicate bit locations, with IRB
denoting the bits that are in the identification register A. IRB7 denotes the first bit of the data stream. Register
values. ASCII value 4
Identification Register C
The identification register C is used to identify the device. IRC0 through IRC7 indicate bit locations, with IRC
denoting the bits that are in the identification register A. IRC7 denotes the first bit of the data stream. Register
values. ASCII value 3.