Professional Documents
Culture Documents
Unit 6
Unit 6
Based Microcontroller
STM32F4xx Interfacing with accelerometer MPU 6050, Ultrasonic Sensor HC-SR04, PWM: Controlling
speed and direction of DC Motor CAN Bus: Features, CAN Frame, sequence of transmitting and
receiving data on CAN Bus.
Unit Objectives
1.To Interface sensor using Bus protocol
2.To Understand Industrial Control application like motor control
3.To Understand the different buses and Protocol
4.To Interface of real time devices using Bus protocol
5.To Design case study for real life application using Input/ Output devices.
6.To write the C Programs for the case study.
Unit Outcomes
1. Interface Sensor using buses Mapping With COs :
2. Select the Bus Protocol for interfacing devices. CO6: Recognize the interfacing of real world
3. Interface DC motors using buses. sensors and standard buses. Will also able
4. Design case study for real life application. to design different case studies.
Mapping With POs : 1, 3
Books :
T3: Shujen Chen, Muhammad Ali Mazidi, Eshragh Ghaemi, STM32 Arm Programming for Embedded
Systems: Using C Language with STM32 Nucleo, MicroDigitalEd., 2018
R3: Joseph Yiu, The Definitive Guide to ARM® Cortex®-M3 and Cortex®-M4 Processors, Newnes;
3rd edition
Accelerometer
• There are small devices indicating
changing orientation in smart
phones, video game remotes,
quad-copters, etc.
• These devices contains gyroscopes
combined with accelerometers and
are referred to as IMU, or Inertial
Measurement Unit
• The number of sensor inputs in an
IMU are referred to as “ DOF” (
Degree of Freedom), so a chip with
a 3-axis gyroscope and a 3-axis
accelerometer would be a 6-DOF
IMU.
Gyroscope
• Many different sensory devices are used to
determine the position and orientation of an
object. The most common of these sensors
are the gyroscope and the accelerometer.
• An accelerometer is a compact device
designed to measure non-gravitational
acceleration.
• Though similar in purpose, they measure
different things. When combined into a single
device, they can create a very powerful array
of information
Accelerometers
• When the object it's integrated
into goes from a standstill to any
velocity, the accelerometer is
designed to respond to the
vibrations associated with such
movement.
• Gyroscopes can produce a better
estimate of orientation than
obtained using accelerometer
data alone.
• Digital accelerometers give
information using a serial
protocol like I2C , SPI or USART.
MPU-6050
• The MPU 6050 is the world’s first integrated 6-
axis Motion Tracking device
• It combines a 3-axis gyroscope, 3-axis
accelerometer, and a Digital Motion Processor™
(DMP) all in a small 4x4x0.9mm package.
• It uses a standard I2C bus for data transmission.
– With it’s I2C bus, it can accepts inputs from an external 3-
axis compass to provide a complete 9-axis Motion Fusion
output.
MPU 6050
• The objective is to measure Tilt Angle using MPU6050 &
STM32F4XX Microcontroller by simply interfacing
MPU6050 6 axis Gyro/Accelerometer Sensor with
STM32.
• The Accelerometer sends X, Y, and Z acceleration forces.
We need to convert the forces into X, Y, Z 3D angle to
determine the 3D Orientation of the sensor.
7
The Pinout of MPU 6050
10
Recording of Accelerometer interfacing
11
Ultrasonic Sensor HCSR 04
12
PINOUT OF ULTRSONIC SENSOR
13
WORKING PRINCIPLE
14
SPECIFICATION
Operating Voltage: 5V DC
Operating Current: 15mA
Trigger Input Signal: 10uS TTL Pulse
Measure Angle: 15°
Ranging Distance: 2cm - 4m
Working Frequency: 40Hz
Ranging Accuracy upto 3mm
15
TIMING DIAGRAM
16
Configuration of pins
As shown in the timing diagram, , we will need to send
a 10-microsecond pulse to the Trig pin of the sensor and
detect the pulse width of the pulse sent from the Echo pin
of the module.
Firstly to generate a pulse we will set PA5 low for
some time and then set it to high for the desired duration
of pulse which happens to be 10 microseconds in this
case and then set to PA5 low again.
Next, to detect the pulse coming in from the Echo pin,
we will configure PA6 as an input pin and continuously
monitor it for the duration of time it remains high.
17
Interfacing of HCSR 04 with STM
To interface an ultrasonic sensor, the HC-SR04 with the Cortex-
M4 based micro-controller, we are reading and writing individual
bits of some registers present on the micro-controller known as
Special Function Registers (SFRs).
Now, these acoustic bursts sent out by the transmitter of the
sensor module travel at the rate of 340m/s in air.
18
Interfacing of HCSR 04 with STM
In this formula, the time taken by the receiver to receive the
signal back is divided by 2, since the total time includes the time
required by the signal to travel both to and back from the object.
21
Ultrasonic Proximity Sensor
ADVANTAGES
Ultrasonic proximity sensors are not affected by
atmospheric dust, snow, rain..etc
Can work in adverse conditions
Sensing distance is more compared to inductive or
capacitive proximity sensors
DISADVANTAGES
Ultrasonic sensors Has More Difficulties Reading
Reflections From Soft, Curved, Thin Or Small Objects.
Code for interfacing HCSR 04
23
DC Motor Control using PWM
PWM signal is a modulated digital logic (0 and 1). PWM
signal have a duty cycle. Duty cycle is measured in
percentage.
PWM signal that has 100% duty cycle would be the same as
setting to fully on. 0% duty cycle would be the same as
ground. Duty cycle in between 0% and 100% are illustrated
on these graphics:
DC Motor Control using PWM
PWM (Pulse Width Modulation) is a technique for generating analog
voltage (average value) by using microcontroller's digital outputs. PWM is
used in DC motor speed control, servo motor control, dimming LED,
audio generation and many more.
Design of DC Motor control using PWM
Prof. M. N. Kakatkar,SCOE 25
Design of DC Motor control using PWM
L293D / L298
Prof. M. N. Kakatkar,SCOE 26
Selected DC Motor Characteristics
(http://www.Jameco.com)
28
Prof. M. N. Kakatkar,SCOE 37
DC Motor Control using PWM
The PWM option is selected by OCxM bits of the
Imx_CCMR register.
The OCxM bit can be set in mode 1 or Mode 2.
41
Serial Bus Overview of STM 32F
Up to 15 communication interfaces
🞑 Up to 3 × I2C interfaces
🞑 Up to 4 USARTs/2 UARTs (10.5 Mbit/s, ISO 7816
interface, LIN, IrDA, modem control)
🞑 Up to 3 SPIs (42 Mbits/s), 2 with full-duplex I2S to
achieve audio class accuracy via internal audio PLL
or external clock
🞑 2 × CAN interfaces (2.0B Active)
CAN Bus Overview
CAN (Controller Area Network) is a serial bus
system used to communicate between several
embedded 8-bit and 16-bit microcontrollers.
It was originally designed for use in the automotive
industry but is used today in many other systems
(e.g. home appliances and industrial machines).
With the use of CAN, point-to-point wiring is
replaced by a serial bus connecting all control
systems.
CAN Bus Overview
CAN is a communication protocol that connects all
devices can communicate with each other. CAN
stands for Controller Area Network.
This technology developed by Bosch Automotive
company to reduce cabling loads and make systems
separates from each other. Because if there is a
problem in any sensor, this problem will harm all
system.
BEFORE CAN
45 2/5/2022
With CAN
The solution to this problem was the connection of the control systems via
serial bus system in a vehicle. Point- to-point wiring is replaced by a serial bus
connecting all control systems. This is done by adding some CAN-specific
hardware to each control unit that provides the "rules" or the protocol for
transmitting and receiving information via the bus.
46
The Development of CAN
The development of CAN began when more and more
electronic devices were implemented into modern motor
vehicles.
Examples include engine management systems, ABS, gear
control, lighting control, air conditioning, and airbags. All this
means more safety and more comfort.
This was usually done by point to point wiring. The
requirement is that a cable network with a length of up to
several miles.
High speed, real-time communication, Error detecting
mechanism and retransmission of faulty massages.
47
The CAN bus
48
CAN bus
49 2/5/2022
Basic Configuration
49
CAN Bus Overview
The physical layer uses differential transmission on a
twisted pair wire.
The nodes are connected to the bus in a wired-and
fashion: if just one node is driving the bus to a logical
0, then the whole bus is in that state regardless of the
number.
Max. transfer rate of 1000 kilobits per second at a
maximum bus length of 40 meters or 130 feet when
using a twisted wire pair.
Message length is short with a maximum of 8 data
bytes per message. The messages are protected by a
CRC type checksum
50
Network Topology of CAN
CAN is a broadcast system. Any node can “broadcast” a
message using a CAN frame on a bus. Multiple controllers
tend to start their messages at the same time.
Every node will see this message. A “message” can be
considered the same as a CAN frame until you need to use
more than one frame to send a long message
CAN Bus Overview
The bus access is handled via the serial
communications protocol Carrier Sense Multiple
Access/Collision Detection with Non-Destructive
Arbitration. This means that collision of messages is
avoided by bitwise arbitration.
There is no explicit address in the messages, instead,
each message carries a numeric value which controls
its priority , and may also serve as an identification of
the contents.
An elaborate error handling scheme that results in
retransmitted messages when they are not properly
received. 53
SAE stands for Society of Automotive Engineers
NMEA stands for National Marine Educators Association
CAN bus
55 2/5/2022
A Basic CAN controller
56
SOF – Start of Frame, Identifier – Tells the content of message and priority
RTR – Remote Transmission Request, IDE – Identifier extension
DLC – Data Length Code Data – holds up to 8 bytes of data
64
EOF – End of Frame IFS – Inte2/r5m/2i0s2s2ion Frame
Space.
ID: Identifier: 11 or 29 bits
The Identifier can be used for any purpose. It is often used as
a node address or to identify requests and responses. 11 bit is
often called Standard CAN and 29 bit is often called Extended
CAN.
If two or more CAN messages are put on the bus at the same
time; the one with the highest priority (the lowest value) ID
will immediately get through.
An ID of 0 has the highest priority and will always get
through. When the first 11 bits of an 11 and 29 bit ID are the
same, the 11 bit ID has priority over the 29 bit ID.
ID: Identifier: 11 or 29 bits
A CAN node is not allowed to start transmitting a
frame in the middle of another node’s frame. This
will cause a bus error.
CAN controllers can be configured to pass only
certain received messages to its host processor.
Choose your ID values carefully to take advantage of
this if needed. This will reduce workload of CPU.
This is why some systems use 29 bit IDs even though
they do not need that many addresses. This facilitates
grouping of IDs for easier filtering.
Data Bytes
You can select from 0 to 8 data bytes using the 4 bit DLC
field.
You can have any number of data bytes in frames on the CAN
77 2/5/2022