Fis Lab Exam

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

LDR:-

Code:-

// Interfacing Arduino uno with LDR sensor


const int ledPin = 11; // digital pin 5
const int ldrPin = A0; // analog pin 0
void setup()
{
// The setup() function will only run once, after each powerup
or reset of the Arduino
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Here LED is determined as an
output or an indicator.
pinMode(ldrPin, INPUT); // Here LDR sensor is determined as
input.
}
void loop()
{
// Void loop is ran again and again and contains main code.
int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 200)
{
digitalWrite(ledPin, HIGH); // If LDR senses darkness led pin
high that means led
Serial.print("Darkness over here,turn on the LED :");
Serial.println(ldrStatus);
}
else
{
digitalWrite(ledPin, LOW); //If LDR senses light led pin lowthat
means led will stop
Serial.print("There is sufficient light , turn off the LED : ");
Serial.println(ldrStatus);
}
}

Gas sensor with busser:-

Pins : D0 : 0
AO : 2
int gasSensor = A0; // Assuming the gas sensor is connected to
analog pin A0
int threshold_LED = 2;

void setup()
{
pinMode(gasSensor, INPUT);
pinMode(threshold_LED, OUTPUT);
digitalWrite(threshold_LED, HIGH);
Serial.begin(9600);
}

void loop()
{
int gasIn = analogRead(gasSensor);
Serial.print("gas= ");
Serial.println(gasIn);

if (gasIn >= 160) {


digitalWrite(threshold_LED, HIGH);
} else {
digitalWrite(threshold_LED, LOW);
}

delay(1000);
}
PUSH BUTTON:-

CODE:-
const int ledpin =13;
const int buttonpin=2;
int buttonstate=0;
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
pinMode(buttonpin,INPUT);
}
void loop ()
{
buttonstate=digitalRead(buttonpin);
if ( buttonstate==HIGH){
digitalWrite(ledpin, HIGH);
Serial.println("led is on");
delay(1000);
}
else

digitalWrite(ledpin,LOW);
Serial.println("led is off");
delay(1000);
}
POTENTIOMETER:-
CODE:-
#define LED_PIN 11
#define POTENTIOMETER_PIN A0
void setup()
{
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
int potentiometerValue = analogRead(POTENTIOMETER_PIN);
int brightness = potentiometerValue / 4;
analogWrite(LED_PIN, brightness);
}

TEMPERATURE SENSOR:-

CODE:-

#define sensorPin A0
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(sensorPin);
float voltage = reading * (5000 / 1024.0);
float temperature = voltage / 10;
float f=1.8*temperature+32;
Serial.println("temperature in fahernheit");
Serial.println(f);
delay(1000);
}

BLINKING OF LIGHT WITH MULTIPLE LED:-

CODE:-
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(1000);
digitalWrite(11, HIGH);
delay(1000);
digitalWrite(11, LOW);
delay(1000);
}

PIR

int inputPin = 2; // choose the input pin (for PIR sensor)


int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}

Ultrasonic sensor

#define echoPin 2
#define trigPin 3
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
Serial.println("Distance measurement using Arduino Uno.");
delay(500);
}
void loop()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite( trigPin,HIGH); // turn on the Trigger to generate
pulse
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
// If pulse reached the receiver echoPin
// become high Then pulseIn() returns the
// time taken by the pulse to reach the
// receiver
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0344 / 2;
Serial.print("Distance: ");
Serial.print( distance); // Print the output in serial monitor
Serial.println(" cm");
delay(100);
}
IR SENSOR
OUT =2

int irSensorPin = 2; // Pin connected to the OUT


pin of the IR sensor
int ledPin = 3; // Pin connected to the LED

void setup() {
pinMode(irSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
int sensorValue = digitalRead(irSensorPin);

if (sensorValue == LOW) {
Serial.println("IR Sensor activated!");
digitalWrite(ledPin, HIGH); // Turn on the LED
// You can add additional actions here when
the sensor is activated
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}

delay(100); // Adjust the delay as needed


}

You might also like