Professional Documents
Culture Documents
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
Boolean Operators
Assume variable A holds 10 and variable B holds 20 then
Operator Operator
Description Example
name simple
Compound Operators
Assume variable A holds 10 and variable B holds 20 then -
Operator Operator
Description Example
name simple
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
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
void setup() {
pinMode(buz, OUTPUT);
void loop(){
6
Analogue and Digital Sensors Lab 3
digitalWrite(led, HIGH); // turn LED ON
digitalWrite(buz, HIGH);
else {
digitalWrite(buz, LOW);
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;}
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.
10
Analogue and Digital Sensors Lab 3
Observations:
In this lab we learned to write program in C++ for arduino. To write we first
operators. Operators play major role in programming they tell the compiter to
which turned LED ON/OFF through learned about Can entered command. At
Digital Sensors: These type of sensors. Measures either only. High low state,
Special Sensors. These sensors require a special library in ardunio for specific
11