You are on page 1of 11

Analogue and Digital Sensors Lab 3

Connecting Analogue and Digital Sensors with Arduino


Learn about Arduino and the Arduino UNO and how you can integrate this board into your makerspace and coding program. Make interactive makerspace projects while learning to code and problem solve.

Use of Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C
language is rich in built-in operators and provides the following types of operators:
 Arithmetic Operators
 Comparison Operators
 Boolean Operators
 Bitwise Operators
 Compound Operators
Comparison Operators
Operator Operator
Description Example
name Simple

Checks if the value of two operands


(A == B) is not
equal to == is equal or not, if yes then condition
true
becomes true.

Checks if the value of two operands


not equal to != is equal or not, if values are not (A != B) is true
equal then condition becomes true.

Checks if the value of left operand


is less than the value of right
less than < (A < B) is true
operand, if yes then condition
becomes true.

Checks if the value of left operand


is greater than the value of right
greater than > (A > B) is not true
operand, if yes then condition
becomes true.

Checks if the value of left operand


less than or is less than or equal to the value of
<= (A <= B) is true
equal to right operand, if yes then condition
becomes true.

Checks if the value of left operand


greater than is greater than or equal to the value (A >= B) is not
>=
or equal to of right operand, if yes then true
condition becomes true.

Boolean Operators
Assume variable A holds 10 and variable B holds 20 then
Operator Operator
Description Example
name simple

Called Logical AND operator. If both


and && the operands are non-zero then then (A && B) is true
condition becomes true.

Called Logical OR Operator. If any of


or || the two operands is non-zero then (A || B) is true
1
Analogue and Digital Sensors Lab 3
then condition becomes true.

Called Logical NOT Operator. Use to


reverses the logical state of its
not ! !(A && B) is false
operand. If a condition is true then
Logical NOT operator will make false.

Compound Operators
Assume variable A holds 10 and variable B holds 20 then -

Operator Operator
Description Example
name simple

Increment operator, increases


increment ++ A++ will give 11
integer value by one

Decrement operator, decreases


decrement -- A-- will give 9
integer value by one

Add AND assignment operator. It B += A is


compound
+= adds right operand to the left operand equivalent to B =
addition
and assign the result to left operand B+ A

Subtract AND assignment operator.


B -= A is
compound It subtracts right operand from the
-= equivalent to B =
subtraction left operand and assign the result to
B-A
left operand

About Serial Monitor


Serial Monitor is one of the tools in Arduino IDE. It is used for two purposes:

 Arduino → PC: Receives data from Arduino and display data on screen. This is usually used for
debugging and monitoring
 PC → Arduino: Sends data (command) from PC to Arduino.

Data is exchanged between Serial Monitor and Arduino via USB cable, which is also used to upload the
code to Arduino. Therefore, To use Serial Monitor, we MUST connect Arduino and PC via this cable.
Commands for print serially
1. Serial.print(“Anything”)
It will print whatever written in the inverted commas and stay the courser in the same line
2. Serial.println(“Anything”)
It will print whatever written in the inverted commas and movethe courser in the next line
1. Arduino to PC
To send data from Arduino to PC, we need to use the following Arduino code:
Example 1
In this example, we will send the “ArduinoGetStarted.com” from Arduino to Serial Monitor every second
void setup() {
Serial.begin(9600);
2
Analogue and Digital Sensors Lab 3
}
void loop() {
Serial.println("ArduinoGetStarted.com");
delay(1000);
}

2. PC To Arduino
How to send data from PC to Aduino and read it on Arduino You will type text on Serial Monitor and then
click Send button. Arduino reads data and process it. To read data, we need to use the following Arduino
code:

Example 2
In this example, we will send the commands from Serial Monitor to Arduino to turn on/off a built-in LED.
The commands include:
• “ON”: turn on LED
• “OFF”: turn off LED
How Arduino can receive a complete command? For example, when we send “OFF” command, how
Arduino can know the command is “O”, “OF” or “OFF”?
⇒ When sending a command, we will append a newline character ('\n') by selecting “newline” option on
Serial Monitor. Arduino will read data until it meets the newline character. In this case, the newline
character is called delimiter.

void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT); // set the digital pin as output:
}
void loop() {
if(Serial.available()) // if there is data comming
{
String command = Serial.readStringUntil('\n'); // read string until meet newline character
if(command == "ON")
{
digitalWrite(LED_BUILTIN, HIGH); // turn on LED
Serial.println("LED is turned ON"); // send action to Serial Monitor
}
else
if(command == "OFF")
{
digitalWrite(LED_BUILTIN, LOW); // turn off LED
Serial.println("LED is turned OFF"); // send action to Serial Monitor
}
}
}
Arduino Sensors
If you want your Arduino to sense the world around it, you will need to add a sensor. There are a wide
range of sensors to choose from and they each have a specific purpose. Below you will find some of the
commonly used sensors in projects.
 Distance Ranging Sensor

3
Analogue and Digital Sensors Lab 3
 PIR Motion Sensor
 Light Sensor
 Degree of Flex Sensor
 Pressure Sensor
 Proximity Sensor
 Sound Detecting Sensor
 RGB and Gesture Sensor
 Humidity and Temperature Sensor

Examples of Arduino Sensors

1. PIR motion sensor


PIR sensors allow you to sense motion. They are used to detect whether a human has moved in or out of
the sensor’s range. They are commonly found in appliances and gadgets used at home or for businesses.
They are often referred to as PIR, "Passive Infrared", "Pyroelectric", or "IR motion" sensors.
Following are the advantages of PIR Sensors −

 Small in size
 Wide lens range
 Easy to interface
 Inexpensive
 Low-power

PIRs have adjustable settings and have a header installed in the 3-pin ground/out/power pads

4
Analogue and Digital Sensors Lab 3

You can adjust the sensor sensitivity and delay time via two variable resistors located at the bottom of the
sensor board.

Passive Infra Red sensors can detect movement of objects that radiate IR light (like
human bodies). Therefore, using these sensors to detect human movement or
occupancy in security systems is very common. Initial setup and calibration of these
sensors takes about 10 to 60 seconds.

5
Analogue and Digital Sensors Lab 3
Circuit Diagram

Example 3

int led = 13;

int buz = 12; // the pin that the LED is atteched to

int sensor = 2; // the pin that the sensor is atteched to

int val = 0; // variable to store the sensor status (value)

void setup() {

pinMode(led, OUTPUT); // initalize LED as an output

pinMode(buz, OUTPUT);

pinMode(sensor, INPUT); // initialize sensor as an input

void loop(){

val = digitalRead(sensor); // read sensor value

if (val == HIGH) { // check if the sensor is HIGH

6
Analogue and Digital Sensors Lab 3
digitalWrite(led, HIGH); // turn LED ON

digitalWrite(buz, HIGH);

delay(200); // delay 200 milliseconds

else {

digitalWrite(led, LOW); // turn LED OFF

digitalWrite(buz, LOW);

delay(200); // delay 200 milliseconds

Ultrasonic Sensor
The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do. It
offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use
package from 2 cm to 400 cm or 1” to 13 feet.

The operation is not affected by sunlight or black material, although acoustically, soft materials like cloth
can be difficult to detect. It comes complete with ultrasonic transmitter and receiver module.

7
Analogue and Digital Sensors Lab 3

Technical Specifications
 Power Supply − +5V DC
 Quiescent Current − <2mA
 Working Current − 15mA
 Effectual Angle − <15°
 Ranging Distance − 2cm – 400 cm/1″ – 13ft
 Resolution − 0.3 cm
 Measuring Angle − 30 degree
Circuit Diagram

Example 4
const int Trigpin = A0; // Trigger Pin of Ultrasonic Sensor
const int echoPin = A1; // Echo Pin of Ultrasonic Sensor
long duration, inches, cm;

void setup() {
Serial.begin(9600); // Starting Serial Terminal
pinMode(Trigpin, OUTPUT);
pinMode(echoPin, INPUT);

8
Analogue and Digital Sensors Lab 3
}

void loop() {
calculateDistance();

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}

void calculateDistance(){

digitalWrite(Trigpin, LOW);
delayMicroseconds(2);
digitalWrite(Trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(Trigpin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
return inches;
return cm;}

long microsecondsToInches(long microseconds) {


return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {


return microseconds / 29 / 2;
}

9
Analogue and Digital Sensors Lab 3

Lab Tasks
1. Display a counting from 0 to 100 on serial monitor by using for loop.
2. Connect 3 leds when you type FIRSTON so 1st led should be turn on and when you type
SECONDON second led should be on and so on. Similarly turn off the 1st led with FIRSTOFF
command and so on.

Home Assignment
1. Make a smart security system if the distance of ultrasonic sensor is less than 3 foot and at the same
time PIR sensor detects the motion so BUZZ the alarm.

Connect the Buzzer positive terminal to the Arduino pin 2 and the negative terminal to
the Gnd. Connect the VCC pin of ultrasonic to +5v pin and the Gnd to the
ground.Connect trig pin to pin 10 and echo pin to pin 9.

int const trigPin = 10;


int const echoPin = 9;
int const buzzPin = 2;
void setup()
{
pinMode(trigPin, OUTPUT); // trig pin will have pulses output
pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}
void loop()
{
// Duration will be the input pulse width and distance will be the distance to the
obstacle in centimeters
int duration, distance;
// Output pulse with 1ms width on trigPin
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)
if (distance <= 50 && distance >= 0) {
// Buzz
digitalWrite(buzzPin, HIGH);
} else {
// Don't buzz
digitalWrite(buzzPin, LOW);
}

10
Analogue and Digital Sensors Lab 3
Observations:

In this lab we learned to write program in C++ for arduino. To write we first

learned about the we of Compound and, a Program Comparison boolean

operators. Operators play major role in programming they tell the compiter to

perform certain mathematical/logical functions. Furthermore, we learned to

the serial monitor for Serial communication from either PC-Arduino or

Arduino-Pc. To implement the use of serial monitor we wrote a program

which turned LED ON/OFF through learned about Can entered command. At

last Three kind of sensors that be connected to Arduino and programmed as

Per work desire.

Analogue Sensors These device produce analogue output to quantity being

calculated.e.g Sound Sensor

Digital Sensors: These type of sensors. Measures either only. High low state,

eg. PIR sensor

Special Sensors. These sensors require a special library in ardunio for specific

applications e.g Ultrasonic sensor

11

You might also like