EMBEDDED SYSTEMS: SPECIALIST SENSORS · 2020. 10. 9. · DS1621 TEMPERATURE SENSOR Measures...
Transcript of EMBEDDED SYSTEMS: SPECIALIST SENSORS · 2020. 10. 9. · DS1621 TEMPERATURE SENSOR Measures...
EMBEDDED SYSTEMS:
SPECIALIST SENSORS
Programming the mbed
Nucleo Boards
I/O SHIELD - REVIEW
In the lab we used a specially designed I/O shield
It has a range of I/O devices
Switches, LEDs, LDR are all ‘two terminal’ sensors
More complex sensors have multiple connections to them
In this final session, we look at a Digital Temperature sensor and a 3-axis Accelerometer
USING SENSORS
Using a sensor requires you to understand a little bit about the quantity you are measuring, and the sensor you are using
Although this is not software, it is still the responsibility of the embedded systems programmer
Today we will look at measuring acceleration and temperature – a feature of both these quantities is that they can take on negative values
Some physical quantities:
change very quickly – acceleration is a good example
change relatively slowly – temperature is a good example
Our measuring system will need to take account of this if it is to be useful
Temperature is a ‘scalar quantity’ – it has no direction
Acceleration is a ‘vector quantity’ – it has direction (in fact it has 3 directions, x, y, and z)
DEVICE SUMMARYDevice Nucleo/
Arduino
Pin(s)
ARM Pins Comment
4 LEDs D10–D13 PB6, PA7,
6, 5
Can output 4 bits, 1 ‘hex’ digit
Nucleo Board ‘LED1’ is D13
4 switches D6-D9 PB10, PA8,
PA9, PC7
Can input 4 bits, one ‘hex’ digit
Push Switch USER_BUTTON PC13 Interrupt, Polling
I2C sensor D14 = SDA
D15 = SCL
PB9
PB8
DS1621 temperature sensor
PWM D13 PA5 Brightness of LED1 will vary
Multi-coloured
LED
D2 = Red
D3 = Blue
D4 = Green
PA10
PB3
PB5
3 colour LED, allowing (almost)
any colour to be produced by
addition
Potentiometer A4 PC1 Adjustable Analogue input
Light dep.
Resistor
A5 PC0 Voltage Vin varies inversely with
light level
Analogue
Accelerometer
A0 = X
A1 = Y
A2 = Z
PA0
PA1
PA4
ADX335 3 axis analogue
accelerometer
Axes are marked on module
(A2 can be configured as DAC –
untested)
Serial ‘TX’, ‘RX’
(on main
Nucleo board,
top left, CN3)
PC10 = ‘RX’
PC11 = ‘TX’
(labels are
misleading)
On
USART3:
PC10 = TX
PC11 = RX
Used by USB-serial system. Note
functions of RX/TX swap (it’s an
age-old problem)
The table summarises all of the device connections on the shield
The DS1621 is an I2C digital temperature sensor
The ADX335 is a 3 axis analogue accelerometer
The table is a summary of all the devices studied during the course
The exam will include questions on how to use some of these
DIGITAL SENSORS
Digital sensors measure an analogue quantity (e.g. temperature) and convert it into a digital signal (a series of 1’s and 0’s)
This is part of a trend in modern technology – most traditionally analogue devices are becoming ‘digital’:
Telephones are digital
Music and other forms of entertainment are stored digitally
Control systems are becoming digital
Our lives are increasingly reliant on digital devices
Computers have almost always been digital (although analogue computers were built, based on operational amplifiers)
Working ‘digitally’ has advantages and disadvantages
DW IO GR IK TI AN LG LY
Advantages:
Digital signals very rarely degrade - a logic 1 is always a logic 1 – it (almost) never gets mistaken for a logic 0
Digital systems are generally ‘noise-free’
Digital information is very easy to store in cheap digital memory
Digital memory does not degrade (have a look at an old ‘analogue’ photograph – the colours fade)
Digital quantities can be manipulated mathematically (they are already numbers) in processor-based systems – this allows all kinds of audio and video ‘effects’ to be applied (including restoring old analogue material)
Disadvantages:
The real world is analogue (and always will be ?)
To work digitally, all analogue signals have to be converted to digital in an ‘ADC’ – this is often where information is lost
Often, digital quantities have to be converted back to analogue before being used in the real world – another source of error
Working digitally often requires a different way of thinking
DS1621 TEMPERATURE SENSOR
Measures analogue temperature to within 0.5 °C and converts it to digital within the sensor
Measures temperatures in the range -55 °C to +125 °C
Provides digital output – easy to record, store, analyse, etc.
Actually contains an inbuilt processor, and multiple internal data registers
Requires communication using a specific protocol, I2C
Requires special hardware at the receiving end
Requires special software at the receiving end
Data can be transferred at 400,000 bits per second
COMPLEX INTERNAL SYSTEM
The DS1621 are addressable (you can have multiple sensors all connected to the same microcontroller)
Note 3 address pins, A0 – A2
There is a one bit digital programmable ‘overtemperature’alarm output, TOUT
Two pins handle the digital communication:
SCL is a clock signal (400 kHz)
SDA is the data signal
Taken from DS1621
data sheet
I2C PROTOCOL
I2C stands for ‘Inter-Integrated Circuit’ (I.I.C.), developed by PHILIPS in 1980’s
One of two main protocols used with sensors – some sensors have both (the other is SPI – Serial Peripheral Interface)
In the diagram above illustrates an exchange of signals between a sensor and an ARM processor
the processor asks for the sensor at address 49 to send a temperature
The sensor responds by sending the value 17.5 The last byte of data has a special meaning: 1000 0000 = 0.5 0000 0000 = 0.0
SCL:
SDA:
USING I2C WITH MBED
mbed provides libraries for communicating with I2C devices, but we need to customise them for the DS1621
The process requires relatively complex code
You will be provided with a file containing a ‘C++ function’ which can interface with the DS1621 (you may want to look at the code inside the file to see how it works)
You will need to build this file into your project, and call the function when you want to read the DS1621 sensor
You will write a program that can output temperature in degrees centigrade to Terminal
Your program will switch on a warning LED when the temperature exceeds a limit of 25 °C
Think abut how a temperature of -10 °C will be stored digitally
REPRESENTING TEMPERATURE
The manufacturers of the DS1621 illustrate how they use 2-byte digital values to represent a floating-point temperature value
Right-hand byte indicates ‘.5’ or ‘.0’ – a single decimal figure
Left hand byte is an 8-bit two’s complement value (which in theory can represent numbers in the range -128 +127)
To convert from a negative (MSB set) 2’s complement 8 bit value:e.g. 1110 1100
Invert all the bits 0001 0011
add 1 0000 0001
0001 0100
convert to decimal 20
add minus sign -20
EXAMPLES – CONVERT THE FOLLOWING TO FLOATING POINT TEMPERATURES
Convert the following 16 bit values to floating point temperatures:
16 bit binary Floating point decimal
0000 1010 1000 0000
1111 0001 0000 0000
1100 0100 1000 0000
0100 1011 0000 0000
RETURNING ARRAYS FROM FUNCTIONS
The DS1621 produces 2 items of data – the first is the ‘integer’ part of the temperature, the second describes the value after the decimal point
The ReadDS1621() function returns these two values in an integer array (a function can only return one ‘item’)
The function returns a ‘pointer’ to (address of) the array containing the temperature:
temp[0] is the integer value of temperaturetemp[1] is 0 or 128 (see earlier, ‘128’ or ‘1000 0000’ means 0.5)
To get access to this data, we need to tell the function where to store it – so we provide a pointer as return type for the function when we call it:
int* temp; //the address to store temperature valuetemp = ReadDS1621(); //call the function to get the temperature
PROGRAM TO READ TEMPERATURE
We need to tell the program about the ReadDS1621() function because it lives in another file (use ‘extern’)
We need to declare an address for the function to store the data from the sensor
This program then prints out: Temperature = x:yx = integer temperaturey = 0 or 128
We will need to do additional processing in order to convert to a proper ‘floating point’ value of temperature
ACCELEROMETER SENSOR
An ANALOGUE sensor which measures acceleration in three dimensions, x, y and z
ACCELERATION is a change in velocity – always caused by a force
ACCELERATION may be positive or negative (see graph).
When the sensor and board are laid flat on the table, the z axis points upwards
Because of the force of gravity, z will already be experiencing an acceleration when placed in this position
Acceleration is often measured in ‘g’, where 1g = 9.81 metre per second squared, m/s2
Whenever we move the accelerometer, it will record an acceleration in one or more of the directions
ACCELEROMETERS
Accelerometer sensors are ‘MEMS’ devices (micro electro mechanical systems)
They are small (very, very small). 1 micron is 1/1000 mm or 10-6 m
They are based on capacitance changes between plates that move under the influence of a force – the diagram indicates the scale of these devices
The change of capacitance is converted into an analogue voltage
ANALOGUE OUTPUT
The ADX335 has three analogue outputs, x, y, and z
The outputs consist an a voltage between 0 and 3.3 V
Because acceleration can be positive or negative, the manufactures decided that 3.3/2 = 1.65 V means zero g
Full scale reading is +/- 3g, so:
1.0 = 3.3 V = +3g
0.5 = 1.65 V = 0g
0.0 = 0 V = -3g
‘g’ reading = (AnalogIn.read – 0.5) * 6
Complete the table:
1.0 = 3.3V = +3g
0.5 = 1.65V = 0g
0.0 = 0V = -3g
AnalogIn
.read()
Output voltage
(V)
Acceleration (g)
0.1 0.33 -2.4
0.4
0.8
USING THE ACCELEROMETERNote from slide 4 that the accelerometer is connected to A0, A1 and
A2 analogue inputs
The code below shows how to read the three acceleration values into Terminal. As you move the board around, the values will change
What does ‘/t’ mean in a printf() statement?
ACCELEROMETER APPLICATIONS
Accelerometers react to movement, sudden mechanical shocks or ‘jolts’. They know which way is ‘up’ because they measure gravity
they are used in cars to sense an impact and set off safety systems like airbags
They can be used to sense earthquakes
They are used on mobile phones to keep the picture the right way up (how?)
They can be used as ‘burglar alarms’ because they can sense if something has been picked up and moved
And (obviously) they can be used to measure acceleration of a vehicle
ACCELEROMETER OUTPUTS
The changes in readings (0 – 1) from the accelerometer tend to be small – they can be mathematically amplified by multiplying by a scaling factor
We can build ‘graphing’ functions on Terminal by printing multiple asterisks (*) indicating the size of the reading (this technique can be used with the temperature sensor also)
The following code produces a line of asterisks proportional to the value of acceleration:
(note use of a ‘cast’ on line 13)
CONCLUSION
mbed provides an easy, low-cost way to implement embedded systems
We have only looked a some of the mbed features
You are encouraged to look at the mbed website which has a searchable reference for all the commands + a forum where you can get help with your programming
The website details over 50 ‘mbed compatible’ boards
The Nucleo boards we use come in 3 sizes, and with a wide choice of ARM processors on them: