Smart Technology Robotics
Smart Technology 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.
➢ 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
System
Robotics
Power LED
Digital&Analog signal
on on on
5V 5V
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
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
Make
common
for
GND
if (inputPin == HIGH)
{
doThingA;
}
else
{
doThingB;
}
Pull down Resistor
5V
pin pin
Arduino
uno Arduino
uno
0V
Pull down Resistor
Make
common
for
GND
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)
Make
common
for
GND
}
}
Connection of three led with pushbutton
Make
common
for
GND
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
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
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
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)
|
|
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
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
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
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.
}
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
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
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
terminals
Basic principle
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)
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
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
Bluetooth is
connected
press
here
password
0000 -1234
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.
➢ 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.
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)
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
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.
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
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.
➢ Supply voltage: 5V
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