UNIT6
UNIT6
AND
ARDUINO
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.
Sensors Example
Sensors we need to
learn
• PIR sensor--- Detecting Movement
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
Serial.begin(9600);
}
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;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){ // we have just turned of
digitalWrite(in_1,HIGH) ; digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
delay(1000) ; }
//For Anti Clock-wise motion
- IN_1 = LOW , IN_2 = HIGH
digitalWrite(in_1,LOW) ;
digitalWrite(in_2,HIGH) ;
delay(3000) ;
Bluetooth Technology
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 2.485 GHz.
It uses a radio technology called frequency hopping
spread spectrum (FHSS).
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 2.402 GHz to 2.480 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
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.
Advantages and
Disadvantages
Advantages
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.
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.
Introduction to HC-05
and HC-06 Modules
HC 05
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.
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.
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
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 115200 bps
Hooking up with
Arduino
The connections are:
HC-05 Arduino
Vcc -----------> 5V
GND -----------> GND
TX -----------> RX
RX -----------> TX
void setup() {
Serial.begin(9600);
pinMode(13,OUTPUT);
}
Let’s control the void loop() {
if(Serial.available())
LED on Arduino {
using a basic code ch=Serial.read();
if(ch=='h')
to send command {
digitalWrite(13,HIGH);
over Bluetooth. }
if(ch=='l')
The code is {
shown: }
digitalWrite(13,LOW);
}
}
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.
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 38400.
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:
http://www.linotux.ch/arduino/HC-
0305_serial_module_AT_commamd_set_201104_revis
ed.pdf
Popular AT
commands
1. AT
2. AT+RESET
3. AT+VERSION?
4. AT+ORGL
5. AT+ADDR?
6. AT+ ROLE?
7. AT+NAME?
8. AT+PSWD?
9. AT+UART?
10. AT+STATE?
11. AT+DISC
12. AT+BIND?
13. AT+RMAAD?
14. AT+ADCN?
15. AT+MRAD?
16. AT+PAIR
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
• Follow these steps:
1. Disconnect the power to the HC-05 module.
2. Load the sketch to the Arduino
3. 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 57600 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.
• 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, 115200, 1 stop bit, 0 parity,
enter AT+UART=115200,1,0
•
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());
• }
Servo Motor
• 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.
• 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.
• 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.
Sweep
#include <Servo.h>
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
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Program
•#include <Servo.h> •void loop() {
•Servo myservo; // create servo • val = analogRead(potpin);
object to control a servo // reads the value of the
•int potpin = 0; // analog pin potentiometer (value between 0
used to connect the and 1023)
potentiometer • val = map(val, 0, 1023, 0,
•int val; // variable to read the 180); // scale it to use it with
value from the analog pin the servo (value between 0 and
•void setup() { 180)
• myservo.attach(9); // attaches •
the servo on pin 9 to the servo myservo.write(val); //
object sets the servo position according
to the scaled value
•}
• delay(15); //
waits for the servo to get there
•}