Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

HAND NOTES – BASICS OF ARDUINO

CMR COLLEGE OF ENGINEERING & TECHNOLOGY


An Autonomous Institution with NAAC Accredited (A-Grade)
*Accredited by NBA *Approved by AICTE *permanently affiliated by JNTUH
Kandlakoya, Medchal Road, Hyderabad-501401

CMR College of Engineering & Technology Page 1


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

INTRODUCTION TO ARDUINO
2.1. WHAT IS ARDUINO?
Arduino is an open-source electronics platform based on easy-to-use hardware and
software. Arduino boards are able to read inputs and turn it into an output. Basically Arduino
is a microcontroller board. You can tell your board what to do by sending a set of
instructions. To do so, the following are required:
● Arduino programming language,

● The Arduino Software (IDE)


A microcontroller is a low-cost electronic system for use in products like toaster, remote
control, washing machine, etc. It is especially useful for controlling external physical devices
(e.g., TV, pump, etc.). The average cost of a microcontroller is around Rs 50. It is to be
noted that 95% of all computer software developed in the world is by using microcontroller-
based systems, called embedded systems. The Arduino board is based on a microcontroller
integrated circuit (IC) chip (Atmega 328), with USB port, LEDs, and readymade connections
called interfaces.
2.2. ARDUINO UNO BOARD ARCHITECTURE:
The following figure shows the architecture of the Arduino Uno Board.

Figure 1.. Arduino Uno Board


The specifications of Arduino Uno are listed in Table1.

CMR College of Engineering & Technology Page 2


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

There are many other types of Arduino boards available, e.g., Mega for more interfaces,
Mini or Nano for small size, LilyPad for wearable use, etc. All Arduino boards have one
thing in common: they are programmed through the Arduino IDE. This is the software that
allows you to write and upload code. Some boards are designed to be embedded and have no
programming interface (hardware) which you would need to buy separately. Some can run
directly from a 3.7V battery, others need at least 5V. The following figure shows various
types of Arduino Boards (fig.2).

CMR College of Engineering & Technology Page 3


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure.2 Various types of Arduino Boards

2.3. WHY ARDUINO?

Arduino is open source, which means anyone can copy, modify, produce new firmware and
use their own Arduino board (often, it is cheaper and easier to buy). Lot of the electronic
hardware and software is required to build Arduino-based applications and systems is
available online, so you can focus on building your own projects and products in a short time
(called Rapid Prototyping). So, even school children in high, middle, or even elementary
schools can start working on Arduino projects.

Arduino projects introduce students to basic electronics, computer hardware, sensors,


motors, robots, and programming. Because Arduino projects interact with real world, this
type of programming is called physical computing or interactive computing, and so is
more fun to learn.
The following are the few points that tend us to choose Arduino:
a) Inexpensive: Arduino boards are relatively inexpensive compared to
other microcontroller platforms. The least expensive version of the
Arduino module can be assembled by hand, and even the pre-assembled
Arduino modules cost less than $50
b) Cross-platform: The Arduino Software (IDE) runs on Windows,
Macintosh OSX, and Linux operating systems. Most microcontroller
systems are limited to Windows.
c) Simple, clear programming environment: The Arduino Software (IDE)
is easy-to-use for beginners, yet flexible enough for advanced users to
take advantage of as well. For teachers, it's conveniently based on the
Processing programming environment, so students learning to program in
that environment will be familiar with how the Arduino IDE works.
d) Open source and extensible software: The Arduino software is
published as open source tools, available for extension by experienced
programmers. The language can be expanded through C++ libraries.
e) Open source and extensible hardware: The plans of the Arduino boards
are published under a Creative Commons license, so experienced circuit
designers can make their own version of the module, extending it and
improving it. Even relatively inexperienced users can build
the breadboard version of the module in order to understand how it works
and save money.

2.4 DOWNLOAD ARDUINO SOFTWARE – INTEGRATED DEVELOPMENT


ENVIRONMENT
a. Access the Internet: In order to use your Arduino, you will first need to download
Arduino software from the Internet (it is free!). This software, known as the Arduino
integrated development environment (IDE), allows us to program the Arduino to do what we
want. It is called integrated, because we can write the code, compile and execute it from the
same program window. You can download the latest or a previous version of the software
from Arduino.cc, the repository for Arduino development. We can also code online using
Arduino Web Editor, with the code stored on the cloud: http://arduino.cc/en/Main/Software

You can download the Installer (.exe; recommended) or Zip package. The Zip package can
be used for portable installation (https://www.arduino.cc/en/Guide/PortableIDE). Choose the
appropriate operating system installation package for your computer (including 32 or 64-bit
OS). Store the software in a hardware directory of your choice.
CMR College of Engineering & Technology Page 4
CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure 3. Arduino Setup: Installation Options and Installation Folder

The install step extracts and installs all required files for operating the Arduino board.

Figure 4. Arduino.cc software download page

b. Connect your Arduino: to one of your computer’s USB ports using the USB Type A to Type
B cable provided.

CMR College of Engineering & Technology Page 5


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure 5. Connecting the Arduino to the PC USB port

2.5 INSTALL THE DRIVERS FOR ARDUINO


A device driver is a software/program that allows the computer to operate the hardware (in this
case Arduino). Follow these steps for installing drivers for the Arduino with Windows7, and
higher versions (https://www.arduino.cc/en/Guide/Windows):

a. Plug in your board and wait for Windows to begin its driver installation process.

b. Click on the Start Menu, and open the Control Panel.

c. While in the Control Panel, navigate to System. Next, open the Device Manager.

d. Look under Ports (COM & LPT). You should see an open port named "Arduino (COMxx)"
where xx stands for two digits representing the port number.

e. Right click on the "Arduino (COMxx)" port and choose the "Update Driver Software" option.

f. Next, choose the "Browse my computer for Driver software" option.

g. Finally, navigate to and select the driver file named arduino.inf, located in the Drivers folder
of the Arduino Software download (not the "FTDI USB Drivers" sub-directory).

h. Windows will finish up the driver installation from there.

2.6 OPEN THE ARDUINO IDE:

Open the Arduino IDE software on your computer by double clicking the Arduino icon in
the directory. Poke around and familiarize yourself with the interface.

CMR College of Engineering & Technology Page 6


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure6. Description of Arduino IDE


Explanation:

1. Verify: Compile (convert the program in high level programming language to


assembly language, object code, or machine code) and approve the code. It will
detectand list any errors in syntax (like missing semi-colons or parenthesis) in the
Message Area.

2. Upload: Send the code from the computer to the Arduino board. When this command is
selected, Arduino will perform the step-step-by instructions in the code.

3. New: This button opens up a new code window tab.

4. Open: This button will let you open an existing sketchor program.

5. Save: This saves the currently active sketch.

6. Serial Monitor: This will open a window that displays any serial information your
Arduino is transmitting. It is very useful for debugging (find and remove errors from)
your code.

7. Sketch Name: This shows the name of the sketch you are currently working on.

CMR College of Engineering & Technology Page 7


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

8. Code Area: This is the area where you compose the code for your sketch.

9. Message Area: This is where the IDE displays messages on the results of operation,
including errors.

Now, select Your Board and Specify the COM Port

Go to Tools under the menu bar, navigate to the Board dropdown menu (indicated by right
arrow ►) and select the board used (e.g., Arduino Uno). The selected board appears on the
Task Bar right side.

Figure 7. Selecting the type of Board (here, Arduino Uno)

Next, select the Serial Port (as noted from Windows Device Manager) of the Arduino board
from the Tools | Port| Serial Ports pull down menu (here, COM3).

Figure 8. Selecting the COM Port


CMR College of Engineering & Technology Page 8
CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Experiment 1: Your First Circuit – Blinking an LED


Objective: To make an LED blink using Arduino.

Components Required:

1. Arduino board

2. Breadboard and jumper wires

3. One LED

4. Resistor (470 Ω)

Circuit diagram:

Figure 9. Circuit diagram illustrating the connections with Arduino

Figure10. Connections using Arduino


Procedure:

1. Make the connections as in circuit diagram Figure 9. That is, connect Pin 9 from Arduino
board to one end of the resistor on the breadboard. The other end of the resistor is connected to
the LED positive lead. The LED negative lead is connected to the Ground Pin of Arduino
through the breadboard. In this circuit, we say that the LED and resistor are connected in series.

2. Connect the Arduino to the PC through the USB port.

3. In the Arduino IDE, select Uno as the board and select the appropriate COM port.

CMR College of Engineering & Technology Page 9


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

4. In Arduino IDE, under … select Examples sketch the proper LED blinking program is
executed by verifying the program by clicking the verify option in menu bar.

5. If the program compiles with success, then upload the coding to Arduino by clicking the
upload option in menu bar.

6. After the program is uploaded to the Arduino board, the LED blinks.

Code:

/*LED Blink Experiment */

int led =9; // LED anode is connected to Arduino Pin 9

// Note that the spaces around = are immaterial, but add to readability
// The setup routine runs once when you press Reset button

Void setup()
{
// Initialize the digital pin as an output
pinMode(led,OUTPUT);
}
// The loop routine runs over and over again forever:
void loop(){
digitalWrite(led,HIGH); // turn the LED on by making voltage HIGH
delay(1000); // wait for a second
digitalWrite(led,LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Results and Discussions:

How does the hardware and software work together in this exercise?
An Arduino program has a setup function and a loop function. The setup function as
the name implies, specifies details like if a pin is used as digital input or output, or as analog
input, and also specifies parameters like communication speed (baud rate), etc. In addition, as in
C programs, we also have declaration statements, e.g., the statement

int led =9;

means that the Pin 9 of Arduino is to be used. digitalWrite and delay are functions
belonging to the Arduino library. We can write another declaration statement as int PAUSE =
1000; and then use PAUSE as parameter for the delay function calls. In this case, if you want
to decrease or increase the delay, it is sufficient to change PAUSE value just once.

The loop function contains the step-by-step instructions of the program for execution.
The statement digitalWrite(led,HIGH); sends a positive (+5 volts) signal to Pin 9 and
therefore to the LED anode and the LED is turned ON. The LED stays ON for the duration) of
the delay (1000, or one second).

Next, when the digitalWrite(led,LOW); is executed, Pin 9 and therefore the LED
anode is sent a LOW (zero volts) signal, and so the LED is turned OFF for a period of one
second. As the loop is executed indefinitely (till you unplug the board!), the LED blinks on and
off for one second each. The LED blinking frequency is 0.5 Hz (hertz, the unit for frequency), or
equivalently the period of LED blinking is two seconds.

CMR College of Engineering & Technology Page 10


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

The Serial Monitor can be used to print out the values of interest on the computer’s
output window, using the command Serial.println( ).

CMR College of Engineering & Technology Page 11


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Experiment 2: To LED glow when controller detects a button pressed using


Arduino.
Objective:

To make an LED glow when controller detects a button pressed using Arduino .

Components Required:

1. Arduino Uno board

2. Breadboard

3. Push button switch

4. Jumper Wires

5. LED

6. Two 10KΩ resistors

Circuit Diagram:

Figure 11. Circuit Diagram using Arduino

Figure 12. Circuit diagram of push button with Arduino


Procedure:

1. First, fix the button in the breadboard; connect the longer end of button with Pin 7 of Arduino
and shorter end of button with ground (first row or bottom row) of breadboard.
CMR College of Engineering & Technology Page 12
CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

2. Connect Arduino GND pin with ground of breadboard.


3. Provide the connections from Arduino +5V pin to ground of breadboard for power supply to
breadboard.
4. Connect Pin 9 of Arduino with anode of LED and connect the cathode of LED with ground of
breadboard.
5. Resistors are connected with LED and Push Button to equalize the fluctuation.
6. Connect the Arduino to PC via USB cable.
7. In PC/Laptop open the Arduino IDE, write the program of push button in new sketch file and
click the verify button to compile the program.
8. Once it will be verified, then click the upload button to execute

Code:

/* Program for Push Button with LED */

// set pin numbers:

int buttonPin = 7; // Pushbutton pin

int ledPin = 9; // LED pin

// variables will change:

void setup() { // initialize the LED pin as an output:

Serial.begin(9600);

pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

void loop(){ // read the state of the pushbutton value:

int buttonState = digitalRead(buttonPin);

Serial.println(buttonState);

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if(buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

delay(1000);
}

else {

// turn LED off:

digitalWrite(ledPin, LOW);
delay(1000);
CMR College of Engineering & Technology Page 13
CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Results & Discussions:


Mechanical contact switches suffer from condition called bouncing typically due to spring
action. To ensure that the reading is stable and the bouncing has stopped, we implement
debouncing in hardware or software. For an example of how the latter is implemented, see
https://www.arduino.cc/en/Tutorial/Debounce

CMR College of Engineering & Technology Page 14


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Experiment - 3 Temperature Sensor


Objective:

To read the temperature and display the same in serial monitor using Arduino

Components Required:
1. Arduino
2. Breadboard
3. LM35 temperature sensor
4. Jumper wires

Circuit diagram:

Figure 13. Circuit illustrating the connections of LM35


Pin Description of LM 35:

CMR College of Engineering & Technology Page 15


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure 14. Pin Description of LM35


Table.2 Data Sheet of LM35
Minimum input voltage -2 V

Maximum input voltage +35 V

Measuring temperature range -55°C to 150°C

Sensitivity 10 mV per degree celcius rise

Accuracy ±0.5°C

Drain Current >60 uA

Output Voltage Directly proportional (linear) to temperature

Procedure:

1. First, mount the LM35 temperature sensor in the breadboard.


2. Connect the GND pin of LM35 to the GND of Arduino.
3. Connect +Vs pin of LM35 to +5V pin of Arduino and then connect the Vout pin of LM35 to
analog pinA4 of Arduino.
4. Connect the Arduino to the PC, write the program for temperature sensor and compile the
program.
5. Once it will be verified, then click the upload button to execute.

Code:

/*Temperature Sensor */
float temp;
int tempPin = 4;
void setup()
{
Serial.begin(9600);
}
void loop()
{
temp = analogRead(tempPin);
temp = temp * 0.48828125; // Calibration for degree Celsius
Serial.print("TEMPRATURE = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();
delay(1000);
}
Results and Discussions:

In addition to temperatures, high humidity can increase the discomfort. Therefore,


temperature-humidity sensors are often used in conjunction, and are available in a single
package, e.g., DHT11 or DHT22 sensor. The temperature and humidity values can be sent over
the Internet for remote access and action, using web access, and using a WAMP/LAMP server
(Windows/Linux-Apache-MySQL-PHP). The data can also be studied using R language, e.g.,
data mining or analytics.

CMR College of Engineering & Technology Page 16


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Temperature control is a major part of climate control systems in homes and offices
(HVAC – heating, ventilation, and air conditioning).

CMR College of Engineering & Technology Page 17


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Experiment 4: Interfacing Arduino with LCD


Objective:

To interface the Liquid Crystal Display (LCD) with the Arduino Uno to enable displaying the
characters on the LCD.

Components used:
1. Arduino or Genuino Board
2. LCD Screen (compatible with Hitachi HD44780 driver)
3. pin headers to solder to the LCD display pins
4. 10k ohm potentiometer
5. 220 ohm resistor
6. hook-up wires
7. breadboard

LCD Schematic:

Figure 15. Circuit Diagram with connections

Technical Specifications:
Parameter Values

Supply Voltage 5.0V

Interface I2C

I2C Address 0x3F or 0x27

Connections Guide:

1. Write the program on the Arduino IDE (The program is given below for your reference)

2. Upload the same on the Arduino board.

3. Connect the supply voltage(Vcc) and ground(GND) pins of the LCD module to the 5V and
GND pins of Arduino microcontroller.
CMR College of Engineering & Technology Page 18
CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

4. Connect the SDA and SCL pins of the LCD module to Analog pins (A4&A5) of the Arduino
microcontroller.

5. Check the connections.

Notes:
The LCDs have a parallel interface, meaning that the microcontroller has to manipulate
several interface pins at once to control the display. The interface consists of the following pins:
- A register select (RS) pin that controls where in the LCD's memory you're
writing data to. You can select either the data register, which holds what goes
on the screen, or an instruction register, which is where the LCD's controller
looks for instructions on what to do next.
- A Read/Write (R/W) pin that selects reading mode or writing mode
- An Enable pin that enables writing to the registers 8 data pins (D0 -D7). The
states of these pins (high or low) are the bits that you're writing to a register
when you write, or the values you're reading when you read.
- There's also a display constrast pin (Vo), power supply pins (+5V and
Gnd) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power
the LCD, control the display contrast, and turn on and off the LED backlight,
respectively.
The process of controlling the display involves putting the data that form the image of what you
want to display into the data registers, then putting instructions in the instruction register.
The LiquidCrystal Library simplifies this for you so you don't need to know the low-level
instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode
requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying
text on the screen, you can do most everything in 4-bit mode, so example shows how to control a
2x16 LCD in 4-bit mode.

Important note:
Before wiring the LCD screen to your Arduino or Genuino board we suggest to solder a pin
header strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image
above.
To wire your LCD screen to your board, connect the following pins:
● LCD RS pin to digital pin 12

● LCD Enable pin to digital pin 11

● LCD D4 pin to digital pin 5

● LCD D5 pin to digital pin 4

● LCD D6 pin to digital pin 3

● LCD D7 pin to digital pin 2


Additionally, wire a 10k pot to +5V and GND, with it's wiper (output) to LCD screens VO pin
(pin3). A 220 ohm resistor is used to power the backlight of the display, usually on pin 15 and 16
of the LCD connector
Program:
/*
LiquidCrystal Library - Hello World

Demonstrates the use a 16x2 LCD display. The LiquidCrystal


library works with all LCD displays that are compatible with the
CMR College of Engineering & Technology Page 19
CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.

This sketch prints "Hello World!" to the LCD


and shows the time.

The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008


by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld

*/

// include the library code:

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin with the arduino pin number
it is connected to

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {

CMR College of Engineering & Technology Page 20


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

// set up the LCD's number of columns and rows:

lcd.begin(16, 2);

// Print a message to the LCD.

lcd.print("hello, world!");

void loop()
{

// set the cursor to column 0, line 1

// (note: line 1 is the second row, since counting begins with 0):

lcd.setCursor(0, 1);

// print the number of seconds since reset:

lcd.print(millis() / 1000);

Conclusion:

In this experiment, the LCD is successfully interfaced with the Arduino board.

Reference: https://www.arduino.cc/en/Tutorial/HelloWorld

CMR College of Engineering & Technology Page 21


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Experiment 5: Interfacing Arduino Uno with Infrared Sensor


Objective:

To interface the FC-51 Infrared sensor (IR) sensor with the Arduino Uno to sense the path is
clear/ indicate the presence of any obstacles.

Components Required:

1. Arduino Uno board

2. FC-51 Infrared sensor

3. USB interface cable

4. Desktop or laptop PC

5. Jumper wires

Infrared sensor:

Figure 16. FC-51 Infrared Sensor

CMR College of Engineering & Technology Page 22


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure 17. Different parts of the IR sensor


Technical Specification
Parameter Values

Supply Voltage 3.0V to 6.0V DC

Detection angle 350

Detection range 2cm – 30cm (Adjustable using potentiometer)

Current
23 mA- 43mA
Consumption

Methodology:

This module has built-in IR transmitter and receiver that sends out IR signals to detect the
presence of any obstacles. The module also has an on board potentiometer to vary the
detection range. It can sense hurdles even in complete darkness. 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.

Figure 18. Working of an IR sensor


Procedure:

CMR College of Engineering & Technology Page 23


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

1. Connect the Vcc and GND of the sensor to the 5V and GND pins of the Arduino Uno
respectively.

2. Connect the OUT pin of the sensor (the input pin) to the digital pin 7 of the Arduino Uno.

3. Upload the program given to the board.

4. Open the serial monitor of the Arduino IDE.

5. When there is any obstacle, it indicates “Stop something is ahead!!”

6. When there isn’t any hurdle, it will indicate “Path is clear”

Figure 19. Connections of the Infrared sensor to Arduino


Program

/* Program to detect the obstacle and indicate on the serial monitor*/

int LED = 13; // Use the onboard Uno LED

int obstaclePin = 7; // This is our input pin

int hasObstacle = HIGH; // HIGH MEANS NO OBSTACLE

void setup()
{

pinMode(LED, OUTPUT);

pinMode(obstaclePin, INPUT);

Serial.begin(9600);

}
void loop()
{
hasObstacle = digitalRead(obstaclePin); //Reads the output of the obstacle
sensor from the 7th PIN of the Digital section of the arduino

CMR College of Engineering & Technology Page 24


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

if (hasObstacle == LOW) //LOW means something is ahead, so illuminates the


13th Port connected LED
{
Serial.println("Stop something is ahead!!");

digitalWrite(LED, HIGH);//Illuminates the 13th Port LED


}

else
{
Serial.println("Path is clear");

digitalWrite(LED, LOW);
}

delay(200);

Output (Screenshot)

Figure 20. Result Screen

CMR College of Engineering & Technology Page 25


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Experiment 6: Interfacing Arduino Uno with Ultrasonic Sensor


(Analog Sensor)
Objective:
To interface the ultrasonic sensor with the Arduino Uno to determine the distance of an object
from the sensor.

Components used:

1. Arduino UNO Board

2. Ultrasonic sensor (HC-SR04)

3. USB/UART interface Cable

4. Desktop or Laptop PC

5. Interfacing program

6. Jumper wires

Ultrasonic Sensor:

Figure 21. HC-SR04 Ultrasonic Sensor

Methodology:

Ultrasonic sensors compute the distance to the target object by emitting short, high-frequency
sound pulses at regular intervals that propagate in air at the velocity of sound. As these pulses
hit the object, they are reflected back as echo signals to the sensor which then computes the
distance based on time-span between the emitting the signal and receiving the echo.
Technical Specifications:
Parameter Values
Supply voltage 5V DC
CMR College of Engineering & Technology Page 26
CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Working current 15mA

Working frequency 40Hz

Minimum Range 2cm

Maximum Range 4m

Trigger Pulse 10uS TTL pulse

Connections guide:

1. Write the program on the Arduino IDE (The program is given below for your reference)

2. Upload the same on the Arduino board.

3. Connect the pins Vcc and Gnd of the sensor to 5v and Gnd of the Arduino.

4. Connect Trigger pin to Digital pin 12.

5. Connect Echo pin to Digital pin 13.

6. Check the connections.

CMR College of Engineering & Technology Page 27


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure 22. Circuit connection for HC-SR04 Ultrasonic Sensor

Program:

const int trigPin = 12;

const int echoPin = 13;

void setup() {

// initialize serial communication: Serial.begin(9600);

}
void loop()

// establish variables for duration of the ping,

// and the distance result in inches and centimeters: long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.

// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

pinMode(trigPin, OUTPUT);

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

CMR College of Engineering & Technology Page 28


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose

// duration is the time (in microseconds) from the sending

// of the ping to the reception of its echo off of an object.

pinMode(echoPin, INPUT);

duration = pulseIn(echoPin, HIGH);

// convert the time into a distance

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)

// The speed of sound is 340 m/s or 29 microseconds per centimeter.

// The ping travels out and back, so to find the distance of the

// object we take half of the distance travelled.

return microseconds / 29 / 2;
}
Output:

CMR College of Engineering & Technology Page 29


CENTRE FOR ENGINEERING EDUCATION RESEARCH– BASICS OF ARDUINO

Figure 23. Result Screen

Conclusion:
In this experiment, the ultrasonic analog sensor is interfaced with the Arduino board and the
distances of various objects were determined using the Arduino microcontroller.

CMR College of Engineering & Technology Page 30

You might also like