WORKING WITH IR-SENSOR
IR-SENSOR WITH LED
i. COMPONENTS REQUIRED:
ARDUINO UNO
IR-SENSOR
LED
JUMPER WIRES
ii. CIRCUIT DIAGRAM
Iii. SOURCE CODE
code 1: Analog pins
int led = 13;
int sensor;
void setup() {
pinMode(A0, INPUT); // analog pin
pinMode(led, OUTPUT);
}
void loop() {
sensor = digitalRead(A0);//reading analog pin A0
if(sensor == LOW){
digitalWrite(led, HIGH);//led on
}
else{
digitalWrite(led, LOW);//led off
}
}
int sensor;
void setup() {
pinMode(A0, INPUT); // analog pin
pinMode(led, OUTPUT);
}
void loop() {
sensor = digitalRead(A0);//reading analog pin A0
if(sensor == LOW){
digitalWrite(led, HIGH);//led on
}
else{
digitalWrite(led, LOW);//led off
}
}
code 2: Digital pins
int led = 13;
int sensor;
void setup() {
pinMode(5, INPUT); // digital pin
pinMode(led, OUTPUT);
}
void loop() {
sensor = digitalRead(5);//reading digital pin 5
if(sensor == LOW){
digitalWrite(led, HIGH);//led on
}
else{
digitalWrite(led, LOW);//led off
}
}
int sensor;
void setup() {
pinMode(5, INPUT); // digital pin
pinMode(led, OUTPUT);
}
void loop() {
sensor = digitalRead(5);//reading digital pin 5
if(sensor == LOW){
digitalWrite(led, HIGH);//led on
}
else{
digitalWrite(led, LOW);//led off
}
}
IR-SENSOR WITH SERVO
i. COMPONENTS REQUIRED:
ARDUINO UNO
IR-SENSOR
SERVO
JUMPER WIRES
ii. CIRCUIT DIAGRAM
iii. SOURCE CODE
code 1: Analog pins
#include<Servo.h>
Servo myservo;
int sensor;
void setup() {
pinMode(A0, INPUT); // analog pin A0
myservo.attach(10); // servo connected to pin 10
}
void loop() {
sensor = digitalRead(A0);//reading analog pin A0
if(sensor == LOW){
myservo.write(180); // rotate 180 degrees
}
else{
myservo.write(0); // rotate return back to 0 degrees
}
}
Servo myservo;
int sensor;
void setup() {
pinMode(8, INPUT); // digital pin 8
myservo.attach(10); // servo connected to pin 10
}
void loop() {
sensor = digitalRead(8);//reading analog pin 8
if(sensor == LOW){
myservo.write(180); // rotate 180 degrees
}
else{
myservo.write(0); // rotate return back to 0 degrees
}
}
C. IR-SENSOR WITH BUZZER AND SERVO
CIRCUIT:
#include<Servo.h>
Servo myservo;
int sensor;
int BUZZER = 10;
void setup() {
pinMode(A0, INPUT); // ANALOG pin A0
myservo.attach(6); // servo connected to pin 6
pinMode(BUZZER, OUTPUT); // DIITAL pin 10
}
void loop() {
digitalWrite(BUZZER, LOW);
sensor = digitalRead(8);//reading analog pin 8
if(sensor == LOW){
myservo.write(180); // rotate 180 degrees
digitalWrite(BUZZER, HIGH);
delay(1000);//BUZZ FOR 1 SECOND
}
else{
myservo.write(0); // rotate return back to 0 degrees
digitalWrite(BUZZER, HIGH);
delay(1000);//BUZZ FOR 1 SECOND
}
}
0 Comments
Please do not Enter any Spam Link in the Comment Box
Emoji