Professional Documents
Culture Documents
! !
" # !
$ %& & '
( #& %& &
' %& &
) & * &
+ &
,+ &
- ., &
% &
/ & * &
" 0 &
$ && %&
( 0 % %&
' +1 %&
) +- %&
Arduino Program
Experiment 1
LED Blinking:
In Arduino the very interesting and basic experiment is LED Blinking. Arduino
board consists of one led connected to pin 13. In this we will turn ON and OFF Led after
some specified time delay with the help of Arduino. To interface LED with Arduino via
programming we have to follow some programming rules and formats.
Circuit:
1
Never Win Akshay D. Shelke
Arduino Program
Program:
void setup()
{
// put your setup code here, to run once:
pinMode(led,OUTPUT); // this makes the pin no 13 work as output.
}
void loop()
{
// put your main code here, to run repeatedly:
digitalWrite(led,HIGH);// Turn ON LED
delay(100);
digitalWrite(led,LOW); // Turn OFF LED
delay(100);
}
2
Never Win Akshay D. Shelke
Arduino Program
Experiment 2
Chaser :
This program is used to blink multiple LEDs one by one. It is commonly known
as Chaser. Here we are using eight LEDs. Each LED will blink one after another. Eight
LEDs are mounted on breadboard along with resistors. Anode of each led is given to
Arduino pin through resistor of 220 Ohm. Cathode of each LED is connected to Ground.
Circuit:
Same chaser logic can be performed by more than one method in first multiple
write functions are used in single program. In another method For loop is used for
chaser programming which reduces size of program
3
Never Win Akshay D. Shelke
Arduino Program
Program:
int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
void setup()
{
// put your setup code here, to run once:
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
// turn ON and OFF LEDs one by one with small delay
digitalWrite(led1,HIGH); delay(100);
digitalWrite(led1,LOW); delay(100);
digitalWrite(led2,HIGH); delay(100);
digitalWrite(led2,LOW); delay(100);
digitalWrite(led3,HIGH); delay(100);
digitalWrite(led3,LOW); delay(100);
digitalWrite(led4,HIGH); delay(100);
digitalWrite(led4,LOW); delay(100);
digitalWrite(led5,HIGH); delay(100);
digitalWrite(led5,LOW); delay(100);
digitalWrite(led6,HIGH); delay(100);
digitalWrite(led6,LOW); delay(100);
digitalWrite(led7,HIGH); delay(100);
digitalWrite(led7,LOW); delay(100);
digitalWrite(led8,HIGH); delay(100);
digitalWrite(led8,LOW); delay(100);
}
4
Never Win Akshay D. Shelke
Arduino Program
Experiment 3
Single Input :
In previous experiments we turned ON / OFF LEDs with the help of
programming only. Here along with programming we will use one switch as INPUT.
This will turn ON /OFF LED according to the program.
Circuit:
In this experiment we will require one LED an d one Switch. Here Switch is used
as INPUT and LED is used as OUTPUT. Whenever switch is pressed LED should turn
ON and when switch is released it should turn OFF. Switch is connected to pin 2 of
Arduino and LEd is connected to pin 3 of Arduino.
Led Connection:
Anode: To pin 3 of Arduino
Cathode: To Ground
Switch connection:
Connect Pin2 of Arduino to one of the terminal of switch and through 1K resistor
connect same pin to VCC
5
Never Win Akshay D. Shelke
Arduino Program
Program:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 3; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton
status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
6
Never Win Akshay D. Shelke
Arduino Program
Experiment 4
Buzzer:
Circuit:
Buzzer Connections:
• Positive Terminal of Buzzer: Pin 9 of Arduino
• Negative Terminal of Buzzer: Ground
7
Never Win Akshay D. Shelke
Arduino Program
Program:
void setup()
{
}
void loop() {
// turn off tone function for pin 9:
noTone(9);
// play a 440Hz frequency on pin 6 for 2000 ms:
tone(9, 440, 200);
delay(2000);
noTone(9); // now tone is stopped
// turn off tone function for pin 6:
delay(2000);
// now we'll use a for loop to give wide range of
// freuquencies to buzzer for small durations
// this will go longer as we'll hear sounds of each
// frequencies for 500 msecs
for(int i = 20; i < 2000; i++) //
{
tone(9,i);
delay(5);
noTone(9);
}
noTone(9);
delay(1000); // we stop here
}
8
Never Win Akshay D. Shelke
Arduino Program
Experiment 5
Liquid Crystal Display:
Liquid Crystal Display is used to display information in its rows and columns.
The information is in the form of letters, Numbers, special characters or graphics.
Depending upon type it displays Alphabets ,numbers or Graphics.
In this experiment we are using 16x2 Alpha numeric LCD which can display
letters (A-Z, a-z), Numbers(0-9),and some special Characters . It has 16 pins. Each pin
performs different Functions.
Pin Diagram:
9
Never Win Akshay D. Shelke
Arduino Program
Pin Description:
1 VSS Ground
2 VCC +5V
3 VEE Power Supply to control Contrast
4 RS RS=0, Select Command Register
RS=1, Select Data Register
5 R/W R/W=0, For Write
R/W=1, For Read
6 EN Enable
7 D0 Data Pin 0
8 D1 Data Pin 1
9 D2 Data Pin 2
10 D3 Data Pin 3
11 D4 Data Pin 4
12 D5 Data Pin 5
13 D6 Data Pin 6
14 D7 Data Pin 7
While interfacing LCD with Arduino connect above mentioned pin to proper
pin.In this experiment we are displaying information on LCD Screen using Arduino
programming. For Data transfer use upper 4 bit pins.This will save some Arduino pins
and will reduce complexity of programming also.
Pin 15 and 16 pin of Alphanumeric LCD are used for backlight control Connect
pin 15 to +5V and pin 16 to Ground through 39 Ohm Resistance.
10
Never Win Akshay D. Shelke
Arduino Program
Circuit:
11
Never Win Akshay D. Shelke
Arduino Program
Program:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2,3,4,5,6,7);
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("e-Vidya");
}
void loop()
{
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
// so lcd.setCursor works as lcd.setCursor(coloumn no, Row no);
// where coloumn no can be 0-15 and row no can be 0-1
lcd.setCursor(0, 1);
// print the number of seconds since program started
lcd.print(millis() / 1000);
}
12
Never Win Akshay D. Shelke
Arduino Program
Experiment 6
Seven Segment Display:
In case of common Anode one Common terminal is fed with +5V and remaining
segments are connected to Ground. In Common Cathode display, Common pin is given
to Ground and remaining pins are fed to +5V. In both cases resistors should be
connected to segment pins.
Circuit:
This project is used to display Numbers from 0 to 9 using a new interface called
as Seven segment Display. Arduino is interfaced with Seven segment display to achieve
desired task. Arduino display numbers from 0-9 continuously with the help of Seven
segment Display with some time delay generated by Arduino itself.
13
Never Win Akshay D. Shelke
Arduino Program
14
Never Win Akshay D. Shelke
Arduino Program
Program:
byte seven_seg_digits[10][7] = { { 0,0,0,0,0,0,1 }, // = 0
{ 1,0,0,1,1,1,1 }, // = 1
{ 0,0,1,0,0,1,0 }, // = 2
{ 0,0,0,0,1,1,0 }, // = 3
{ 1,0,0,1,1,0,0 }, // = 4
{ 0,1,0,0,1,0,0 }, // = 5
{ 0,1,0,0,0,0,0 }, // = 6
{ 0,0,0,1,1,1,1 }, // = 7
{ 0,0,0,0,0,0,0 }, // = 8
{ 0,0,0,1,1,0,0 } // = 9
};
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
writeDot(1); // start with the "dot" off
}
void writeDot(byte dot)
{
digitalWrite(9, dot);
}
void sevenSegWrite(byte digit)
{
byte pin = 8;
for (byte segCount = 0; segCount < 7; ++segCount)
{
digitalWrite(pin, seven_seg_digits[digit][segCount]);
pin--;
}
}
void loop()
{
for (int i = 0; i < 10; i++)
{
sevenSegWrite(i);
delay(1000);
} delay(1000);
}
15
Never Win Akshay D. Shelke
Arduino Program
Experiment 7
DC motor and L293 Driver:
Whenever a robotics hobbyist talk about making a robot, the first thing comes to
his mind is making the robot move on the ground. And there are always two options in
front of the designer whether to use a DC motor or a stepper motor. When it comes to
speed, weight, size, cost... DC motors are always preferred over stepper motors.
There are many things which you can do with your DC motor when interfaced
with a microcontroller. For example you can control the speed of motor, you can control
the direction of rotation. Usually H-bridge is preffered way of interfacing a DC motor.
These days many IC manufacturers have H-bridge motor drivers available in the
market like L293D is most used H-Bridge driver IC. l293D is a dual H-Bridge motor
driver, So with one IC we can interface two DC motors which can be controlled in both
clockwise and counter clockwise direction.
L293D has output current of 600mA and peak output current of 1.2A per
channel. Moreover for protection of circuit from back EMF ouput diodes are included
within the IC. The output supply (VCC2) has a wide range from 4.5V to 36V, which has
made L293D a best choice for DC motor driver.
16
Never Win Akshay D. Shelke
Arduino Program
17
Never Win Akshay D. Shelke
Arduino Program
Circuit:
L293D Connections:
Pin 1 (EN1) : To digital Pin 11 of Arduino
Pin 2(IN1) : To digital Pin 10 of Arduino
Pin 3 : To DC Motor 1
Pin 4 & Pin 5 : Ground
Pin 6 : To DC Motor 1
Pin 7 :To DC Motor 1
Pin 8 : To +12 V
Pin 9(EN2) : To digital Pin 6 of Arduino
Pin 10(IN3) : To digital Pin 8 of Arduino
Pin 11 : To DC Motor 2
Pin 12 & Pin 13 : Ground
Pin 14 : To DC Motor 2
Pin 15(IN4) : To digital Pin 7 of Arduino
Pin 16 :+5V
18
Never Win Akshay D. Shelke
Arduino Program
Program:
void setup()
{
// put your setup code here, to run once:
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void forward()
{
digitalWrite(2,HIGH);
digitalWrite(7,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(6,HIGH);
digitalWrite(5,LOW);
}
void reverse()
{
digitalWrite(2,HIGH);
digitalWrite(7,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(6,LOW);
digitalWrite(5,HIGH);
}
void left()
{
digitalWrite(2,HIGH);
digitalWrite(7,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(6,LOW);
digitalWrite(5,HIGH);
}
void right()
{
digitalWrite(2,HIGH);
digitalWrite(7,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(6,HIGH);
digitalWrite(5,LOW);
19
Never Win Akshay D. Shelke
Arduino Program
void stop()
{
digitalWrite(2,LOW);
digitalWrite(7,LOW);
}
void loop()
{
forward();
delay(5000);
stop();
delay(2000);
right();
delay(350);
stop();
delay(2000);
reverse();
delay(4000);
stop();
delay(2000);
left();
delay(350);
stop();
delay(2000);
}
20
Never Win Akshay D. Shelke
Arduino Program
Experiment 8
Servo Motor:
Servos are DC motors with built in gearing and feedback control loop circuitry.
Most servo motors can rotate about 90 to 180 degrees. Some rotate through a full 360
degrees or more. However, servos are unable to continually rotate, meaning they can't
be used for driving wheels (unless modified), but their precision positioning makes
them ideal for robot arms. Since servos are fully self contained, the velocity and angle
control loops are very easy to impliment, while prices remain very affordable.
To use a servo, simply connect the black wire to ground, the red to a 4.8-6V
source, and the yellow/white wire to a signal generator (such as from your
microcontroller). Vary the square wave pulse width from 1-2ms and your servo is now
position/velocity controlled. Servo Wiring All servos have three wires:
1.Black or Brown is for ground.
2.Red is for power(4.8-6V)
3.Yellow, Orange, or White is the signal wire (3-5V).
The standard time vs. angle is represented in this chart:
Program:
21
Never Win Akshay D. Shelke
Arduino Program
Circuit:
22
Never Win Akshay D. Shelke
Arduino Program
Program:
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup()
{
myservo.attach(9);
}
void loop()
{
for(pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=0; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
23
Never Win Akshay D. Shelke
Arduino Program
Experiment 9
Stepper Motor:
Stepping motors fill a unique niche in the motor control.These motors are
commonly used in measurement and control applications Sample applications include
ink jet printers, CNC machines and volumetri pumps. Several features common to all
stepper motors make them ideally suited for these types of applications.
Working:
The stepper motor is known by its property to convert a train of input pulses
(typically square wave pulses) into a precisely defined increment in the shaft position.
Each pulse moves the shaft through a fixed angle.
In order to do this, you need to send pulse trains of varying polarity to multiple
windings, the speed of the motor being determined by the frequency of the pulses, and
the direction of motor motion being determined by the phasing between the pulses
being applied to the various windings.
24
Never Win Akshay D. Shelke
Arduino Program
Stepping Sequence:
Circuit:
25
Never Win Akshay D. Shelke
Arduino Program
ULN connections:
Pin 1 (INPUT1) : To digital Pin 11 of Arduino
Pin 2(INPUT2) : To digital Pin 10 of Arduino
Pin 3 (INPUT3) : To digital Pin 9 of Arduino
Pin 4 (INPUT4) : To digital Pin 8 of Arduino
Pin 9 : Ground
Pin 10 : +12V
Pin 15 to Pin 18 :To Stepper Motor
26
Never Win Akshay D. Shelke
Arduino Program
Program:
#include <Stepper.h>
void clk()
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(50);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(50);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(50);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
delay(50);
}
void cclk()
digitalWrite(11,HIGH);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
delay(50);
digitalWrite(11,LOW);
27
Never Win Akshay D. Shelke
Arduino Program
digitalWrite(10,HIGH);
digitalWrite(9,LOW);
digitalWrite(8,LOW);
delay(50);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,HIGH);
digitalWrite(8,LOW);
delay(50);
digitalWrite(11,LOW);
digitalWrite(10,LOW);
digitalWrite(9,LOW);
digitalWrite(8,HIGH);
delay(50);
}
void setup()
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
//Serial.begin(9600);
void loop()
clk();
delay(1000);
28
Never Win Akshay D. Shelke
Arduino Program
cclk();
delay(1000);
29
Never Win Akshay D. Shelke
Arduino Program
Experiment 10
Serial Communication:
It is used for communication between the Arduino board and a computer or
other devices. All Arduino boards have at least one serial port (also known as a UART
or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the
computer via USB.
Circuit:
In this project PC based Device control takes place. With Arduino platform as
mediator we are going to perform this task. Serial communication is used inside this
program. When we enter one letter in COM port window which is normally used for
serial communication purpose it will turn ON the LED. To turn off LED again we will
enter different letter.
30
Never Win Akshay D. Shelke
Arduino Program
Program:
int led1 = 13;
int rx;
void setup()
{
Serial.begin(9600);
pinMode(led1,OUTPUT);
}
void loop()
{
if(Serial.available() > 0)
{
rx = Serial.read();
if(rx == 'a' || rx == 'A')
{
digitalWrite(led1,HIGH);
}
if(rx == 'w' || rx == 'W')
{
digitalWrite(led1,LOW);
}
}
}
31
Never Win Akshay D. Shelke
Arduino Program
Experiment 11
IR Sensor:
IR Sensors work by using a specific light sensor to detect a select light
wavelength in the Infra-Red (IR) spectrum. By using an LED which produces light at
the same wavelength as what the sensor is looking for, you can look at the intensity of
the received light.
When an object is close to the sensor, the light from the LED bounces off the
object and into the light sensor. This results in a large jump in the intensity, which we
already know can be detected using a threshold.
Circuit:
IR Connections:
VCC of Sensor : +5V
Gnd of Sensor: Ground
Output :To Digital pin 5 of Arduino
32
Never Win Akshay D. Shelke
Arduino Program
Program:
int count=0;
int IR;
void setup()
{
// put your setup code here, to run once:
pinMode(8,INPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
IR=digitalRead(8);
if (IR==LOW)
{
count++;
Serial.println(count);
delay(100);
if(count >0)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
}
}
33
Never Win Akshay D. Shelke
Arduino Program
Experiment 12
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 often referred to as PIR,
"Passive Infrared", "Pyroelectric", or "IR motion" sensors.
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.
The sensor in a motion detector is actually split in two halves. The reason for
that is that we are looking to detect motion (change) not average IR levels. The two
halves are wired up so that they cancel each other out. If one half sees more or less IR
radiation than the other, the output will swing high or low.
Circuit:
In this experiment we are going to test PIR Sensor as well as we are going to
turn ON LED whenever motion is detected.
PIR Connections:
34
Never Win Akshay D. Shelke
Arduino Program
Program:
int sen;
void setup()
{
pinMode(8,INPUT);
pinMode(13,OUTPUT);
}
void loop()
{
if(sen == HIGH)
{
digitalWrite(13,HIGH); // Turn ON the LED
}
else
{
digitalWrite(13,LOW); // Keep LED OFF
}
}
35
Never Win Akshay D. Shelke
Arduino Program
Experiment 13
TSOP 1738:
The TSOP 1738 is a member of IR remote control receiver series. This IR sensor
module consists of a PIN diode and a pre amplifier which are embedded into a single
package. The output of TSOP is active low and it gives +5V in off state. When IR waves,
from a source, with a centre frequency of 38 kHz incident on it, its output goes low.
Pin Diagram:
In this experiment we are going to find out different codes for each button of TV
remote with the help of TSOP1738. Our TV remote consists of IR LED which acts as
Transmitter. With the help of TSOP and Arduino we will find codes for remote keys.
36
Never Win Akshay D. Shelke
Arduino Program
Circuit:
TSOP Connections:
Pin 1: Ground
Pin 2 : VS - +5V
Pin 3:OUTPUT - To pin 11 of Arduino
37
Never Win Akshay D. Shelke
Arduino Program
Program:
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}
38
Never Win Akshay D. Shelke
Arduino Program
Experiment 14
LM 35:
The LM35 is an integrated circuit sensor that can be used to measure
temperature with an electrical output proportional to the temperature (in oC).The scale
factor is 10mV/oC.
Pin Description:
Use proper conversion formula for LM35 and display all values in Serial
window using Serial Communication.
39
Never Win Akshay D. Shelke
Arduino Program
Circuit:
LM 35 Connections:
Pin 1: VS - +5V
Pin 2 : OUT –Analog pin A1 of Arduino
Pin 3: Ground
40
Never Win Akshay D. Shelke
Arduino Program
Program:
int temp;
int rx;
void setup()
{
pinMode(A1,INPUT);
}
void loop()
{
rx = analogRead(A1);
temp = (rx * 4.88) / 10; // conversion to temp
Serial.println(temp); // Display Values
delay(300);
}
41
Never Win Akshay D. Shelke
Arduino Program
Experiment 15
Ultrasonic Sensor:
The Ultrasonic module has two Transducers ,one for Transmit & the other for
Receive. Ultrasound is a high frequency sound of frequency 40 KHz (we can’t hear this
frequency, but animals can).
Principle of operation:
Generate a short 10uS pulse to the Trigger input to start the ranging. The
Ultrasonic Module will send out an 8 cycle burst of ultrasound at 40khz and raise its
echo line high. It then listens for an echo, and as soon as it detects one it lowers the echo
line again. The echo line is therefore a pulse whose width is proportional to the distance
to the object. By timing the pulse it is possible to calculate the range in
inches/centimeters .
If nothing is detected then the module will lower its echo line anyway after
about 30mS. The module provides an echo pulse proportional to distance. If the width
of the pulse is measured in uS, then dividing by 58 will give you the distance in cm, or
dividing by 148 will give the distance in inches.
The module can be triggered as fast as every 50ms, or 20 times each second. You
should wait 50ms before the next trigger, even if the SRF05 detects a close object and the
echo pulse is shorter. This is to ensure the ultrasonic "beep" has faded away and will not
cause a false echo on the next ranging.
The sensor can detect objects within 3cm to 3m range. In this experiment we are
going to measure as well as display the distance between obstacle and Sensor using
Arduino and Serial Communication.
42
Never Win Akshay D. Shelke
Arduino Program
Circuit:
HC SR-05 Connections:
VCC : Ground
Pin 2: VS - +5V
Pin 7: Trigger Pin
Pin 8: Echo Pin
43
Never Win Akshay D. Shelke
Arduino Program
Program:
int trigPin = 7;
int echoPin = 8;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
long duration, inches, cm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
44
Never Win Akshay D. Shelke
Arduino Program
Experiment 16
MQ Series Gas Sensor:
The MQ series of gas sensors use a small heater inside with an electro-chemical
sensor. They are sensitive for a range of gasses and are used indoors at room
temperature. They can be calibrated more or less (see the section about "Load-resistor"
and "Burn-in") but a know concentration of the measured gas or gasses is needed for
that.
MQ 6:
This is a simple-to-use liquefied petroleum gas (LPG) sensor, suitable for sensing
LPG (composed of mostly propane and butane) concentrations in the air. The MQ-6 can
detect gas concentrations anywhere from 200 to 10000ppm.
This sensor has a high sensitivity and fast response time. The sensor’s output is
an analog resistance. The drive circuit is very simple; all you need to do is power the
heater coil with 5V, add a load resistance, and connect the output to an ADC. In this
Experiment we are going to find Parts Per Million value for MQ6 Sensor.
Circuit:
MQ-6 Connections:
Pin 1: Ground
Pin 2: VS - +5V
Pin 3 : OUT –Analog pin A1 of Arduino
45
Never Win Akshay D. Shelke
Arduino Program
Program:
int gas;
int rx;
void setup()
{
pinMode(A1,INPUT);
}
void loop()
{
rx = analogRead(A1);
int g= (rx * 4.88); // conversion of value to temperature
gas= g*2; // Convert this into Parts Per Million
Serial.println(gas); // Display Values in Serial window
delay(300);
}
46
Never Win Akshay D. Shelke
Arduino Program
Experiment 17
Bluetooth Module:
Bluetooth is a wireless technology standard for exchanging data over short
distances (using short-wavelength UHF radio waves in the ISM band from 2.4 to
2.485 GHz) from fixed and mobile devices.
In this we are using HC-05 is a class-2 bluetooth module with Serial Port Profile,
which can configure as either Master or slave. You can use it simply for a serial port
replacement to establish connection between MCU, PC to your embedded project and
etc. These modems work as a serial (RX/TX) pipe. Any serial stream from 9600 to
115200bps can be passed.
Circuit:
Bluetooth Connections:
Ground : To Ground pin of Arduino
VCC :+5V
TX :To Pin 0 of Arduino
RX :To pin Pin 1of Arduino
Connect anode of four LEDs to Digital pins of Arduino(4 to 7) through Resistor
Connect cathode of all LEDs to Ground
47
Never Win Akshay D. Shelke
Arduino Program
Program:
void setup()
{
Serial.begin(9600); //Initialise Serial Communication
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop()
{
// here we'll check if some data is received or not
if(Serial.available()>0)
{
int rxd=Serial.read();
if(rxd=='U')
{
digitalWrite(4,HIGH);
}
if(rxd=='D')
{
digitalWrite(5,HIGH);
}
if(rxd=='L')
{
digitalWrite(6,HIGH);
}
if(rxd=='R')
{
digitalWrite(7,HIGH);
}
if(rxd=='C')
{
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
}
}
48
Never Win Akshay D. Shelke
Arduino Program
Experiment 18
GSM:
GSM module is used to establish communication between a computer and a
GSM- system. Global System for Mobile communication (GSM) is an architecture used
for mobile communication in most of the countries. GSM/GPRS module consists of a
GSM modem assembled together with power supply circuit and communication
interfaces (like RS-232, USB, etc) for computer. The MODEM is the soul of such
modules.
Wireless MODEMs are the MODEM devices that generate, transmit or decode
data from a cellular network, for establishing communication between the cellular
network and the computer. These are manufactured for specific cellular network
GSM/UMTS/CDMA) or specific cellular data standard (GSM/UMTS/GPRS/EDGE/
HSDPA) or technology (GPS/SIM).
Wireless MODEMs like other MODEM devices use serial communication to
interface with and need Hayes compatible AT commands for communication with the
computer (any microprocessor or microcontroller system).
GSM MODEM is a class of wireless MODEM devices that are designed for
communication of a computer with the GSM and GPRS network. It requires a SIM
(Subscriber Identity Module) card just like mobile phones to activate communication
with the network.
Also they have IMEI (International Mobile Equipment Identity) number similar
to mobile phones for their identification. A GSM/GPRS MODEM can perform the
following operations:
49
Never Win Akshay D. Shelke
Arduino Program
Circuit:
50
Never Win Akshay D. Shelke
Arduino Program
Program:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,8);
char mobno[15] = "91xxxxxxxxxxx";
char msg[50] = "This is Trial message from mobile";
void setup()
{
mySerial.begin(9600); // Setting the baud rate
Serial.begin(9600); // Setting the baud rate
delay(100);
gsminit();
}
void loop()
{
}
void gsminit()
{
mySerial.println("AT");
delay(500);
mySerial.println("ATE0");
delay(500);
mySerial.println("AT+CMGF=1");
}
void sendsms()
{
mySerial.println("AT+CMGF=1");
delay(1000);
mySerial.println("AT+CMGS=\"+91xxxxxxxxxx\"\r");
mySerial.println("AT+CMGS=");
mySerial.print("\"");
mySerial.print(mobno);
mySerial.print("\"\r");
delay(1000);
mySerial.println(msg);
delay(100);
mySerial.println((char)26);
delay(1000);
}
void makeCall()
{
mySerial.print("ATD");
mySerial.print(mobno);
mySerial.println(";");
delay(1000);
}
void hangup()
{
mySerial.println("ATH");
}
51
Never Win Akshay D. Shelke
Arduino Program
Experiment 19
RFID:
RFID is Radio Frequency Identification. An RFID reader is used to read RFID
tags (which contain certain unique data stored in a chip). An RFID reader and an RFID
tag, both have a coil surrounding them. When an RFID tag is shown near an RFID
Reader, it collects the unique tag data (a combination of digits and characters) from the
RFID tag.
This is made possible via Electromagnetic Induction. RFID reader and RFID tag
comes with a coil in them. We power the RFID reader from power supply for reading
purpose. Now when an RFID tag is shown near the reader, electromagnetic induction
will take place between the coils and this powers the chip inside tag. This chip will send
data electromagnetically to the reader.
The reader will receive this electromagnetically transferred data and outputs it
serially. Every RFID reader comes with Serial output pins. We can collect the read data
through these serial pins using arduino or any other micro controller.
Circuit:
Connections:
Txd or DataOut of RFID reader to Pin 8 of Arduino
52
Never Win Akshay D. Shelke
Arduino Program
Program:
#include<SoftwareSerial.h>
SoftwareSerial myserial(8,9);
int i = 0;
char rfid[15];
void setup()
{
Serial.begin(9600);
myserial.begin(9600);
}
void loop()
{
53
Never Win Akshay D. Shelke
Arduino Program
Experiment 20
Real Time Clock:
A real-time clock (RTC) is a computer clock (most often in the form of an
integrated circuit) that keeps track of the current time. Although the term often refers to
the devices in personal computers, servers and embedded systems, RTCs are present in
almost any electronic device which needs to keep accurate time.
Although keeping time can be done without an RTC,using one has benefits:
In this experiment we are displaying date, day and time in serial port of
Arduino.
Circuit:
54
Never Win Akshay D. Shelke
Arduino Program
Program:
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup () {
Serial.begin(57600);
Wire.begin();
rtc.begin();
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now.unixtime() + 7 * 86400L + 30);
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
}
55
Never Win Akshay D. Shelke
"Failure will never
overtake me if my
Determination to
succeed is strong
enough."
-Akshay Shelke
Never Win
Rs/-100