Download presentation
Presentation is loading. Please wait.
1
SENSORS AND ARDUINO
2
Sensor A sensor is a device that detects and responds to some type of input from the physical environment. The specific input could be light, heat, motion, moisture, pressure, or any one of a great number of other environmental phenomena. The output is generally a signal that is converted to human-readable display at the sensor location or transmitted electronically over a network for reading or further processing.
3
Sensors Example
4
Sensors we need to learn
PIR sensor--- Detecting Movement Ultrasonic Sensor--- Measuring Distance
5
PIR SENSOR PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don't wear out. For that reason they are commonly found in appliances and gadgets used in homes or businesses. They are often referred to as PIR, "Passive Infrared", "Pyroelectric", or "IR motion" sensors.
6
PIR Sensor PIRs are basically made of a pyroelectric sensor (which you can see above as the round metal can with a rectangular crystal in the center), which can detect levels of infrared radiation. Everything emits some low level radiation, and the hotter something is, the more radiation is emitted.
7
The PIR sensor itself has two slots in it, each slot is made of a special material that is sensitive to IR. When the sensor is idle, both slots detect the same amount of IR, the ambient amount radiated from the room or walls or outdoors. When a warm body like a human or animal passes by, it first intercepts one half of the PIR sensor, which causes a positive differential change between the two halves. When the warm body leaves the sensing area, the reverse happens, whereby the sensor generates a negative differential change. These change pulses are what is detected.
8
Circuit Diagram Pin1 -VCC
Pin 2- Output of PIR sensor connected to 2 pin of arduino Pin 3-Ground
9
int ledPin = 13; // choose the pin for the LED int inputPin = 2; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input Serial.begin(9600); }
10
void loop() { val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON if (pirState == LOW) // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; }
11
else { digitalWrite(ledPin, LOW); // turn LED OFF if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; }
12
Ultrasonic Sensor
13
An Ultrasonic sensor is a device that can measure the distance to an object by using sound waves.
It measures distance by sending out a sound wave at a specific frequency and listening for that sound wave to bounce back. By recording the elapsed time between the sound wave being generated and the sound wave bouncing back, it is possible to calculate the distance between the sonar sensor and the object.
14
Since it is known that sound travels through air at about 344 m/s (1129 ft/s), you can take the time for the sound wave to return and multiply it by 344 meters (or 1129 feet) to find the total round-trip distance of the sound wave. Round-trip means that the sound wave traveled 2 times the distance to the object before it was detected by the sensor; it includes the 'trip' from the sonar sensor to the object AND the 'trip' from the object to the Ultrasonic sensor (after the sound wave bounced off the object). To find the distance to the object, simply divide the round-trip distance in half.
16
The Trig pin will be used to send the signal and the Echo pin will be used to listen for returning signal
17
Example- Distance Measurement
#define echoPin 7 // Echo Pin #define trigPin 8 // Trigger Pin #define LEDPin 13 // Onboard LED int maximumRange = 200; // Maximum range needed int minimumRange = 0; // Minimum range needed long duration, distance; // Duration used to calculate distance void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDPin, OUTPUT); // Use LED indicator (if required) }
18
void loop() /* The following trigPin/echoPin cycle is used to determine the distance of the nearest object by bouncing soundwaves off of it. */ { duration = pulseIn(echoPin, HIGH); //Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2; Serial.print(Distance); Dealy(1000);}
19
Interfacing DC Motor With Arduino
20
DC Motor Working principle of a DC motor --A motor is an electrical machine which converts electrical energy into mechanical energy. The principle of working of a DC motor is that "whenever a current carrying conductor is placed in a magnetic field, it experiences a mechanical force". A DC motor (Direct Current motor) is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.
21
To control the direction of the spin of DC motor, without changing the way that the leads are connected, you can use a circuit called an H-Bridge. An H bridge is an electronic circuit that can drive the motor in both directions. H-bridges are used in many different applications, one of the most common being to control motors in robots. It is called an H-bridge because it uses four transistors connected in such a way that the schematic diagram looks like an "H."
22
You can use discrete transistors to make this circuit, but for this lecture, we will be using the L293d/L298 H-Bridge IC. In L293 all four input- output lines are independent, while in L298, a half H driver cannot be used independently, full H driver has to be used. Protective Diodes against back EMF are provided internally in L293D but must be provided externally in L298. The L298 can control the speed and direction of DC motors and stepper motors and can control two motors simultaneously. Its current rating is 2A for each motor. At these currents, however, you will need to use heat sinks.
23
L293D Motor Driver IC
25
Controlling Speed of DC Motor
We will connect the Arduino to IN1 (pin 5), IN2 (pin 7), and Enable1 (pin 6) of the L293d IC. Pins 5 and 7 are digital, i.e. ON or OFF inputs, while pin 6 needs a pulse-width modulated (PWM) signal to control the motor speed.
26
const int pwm = 2 ; //initializing pin 2 as pwm const int in_1 = 8 ; const int in_2 = 9 ; //For providing logic to L293d IC to choose the direction of the DC motor void setup() { pinMode(pwm,OUTPUT) ; //we have to set PWM pin as output pinMode(in_1,OUTPUT) ; //Logic pins are also set as output pinMode(in_2,OUTPUT) ; }
27
void loop() { //For Clock wise motion , in_1 = High , in_2 = Low digitalWrite(in_1,HIGH) ; digitalWrite(in_2,LOW) ; analogWrite(pwm,255) ; delay(3000) ; /*setting pwm of the motor to 255 we can change the speed of rotaion by chaning pwm input but we are only using arduino so we are using higest value to driver the motor */
28
digitalWrite(in_1,HIGH) ; digitalWrite(in_2,HIGH) ; delay(1000) ; }
//For brake digitalWrite(in_1,HIGH) ; digitalWrite(in_2,HIGH) ; delay(1000) ; //For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH digitalWrite(in_1,LOW) ; digitalWrite(in_2,HIGH) ; delay(3000) ; //For brake digitalWrite(in_1,HIGH) ; digitalWrite(in_2,HIGH) ; delay(1000) ; }
29
Bluetooth Technology
30
Introduction Bluetooth is a wireless communication technology used for exchange of data over short distances. It is found in many devices ranging from mobile phones and computers. Bluetooth technology is a combination of both hardware and software. It is intended to create a personal area networks (PAN) over a short range. It operates in the unlicensed industrial, scientific and medical (ISM) band at 2.4 to GHz. It uses a radio technology called frequency hopping spread spectrum (FHSS).
31
FHSS Frequency hopping spread spectrum (FHSS) is a method of transmitting radio signals by shifting carriers across numerous channels with pseudorandom sequence which is already known to the sender and receiver. Frequency hopping spread spectrum is defined in the 2.4 GHz band and operates in around 79 frequencies ranging from GHz to GHz. Every frequency is GFSK modulated with channel width of 1MHz and rates defined as 1 Mbps and 2 Mbps respectively. Frequency hopping spread spectrum is a robust technology with only very little influence from reflections, noise and other environmental factors
32
It is a packet-based protocol with a master-slave structure.
Each master can communicate with up to 7 slaves in a piconet. The range of Bluetooth depends upon the class of radio using. Class 3 radios have range up to 1 meter or 3 feet. Class 2 radios have range of 10 meters or 33 feet. Class 1 radios have range of 100 meters or 300 feet. The most commonly used radio is Class 2.
33
Advantages and Disadvantages
The biggest advantage of using this technology is that there are no cables or wires required for the transfer of data over short ranges. Bluetooth technology consumes less power when compared with other wireless communication technologies. For example Bluetooth technology using Class 2 radio uses power of 2.5 mW. As it is using frequency hopping spread spectrum radio technology there is less prone to interference of data if the other device also operates in the same frequency range. Bluetooth doesn’t require clear line of sight between the synced devices.
34
Disadvantage Since it uses the greater range of Radio Frequency (RF), it is much more open to interception and attack. It can be used for short range communications only. Although there are fewer disadvantages, Bluetooth still remains best for short wireless technology.
35
Introduction to HC-05 and HC-06 Modules
HC-05 is a class 2 Bluetooth module with Serial Port Profile (SPP). It can be configure either as master or salve. It is a replacement for wired serial connection. The module has two modes of operation, Command Mode where we can send AT commands to it and Data Mode where it transmits and receives data to another bluetooth module.
37
HC-05 Specification: Bluetooth Protocol : Bluetooth Specificatio v2.0 + EDR Frequency : 2.4 GHz ISM band Profiles : Bluetooth Serial Port Working Temperature : -20 to + 75 centigrade Power of emitting : 3 dBm The pins on the module are: Vcc – Power supply for the module. GND – Ground of the module. TX – Transmitter of Bluetooth module. RX – Receiver of the module. Key – Used for the module to enter into AT command mode.
38
The default mode is DATA Mode, and this is the default configuration, that may work fine for many applications: Baud Rate: 9600 bps, Data : 8 bits, Stop Bits: 1 bit, Parity : None, Handshake: None Passkey: 1234 Device Name: HC-05
39
HC 06 HC-06 is a drop-in replacement for wired serial connection. This can be used as serial port replacement to establish connection between PC and MCU (Microcontroller). This is a Slave Mode only Bluetooth device. HC-06 offers encrypted connection This module can be configured for baud rates 1200 to bps
40
Hooking up with Arduino
The connections are: HC-05 Arduino Vcc > 5V GND > GND TX > RX RX > TX
41
void setup() { Serial.begin(9600); pinMode(13,OUTPUT); } void loop() { if(Serial.available()) { ch=Serial.read(); if(ch=='h') digitalWrite(13,HIGH); if(ch=='l') digitalWrite(13,LOW); Let’s control the LED on Arduino using a basic code to send command over Bluetooth. The code is shown:
42
Configuring HC-05 with AT commands
HC-05 can be configured i.e. its name and password can be changed and many more using AT commands. It can also be configured as either master or slave. To set the HC-05 into AT command mode the KEY pin of the module is made high i.e. it is connected to either 5V or 3.3V pin of Arduino and TX of Arduino is connected to TX of HC-05 and RX of HC-05 to RX of Arduino.
44
To enter the AT commands open up the COM port of the Arduino to which the Bluetooth module is connected and set the baud rate to After opening COM port if you enter command “AT” (without quotes) it should return “OK” thus it can be said that HC-05 entered into AT command mode. Refer following link for detailed AT commands and its descriptions:
45
Popular AT commands AT AT+RESET AT+VERSION? AT+ORGL AT+ADDR? AT+ ROLE?
AT+NAME? AT+PSWD? AT+UART? AT+STATE? AT+DISC AT+BIND? AT+RMAAD? AT+ADCN? AT+MRAD? AT+PAIR
46
Alternate method to access AT command mode
Some bluetooth mode does not have KEY pin. In such module, different approach is considered: first connect the Bluetooth module to an Arduino (Nano, UNO or whatever) using the following connections… HC-05 GND –> Gnd HC-05 VCC –> +5V (initially disconnected) * HC-05 TX –> D2 (any pin as required) HC-05 RX –> D3 (any pin as required) STATE (output) and EN (input) are not connected
47
Follow these steps: Disconnect the power to the HC-05 module. Load the sketch to the Arduino Depress the small reset button on the HC-05 module and hold it down while you connect its Vcc pin to +5V. The red LED on the module (that would otherwise be blinking quickly) will flash slowly indicating it is in AT mode. Open a serial monitor in the Arduino IDE (or any other terminal software) and set its baud rate to and ensure that on the IDE serial terminal, ensure “Both NL & CR” is selected. You should then see the prompt “Enter AT commands: “. You can then type your AT commands into the terminal input line and have them control the HC-05.
48
Here are a few AT commands that I found useful:
To ensure the unit is responding, enter AT. The unit should respond OK To return the HC-05 to its default settings, enter AT+ORGL To see the version of your HC-05 enter AT+VERSION? To change the name of the device to AJCLOCK, for example, enter AT+NAME=AJCLOCK To change the default security code (1234) to 2332 enter AT+PSWD=2332 To check baud rate, enter AT+UART? (my unit reset to 38400) To change baud rate to say, , 1 stop bit, 0 parity, enter AT+UART=115200,1,0
49
Code #include <SoftwareSerial.h>
SoftwareSerial btSerial(2, 3); // RX | TX void setup() { Serial.begin(57600); Serial.println("Enter AT commands:"); btSerial.begin(38400); // HC-05 default speed in AT command more } void loop() { if (btSerial.available()) Serial.write(btSerial.read()); if (Serial.available()) btSerial.write(Serial.read());
50
Servo Motor
51
A servo motor allows a precise control of the angular position, velocity, and acceleration. It’s like you’re at the steering wheel of your car. You control precisely the speed of the car and the direction. These servos are essential parts if we need to control the position of objects, rotate sensors, move arms and legs, drive wheels and tracks, and more.
52
Inside the micro servo, you will find the pieces from the above image.
The top cover hosts the plastic gears while the middle cover hosts a DC motor, a controller, and the potentiometer.
53
The servo motor has three leads, with one more than a DC motor.
Each lead has a color code. So you have to connect the brown wire from the micro servo to the GND pin on the Arduino. Connect the red wire from the servo to the +5V on the Arduino. And finally, connect the orange wire from the SG90 servo to a digital pin (pin 9) on the Arduino.
55
Sweep #include <Servo.h>
Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
56
Program #include <Servo.h>
Servo myservo; // create servo object to control a servo int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.