Mechatronics Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

ASSINGNMENT NO : 01

ME 2114
CONTROL SYSTEM AND INTRODUCTION
TO MECHATRONICS

NAME : D.D.D. JAYATHISSA


REG. NO. : GAL/ME/2020/F/0064
FIELD : MECHANICAL
DATE OF SUB : 12/12/2022
Chapter 1
Introduction
1. Introduction about Mechatronics system
2. Sensors & Actuators

Chapter 2
Arduino Programming
1. How to install and start to programming
2. How to import the libraries and how to use specific libraries
3. How to write simple program (LED blinking) in Arduino platform and how to run that
code
4. How to use serial monitor and serial plotter in Arduino platform (with example).

Chapter 3
Applications
1. Ultrasonic sensor base water level indicator
2. Motor controlling (Speed and Direction) circuit to drive DC motor by using L293D Motor Drive
IC.
Chapter 1
Introduction
1. Introduction about Mechatronics system
Mechatronics basically refers to mechanical electronic systems and normally described as a
synergistic combination of mechanics, electrical, electronics, computer and control which, when
combined, make possible the generation of simple, more economic, and reliable systems.

• The word, mechatronics, is composed of “mecha” from mechanism and the “tronics”
from electronics.
• The synergistic integration of mechanical engineering, with electronics and intelligent
computer control in the design and manufacturing of industrial products and
processes.
• Mechatronics is the application of complex decision making to the operation of
physical systems.
• Mechatronics is a methodology used for the optimal design of electromechanical
products.
• A mechatronic system is not just a marriage of electrical and mechanical systems and is
more than just a control system; it is a complete integration of all of them.

Mechatronics Engineering is the


o Analysis
o Design
o Manufacturing
o Integration
o maintenance of mechanics with electronics through intelligent computer
control.

History of Mechatronics system


• Mechanical Engineering experienced an exponential growth in the early 19th century
because of the industrial revolution.
• The rise of semiconductors in the 1950s and computers in the 1980s have
revolutionized all engineering products and processes which in turn affected
mechanical engineering systems.
• The term mechatronics was first used in the late 1960s by a Japanese Electric
Company to describe the engineering integration between mechanical “mecha” and
electronics “tronics” systems.
• Since then, it has spread throughout Europe and is growing is the USA.
• Today, many mechanical systems use some form of electronics and computers to
control its functionality.
• Mechatronics system engineering has gained much recognition and importance in the
industrial world.
• In the late 1970s, the Japan Society for the Promotion of Machine Industry (JSPMI)
classified mechatronics products into four categories
Class I:
Primarily mechanical products with electronics incorporated to enhance functionality.

Examples: include numerically controlled machine tools and variable speed drives in
manufacturing machines.

Class II:
Traditional mechanical systems with significantly updated internal devices incorporating
electronics.

Examples include the modern sewing machine and automated manufacturing systems.

Class III:
Systems that retain the functionality of the traditional mechanical system, but the internal
mechanisms are replaced by electronics.

An example is the digital watch.

Class IV:
Products designed with mechanical and electronic technologies through synergistic
integration.

Examples include photocopiers, intelligent washers and dryers, rice cookers, and automatic
ovens.
Physically, a mechatronic system is composed of four prime components. They are sensors,
actuators, controllers and mechanical components.

Application of Mechatronics systems


2.Sensors & Actuators

Sensors
There are numerous definitions as to what a sensor is but I would like to define a Sensor as an
input device which provides an output (signal) with respect to a specific physical quantity
(input).The term “input device” in the definition of a Sensor means that it is part of a bigger
system which provides input to a main control system (like a Processor or a
Microcontroller).Another unique definition of a Sensor is as follows: It is a device that converts
signals from one energy domain to electrical domain. The definition of the Sensor can be better
understood if we take an example in to consideration.

The simplest example of a sensor is an LDR or a Light Dependent Resistor. It is a device, whose
resistance varies according to intensity of light it is subjected to. When the light falling on an LDR is
more, its resistance becomes very less and when the light is less, well, the resistance of the LDR
becomes very high. We can connect this LDR in a voltage divider (along with other resistor) and
check the voltage drop across the LDR. This voltage can be calibrated to the amount of light falling
on the LDR. Hence, a Light Sensor.

Now that we have seen what a sensor is, we will proceed further with the classification of Sensors.
Classification of Sensors

There are several classifications of sensors made by different authors and experts. Some are very
simple and some are very complex. The following classification of sensors may already be used by
an expert in the subject but this is a very simple classification of sensors.

In the first classification of the sensors, they are divided in to Active and Passive. Active Sensors
are those which require an external excitation signal or a power signal.

Passive Sensors, on the other hand, do not require any external power signal and directly
generates output response.

The other type of classification is based on the means of detection used in the sensor. Some of the
means of detection are Electric, Biological, Chemical, Radioactive etc.

The next classification is based on conversion phenomenon i.e., the input and the output. Some of
the common conversion phenomena are Photoelectric, Thermoelectric, Electrochemical,
Electromagnetic, Thermooptic, etc.

The final classification of the sensors are Analog and Digital Sensors. Analog Sensors produce an
analog output i.e., a continuous output signal (usually voltage but sometimes other quantities like
Resistance etc.) with respect to the quantity being measured.

Digital Sensors, in contrast to Analog Sensors, work with discrete or digital data. The data in digital
sensors, which is used for conversion and transmission, is digital in nature.

Different Types of Sensors

The following is a list of different types of sensors that are commonly used in various applications.
All these sensors are used for measuring one of the physical properties like Temperature,
Resistance, Capacitance, Conduction, Heat Transfer etc.

1. Temperature Sensor
2. Proximity Sensor
3. Accelerometer
4. IR Sensor (Infrared Sensor)
5. Pressure Sensor
6. Light Sensor
7. Ultrasonic Sensor
8. Smoke, Gas and Alcohol Sensor
9. Touch Sensor
10. Color Sensor
11. Humidity Sensor
12. Position Sensor
13. Magnetic Sensor (Hall Effect Sensor)
14. Microphone (Sound Sensor)
15. Tilt Sensor
16. Flow and Level Sensor
17. PIR Sensor
18. Touch Sensor
19. Strain and Weight Sensor

We will see about few of the above-mentioned sensors in brief. More information about the
sensors will be added subsequently. A list of projects using the above sensors is given at the end of
the page.

Temperature Sensor

One of the most common and most popular sensors is the Temperature Sensor. A Temperature
Sensor, as the name suggests, senses the temperature i.e., it measures the changes in the
temperature.

There are different types of Temperature Sensors like Temperature Sensor ICs (like LM35,
DS18B20), Thermistors, Thermocouples, RTD (Resistive Temperature Devices), etc.

Temperature Sensors can be analog or digital. In an Analog Temperature Sensor, the changes in
the Temperature correspond to change in its physical property like resistance or voltage. LM35 is a
classic Analog Temperature Sensor.

Coming to the Digital Temperature Sensor, the output is a discrete digital value (usually, some
numerical data after converting analog value to digital value). DS18B20 is a simple Digital
Temperature Sensor.

Temperature Sensors are used everywhere like computers, mobile phones, automobiles, air
conditioning systems, industries etc.
Proximity Sensors

A Proximity Sensor is a non-contact type sensor that detects the presence of an object. Proximity
Sensors can be implemented using different techniques like Optical (like Infrared or Laser), Sound
(Ultrasonic), Magnetic (Hall Effect), Capacitive, etc.

Some of the applications of Proximity Sensors are Mobile Phones, Cars (Parking Sensors),
industries (object alignment), Ground Proximity in Aircrafts, etc.

Infrared Sensor (IR Sensor)

IR Sensors or Infrared Sensor are light based sensor that are used in various applications like
Proximity and Object Detection. IR Sensors are used as proximity sensors in almost all mobile
phones.

There are two types of Infrared or IR Sensors: Transmissive Type and Reflective Type. In
Transmissive Type IR Sensor, the IR Transmitter (usually an IR LED) and the IR Detector (usually a
Photo Diode) are positioned facing each other so that when an object passes between them, the
sensor detects the object.
The other type of IR Sensor is a Reflective Type IR Sensor. In this, the transmitter and the detector
are positioned adjacent to each other facing the object. When an object comes in front of the
sensor, the infrared light from the IR Transmitter is reflected from the object and is detected by
the IR Receiver and thus the sensor detects the object.

Different applications where IR Sensor is implemented are Mobile Phones, Robots, Industrial
assembly, automobiles etc.

Ultrasonic Sensor

An Ultrasonic Sensor is a non-contact type device that can be used to measure distance as well as
velocity of an object. An Ultrasonic Sensor works based on the properties of the sound waves with
frequency greater than that of the human audible range.

Using the time of flight of the sound wave, an Ultrasonic Sensor can measure the distance of the
object (similar to SONAR). The Doppler Shift property of the sound wave is used to measure the
velocity of an object.

Light Sensor

Sometimes also known as Photo Sensors, Light Sensors are one of the important sensors. A simple
Light Sensor available today is the Light Dependent Resistor or LDR. The property of LDR is that its
resistance is inversely proportional to the intensity of the ambient light i.e., when the intensity of
light increases, its resistance decreases and vise-versa.

By using LDR is a circuit, we can calibrate the changes in its resistance to measure the intensity of
Light. There are two other Light Sensors (or Photo Sensors) which are often used in complex
electronic system design. They are Photo Diode and Photo Transistor. All these are Analog
Sensors.
There are also Digital Light Sensors like BH1750, TSL2561, etc., which can calculate intensity of
light and provide a digital equivalent value.

Smoke and Gas Sensors

One of the very useful sensors in safety related applications are Smoke and Gas Sensors. Almost all
offices and industries are equipped with several smoke detectors, which detect any smoke (due to
fire) and sound an alarm.

Gas Sensors are more common in laboratories, large scale kitchens and industries. They can detect
different gases like LPG, Propane, Butane, Methane (CH4), etc.

Now-a-days, smoke sensors (which often can detect smoke as well gas) are also installed in most
homes as a safety measure.

The “MQ” series of sensors are a bunch of cheap sensors for detecting CO, CO2, CH4, Alcohol,
Propane, Butane, LPG etc. You can use these sensors to build your own Smoke Sensor Application.
Alcohol Sensor

As the name suggests, an Alcohol Sensor detects alcohol. Usually, alcohol sensors are used in
breathalyzer devices, which determine whether a person is drunk or not. Law enforcement
personnel uses breathalyzers to catch drunk-and-drive culprits.

Touch Sensor

We do not give much importance to touch sensors but they became an integral part of our life.
Whether you know or not, all touch screen devices (Mobile Phones, Tablets, Laptops, etc.) have
touch sensors in them. Another common application of touch sensor is trackpads in our laptops.

Touch Sensors, as the name suggests, detect touch of a finger or a stylus. Often touch sensors are
classified into Resistive and Capacitive type. Almost all modern touch sensors are of Capacitive
Types as they are more accurate and have better signal to noise ratio.

Color Sensor

A Color Sensor is a useful device in building color sensing applications in the field of image
processing, color identification, industrial object tracking etc. The TCS3200 is a simple Color
Sensor, which can detect any color and output a square wave proportional to the wavelength of
the detected color.
Humidity Sensor

If you see Weather Monitoring Systems, they often provide temperature as well as humidity data.
So, measuring humidity is an important task in many applications and Humidity Sensors help us in
achieving this. Often all humidity sensors measure relative humidity (a ratio of water content in air
to maximum potential of air to hold water). Since relative humidity is dependent on temperature
of air, almost all Humidity Sensors can also measure Temperature.

Humidity Sensors are classified into Capacitive Type, Resistive Type and Thermal Conductive Type.
DHT11 and DHT22 are two of the frequently used Humidity Sensors in DIY Community (the former
is a resistive type while the latter is capacitive type).

Tilt Sensor

Often used to detect inclination or orientation, Tilt Sensors are one of the simplest and
inexpensive sensors out there. Previously, tilt sensors are made up of Mercury (and hence they
are sometimes called as Mercury Switches) but most modern tilt sensors contain a roller ball.
Actuator
An actuator is a machine part that initiates movements by receiving feedback from a control
signal. Once it has power, the actuator creates specific motions depending on the purpose of the
machine.

What Are Some Devices with Actuators?

Machines and systems have featured actuators since their popularization back in World War II.
The most well-known examples of actuators include:

• Electric motors: Any part of a piece of equipment or appliance that translates electrical
energy into motion, such as those found in ventilation fans, blenders, or refrigerators, contains at
least one actuator. Electric cars also use actuators.

• Stepper motors: These actuators are best known for receiving digital pulses and converting
them into mechanical motion. Stepper motors are often seen in robots, smart tools, or automated
cutting equipment.

• Hydraulic cylinders: These are linear-motion devices that operate using a tube, piston, and
rod. Many vehicles operate using hydraulic motion, such as bulldozers, backhoes, or excavators.
What Are Some Different Types of Actuators?

Actuators can be classified by the motion they produce and the power source they use.

Motion

Actuators can create two main types of motion: linear and rotary.

Linear Actuators
Implied by their name, linear actuators are devices that produce movement within a straight path.
They can either be mechanical or electrical and are mostly seen in hydraulic or pneumatic devices.
Any machine, equipment, or gadget that requires some form of straight motion typically has a
linear actuator.

In a simple linear actuator, there is a nut, cover, and a sliding tube. The sliding tube provides the
space for the motion, whereas the nut and cover provide the interlocking movement that keeps
the actuator in a straight path. Other complex linear actuators will have additional parts, but the
system mentioned above is the foundation for straight movement.

Rotary Actuators

In contrast to linear actuators, rotary actuators create a circular motion. From the term “rotary,”
most machines use these rotating parts to complete a turning movement. They are often used in
conjunction with a linear actuator if a machine requires moving forward, backward, up, or down.

Many rotary actuators are electrically powered, but some are powered using a hydraulic or
pneumatic system. You can find rotary actuators in windshield wipers, electric fans, or
manufacturing machines that transport goods from one area to another.
Source of Energy

To further distinguish different types of actuators, we can also sort them according to the power
source or system they use to move. Below are the most common actuators according to energy
source:

Hydraulic Actuators

Hydraulic actuators operate by the use of a fluid-filled cylinder with a piston suspended at the
center. Commonly, hydraulic actuators produce linear movements, and a spring is attached to one
end as a part of the return motion. These actuators are widely seen in exercise equipment such as
steppers or car transport carriers.

Pneumatic Actuators

Pneumatic actuators are one of the most reliable options for machine motion. They use
pressurized gases to create mechanical movement. Many companies prefer pneumatic-powered
actuators because they can make very precise motions, especially when starting and stopping a
machine.

Examples of equipment that uses pneumatic actuators include:

• Bus brakes
• Exercise machines
• Vane motors
• Pressure sensors
• Pneumatic mailing systems

Electric Actuators
Electric actuators, as you may have guessed, require electricity to work. Well-known examples
include electric cars, manufacturing machinery, and robotics equipment. Similar to pneumatic
actuators, they also create precise motion as the flow of electrical power is constant.

The different types of electrical actuators include:

• Electromechanical actuators: These actuators convert electric signals into rotary or linear
movements and may even be capable of a combination of both.

• Electrohydraulic actuators: This type of actuator is also powered electrically but gives
movement to a hydraulic accumulator. The accumulator then provides the force for movement,
usually seen in heavy industrial equipment.

Thermal and Magnetic Actuators

Thermal and magnetic actuators usually consist of shape memory alloys that can be heated to
produce movement. The motion of thermal or magnetic actuators often comes from the Joule
effect, but it can also occur when a coil is placed in a static magnetic field. The magnetic field
causes constant motion called the Laplace-Lorentz force. Most thermal and magnetic actuators
can produce a wide and powerful range of motion while remaining lightweight.
Mechanical Actuators

Some actuators are mostly mechanical, such as pulleys or rack and pinion systems. Another
mechanical force is applied, such as pulling or pushing, and the actuator will leverage that single
movement to produce the desired results. For instance, turning a single gear on a set of rack and
pinions can mobilize an object from point A to point B. The tugging movement applied on the
pulley can bring the other side upwards or towards the desired location.

Supercoiled Polymer Actuators

Supercoiled polymer actuators are a relatively new addition to the different types of actuators.
They are used in robotics and prosthetic limbs as they can replicate the motion of human muscle
via a coil that contracts and expands when heated or cooled.
Chapter 2
Arduino Programming

1.How to install and start to programming


1. Visit http://www.arduino.cc/en/main/software to download the latest Arduino IDE version for
your computer’s operating system. There are versions for Windows, Mac, and Linux systems. At
the download page, click on the “Windows Installer” option for the easiest installation.
2. Save the .exe file to your hard drive.

3. Open the .exe file.

4. Click the button to agree to the licensing agreement:

5. Decide which components to install, then click “Next”:

6.Select which folder to install the program to, then click “Install”:
7. Wait for the program to finish installing, then click “Close”:

8. Now find the Arduino shortcut on your Desktop and click on it. The IDE will open up and you’ll
see the code editor:
CONFIGURING THE ARDUINO IDE
The next thing to do is to make sure the software is set up for your particular Arduino board. Go to
the “Tools” drop-down menu, and find “Board”. Another menu will appear, where you can select
from a list of Arduino models. I have the Arduino Uno R3, so I chose “Arduino Uno”.

EXPLORING THE ARDUINO IDE


If you want, take a minute to browse through the different menus in the IDE. There is a good
variety of example programs that come with the IDE in the “Examples” menu. These will help you
get started with your Arduino right away without having to do lots of research:
EXPERIMENTING WITH THE ARDUINO
Play around with the example programs and try changing parts of the code to see what happens.
But if you want to learn programming as a skill, it’s best not to rely too much on these examples in
your projects. You’ll learn much more by experimenting and writing your own code from scratch.
A good way to learn programming is to get a book and work through the example projects.

2. How to import the libraries and how to use specific


libraries
Libraries - Libraries are a collection of code that makes it easy for you to connect to a sensor,
display, module, etc. For example, the Liquid Crystal library makes it easy to talk to character
LCD displays. There are thousands of libraries available for download directly through the
Arduino IDE, and you can find all of them listed at the Arduino Library Reference.

Using the Library Manager

1. To install a new library into your Arduino IDE you can use the Library Manager (available
from IDE version 1.6.2). Open the IDE and click to the "Sketch" menu and then Include
Library > Manage Libraries.
2. Then the Library Manager will open and you will find a list of libraries that are already
installed or ready for installation. In this example we will install the Bridge library. Scroll
the list to find it, click on it, then select the version of the library you want to install.
Sometimes only one version of the library is available. If the version selection menu does
not appear, don't worry: it is normal.

3. Finally click on install and wait for the IDE to install the new library. Downloading may
take time depending on your connection speed. Once it has finished, an Installed tag
should appear next to the Bridge library. You can close the library manager.
Importing a .zip Library
Libraries are often distributed as a ZIP file or folder. The name of the folder is the name of the
library. Inside the folder will be a .cpp file, a .h file and often a keywords.txt file, examples
folder, and other files required by the library. Starting with version 1.0.5, you can install 3rd
party libraries in the IDE. Do not unzip the downloaded library, leave it as is.

In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library. At the top of the drop
down list, select the option to "Add .ZIP Library''.

You will be prompted to select the library you would like to add. Navigate to the .zip file's
location and open it.
Return to the Sketch > Include Library menu. menu. You should now see the library at the
bottom of the drop-down menu. It is ready to be used in your sketch. The zip file will have been
expanded in the libraries folder in your Arduino sketches directory.

NB: the Library will be available to use in sketches, but with older IDE versions examples for the
library will not be exposed in the File > Examples until after the IDE has restarted.

Manual Installation

When you want to add a library manually, you need to download it as a ZIP file, expand it and
put in the proper directory. The ZIP file contains all you need, including usage examples if the
author has provided them. The library manager is designed to install this ZIP file automatically as
explained in the former chapter, but there are cases where you may want to perform the
installation process manually and put the library in the libraries folder of your sketchbook by
yourself.

You can find or change the location of your sketchbook folder at File > Preferences >
Sketchbook location.

Go to the directory where you have downloaded the ZIP file of the library
Extract the ZIP file with all its folder structure in a temporary folder, then select the main folder,
that should have the library name

Copy it in the "libraries" folder inside your sketchbook.

Start the Arduino Software (IDE), go to Sketch > Include Library. Verify that the library you just
added is available in the list.
3. How to write simple program (LED blinking) in
Arduino platform and how to run that code

Making the Arduino Blinking LED Project

In most programming languages, you start with a program that simply prints “Hello, World” to the
screen. The equivalent in the micro-controller world, such as Arduino, is getting light to blink on
and off. The LED blinking sketch is the first program that you should run to test whether your
Arduino board is working and is configured correctly. An LED, which stands for Light-Emitting
Diode, is a small electronic component that’s a bit like a lightbulb, but is more efficient and
requires a lower voltage to operate.

Before we get to the programming part, let’s talk first about how to connect an LED to the Arduino
microcontroller. To help you make the proper connection, you can download the Learn Arduino
Intro app from the Google Play store. The Arduino Intro app contains a lot of Arduino projects to
help you get started with Arduino. It will show you the materials you need, the breadboard
diagram, and of course the Arduino code complete with comments.

Important Things to Know About LED


LED stands for Light Emitting Diode. A diode only allows electricity to flow through it one way, so if
you hook it up backward, it won’t work. An LED has two leads or sometimes called legs. The longer
leg, which is the positive side, is called the anode. The shorter leg, which is the negative side, is
called the cathode. LEDs can come in many shapes, colors, and sizes, for this video, you’ll be using
a 5mm LED.
The positive side is also called the anode, while the negative side is called the cathode. The
cathode can also be determined by a small flat portion along the side of the bulb.

Now, each digital pin of the Arduino is outputting 5v DC at 40mA, and most LEDs require a voltage
of 2v and a current of 35mA. Anything higher than 2 volts may damage your LED. Therefore, you
need a resistor that will reduce the 5v to 2v and the current from 40mA to 35mA, if you want to
display the LED at its maximum brightness.

Why You Need Resistors

A resistor is a device designed to cause resistance to an electric current in order to cause a drop in
voltage across its terminals. You use resistors to decrease voltage or current to other devices. If
you want the LED to be dimmer, you could use a higher value of resistance. To get the correct
resistance, you need to know Ohm’s Law. The value of resistance is known as ohms. According to
Ohms Law, Voltage is equal to Current times Resistance. This is an entirely separate topic, so we’ll
not talk about it in this article.

Resistors have "color bands" that tell their resistance value.

For now, you can use a 220-ohm resistor. Resistors have color bands on them that let you know
what value they have. A 220-ohm resistor has the colors red-red-brown. Resistors don’t have
polarity, meaning they don’t have a positive and a negative side. Therefore, you don’t have to
worry if it’s inverted or not.

The color bands of a 220-ohm resistor

Lastly, you’ll be needing some jumper wires. The jumper wires you use can either be commercially
available jumper wires (usually with molded ends to make insertion into the breadboard easier) or
you can make your own by cutting short strips of the stiff single core wire and stripping away
about 1 inch from the end. For a 1 LED circuit, jumper wires are not necessarily required, but it’s
good to practice using jumper wires as early as now.
Jumper wires come in different colors.

Again, for our blinking LED project, you’ll be needing an LED, a 220-ohm resistor, jumper wires,
and of course a breadboard and your Arduino board. In this article, I’ll be using an Arduino Uno
board, but you can use any Arduino board for this one.

Assembling the Components on the Breadboard

To start, let’s open up the Arduino Intro app and tap on Project number 1, Blink, from the Projects
tab.
Now we can see the materials we need. By default, you should have an Arduino board,
breadboard, and jumper wires at hand. Now, let’s tap on the Breadboard tab to view the
breadboard diagram or how the components are connected.

In this diagram, the straight leg of the LED which is called is the cathode or the negative side, is
inserted in J5. While the bent leg, which is the anode or the positive side, is inserted in J6. You
don’t have to follow this exactly. You can insert the legs of the LED anywhere on the terminal
strips as long as the negative side and the positive side are not directly connected. Just be sure
that the LED is inserted firmly into the holes.

Next, the negative side of the LED should be connected to the Ground or GND pin of your Arduino
board. You can achieve this by inserting one end of a jumper wire in H5 and the other end in the
GND pin. Note that F5 to J5 are connected via the metal strip found underneath the holes of the
breadboard. You can use any color of jumper wire.

Next, you connect the positive side of the LED to any of the digital pins. Well, except pins 0 and 1.
These pins are reserved for serial communications. In this case, it is connected to digital pin 9. But
before you make the connection, you have to put a 220-ohm resistor between the positive side of
the LED and pin 9. This will bring down the 5v of the pin to 2 volts. If the full 5 volts will be fed
directly to the LED, your LED will get damaged. Note again that the resistor has no polarity so you
can place it in any way. In this diagram one end of the resistor is placed on I6 and the other end on
I10. Then a jumper wire is inserted in H6 going to digital pin 9 of the Arduino. Again F10-J10 are all
connected in the breadboard. However, they are not connected to A10-E10 because of the break
in the middle called the groove.

Now your circuit is complete. You’ll now have to connect the Arduino circuit to your computer via
a USB cable.

Writing the Code in Arduino IDE

Let’s go now to the Arduino code or sketch. You can see the sketch for the Blink project in the
Code tab of the Arduino Intro app. If you have the Arduino IDE installed already, you can open it
up and type in the code you see in the app. You don’t have to copy the green-colored text. These
are called comments, and they will not be read by the compiler.

Press the Verify/ Compile button at the top of the IDE to make sure there are no errors in your
code.
If this is successful, click the Upload button to upload the code to your Arduino. If you have done
everything right, you should now see the red LED on the breadboard flashing on and off every
second.

Let's take a look at the code and the hardware to find out how they both work.

/*

blink

1. Turn on LED for 1 second

2. Turn off LED for 1 second

3. Repeat

*/

int pinled = 9;

void setup() {

// initialize the digital pin as an output.

pinMode(pinled, OUTPUT);

// the loop routine runs over and over again forever:

void loop() {

digitalWrite(pinled, HIGH); // turn the LED on

delay(1000); // wait for a second

digitalWrite(pinled, LOW); // turn the LED off

delay(1000); // wait for a second

}
This is the code to control the LED. An Arduino code or sketch must contain at least two functions.
A function is a series of programming statements that can be called by name.

1. setup() which is called once when the program starts.


2. loop() which is called repetitively over and over again as long as the Arduino has power.

So the shortest valid Arduino program (even though it does nothing) is:

void setup() {
// put your setup code here, to run once:

void loop() {

// put your main code here, to run repeatedly:

Full Breakdown of the Arduino LED Blink Code

/*

blink

1. Turn on LED for 1 second

2. Turn off LED for 1 second

3. Repeat

*/

The first 6 lines are what you call a comment. A comment is just text meant to be read by humans.
As a code, it is just ignored. You are not really required to write comments. However, they are
helpful if you have plenty of lines of code so you can keep track of what’s happening in your
program. There are 2 types of comments.

The first one is a multi-line comment. It starts with the /* symbol and ends with the */ symbol.
Anything in between is considered a comment and it will not produce an error. It can span many
lines.

The 2nd one is the single-line comment. It starts with a double forward slash. Anything after that
will be considered a comment. You don’t have to end the single-line comment with another
symbol as long as the text is written on the same line.
int pinled = 9;

Line 8 declares a variable named pinled. A variable is a place to store data. In this case, you are
setting up a variable of type int or integer. An integer is a whole number within the range of -
32,768 to 32,767. Next, you have assigned that integer the name of pinled and have given it a
value of 9. You don't have to call it pinled, you could call it anything you wanted to. But you want
your variable name to be descriptive, so you call it pinled to show that this variable is for the pin
on the Arduino where the LED is connected. In this case, you are using digital pin 9. At the end of
this statement is a semi colon. This symbol tells the compiler that this statement is now complete.

Although you can call your variables anything, every variable name in a C-based program like
Arduino must start with a letter; the rest of the name can consist of letters, numbers, and
underscore characters. Note that Arduino recognizes upper and lower case characters as being
different. Finally, you cannot use any of Arduino’s keywords like main, while, switch, and many
more as variable names. Keywords are constants, variables, and function names that are defined
as part of the Arduino language. To help you avoid naming a variable after a keyword, all keywords
within the sketch will appear in red. It also helps if the first letter in your variable name is
capitalized.

What’s Inside the setup() and loop() Functions?

void setup() {
// initialize the digital pin as an output.

pinMode(pinled, OUTPUT);

Your setup function only has one statement and that is pinMode, which telling the Arduino that
you want to set the mode of one of your pins to be in OUTPUT mode, rather than INPUT mode.
This also means that the Arduino is writing to a pin instead of reading from it.

Within the parenthesis, you put the pin number and the mode (OUTPUT or INPUT). Your pin
number is pinled, which is the variable that has been previously set to the value 9. Therefore, this
statement is simply telling the Arduino that Digital Pin 9 is to be set to OUTPUT mode. As
the setup( ) function runs only once, you now move onto the main function which is the loop
function.

void loop() {

digitalWrite(pinled, HIGH); // turn the LED on

delay(1000); // wait for a second

digitalWrite(pinled, LOW); // turn the LED off


delay(1000); // wait for a second

}
The loop( ) function runs continuously as long as the Arduino is turned on. Every statement within
the loop( ) function (that is within it’s curly braces) is carried out, one by one, step by step, until
the bottom of the function is reached, then the loop starts again at the top of the function, and so
on forever or until you turn the Arduino off or press the Reset switch on your Arduino board.

In this project, you want the LED to turn on, stay on for one second, turn off and remain off for
one second, and then repeat. The commands to tell the Arduino to do this are contained within
the loop ( ) function because you want them to repeat over and over. The first statement is
digitalWrite(pinled, HIGH);

The digitalWrite command sets a HIGH or a LOW value to the pin within the statement (in this
case pinled, which is Digital Pin 9). When you set a pin to HIGH, you are sending out 5 volts to that
pin. When you set it to LOW, the pin will have 0 volts. This statement, therefore, sends out 5v to
pin 9 and turns the LED on.

After that is delay (1000);

This statement simply tells the Arduino to wait for 1000 milliseconds or 1 second before carrying
out the next statement.

The next line is digitalWrite(pinled, LOW);

This will turn off the power going to Digital Pin 9 and therefore turn the LED off.

Then there is another delay statement for another 1000 milliseconds and then the function ends.
However, as this is your main loop( ) function, the function will start again at the beginning.

Uploading the Code from the Computer to the Arduino Board

Now that your sketch is complete, the next thing you need to do is upload this sketch into your
Arduino board. First, make sure that you connect your board to your computer via USB cable.
Next, go to Tools->Port and select the correct port number. In windows, it starts with COM which
is followed by a number. Normally, it’s a number higher than 1. You can check this out in the
Device Manager settings in windows. Then select your board by going to Tools->Board. Since I’m
using an Arduino Uno board, that’s what I’m going to select. Finally, click on the Upload button
(it’s the button with the right-facing arrow)

If everything is ok, you’ll receive a done uploading message on the lower portion of the IDE. If not,
the IDE will give you an error message with some descriptions of what went wrong with your
sketch.
Common Errors in Your Arduino Code

If your program won’t compile (or it doesn’t do what you expect), here are a few things to check:

• The Arduino programming language is case-sensitive. In other words, pinled with a small
p is different from Pinled with a capital P.
• Blocks of code are encapsulated with curly braces. Make sure an open brace is paired
with a close brace
• Every open parenthesis must have a matching close parenthesis
• There are no commas in numbers.
• In general, each program statement needs to end with a semicolon. This means that
each line of your program will have a semicolon.

By following the program structure step by step, you can see that it is really very simple: To
summarize everything:

You start off by assigning a variable called pinled, giving that variable a value of 9, which is where
the positive side of the LED is connected. Then you move on to the setup( ) function where you set
the mode for Digital Pin 9 as an output. In the main program loop, you set Digital Pin 9 to HIGH,
sending out 5v. Then you wait for a second and then turn off the 5v to Digital Pin 9, before waiting
another second. The loop then starts again at the beginning. The LED will tum on and off
continuously for as long as the Arduino has power.
4. How to use serial monitor and serial plotter in
Arduino platform (with example).
Using the Serial Monitor tool
Learn how to use the new Serial Monitor tool in the Arduino IDE 2.0, and how it works differently
from older versions.

The Serial Monitor is an essential tool when creating projects with Arduino. It can be used as a
debugging tool, testing out concepts or to communicate directly with the Arduino board.

The Arduino IDE 2.0 has the Serial Monitor tool integrated with the editor, which means that no
external window is opened when using the Serial Monitor. This means that you can have
multiple windows open, each with its own Serial Monitor.

Requirements

• Arduino IDE 2.0 installed.


• Arduino board.
• Core installed for the board used.

Notable Changes
Integrated Monitor

One major change that was introduced with the Arduino IDE 2.0 is the integrated Serial
Monitor. The older versions of the editor feature an external window that matches the
port/board that we select.

The Arduino IDE 2.0 works a bit differently. Instead of opening an external window for the
Serial Monitor, it shows up where the console log is located, as an additional tab. To
understand how this works, let's take a look at how the old editor works:
Serial Monitor on the older version.

Now, let's take a look at the IDE 2.0. Notice how the Serial Monitor is located at the bottom of
the editor:

Serial Monitor on the new version.

The Serial Monitor settings are also located here, such as adjusting the baud rate and sending
messages.

Advantages

A major advantage with having the Serial Monitor integrated with the editor is the possibility to
have multiple monitors open simultaneously. In the old editor, when changing the port/board, it
changed across all windows. In addition, we were limited to one Serial Monitor window, an
obstacle that the IDE 2.0 removes.

You will find an example with more information on how to use this feature, further down this
tutorial under the Using multiple Serial Monitors simultaneously section.

Using the Serial Monitor Tool

The Serial Monitor tool is a really great tool for debugging and establishing communication
between a computer and an Arduino. To use it is really easy, but there are some things that we
need to do first.
1. First we need to open the Arduino IDE 2.0.

An empty Arduino IDE sketch window.

2. Now, we need to create a sketch that includes some configurations needed, so that our board
can communicate with our computer. Mainly, we need to set a baud rate, which is done by
writing

Serial.begin(9600);
. Here, the
9600
represents the baud rate, which is the maximum bits per seconds that can be transferred. The
sketch that we need to use can be found in the snippet below:

COPY

1void setup() {

2Serial.begin(9600);

3}

5void loop () {

6Serial.println("Hello world!");

7delay (1000);

8}
3. This will print

"Hello world!"
, every one second to the Serial Monitor. Let's select the board we want to use, and upload the
sketch to the board.

Select the board and upload the sketch.

4. When it has finished uploading, click on the Serial Monitor button, located at the top right
corner of the IDE. This will launch the Serial Monitor in the bottom of the IDE, replacing the
console section.

Open the Serial Monitor, and viewing the output.


The text

"Hello world!"
is now printed every one second. Congratulations, you have now successfully sent a message
from your Arduino, to your computer.

Using Multiple Serial Monitors Simultaneously

A really cool feature with the Arduino IDE 2.0 is that the Serial Monitor is linked to the sketch
windows you have open. For example, if we have two sketch windows,
named sketch_1 and sketch_2, we can select the port and board for each window, and have two
Serial Monitors running at the same time.

This is really useful when working with various communication / connectivity projects, where we
want to know what's going on both boards at the same time. If you have two Arduino boards,
you can try out this feature using the instructions below.

1. First, we need to open a new file, through File > New.

Open a new sketch.

2. Now, we need to choose another board. In this example, we are using an Arduino Nano 33
IoT. If you have connected it to your computer and installed the necessary core for it, it will
show up in the board list.
List of available boards.

3. For the new sketch, let's use the same sketch we uploaded to the other board, but replace
the

"Hello world!"
, with something else. In this example, we used
"Hello Mars!"
, as you can see in the code snippet below:

COPY

1void setup() {

2Serial.begin(9600);

3}

5void loop() {

6Serial.println("Hello Mars!");

7delay(1000);

8}

4. Upload the code to the board, and open the Serial Monitor. We should now see

"Hello Mars!"
being printed every one second. If we put the two sketch windows side by side, we can see how
they are printing at the same time.
Two Serial Monitors running simultaneously.

Congratulations, you can now check what is going on with two boards simultaneously!

Note: Using several sketch windows and Serial Monitor at the same time can be quite heavy on
your machine.

Using the Serial Plotter


The Serial Plotter is an alternative to the Serial Monitor for displaying numerical data. The data
sent to the host via serial.print and serial.println commands needs to be only numeric i.e.,
with no text on each row of data.

The following sketch demonstrates how to format data for the Serial Plotter by generating two
columns of numbers and no extraneous text labels. The cosine and sine functions are used as
convenient sources of data.

Figure (a)

Before uploading the sketch, close the Serial Monitor if it is open. You cannot use both the
Serial Monitor and Serial Plotter at the same time. The Arduino IDE may also give error messages
when you first try to open the Serial Plotter window. If that happens, try opening the Serial Plotter
window and uploading the sketch again.
Figure (a)

Running the demo_serialplotter sketch produces a dynamic plot captured in the following
screenshot.
Waiting for the Serial object to start
Depending on the host operating system and the Arduino board, there is usually a slight delay
between the completion of the Serial.begin command and the time that the serial connection
between the host and Arduino board is ready for communication. This delay is usually not
important except when messages to the Serial Monitor are sent from within the setup function.

Consider the setup function from the sample code above

void setup() {
Serial.begin(9600); // Initialize the Serial object at 9600 baud
delay(3000); // Wait for the USB connection to be established
Serial.println("Ready to go"); // Confirmation message
}

If the delay(3000) statment is removed, then for some combinations of host and Arduino board,
the “Ready to go” message might not appear on the Serial Monitor. Without the delay(3000) , the
“Ready to go” message might be sent to the Serial object before the serial (USB) connection
between the host and Arduino board is established. In that case, the “Ready to go” message is
lost.

Instead of using the delay(3000) statement, Arduino programmers can use a while loop as
follows

void setup() {
Serial.begin(9600); // Initialize the Serial object at 9600 baud
while( !Serial ) { delay(10); } // Wait for the USB connection to be established
Serial.println("Ready to go"); // Confirmation message
}

The !Serial expression will be true until the Serial object is ready. In other words, the value
returned by Serial will be equivalent to false (and !Serial will be true ) until the Serial object
returns a non-zero value. The amount of the delay (10 milliseconds in this example) in the body of
the while loop does not matter as long as it is small.

The use of the while( !Serial ) { delay(10); } expression has the advantage of creating the shortest
possible wait. As soon as !Serial is false the code can continue, which is usually shorter than the
3000 millisecond delay caused by delay(3000) . Usually differences in short wait time at the start
of the sketch doesn’t have a significant practical impact.

However, there is a big disadvantage of using the while( !Serial ) { delay(10); } expression if the
Arduino board is going to be deployed untethered from the host. For example, if the board is
disconnected from the host after the sketch is uploaded and the board is connected to a battery,
the value of !Serial will never by true . This causes the sketch to stall and become useless.
As long as the sketch is always running while connected to the host, both
the delay(3000) and while( !Serial ) { delay(10); } expression will work. In that case, the choice of
technique to wait for the Serial object to start up is up to the programmer.

Finally, note that in many applications, the loss of the first few lines of output to the Serial
Monitor is inconsequential, especially after the sketch is debugged.
Chapter 3
Applications
1.Ultrasonic sensor base water level indicator
Components

Name Quantity Component

U1 1 Arduino Uno R3
R1 9 270 Ω Resistor
R2
R3
R4
R5
R6
R7
R8
R9
D1 3 Red LED
D2
D3
D4 3 Yellow LED
D5
D6
D7 3 Green LED
D8
D9
DIST1 1 Ultrasonic Distance Sensor

Circuit Diagram
Pin layout

Circuit layout
Simulation

In this simulation run depth was set around 85 cm and 6 LEDs were lighten up showing
6 level of water.

In this simulation run depth was set around 20 cm and 9 LEDs were lighten up showing
9 level of water.
Code
1. #define Trigpin 8
2. #define Echopin 7
3.
4. int led_b[9]={13,5,6,2,3,9,10,11,12};
5.
6. float distance;
7.
8. void setup() {
9. pinMode (Trigpin, OUTPUT);
10. pinMode (led_b[0], OUTPUT);
11. pinMode (led_b[1], OUTPUT);
12. pinMode (led_b[2], OUTPUT);
13. pinMode (led_b[3], OUTPUT);
14. pinMode (led_b[4], OUTPUT);
15. pinMode (led_b[5], OUTPUT);
16. pinMode (led_b[6], OUTPUT);
17. pinMode (led_b[7], OUTPUT);
18. pinMode (led_b[8], OUTPUT);
19.
20. pinMode (Echopin, INPUT);
21.
22. Serial.begin(91050);
23.
24. digitalWrite (led_b[9], LOW);
25. digitalWrite (led_b[1], LOW);
26. digitalWrite (led_b[2], LOW);
27. digitalWrite (led_b[3], LOW);
28. digitalWrite (led_b[4], LOW);
29. digitalWrite (led_b[5], LOW);
30. digitalWrite (led_b[6], LOW);
31. digitalWrite (led_b[7], LOW);
32. digitalWrite (led_b[8], LOW);
33. }
34.
35. void loop() {
36.
37. digitalWrite(Trigpin,LOW);
38. delayMicroseconds(5);
39. digitalWrite(Trigpin,HIGH);
40. delayMicroseconds(10);
41. digitalWrite(Trigpin,LOW);
42. distance=pulseIn(Echopin,HIGH);
43. distance = distance/58;
44.
45. if (150 < distance && distance <= 900)
46. {
47. Serial.println (distance);
48. digitalWrite(led_b[0],HIGH);
49. for(int i=1;i<=8;i++){
50. digitalWrite(led_b[i],LOW);
51. }
52. }
53. else if(135 < distance && distance <= 150)
54. {
55. Serial.println (distance);
56. digitalWrite(led_b[0],HIGH);
57. digitalWrite(led_b[1],HIGH);
58.
59. for(int i=2;i<=8;i++){
60. digitalWrite(led_b[i],LOW);}
61.
62. }
63. else if(120 < distance && distance <=135)
64. {
65. Serial.println (distance);
66. digitalWrite(led_b[0],HIGH);
67. digitalWrite(led_b[1],HIGH);
68. digitalWrite(led_b[2],HIGH);
69.
70.
71. for(int i=3;i<=9;i++){
72. digitalWrite(led_b[i],LOW);}
73.
74. }
75.
76. else if(105 < distance && distance <= 120)
77. {
78. digitalWrite(led_b[0],HIGH);
79. digitalWrite(led_b[1],HIGH);
80. digitalWrite(led_b[2],HIGH);
81. digitalWrite(led_b[3],HIGH);
82.
83. for(int i=4;i<=9;i++){
84. digitalWrite(led_b[i],LOW);}
85.
86. }
87. else if(90 < distance && distance <= 105)
88. {
89. Serial.println (distance);
90. digitalWrite(led_b[0],HIGH);
91. digitalWrite(led_b[1],HIGH);
92. digitalWrite(led_b[2],HIGH);
93. digitalWrite(led_b[3],HIGH);
94. digitalWrite(led_b[4],HIGH);
95. for(int i=5;i<=8;i++){
96. digitalWrite(led_b[i],LOW);}
97.
98.
99. }
100. else if(75 < distance && distance <=90)
101. {
102. Serial.println (distance);
103. digitalWrite(led_b[0],HIGH);
104. digitalWrite(led_b[1],HIGH);
105. digitalWrite(led_b[2],HIGH);
106. digitalWrite(led_b[3],HIGH);
107. digitalWrite(led_b[4],HIGH);
108. digitalWrite(led_b[5],HIGH);
109.
110. for(int i=7;i<=8;i++){
111. digitalWrite(led_b[i],LOW);}
112.
113. }
114. else if(60 < distance && distance <= 75)
115. {
116. Serial.println (distance);
117. digitalWrite(led_b[0],HIGH);
118. digitalWrite(led_b[1],HIGH);
119. digitalWrite(led_b[2],HIGH);
120. digitalWrite(led_b[3],HIGH);
121. digitalWrite(led_b[4],HIGH);
122. digitalWrite(led_b[5],HIGH);
123. digitalWrite(led_b[6],HIGH);
124.
125. for(int i=7;i<=8;i++){
126. digitalWrite(led_b[i],LOW);}
127.
128. }
129. else if(45 < distance && distance <=60)
130. {
131. Serial.println (distance);
132. digitalWrite(led_b[0],HIGH);
133. digitalWrite(led_b[1],HIGH);
134. digitalWrite(led_b[2],HIGH);
135. digitalWrite(led_b[3],HIGH);
136. digitalWrite(led_b[4],HIGH);
137. digitalWrite(led_b[5],HIGH);
138. digitalWrite(led_b[6],HIGH);
139. digitalWrite(led_b[7],HIGH);
140.
141. for(int i=8;i<=8;i++){
142. digitalWrite(led_b[i],LOW);}
143.
144. }
145. else if(30< distance && distance <=45)
146. {
147. Serial.println (distance);
148. digitalWrite(led_b[0],HIGH);
149. digitalWrite(led_b[1],HIGH);
150. digitalWrite(led_b[2],HIGH);
151. digitalWrite(led_b[3],HIGH);
152. digitalWrite(led_b[4],HIGH);
153. digitalWrite(led_b[5],HIGH);
154. digitalWrite(led_b[6],HIGH);
155. digitalWrite(led_b[7],HIGH);
156. digitalWrite(led_b[8],HIGH);
157. digitalWrite(led_b[9],HIGH);
158.
159. }
160. delay (30);
161. }
162.
In this application we were needed to check a water level of the water tank. For that using
an arduino board with an ultrasonic sonic sensor our programme was developed. The
operation of the circuit is fairly simple. The ultra sonic sensor sent a sound wave and receive
it. By using time and speed the distance is calculated to the reflective boundary. Using echo
pin this distance in our case the water level was sent to the arduino board. In the code we
check this value of the distance using a if condition. Before that every used pin was setup and
set to LOW voltage status. By using the above if condition relative LED out pins were set to
high to indicate the sensor reading and the water level. In my case i choose 1.5m tank and
water level be indicated for every 15cm, minimum being one RED LED and maximum water
level being all 9 LEDs lighten up.

3. Motor controlling (Speed and Direction) circuit to


drive DC motor by using L293D Motor Drive IC.

Components
Circuit diagram
Pin layout

Circuit layout
Simulation

In this simulation run Motor A was rotated counter clockwise using the push button 1

In this simulation run Motor A was rotated counter clockwise using the push button 2
Code
//L293D
//Motor A
#define BTN_DIR1 13
#define BTN_DIR2 12
#define BTN_DIR3 11
#define BTN_DIR4 8

//Motor A
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
//Motor B
const int motorPin3 = 10; // Pin 7 of L293
const int motorPin4 = 9; // Pin 2 of L293

//This will run only one time.


void setup(){

//Set pins as outputs


pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);

pinMode(BTN_DIR1, INPUT);
pinMode(BTN_DIR2, INPUT);
pinMode(BTN_DIR3, INPUT);
pinMode(BTN_DIR4, INPUT);
}

void loop(){

if (digitalRead(BTN_DIR1)==HIGH){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);

}else {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);

if (digitalRead(BTN_DIR2)==HIGH){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);

}else {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
}
if (digitalRead(BTN_DIR3)==HIGH){
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);

}else {
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
if (digitalRead(BTN_DIR4)==HIGH){
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);

}else {
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
}

Our purpose of this application is controlling the directions of 2 motors in opposite directions
using 4 push buttons, each for each direction. First the motor voltages and the IC operating
voltages were given to the IC using power pins. Next 4 Inputs from push buttons were sent to
the arduino. The arduino has 4 out puts to the motor controlling IC. To change the direction of
a motor it's need to change the each pairs voltage to HIGH and LOW. By choosing the which pin
is the HIGH one or LOW one the direction of the motor can be controlled. Using our code button
conditions were read by an if condition and in the logic motor pins were set to relevant HIGH
LOW conditions. If we pressed a button the condition relevant to that switch is checked and
motor is rotated in that specified direction.

You might also like