Class: Introduction to Arduino Sensors
1. Serial Monitor:
Introductory Explanation: The serial monitor is a tool used for communication between the Arduino board and a computer. It allows you to send data from the Arduino to your computer for debugging and monitoring.
Pins Introduction: No specific pins are used for the serial monitor. However, it requires connection to your computer via USB.
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(A0); // Read analog input from pin A0
Serial.println(sensorValue); // Print sensor value to serial monitor
delay(1000); // Wait for 1 second
}
2. Servo Motor:
Introductory Explanation: A servo motor is a rotary actuator that allows for precise control of angular position. It is commonly used in robotics and automation projects for controlling the movement of mechanical parts.
Pins Introduction: Servo motors typically have three pins: power (+5V), ground (GND), and control signal (usually connected to a PWM pin). In the example, pin 9 is used for the control signal.
#include <Servo.h>
Servo myser;
int i;
void setup() {
myser.attach(10);
}
void loop() {
myser.write(0);
delay(1000);
myser.write(180);
delay(3000);
for (i = 180; i > 0; i=i-1) {
myser.write(i);
delay(200);
}
}
3. IR Sensor:
Introductory Explanation: An IR (Infrared) sensor detects infrared radiation emitted or reflected by objects. It is commonly used for proximity sensing, obstacle detection, and line following in robotics projects.
Pins Introduction: IR sensors usually have three pins: power (+5V), ground (GND), and signal (connected to a digital pin). In the example, pin 7 is used for the signal.
int irPin = 7; // IR sensor connected to digital pin 7
void setup() {
pinMode(irPin, INPUT); // Set IR pin as input
}
void loop() {
int irValue = digitalRead(irPin); // Read IR sensor value
Serial.println(irValue); // Print IR sensor value to serial monitor
delay(1000); // Wait for 1 second
}
IR sensor with LED

/*** Arduino with IR Sensor ***/
int SensorPin = 2;
int OutputPin = 13;
void setup() {
pinMode(OutputPin, OUTPUT);
pinMode(SensorPin, INPUT);
Serial.begin(9600);
}
void loop() {
int SensorValue = digitalRead(SensorPin);
Serial.print("SensorPin Value: ");
Serial.println(SensorValue);
delay(1000);
if (SensorValue==LOW){ // LOW MEANS Object Detected
digitalWrite(OutputPin, HIGH);
}
else
{
digitalWrite(OutputPin, LOW);
}
}
4. Ultrasonic Sensor:
Introductory Explanation: An ultrasonic sensor measures distance by emitting ultrasonic waves and calculating the time it takes for the waves to bounce back after hitting an object. It is commonly used in robotics for obstacle avoidance and distance measurement.
Pins Introduction: Ultrasonic sensors typically have four pins: VCC (power), GND (ground), TRIG (trigger), and ECHO (echo). In the example, pins 9 and 10 are used for TRIG and ECHO, respectively