0% found this document useful (0 votes)
62 views217 pages

Smart Technology Robotics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views217 pages

Smart Technology Robotics

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 217

Robotics

ENG:Abderahman bayoume
Course Outline :
• Introduction to Arduino & control
• Types of Arduino .
• Breadboard
• Arduino c programming( if statement –for loop – while loop -switch case)
• Computers interfacing with Arduino
• Serial communication
• Some basic electronic Projects:
• Digital and analog pin . 1-ultrasonic car
• Pulses Width Modulation (PWM) 2-Bluetooth car
• Sensors : (Pushbutton – Potentiometer - LDR – line tracker – PIR - 3-line follower
4-Automatic Night Light
LM35 – smoke detector-flame sensor - ultrasonic )
• LCD Display- Seven Segment- RGB Led
• Keypad-Relay.
• Motor: DC Motor- Motor Driver- Servo Motor
• Interfacing with Bluetooth Module
• Wireless communication :Two Bluetooth
History
➢ The term "robot" was first used in 1920 in a play called
"R.U.R." Or "Rossum's universal robots" by the Czech
writer Karel Capek.

➢The word “Robot”comes from the word“Robota”,meaning


in Czech”,forced labour.
What is Robotics ?!
 A robot is a machine that can be programmed.
 Robotics is the design, manufacture, and application of robots.
 Robotics combines a working knowledge of electronics, mechanics and
software.
Laws of Robotics
Asimov proposed three “Laws of Robotics”

➢ Law 1: A robot may not injure a human being or through inaction, allow a
human being to come to harm.

➢ Law 2: A robot must obey orders given to it by human beings, except where
such orders would conflict with the first law.

➢ Law 3: A robot must protect its own existence as long as such protection does
not conflict with the first law.
Types of robots
➢ Mobile robots.
➢ Stationary robots
➢ Autonomous robots
➢ Remote-controlled robots.
Mobile robots
Mobile robots are able to move, usually they perform tasks such as
searching.
They are of 2 types: -

➢ Rolling robots : Rolling robots have wheels to move around . They can
quickly and easily search . However they are only useful in flat areas.

➢ Walking robots : Robots on legs are usually brought in when the terrain
is rocky. Most robots have at least 4 legs; usually they have 6 or more
Mobile robots

Rolling robots Walking robots Walking and Rolling robots


Stationary Robot
Most of these robots perform repeating tasks without ever moving.
Autonomous Robots
➢ They run a program that
gives them the opportunity
to decide on the action to
perform depending on their
surroundings.

➢ these robots even learn


new behavior
Remote-controlled Robots
➢ A person can guide a robot by remote control.

➢ A person can perform difficult and usually dangerous


tasks without being at the spot where the tasks are
performed.
Application
Industrial robots
Space robots
Medical robots
House robots
Military robots
Entertaining robots
System Components
( Robotics – Smart Home)

System

Control Mechanical Electrical Graphical


User
System System Circuit Interface
Control system

A control system manages,


directs, or regulates the
behavior of other devices or
systems using control loop
Mechanical System
 Mechanical System includes: stress analysis, steering
mechanisms, driving mechanisms and lifting mechanisms.
 The Mechanical Design will be done using SolidWorks.
Electrical Circuit
 Motor driving circuits and interface boards.
 Sensor circuits.
 power supply
Graphical User Interface
 Graphical user interface or (GUI) is the very important as it’ll
represents your robot in one application on smart phone or pc.
 Keep it simple
 You can use c# or processing IDE for your GUI or any other software
you’re comfortable with.
Micro-Controller :
It is a micro-computer. As any computer it has
internal CPU, RAM, IOs interface.
It is used for control purposes, and for data
analysis.

Famous microcontroller are


• PIC from MicroChip
• AVR from Atmel
• ARM
Atmel AVR
 Atmel microcontroller unit (MCU) is used as the brain of the Arduino
boards.
 Holds the compiled code.
 Responsible of the execution of the program.
What’s Arduino?
 Arduino boards are open-source electronics
prototyping platforms which are widely used
between both engineers and hobbyists to build DIY
robots or any application that needs a
microcontroller.
Raspberry Pi
Arduino boards
❑ Many different versions
❑ Number of input/output channels
❑ Processor
Arduino UNO
Arduino MEGA
Arduino mini
Arduino Nano
Arduino DIY?
Applications
 Light control
 Motor control

 Robotics

 any simple project


Arduino Hardware.
Arduino UNO
USB chip

Power LED
Digital&Analog signal

on on on
5V 5V

off off off


0V 0V

Digital signal Analog signal


Arduino UNO
 Digital inputs &output
on or off (HIGH or LOW).
 HIGH is 5VDC.
 LOW is 0VDC.

 Analog inputs
a range of numbers.
 0 to 1023
 .0049 V per digit (4.9 mV)

 Analog output
a range of numbers.
 0 to 255
Schematic diagram

Resistor

led
Using the breadboard

The bread board has many strips of metal The long top and bottom row of
(copper usually) which run underneath the holes are usually used for power
board. supply connections.
Connection on breadboard
make more circuit in fritzing
Arduino as power

RESISTOR

220
OR
330

“+” (long) lead of LED should be connected to Pin 5V


The other (short) lead of the LED goes to “Ground”
Arduino Software.
Getting the software
 http://www.arduino.cc/en/Main/Software
 Source code: https://github.com/arduino/Arduino/
 Optional: fritzing for pc
 For installation: http://www.arduino.cc/en/Guide/Windows
Arduino IDE
Select Serial Port and Board
Status Messages
Arduino Programming Basics
1. GLOBAL Variables

Void setup()
2. {
//your initialization code
}

Void loop()
3. {
//your repeating code
}
Global Variables

Char

Int

Float
Void setup( )
Put the code For the initialization
void setup()
{
Serial.begin(9600); //serial port baudrate=9600bps
pinMode(led, OUTPUT); // configure pin 13 as output port
pinMode(button, INPUT);
}
Void loop( )
Put the code that is required to be performed by System.

digitalRead(INPUT) other
HIGH-LOW
analogRead(INPUT)
analogWrite(OUTPUT,0-255)
digitalWrite(OUTPUT,HIGH) 0-1024
LOW
Ex:
void loop()
{
digitalWrite(ledinput, high); //led=on
delay(500);
digitalWrite(ledinput, low); //led=off
delay(500);
}
Blink

Led on
&
Led off

every
specific
time

“+” (long) lead of LED should be connected to Pin 13


The other (short) lead of the LED goes to “Ground”
Blink
GLOBAL Variables
/*
Turns an LED ON and OFF repeatedly.
There is already an LED on the board connected to pin 13.
*/ initialization code
13 -->OUTPUT
void setup()
{
pinMode(13, OUTPUT); // initialize the digital pin as an output
repeating code
}
ledPin->HIGH
delay->1000
void loop()
ledPin->LOW
{
delay->1000
digitalWrite(13, HIGH); // set the LED on ledPin->HIGH
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
Blink
GLOBAL Variables
/*
Turns an LED ON and OFF repeatedly. ledPin = 13
There is already an LED on the board connected to pin 13.
*/
initialization code
int ledPin = 13; // LED connected to digital pin 13
ledPin-->OUTPUT
void setup()
{
repeating code
pinMode(ledPin , OUTPUT); // initialize the digital pin as an output
} ledPin->HIGH
delay->1000
void loop() ledPin->LOW
{ delay->1000
digitalWrite(ledPin, HIGH); // set the LED on ledPin->HIGH
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
Another way of coding
/*
Turns an LED ON and OFF repeatedly.
There is already an LED on the board connected to pin 13. GLOBAL Variables
*/
ledPin =13
int ledPin = 13; // LED connected to digital pin 13 ON = 100
int ON = 100; // (ms) time for LED to be ON OFF = 100
int OFF = 100; // (ms) time for LED to be OFF
initialization code
void setup() ledPin->OUTPUT
{
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output repeating code
}
ledPin->HIGH
void loop() delay-> ON
{ ledPin->LOW
digitalWrite(ledPin, HIGH); // set the LED on delay->OFF
delay(ON); // wait for a second ledPin->HIGH
digitalWrite(ledPin, LOW); // set the LED off
delay(OFF); // wait for a second
}
Play with values of constants “ON” and “OFF”
Connection of three led

Make
common
for
GND

3 LEDs connected from digital pins 9, 10, 11


Use the leg of resistors on common gnd directly →breadboard
LEDs → led on&& off after previous
/* The circuit: 3 LEDs connected from digital pins 9,10,11 to ground through 220 GLOBAL Variables
ohm resistors. */
red = 9
int red = 9; // LED 9 connected to digital pin 9 yellow = 10
int yellow = 10; // LED 10 connected to digital pin 10 green = 11
int green = 11; // LED 11 connected to digital pin 11 ON = 1000
int ON = 1000; // (ms) time for LED to be ON OFF = 1000
int OFF = 1000; // (ms) time for LED to be OFF
initialization code
void setup() // initialize digital pins as an output: Red --->OUTPUT
{ yellow->OUTPUT
pinMode(red, OUTPUT); Green-->OUTPUT
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT); repeating code
} red-->HIGH
delay(ON)
void loop() red-->LOW
{ delay(OFF)
digitalWrite(red, HIGH); // set the LED on yellow-->HIGH
delay(ON); // wait for time “ON” (ms) delay(ON(
digitalWrite(red, LOW); // set the LED off yellow-->LOW
delay(OFF); // wait for time “OFF” (ms) delay(OFF)
green-->HIGH
digitalWrite(yellow, HIGH); delay(ON); digitalWrite(yellow, LOW); delay(OFF); delay(ON)
green-->LOW
digitalWrite(green, HIGH; delay(ON); digitalWrite(green, LOW); delay(OFF); delay(OFF)
}
LEDs → leds on as same time & off as same time
/* The circuit: 3 LEDs connected from digital pins 9,10,11 to ground through 220 GLOBAL Variables
ohm resistors. */
red = 9
int red = 9; // LED 9 connected to digital pin 9 yellow = 10
int yellow = 10; // LED 10 connected to digital pin 10 green = 11
int green = 11; // LED 11 connected to digital pin 11 ON = 1000
int ON = 1000; // (ms) time for LED to be ON OFF = 1000
int OFF = 1000; // (ms) time for LED to be OFF
initialization code
void setup() // initialize digital pins as an output: Red --->OUTPUT
{ yellow->OUTPUT
pinMode(red, OUTPUT); Green-->OUTPUT
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT); repeating code
} red-->HIGH
yellow-->HIGH
void loop() green-->HIGH
{ delay(ON(
digitalWrite(red, HIGH); // set the three LEDs on red-->LOW
digitalWrite(yellow,HIGH) // wait for time “ON” (ms) yellow-->LOW
digitalWrite(green, HIGH); green-->LOW
delay(ON); delay(OFF)
digitalWrite(red, LOW); // set the three LEDs off
digitalWrite(yellow, LOW); // wait for time “OFF” (ms)
digitalWrite(green, LOW);
delay(OFF);
}
LEDs on→ sequentially && off→ sequentially
/* The circuit: 3 LEDs connected from digital pins 9,10,11 to ground through 220 GLOBAL Variables
ohm resistors. */
red = 9
int red = 9; // LED 9 connected to digital pin 9 yellow = 10
int yellow = 10; // LED 10 connected to digital pin 10 green = 11
int green = 11; // LED 11 connected to digital pin 11 ON = 1000
int ON = 1000; // (ms) time for LED to be ON OFF = 1000
int OFF = 1000; // (ms) time for LED to be OFF
initialization code
void setup() // initialize digital pins as an output: Red --->OUTPUT
{ yellow->OUTPUT
pinMode(red, OUTPUT); Green-->OUTPUT
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT); repeating code
} red-->HIGH
delay(ON)
void loop() yellow-->HIGH
{ delay(ON(
digitalWrite(red, HIGH); // set the LED on green-->HIGH
delay(ON); // wait for time “ON” (ms) delay(ON)
digitalWrite(yellow,HIGH); // set the LED off red-->LOW
delay(ON); // wait for time “OFF” (ms) delay(OFF)
yellow-->LOW
digitalWrite(green, HIGH); delay(ON); digitalWrite(red, LOW); delay(OFF); delay(OFF)
green-->LOW
digitalWrite(yellow,LOW); delay(Off); digitalWrite(green, LOW); delay(OFF); delay(OFF)
}
“ if ” condition
if (someVariable > 50)
{ comparison operator
// do something here
}

if (inputPin == HIGH)
{
doThingA;
}
else
{
doThingB;
}
Pull down Resistor

5V

pin pin

Arduino
uno Arduino
uno

0V
Pull down Resistor

Make
common
for
GND

The connection name of pushbutton


is Pulldown resistor
LED operated by a button
/* LED on pin #13 is operated by a button on pin #2 */
GLOBAL Variables
int led = 9;
int button = 11; led = 9
int Reading = 0; // variable for reading the pushbutton status button = 11
Reading = 0
void setup()
{ initialization code
pinMode(led, OUTPUT); Reading
led --->OUTPUT
pinMode(button, INPUT);
} LOW
HIGH button->INPUT

void loop()
repeating code
{ Reading → LOW
Reading = digitalRead(button); Led → off
Reading → HIGH
if (Reading == HIGH) Led → on
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
}
Save the state of value (led)
/* LED on pin #13 is operated by a button on pin #2 */ GLOBAL Variables
led = 9
int led = 9;
button = 11
int button = 11;
Reading = 0
int Reading = 0; // variable for reading the pushbutton status
Counter = 0
int counter = 0;
void setup() initialization code
{
pinMode(led, OUTPUT); Reading led --->OUTPUT
pinMode(button, INPUT); button->INPUT
} LOW
HIGH repeating code
void loop() Reading → LOW
{ Nothing happens
Reading = digitalRead(button); Reading → HIGH
if (Reading == HIGH) counter Counter = 1
{ Led→ON
counter++; 0
2
1 Delay(250)
if(counter==1) Still led on
{ digitalWrite(led, HIGH);} Reading → HIGH
else Counter = 2
{ digitalWrite(led, LOW); counter=0; } Led → OFF
delay(250); Still led off
} Counter = 0
} Delay(250)
debouncing

Pushbuttons often generate spurious open/close transitions when pressed, due to mechanical
and physical issues: these transitions may be read as multiple presses in a very short time
fooling the program.

solution

software hardware
save the state of value without change
const int led=9;
const int button=11;
int ButtonState=0;
int lastButtonState=0; ButtonState lastButtonState ledState
int ledState=0;
void setup() 0 0 0
{
pinMode(led,OUTPUT);
1 1 1
pinMode(button,INPUT); 0 0 0
}
void loop()
1 1
{
ButtonState=digitalRead(button);
if(ButtonState != lastButtonState) {
if(ButtonState ==HIGH){
ledState= ~ledState;
}
delay(50); //for debouncing
}
digitalWrite(led,ledState);
lastButtonState=ButtonState ;
}
Note
1 Else if
2
3
4
“for” statement
initialization condition increment
i repeating in for
Led→ON
false
true Delay(250)
Led → OFF
for (int i=0; i <= 2; i++) Delay(250)

{ Led→ON
digitalWrite(led, HIGH); Delay(250)
statement Led→OFF
delay(250); Delay(250)

digitalWrite(led, LOW); Led→ON


delay(250); Delay(250)
Led→OFF
Delay(250)
}
Use for loop (Repetition)
/* The circuit: 3 LEDs connected from digital pins 9,10,11 to ground through 220 GLOBAL Variables
ohm resistors. */
led = 9
int led = 9; // LED 9 connected to digital pin 9 button = 11
int button = 11; Reading = 0
int Reading = 0 ; initialization code
void setup() // initialize digital pins as an output:
{ Red --->OUTPUT
pinMode(led, OUTPUT); button-->INPUT
pinMode(button, INPUT); Reading
}
HIGH
LOW
void loop()
{ repeating in for
Reading = digitalRead(button); led-->HIGH
if(Reading==HIGH) delay(250)
{ led-->LOW
for(int i=0;i<=2;i++) delay(250)
{ led-->HIGH
digitalWrite(led,HIGH); delay(250(
delay(250); led-->LOW
digitalWrite(led,LOW); delay(250)
delay(250); led-->HIGH
} delay(250)
} led-->LOW
} delay(250)
void setup()
{
for(int i=9;i<=11;i++)
condition i initialization code
pinMode(9,OUTPUT)
pinMode(10,OUTPUT)
{
pinMode(i,OUTPUT);
true
false pinMode(11,OUTPUT)
}
}
repeating in for
void loop() digitalWrite(9,HIGH)
{ delay(250)
for(int i=9;i<=11;i++) digitalWrite(10,HIGH)
{ delay(250)
digitalWrite(11,HIGH)
digitalWrite(i,HIGH);
delay(250(
delay(250); digitalWrite(11,LOW)
} delay(250)
for(int i=11;i>=9;i--) digitalWrite(10,LOW)
{ delay(250)
digitalWrite(i,LOW); digitalWrite(9,LOW)
delay(250); delay(250)
}
9 10 11
}
Connection of three led

Make
common
for
GND

3 LEDs connected from digital pins 9, 10, 11


Use the leg of resistors on common gnd directly →breadboard
“switch ... case”

switch (var) If-esle statement checks for equality,rang of value as well as


for logical expression . On the other hand, switch checks only
{ for equality and expressions based only on a single char
case label: // statements
the expression in switch statement decide which case to
break; execute and if you do not apply a break statement after
case label: // statements each case it will execute till the end of switch statement
break; A switch statement might prove to be faster than ifs
default: // statements provided number of cases are good. If there are only few
cases, it might not effect the speed in any case. Prefer switch
} if the number of cases are more than 5 otherwise, you may
use if-else too.

If expression inside if turn outs to be false, statement inside


else block will be executed. If expression inside switch
statement turn out to be false then default statements is
executed
const int red = 9; void loop( )
const int yellow = 10; {
const int green=11; Reading = digitalRead(button);
const int button=7; if(Reading==HIGH)
int Reading=0; { counter++;
int counter=0; switch (counter)
{
void setup( ) { case 1 : digitalWrite(red, HIGH);
pinMode(red, OUTPUT); break;
pinMode(yellow, OUTPUT); case 2 : digitalWrite(yellow,HIGH);
Reading counter
pinMode(green, OUTPUT); break;
pinMode(button, INPUT); case 3 : digitalWrite(green,HIGH);
HIGH
LOW 0
4
3
2
1
} break;
default : digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
digitalWrite(red,LOW);
counter=0;
break;
}
delay(250);

}
}
Connection of three led with pushbutton

Make
common
for
GND

 3 LEDs connected from digital pins 9, 10,11


 Pushbutton on pin 7 with pulldown Resistor
“while” loop
while(someVariable ?? value)
{
doSomething;
}

Example:
const int button2Pin = 2;
buttonState = LOW;
while(buttonState == LOW)
{
buttonState = digitalRead(button2Pin);
}
const int red = 9; void loop( )
const int yellow = 10; {
const int green=11; Reading = digitalRead(button);
const int button=7; if(Reading==HIGH)
int Reading=0; { counter++;
int counter=0; switch (counter)
{
void setup( ) { case 1 : digitalWrite(red, HIGH);
pinMode(red, OUTPUT); break;
pinMode(yellow, OUTPUT); case 2 : digitalWrite(yellow,HIGH);
Reading counter
pinMode(green, OUTPUT); break;
pinMode(button, INPUT); case 3 : digitalWrite(green,HIGH);
HIGH
LOW 0
4
3
2
1
} break;
default : digitalWrite(green,LOW);
digitalWrite(yellow,LOW);
digitalWrite(red,LOW);
counter=0;
break;
}
while(digitalRead(button)==HIGH);

}
}
Virtual Electrical Prototyping Project
started in 2007 by the Interaction Design Lab
at the University of Applied Science Potsdam, Germany

Open Source

Prototypes: Document, Share, Teach, Manufacture


PWM (pulse width modulation)

➢ PWM frequency at about 500Hz, the green


lines would measure 2 milliseconds each.

➢ A call to analogWrite() is on a scale of

0 – 255 →MAX Value(255)

➢ analogWrite(255) requests a 100% duty


cycle (always on)

➢ and analogWrite(127) is a 50% duty cycle

(on half the time) for example.


Using the PWM
int led = 3;

void setup()
{
pinMode(led, OUTPUT);
}

void loop()
{
analogWrite(led, 0);
delay(1000);
analogWrite(led, 65);
delay(1000);
analogWrite(led,128);
delay(1000);
analogWrite(led,255);
delay(1000);
}
Logical operator
operator meaning 1 parameter 2 parameter example return
&& AND Y= 5 X= 6
false true Y > 5 && X < 9 false
false false Y > 5 && X < 3 false
true true Y > 3 && X < 9 True
.............. ....................... ………………. ………………… …………………………………………. ……………...
|| OR Y= 5 X= 6

false true Y > 5 || X < 9 True


false false Y > 5 || X < 3 false
true true Y > 3 || X < 9 True
.............. ....................... ………………. ………………… …………………………………………. ……………...
!= NOT equal Y= 5 Y!=4 True
example

if (digitalRead(Bot1) == 1 && digitalRead(Bot3) == 0 || digitalRead(Bot2) == 0 && digitalRead(Bot2) == 1)


{
digitalWrite(LED, HIGH);
}

else
{
digitalWrite(LED, LOW);
}
Fade
int led = 9; // the PWM pin the LED is attached to
brightness fadeAmount
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by 0 5
void setup() { 5
pinMode(led, OUTPUT); 10
}
| -5
|
void loop()
|
{ 5
|
analogWrite(led, brightness);
255
brightness = brightness + fadeAmount; 250
if (brightness <= 0 || brightness >= 255) |
{ |
fadeAmount = -fadeAmount; |
} 0
delay(30);
}
Pull up Resistor

5V

pin pin

Arduino Arduino
uno uno

0V
Pull up & pull down Resistor
5V pinMode(pin,INPUT); 5V pinMode(pin,INPUT_PULLUP);
-------------------------- --------------------------
If(digitalRead(pin)==HIGH) If(digitalRead(pin)==LOW)
{ } { }

pin pin

Arduino Arduino
uno uno

0V 0V

Pulldown Resistor Pullup Resistor


Why use Pullup?

internal pull-up resistor pinMode( 3 , INPUT_PULLUP )


Two switch control in brightness

 LED connected from


digital pin 3

 2 Pushbutton on pin
10&11 with internal
pulldup Resistor
const int pushbutton1 = 10;
const int pushbutton2 = 11;
Button 1
const int Led = 9; ledBrightness
int ledBrightness = 128;
void setup( ) 128
{ 113
pinMode(pushbutton1, INPUT_PULLUP); 128
pinMode(pushbutton2, INPUT_PULLUP); |
pinMode(Led, OUTPUT); |
} |
void loop( ) Button 2 |
{ |
if(digitalRead(pushbutton1) == LOW){ |
ledBrightness -=15; //ledBrightness=ledBrightness-15 |
} | 8
else if(digitalRead(pushbutton2) == LOW){ 248
ledBrightness+=15; //ledBrightness=ledBrightness+15 263
} 255
ledBrightness = constrain(ledBrightness, 0, 255);
analogWrite(Led, ledBrightness);
delay(250);
}
ADC (analog to digital converter)

• 5V divided to 1024 level volt level


between 0 and 1023 5V |
1023
|
|
• (4.9 mV) per unit 2.5V |
512

|
|
0.0098 V 2
0.0049 V 1
Potentiometers
Control of flasher
int Pot = A0; 50%
int Led = 3; sensorValue
int sensorValue=0;
void setup()
{ 5V GND 1023
512
255
0
767
pinMode(Led, OUTPUT);
pinMode(Pot , INPUT);
}

void loop() A0
{ 5V GND
sensorValue = analogRead(Pot);
digitalWrite(Led, HIGH);
delay(sensorValue);
digitalWrite(Led, LOW); delay(512)
delay(255)
delay(0)
delay(767)
delay(1023)
delay(sensorValue);
}
Map function
name of range : ST name of range : smart

0 2 4 6 8 10 0 255 512 768 1023

0 2 4 6 8 10 12 14 16 18 20 0 64 128 192 255

Map ( value , fromLow, fromHigh , toLow , toHigh ) Map ( value , fromLow, fromHigh , toLow , toHigh )
Map ( ST , 0 , 10 , 0 , 20 ) Map (Smart , 0 , 1023 , 0 , 255 )
Control of brightness
int Pot = A0; 50% sensorValue
int Led = 3;
int ledBrightness = 0; 1023
512
255
0
768
int sensorValue = 0; 5V GND
void setup() ledBrightness
{
pinMode(Pot, INPUT);
pinMode(Led, OUTPUT); 0
192
64
128
255
A0
}
5V GND
void loop()
{
sensorValue = analogRead(Pot);
ledBrightness = map(sensorValue, 0, 1023, 0, 255);
analogWrite(Led, ledBrightness);
}
RGB Led
Table of color
Connection with arduino
Code of RGB Led
#define potB A0 void loop()
#define potG A1 {
#define potR A2 potValue = analogRead(potR);
#define ledR 3 ledValue = map(potValue, 0, 1023, 0, 255);
#define ledG 5 analogWrite(ledR, ledValue);
#define ledB 6 potValue = analogRead(potG);
int potValue; ledValue = map(potValue, 0, 1023, 0, 255);
int ledValue; analogWrite(ledG, ledValue);
void setup() potValue = analogRead(potB);
{ ledValue = map(potValue, 0, 1023, 0, 255);
pinMode(ledR, OUTPUT); analogWrite(ledB, ledValue);
pinMode(ledG, OUTPUT); }
pinMode(ledB, OUTPUT);
}
7 Segment Display

Common Anode Common Cathode


+ - HIGH
+ -
LOW

LOW HIGH

LOW HIGH
5V
LOW HIGH

LOW HIGH 0V

LOW HIGH

LOW HIGH
7 Segment Display
s s s s

8
1
3

s s s
Truth table of 7segment
Connection with arduino
Atmega pin on Arduino uno
Code of 7seg Display
char b=2; char a=3; char f=4; char g=5;
char e=6; char d=7; char c=8; void one(){ void three(){
void setup(){ digitalWrite(a,0); digitalWrite(a,1);
pinMode(a,OUTPUT); digitalWrite(b,1); digitalWrite(b,1);
pinMode(b,OUTPUT); digitalWrite(c,1); digitalWrite(c,1);
pinMode(c,OUTPUT); digitalWrite(d,0); digitalWrite(d,1);
pinMode(d,OUTPUT); digitalWrite(e,0); digitalWrite(e,0);
pinMode(e,OUTPUT); digitalWrite(f,0); digitalWrite(f,0);
pinMode(f,OUTPUT); digitalWrite(g,0); digitalWrite(g,1);
pinMode(g,OUTPUT); } }
} void two(){ void four(){
void zero(){ digitalWrite(a,1); digitalWrite(a,0);
digitalWrite(a,1); digitalWrite(b,1); digitalWrite(b,1);
digitalWrite(b,1); digitalWrite(c,0); digitalWrite(c,1);
digitalWrite(c,1); digitalWrite(d,1); digitalWrite(d,0);
digitalWrite(d,1); digitalWrite(e,1); digitalWrite(e,0);
digitalWrite(e,1); digitalWrite(f,0); digitalWrite(f,1);
digitalWrite(f,1); digitalWrite(g,1); digitalWrite(g,1);
digitalWrite(g,0);} } }
Code of 7seg Display
void nine(){
void five(){ void seven(){
digitalWrite(a,1);
digitalWrite(a,1); digitalWrite(a,1);
digitalWrite(b,1);
digitalWrite(b,0); digitalWrite(b,1);
digitalWrite(c,1);
digitalWrite(c,1); digitalWrite(c,1);
digitalWrite(d,1);
digitalWrite(d,1); digitalWrite(d,0);
digitalWrite(e,0);
digitalWrite(e,0); digitalWrite(e,0);
digitalWrite(f,1);
digitalWrite(f,1); digitalWrite(f,0);
digitalWrite(g,1);}
digitalWrite(g,1); digitalWrite(g,0);
void loop(){
} }
zero(); delay(1000);
void six(){ void eight(){
one(); delay(1000);
digitalWrite(a,1); digitalWrite(a,1);
two(); delay(1000);
digitalWrite(b,0); digitalWrite(b,1);
three(); delay(1000);
digitalWrite(c,1); digitalWrite(c,1);
four(); delay(1000);
digitalWrite(d,1); digitalWrite(d,1);
five(); delay(1000);
digitalWrite(e,1); digitalWrite(e,1);
six(); delay(1000);
digitalWrite(f,1); digitalWrite(f,1);
seven();delay(1000);
digitalWrite(g,1); digitalWrite(g,1);
eight(); delay(1000);
} }
nine(); delay(1000); }
Serial communication(UART)

The rate at which individual bits of data are sent is called the
baud rate. One of the most common baud rates for UART (the
closest thing to default) is 9600 bits per second (bps). Other
common baud rates are 300, 600, 1200, 2400, 4800, 19200,
38400, 57600, 74880, and 115200 bps
Serial
➢ All Arduino boards have at least one serial port ( also known as a UART )
➢ pins 0 and 1 are used for communication with the computer.
➢ 0(RX), 1(TX) .
Serial

uno

Serial.print( ) : Prints data to the serial port as human-readable

Serial.print (78) gives ” 78”


Serial.print (1.23456 , 4) gives ” 1.2345”
Serial.print (“ Hello world. ”) gives ” Hello world”

Serial.println( ) : Prints data to the serial port as human-readable & Newline


Serial

uno
data we send to arduino
serial port

HI

Serial.available( ) : Get the number of bytes (characters) available for reading from the serial port.
Serial.read( ) : Reads incoming serial data.
Serial communication

input

output

speed
example (Serial communication)
void loop( ) {
const int red = 9; if(Serial.available()>0) {
const int yellow = 10; reading=Serial.read(); F
B
L
R send
const int green=11; switch(reading){
char reading; // data type used to store a character value. case ‘F’: digitalWrite(red,1); red is on
void setup( ) Serial.println(“red is on”);
{ break;
pinMode(red, OUTPUT); case ‘B’: digitalwrite(yellow,1);
pinMode(yellow, OUTPUT); break;
pinMode(green, OUTPUT); case ‘R’: digitalWrite(green,1);
Serial.begin(9600); // Adjust speed of serial monitor break;
} case ‘L’: digitalWrite(red,0);
digitalwrite(yellow,0);
digitalWrite(green,0);
break;
}
}
}
buzzer
tone function
• Generates a square wave of the specified frequency (and 50% duty cycle) on a pin
• the wave continues until a call to noTone()
• Only one tone can be generated at a time
• function will interfere with PWM output on pins 3 and 11
• It is not possible to generate tones lower than 31Hz
• the maximum frequency that can be produced is 65535 Hz
• the human hearing range is typically as high as 20 kHz
Syntax
tone(pin, frequency)
tone(pin, frequency, duration(
Parameters
pin: the Arduino pin on which to generate the tone.
frequency: the frequency of the tone in hertz. Allowed data types: unsigned int.
duration: the duration of the tone in milliseconds (optional). Allowed data types: unsigned long.

Notes and Warnings


If you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.
example tone
void setup()
{

}
void loop() {

tone(6, 440, 200); // play a note on pin 6 for 200 ms: (tone 1)
delay(200);
noTone(6); // turn off tone function for pin 6:
tone(7, 494, 500); // play a note on pin 7 for 500 ms: (tone 2)
delay(500);
noTone(7); // turn off tone function for pin 7: (tone 3)
tone(8, 523, 300); // play a note on pin 8 for 300 ms:
delay(300);
noTone(8);
}
delay is very important to stop processor until the tone end
without delay the processor execute the lines without
waiting for the tone to end
line tracker sensor (TCRT5000)
Basic principle

reflection distance: 1mm to 25mm

Reading Reading
1 0
Code
int lineTracker = 8;
int led = 2;
int state = 0;
void setup()
{
pinMode(lineTracker,INPUT);
pinMode(led,OUTPUT);
}
void loop()
{
state=digitalRead(lineTracker);
digitalWrite(led,state);
}
Ultrasonic sensor

➢The HC-SR04 ultrasonic sensor uses sonar to determine


distance to an object like bats do

➢Power Supply :+5V DC

➢Ranging Distance : 2cm – 400 cm

➢Trigger Input Pulse width: 10uS

➢waves are sound waves


Basic principle

• signal HIGH → 10usec trig

• We need to divide the travel time by 2


echo

• speed sound 343 m/s

• Arduino calculate the time

• after receiving wave echo send


low signal
Connection of ultrasonic
Code
#define trigPin 12
#define echoPin 11
long duration, distance;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) * 0.0343; // 343 * (100/1000000) = 343/10000
Serial.println(distance);
delay(5);
}
Bluetooth hc-05
Pin Configuration
button

Pin number Pin Name Description


1 Enable / Key This pin is used to toggle between Data Mode (set low) and AT command mode
(set high). By default it is in Data mode
2 Vcc Powers the module. Connect to +5V Supply voltage
3 Ground Ground pin of module, connect to system ground.
4 TX – Transmitter Transmits Serial Data. Everything received via Bluetooth will be given out by this
pin as serial data.
5 RX – Receiver Receive Serial Data. Every serial data given to this pin will be broadcasted via
Bluetooth
6 State The state pin is connected to on board LED, it can be used as a feedback to
check if Bluetooth is working properly.
7 Button Used to control the Key/Enable pin to toggle between Data and command Mode
HC-05 Default Settings

➢Default Bluetooth Name: “HC-05”


➢Default Password: 1234 or 0000
➢Default Communication: Slave
➢Default Mode: Data Mode
➢Data Mode Baud Rate: 9600
➢Command Mode Baud Rate: 38400
➢This Bluetooth module covers 9 meters (30ft)
➢Operating Voltage: 4V to 6V (Typically +5V)
➢Operating Current: 30mA
Connection with arduino
Code
const int red = 13;
char reading; // data type used to store a character value.
void setup( )
{
pinMode(red, OUTPUT);
Serial.begin(9600); // Adjust speed of serial monitor
}
void loop( ) {
if(Serial.available()>0) {
reading=Serial.read();
switch(reading){
case ‘F’: digitalWrite(red,1);
break;
case ‘S’: digitalWrite(red,0);
break;
}
}
}
Application in smart phone
Bluetooth Electronic
Bluetooth Electronic

drag and drop


Bluetooth Electronic
Bluetooth Electronic
Bluetooth Electronic

S
Bluetooth Electronic
Bluetooth Electronic

S
Bluetooth Electronic
Bluetooth Electronic
Bluetooth Electronic

return to background
Bluetooth Electronic

final
control panel
password
0000 -1234
DC Motor

Operating Voltage 3V-12VDC Operating Voltage


3V-12VDC
Maximum Torque 800g/cm max Maximum Torque
800g/cm max. @ 3V
Gear Ratio
Gear Ratio 1:48 1:48
Load Current
Load Current 70mA (250mA max. @ 3V) 70mA (250mA max. @ 3V)
Weight
29g
DC Motor

DC Motor is any of a class of rotary electrical motors that converts


direct current electrical energy into mechanical energy
DC Motor

terminals
Basic principle

blue → positive forword


purpule→negative

VS GND
6V , 12V 0V
Basic principle

backword

GND VS
0V 6V , 12V
Mobile Robot
Differential steering

Forword
Left
Backword
Right
WHY DO I NEED A MOTOR DRIVER
MODULE ?
Driver Motor ( L298N)

Motor driver receives signals from the microprocessor and eventually,


it transmits the converted signal to the motors.
we will agree:
car is forword when
blue cable → HIGH signal
purpule cable→LOW signal
out 1 out 4
left motor
blue cable →out1
ML out 2 out 3
MR
purpule cable→out2
right motor
blue cable→out3
purpule cable→out4

speed L IN1 IN2 IN3 IN4 speed R

5V 0V 5V 0V
we will agree:
car is forword when
blue cable → HIGH signal
purpule cable→LOW signal
out 1 out 4
left motor
blue cable →out1
ML out 2 out 3
MR
purpule cable→out2
right motor
blue cable→out3
purpule cable→out4

speed L IN1 IN2 IN3 IN4 speed R

0V 5V 0V 5V
Bluetooth Car Connection
Code Bluetooth car
#define IN1 7 void backword(){ void stopp(){
#define IN2 6 digitalWrite(IN1, LOW); digitalWrite(IN1, LOW);
#define IN3 5 digitalWrite(IN2, HIGH); digitalWrite(IN2, LOW);
#define IN4 4 digitalWrite(IN3, LOW); digitalWrite(IN3, LOW);
char Reading; digitalWrite(IN4, HIGH); digitalWrite(IN4, LOW);
void setup() { } }
Serial.begin (9600); void left(){ void loop(){
pinMode(IN1, OUTPUT); digitalWrite(IN1, LOW); if(Serial.available()>0){
pinMode(IN2, OUTPUT); digitalWrite(IN2, LOW); Reading=Serial.read();
pinMode(IN3, OUTPUT); digitalWrite(IN3, HIGH); switch(Reading){
pinMode(IN4, OUTPUT); digitalWrite(IN4, LOW); case ‘F’: forword(); break;
} } case ’B’: backword(); break;
void forword() void right() case ‘R’: right(); break;
{ { case ‘L’ : left(); break;
digitalWrite(IN1, HIGH); digitalWrite(IN1, HIGH); case ‘S’ :stopp(); break;
digitalWrite(IN2, LOW); digitalWrite(IN2, LOW); }
digitalWrite(IN3, HIGH); digitalWrite(IN3, LOW); }
digitalWrite(IN4, LOW); digitalWrite(IN4, LOW); }
} }
Bluetooth RC Controller

not connect press


with any here press
bluetooth here
Code Bluetooth car

Bluetooth is
connected
press
here

password
0000 -1234

press control with


here these buttons
Bluetooth RC Controller

press
here
Line followe car
SL SR Direction 1 1
0 1

0 0

0 1
Forword
Right
Left
Stop 0 0 0 0 0 0

N 2

0 0

1 0
2 =2 =4
N : number of sensor 0 1
Code Line follower car
#define speedL 10 void forword() void left() void stopp(){
#define IN1 9 { { digitalWrite(IN1, LOW);
#define IN2 8 digitalWrite(IN1, HIGH); digitalWrite(IN1, LOW); digitalWrite(IN2, LOW);
#define IN3 7 digitalWrite(IN2, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW);
#define IN4 6 digitalWrite(IN3, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW);
#define speedR 5 digitalWrite(IN4, LOW); digitalWrite(IN4, LOW); analogWrite(speedL,0);
#define sensorL 4 analogWrite(speedL,80); analogWrite(speedL,0); analogWrite(speedR,0);
#define sensorR 3 analogWrite(speedR,80); analogWrite(speedR,80); }
int sl=0; } } void loop(){
int sr=0; void backword() void right() sl=digitalRead(sensorL);
void setup() { { { sr=digitalRead(sensorR);
digitalWrite(IN1, LOW); digitalWrite(IN1, HIGH); if (sl==0&&sr==0)
for(int i=5;i<=10;i++)
digitalWrite(IN2, HIGH); digitalWrite(IN2, LOW); forword();
{ digitalWrite(IN3, LOW); digitalWrite(IN3, LOW); else if (sl==0&&sr==1)
pinMode(i, OUTPUT); digitalWrite(IN4, HIGH); digitalWrite(IN4, LOW); right();
} analogWrite(speedL,80); analogWrite(speedL,80); else if (sl==1&&sr==0)
pinMode(sensorR, INPUT); analogWrite(speedR,80); analogWrite(speedR,0); left();
pinMode(sensorL, INPUT); } } else if (sl==1&&sr==1)
} stopp(); }
Ultrasonic car

20cm 150cm
20cm
50cm
Code Ultrasonic car
#define IN1 7 void Ultrasonic(){ void backword(){ void stopp(){
#define IN2 6 digitalWrite(trig, LOW); digitalWrite(IN1, LOW); digitalWrite(IN1, LOW);
#define IN3 5 delayMicroseconds(2); digitalWrite(IN2, HIGH); digitalWrite(IN2, LOW);
#define IN4 4 digitalWrite(trig, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN3, LOW);
#define trig 8 delayMicroseconds(10); digitalWrite(IN4, HIGH); digitalWrite(IN4, LOW);
#define echo 9 digitalWrite(trig, LOW); } }
long duration,distance; duration = pulseIn(echo, HIGH); void left(){ void loop(){
void setup() { distance = (duration/2) * 0.0343; digitalWrite(IN1, LOW); Ultrasonic();
digitalWrite(IN2, LOW); if(distance<20){
for(int i=4;i<=8;i++) }
void forword() digitalWrite(IN3, HIGH); stopp();
{ digitalWrite(IN4, LOW);
{ delay(250);
pinMode(i, OUTPUT); digitalWrite(IN1, HIGH); } right(); //or left as you like
} digitalWrite(IN2, LOW); void right() delay(500);
pinMode(echo, INPUT); digitalWrite(IN3, HIGH); { }
} digitalWrite(IN4, LOW); digitalWrite(IN1, HIGH); else{
} digitalWrite(IN2, LOW); forword();
digitalWrite(IN3, LOW); }
digitalWrite(IN4, LOW); }
}
PIR (HC-SR501)
PIR stands for Pyroelectric Infrared Radial Sensor or Passive
Infrared Sensor. PIR is an electronic sensor which detects the
changes in the infrared light across certain distance and gives
out an electrical signal at its output in response to a detected IR
signal. It can detect any infrared emitting object such as human
beings or animals if it is the range of the sensor, or moves away
from the range, or moves within the range of the sensor.

Fresnel lens Pyroelectric sensor


Basic principle
Any object which has a temperature i.e. anything above absolute zero
(-459.67 degrees Fahrenheit or -273.15 degrees Celsius or 0 degrees Kelvin)
How PIR Works
Specification of PIR

➢ sensor module is powered up after a minute


➢ Operating Voltage: 5V to 20 V

➢ Output Voltage: HIGH at 3.3 V , LOW or 0 V

➢ Delay time: from 5 seconds to 5 minute

➢ Sensitivity Range: 3 meter to 7 meters

➢ angle : 110 degree cone

➢ Block Time: is 3 seconds by default

➢ Trigger modes:
1-Single Trigger Mode / Non- repeat Mode
2-Repeat Trigger Mode
Connection with arduino
Code of PIR
int detectedLED = 13; void loop() {
int readyLED = 12; pirValue = digitalRead(pirPin);
int waitLED = 11; if (pirValue == 1) {
int pirPin = 7; digitalWrite(detectedLED, HIGH);
int pirValue; digitalWrite(readyLED, LOW);
int motionDetected = 0 motionDetected = 1;
delay(3000); //turn on led for 3 sec
void setup() { }
else {
pinMode(detectedLED, OUTPUT); digitalWrite(detectedLED, LOW);
pinMode(readyLED, OUTPUT); }
pinMode(waitLED, OUTPUT); if (motionDetected == 1) {
pinMode(pirPin, INPUT); digitalWrite(detectedLED, LOW);
digitalWrite(detectedLED, LOW); digitalWrite(readyLED, LOW);
digitalWrite(readyLED, LOW); digitalWrite(waitLED, HIGH);
digitalWrite(waitLED, HIGH); delay(6000);
delay(60000); digitalWrite(readyLED, HIGH);
digitalWrite(readyLED, HIGH); digitalWrite(waitLED, LOW);
digitalWrite(waitLED, LOW); motionDetected = 0;
} }
}
Lm35 (temperature sensor)
➢ LM35 is a temperature measuring device having an
analog output voltage proportional to the temperature.

➢ It provides output voltage in Centigrade (Celsius). It


does not require any external calibration circuitry.

➢ The sensitivity of LM35 is 10 mV/degree Celsius. As


temperature increases, output voltage also increases.
E.g. 250 mV means 25°C.

➢ It is a 3-terminal sensor used to measure surrounding


temperature ranging from -55 °C to 150 °C.

➢ LM35 gives temperature output which is more precise


than thermistor output.
Connection with arduino
Code of lm35
#define tempPin A0 Volt Level
float val;
float volt; 5V 1023
float temp;
void setup()
{
Serial.begin(9600); ?? val
}
void loop()
{
val = analogRead(tempPin);
volt = (val*5)/1023
volt= ( val*5) /1023.0;
temp = volt/0.01;
Serial.print("TEMPRATURE = "); volt = 10mv * temp data sheet
Serial.print(temp);
Serial.println("*C");
delay(1000(; temp = Volt / 10mV
}
Voltage divider

Vout = VS X R2
R1+R2
Vout = 5 X 1 = 2.5 V
R1 1K
1K
1+1
VS 5V Vout 3
Vout = 5 X = 3.75 V
R2 0.33K
1K
3K 1+3
Vout = 5 X 0.33 = 1.24 V
1+0.33
LDR (Light Dependent Resistor)

➢ An LDR is a component that has a (variable)


resistance that changes with the light intensity that
falls upon it. This allows them to be used in light
sensing circuits.

➢ The most common type of LDR has a resistance that


falls with an increase in the light intensity falling
upon the device

➢ The resistance of an LDR may typically have the


following resistances:

Daylight = 5000Ω
Dark = 20MΩ
Basic principle
Dark
Light

30K
5K
R1
0.16
0.83
5V Vout
R2 1K
Connection with arduino
Code of LDR
#define ldr A0
#define led 3
int threshold=40;
int level;
void setup(){
Serial.begin(9600);
pinMode(led,OUTPUT);
}
void loop(){
level= analogRead(ldr);
Serial.println(level);
if(level< threshold){
digitalWrite(led,1); }
else{
digitalWrite(led,0);}
}
Relay

An electrical relay is an electromagnetically


operated electrical switch - an electromechanical
switch. A relatively small current is used to create
a magnetic field in a coil within a magnetic core
and this is used to operate a switch that can
control a much larger current.
Basic principle
Basic principle

220V ~

5V
Connection with arduino

VCC
GND
IN1
Code of Relay
#define on 0
#define off 1
int relay=4 ;
void setup()
{
pinMode(relay,OUTPUT);
}
void loop()
{
digitalWrite(relay,on);
delay(5000);
digitalWrite(relay,off);
delay(5000);
}
Flame sensor
➢ Detects a flame or a light source of a wavelength in
the range of 760nm-1100 nm.

➢ Detection range: up to 80 cm.

➢ Operating temp: -25° ~ 85°


➢ Adjustable detection range.

➢ Detection angle about 60 degrees, it is sensitive to


the flame spectrum.

➢ Operating voltage 3.3V-5V.

➢ Digital and Analog Output.


Basic principle

principle that the infrared ray is sensitive to flame.


It uses the photo diode to detect flame and converts
the brightness of flame to the variable level signal.
Calibrate Flame Sensor Module

Expose the module to flame or strong light and turn the knob of the
potentiometer gently till the D0 indicator light is on
Connection with arduino
Code of flame sensor
#define flame 3 void loop()
#define buzzer 5 {
int Val = 0; Val = digitalRead(flame);
void setup() Serial.print(“value of flame:");
{ Serial.println(Val);
Serial.begin(9600); if (Val == LOW) {
pinMode(flame , INPUT); digitalWrite(buzzer, HIGH);
pinMode(buzzer,OUTPUT); }
} else {
digitalWrite(buzzer, LOW);
}
}
Smoke sensor

➢ MQ2 Gas sensor works on 5V DC and draws around 800mW.

➢ It can detect LPG, Smoke, Alcohol, Propane, Hydrogen,


Butane, Methane and Carbon Monoxide concentrations
anywhere from 200 to 10000ppm
Parts-per-million (abbreviated ppm) is the ratio of one gas to
another. For example, 1,000ppm of CO means that if you could
count a million gas molecules, 1,000 of them would be of carbon
monoxide and 999,000 molecules would be some other gases.

➢ preheat time : NOT less than 48 hours


Basic principle
Basic principle

When tin dioxide (semiconductor


particles) is heated in air at high
temperature, oxygen is adsorbed on the
surface. In clean air, donor electrons in tin
dioxide are attracted toward oxygen which
is adsorbed on the surface of the sensing
material. This prevents electric current
flow.
In the presence of reducing gases, the
surface density of adsorbed oxygen
decreases as it reacts with the reducing
gases. Electrons are then released into the
tin dioxide, allowing current to flow freely
through the sensor.
Basic principle
The analog output voltage provided by the sensor changes in proportional to the concentration of
smoke/gas. The greater the gas concentration, the higher is the output voltage; while lesser gas
concentration results in low output voltage. The following animation illustrates the relationship
between gas concentration and output voltage.
Calibrate MQ2 Gas Sensor Module

To calibrate the gas sensor you can hold the gas sensor near smoke/gas you want to detect and
keep turning the potentiometer until the Red LED on the module starts glowing. Turn the screw
clockwise to increase sensitivity or anticlockwise to decrease sensitivity.
Connect with arduino
Code of smoke detector
#define MQ2pin A5 void loop()
#define buzzer 10 {
int sensorValue; sensorValue = analogRead(MQ2pin);
void setup() Serial.print("Sensor Value: ");
{ Serial.println(sensorValue);
Serial.begin(9600); if(sensorValue > 300)
pinMode(buzzer,OUTPUT); {
Serial.println("Gas sensor warming up!"); Serial.print(" | Smoke detected!");
delay(20000); // allow the MQ-2 to warm up digitalWrite(buzzer,HIGH);
} }
else
{
digitalWrite(buzzer,LOW);
}
delay(2000); // wait 2s for next reading
}
LCD Display
Terminal Functions
Pin Number Name Function
1 VSS Ground Voltage
2 VCC Power 5V
3 VO Contrast Voltage
Register Select
4 RS 1=transferring display data
0=transferring instruction data
Read/Write control bus
5 R/W 1=Read Mode
0=Write Mode
Enable
6 EN 0=Enable
1=Disable
7~10 D0~D3 Bi-directional data bus
11~14 D4~D7 Bi-directional data bus
15 BLA Backlight positive supply
16 BLK Backlight negative supply
Connection with arduino
Code of LCD display without I2C
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
lcd.print(“ST SMART");
lcd.setCursor(7,1);
lcd.print(“2020");
}
void loop() {
for(int i=0;i<15;i++){
lcd.scrollDisplayRight();
delay(200);
}
for(int i=0;i<15;i++){
lcd.scrollDisplayLeft();
delay(200);
}
}
I2C protocol
• The two wires, or lines are called Serial Clock (or
SCL) and Serial Data (or SDA). The SCL line is
the clock signal which synchronize the data
transfer between the devices on the I2C bus and
it’s generated by the master device. The other line
is the SDA line which carries the data.

• The two lines are “open-drain” which means that


pull up resistors needs to be attached to them so
that the lines are high because the devices on the
I2C bus are active low. Commonly used values for
the resistors are from 2K for higher speeds at
about 400 kbps, to 10K for lower speed at about
100 kbps.

• The speed (standard mode: 100 kbit/s,


full speed:400 kbit/s,
fast mode: 1 mbit/s,
high speed: 3,2 Mbit/s)

• Wire Library on Arduino programming language


I2C protocol
I2C module

➢ Supply voltage: 5V

➢ I2C Address: 0X20~0X27 (the original address is


0X27,you can change it yourself)

➢ the backlight and contrast is adjusted by


potentiometer

➢ SCL is A5 pin , SDA is A4 pin


WHY WE USE I2C MODULE?
Address of I2C Module

A0 A1 A2 Address
Open Open Open 0X27
Jumper Open Open 0X26
Open Jumper Open 0X25
Jumper Jumper Open 0X24
Open Open Jumper 0X23
Jumper Open Jumper 0X22
Open Jumper Jumper 0X21
Jumper Jumper Jumper 0X20
Soldering I2C module with lcd
Connection with arduino
Code of LCD display with I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
lcd.begin(); // or some librarys are lcd.init();
lcd.backlight();
lcd.print(“ST SMART");
lcd.setCursor(7,1);
lcd.print(“2020");
}
void loop() {
for(int i=0;i<15;i++){
lcd.scrollDisplayRight();
delay(200);
}
for(int i=0;i<15;i++){
lcd.scrollDisplayLeft();
delay(200);
}
}
keypad
diagram keypad
connection of keypad
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
}

void loop(){
char key = keypad.getKey();
if (key){
Serial.println(key);
}
}
Servo motor
code servo
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup() {
myservo.attach(9);
}

void loop() {
for (pos = 0; pos <= 180; pos++)
{
myservo.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos--)
{
myservo.write(pos);
delay(15);
}
}
Gyro sensor
Bluetooth Transmitter&Receiver
AT COMMAND
Slave Configuration
Master Configuration
Functions used in setup()
1. pinMode(13, OUTPUT); makes pin 13 as output pin
2. pinMode(8, INPUT); makes pin 8 as input pin
3. Serial.begin(9600) ; starts serial communication with Baudrate 9600

Functions used in loop()

1. digitalWrite(13, HIGH): makes pin 13 high ie pin13=ON;


2. digitalRead(button) – read a digital pin’s state
3. delay(500) : delays system by 500 ms.
4. analogRead() : Reads analog value
5. analogWrite() : writes anlog value(PWM)
6. Serial.print(): Prints at serial monitor
7. Serial.println() : prints at serial monitor with line break

• And many others. And libraries. And examples!


Thanks for coming
ENG : Abdelrahman Bayoume

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy