Microprocessor, Interfacing and System Design
Lecture 20
Sensors
Md Shaikh Abrar Kabir, Lecturer, EEE 1
Introduction
What is a Sensor?
It is an "agent" that detects the environmental parameters like pressure, temperature, humidity,
altitude, weight, color, etc. and produces proportional electrical signal. For example: LM35 is
a temperature sensor, and it produces an electrical voltage of 250 mV when the environment
temperature is 250 C. The sensor produces 300 mV signals when the temperature is 300 C.
LM35 is a Sensor that senses temperature (analog type)
DS18B20 is also a temperature sensor (digital type)
BME280 is a Sensor that senses Pressure, Temperature, and Humidity
BMP280 is a Pressure, temperature Sensor
MPU6050 is a sensor that senses acceleration.
Sensors 2
DS18B20: Digital type Temperature Sensor
(1) Pictorial view and connection diagram with UNO (Fig-20.1). It is possible to connect more
than one sensor at the same DPin which means DS18B20 sensor allows parallel operation.
(2) The sensor has an internal ADC to convert the temperature signal into digital form.
GND
Data
Vcc
Figure-20.1: DS18B20 temperature sensor with UNO
(3) There is a Temperature Register (Fig-20.2) inside the DS18B20 sensor.
Sensors 3
DS18B20: Digital type Temperature Sensor
Figure-20.2: Temperature Register of DS18B20 Sensor
(4) Scratchpad Memory Structure
Figure-20.3
Sensors 4
DS18B20: Digital type Temperature Sensor
(5) Every sensor has a 64-bit (8-byte) identification code (Fig-20.4) called Address Code/ROM
Code.
Figure-20.4: 64-bit Address Code that contains sensor’s Serial Number
Codes to obtain Address Code
#include<OneWire.h>
OneWire ds(7); //( D for Dallas; s = S for Semiconductor; Dallas Semiconductor Co. )
byte sendAddr[8]; //array to hold 64-bit (8-byte) Address Code
ds.reset(); //reset the 1-Wire Bus
ds.search(sendAddr); //the 64-bit data is saved in the array named sendAddr[]
Sensors 5
DS18B20: Digital type Temperature Sensor
LSByte 1 1 0 1 0 1 1 0
MSByte 0 0 0 0 0 1 1 0
byte intPart = LSByte >> 4 | MSByte << 4;
float floatPart = 0; 7 bits required for intPart (0b1101101 = 109)
for(int i = 0; i<4;i++)
{
floatPart += pow(2,i-4) * bitRead(LSByte,i)); 1 1 0 1 1 0 1
}
if(bitRead(intPart,7)==0) 4 bits required for floatPart (0*(1/2)+1*(1/4)+1*(1/8)+0*(1/16))
{
float temp = (float) (intPart & 0x7F) + floatPart;
} 0 1 1 0
else
{
1 Sign bit 0 temp = 109+(1/4)+(1/8) = 109.375
float temp = - ( (float) (intPart & 0x7F) + floatPart );
}
Sensors 6
DS18B20: Digital type Temperature Sensor
(6) Temperature Acquisition and Display using OneWire.h Library
Download the sketch
I2C/SPI Compatible 3.3V Supply BMP280 Pressure-Temperature Breakout Board without
level Shifter and Voltage Regulator
Figure-20.5: BMP280 Sensor Breakout board without level sifter and VR (Voltage Regulator)
Sensors 7
BMP280
(1) The 3.3V BMP280 Sensor Module has 8 pins with signal signatures Fig-20.6.
Figure-20.6: Pins of BME280 Sensor
Figure-20.7: Table showing pin connection options
(2) Pin Connection Options of BMP280 with I2C Bus and SPI Port according to table shown in
Fig-20.7.
Sensors 8
BMP280
(3) 4-Wire SPI Port Connection (Fig-20.9) with “3.3V Supply BMP280 Sensor Breakout Board”
using external level shifters.
Figure-20.8: External Level Shifter
Sensors 9
BMP280
(3) 4-Wire SPI Port Connection (Fig-20.9) with “3.3V Supply BMP280 Sensor Breakout Board”
using external level shifters.
Figure-20.9: 4-Wire SPI Port connection of 3.3V BME280 Breakout
Sensors 10
BMP280
(4) I2C Bus Connection of “3.3V Supply BMP280 Sensor Breakout Board” using External Level
Sifters.
Figure-20.10: I2C Bus Connection diagram between 3.3V BME280 Breakout and UNO using external level shifters
Sensors 11
Speed Calculation of DC Motor using Encoder disc with IR sensor
Figure-20.11: Speed Calculation of DC Motor using Encoder disc with IR sensor
Sensors 12
Speed Calculation of DC Motor using Encoder disc with IR sensor (Wheel radius 3.5 cm)
#include <AFMotor.h> //to start Motor
AF_DCMotor motor2(2, MOTOR12_1KHZ); //1 kHz PWM for Motor-2
char myData[20];
volatile byte enCounter = 0; //pulses from encoder disk
unsigned long prMillis = 0;
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
motor2.setSpeed(200); //
motor2.run(FORWARD); //moves forward with speed 200
attachInterrupt(digitalPinToInterrupt(2), ISRINT0, FALLING);
}
Sensors 13
Speed Calculation of DC Motor using Encoder disc with IR sensor
void loop()
{
if (millis() - prMillis >= 10000)
{
prMillis = millis();
noInterrupts(); //crtical section
byte n = enCounter;
enCounter = 0;
interrupts();
Serial.println(n, DEC);
float w2Distance = 2 * 3.14 * 3.5 * n / 20; //Wheel-2 speed of Motor-2
w2Speed = w2Speed / 10.0;
Sensors 14
Speed Calculation of DC Motor using Encoder disc with IR sensor
Serial.print("Wheel-2 Speed: ");
Serial.print(w2Speed, 2);
Serial.println(" cm/sec");
}
}
void ISRINT0()
{
enCounter++;
}
Sensors 15
Any Questions?
Sensors 16