1A.
Blink an LED with delay
of 2 seconds (digitalWrite)?
void setup() { Serial.begin(9600);
pinMode(13, OUTPUT); } void loop()
{ digitalWrite(13, HIGH); delay(2000);
digitalWrite(13, LOW); delay(2000); }
1B. Blink 2 LED’s alternatively with a delay of
one second (digitalWrite)?
void setup() { Serial.begin(9600);
pinMode(13, OUTPUT); pinMode(12, OUTPUT);
} void loop() { digitalWrite(13, HIGH);
digitalWrite(12, LOW); delay(1000);
digitalWrite(13, LOW); digitalWrite(12, HIGH);
delay(1000); }
2A. To read the Push Button State
Using Serial Monitor (digitalRead)?
int pushButton = 2; void setup()
{ Serial.begin(9600); pinMode(pushButton, INPUT);
} void loop() { int buttonState =
digitalRead(pushButton); Serial.println(buttonState);
delay(1); }
2B. Turn ON an LED when a button is pressed, OFF
when button is released (digitalRead & digitalWrite)?
int buttonPin = 7; int ledPin = 8; void setup()
{ pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT);
} void loop() { int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) { digitalWrite(ledPin, HIGH);
} else { digitalWrite(ledPin, LOW); } }
3A. Interface potentiometer with Arduino (analogRead)?
int potPin=A0; void setup() { Serial.begin(9600);
} void loop() { int potvalue = analogRead(potPin);
Serial.println(potvalue); delay(1); }
3B. Control the brightness of LED with potentiometer
(analogRead & analogWrite)?
int potPin=A0; int ledPin=13; void setup()
{ Serial.begin(9600); pinMode(potPin, INPUT);
pinMode(ledPin, OUTPUT); } void loop()
{ int potentiometervalue=analogRead(potPin);
int brightness = potentiometervalue/4;
Serial.println(potentiometervalue);
analogWrite(ledPin, brightness); }
4A. Display the value of LDR in serial monitor
(analogRead)?
const int ldrPin = A0; void setup()
{ Serial.begin(9600); pinMode(ldrPin, INPUT);
} void loop() { int ldrStatus = analogRead(ldrPin);
Serial.println(ldrStatus); }
4B. Controlling LED based on intensity of light
using LDR (or) Post Lab- Turning ON/OFF LEDs
and display the message in serial monitor
(analogRead & digitalWrite)?
const int ledPin = 13; const int ldrPin = A0;
void setup() { Serial.begin(9600);
pinMode(ldrPin, INPUT); pinMode(ledPin, OUTPUT);
} void loop() { int ldrStatus = analogRead(ldrPin);
if (ldrStatus <= 200) { digitalWrite(ledPin,HIGH);
Serial.print("Darkness over here: ");
Serial.println(ldrStatus); } else {
digitalWrite(ledPin,LOW);
Serial.print("There is sufficient light: ");
Serial.println(ldrStatus); } }
5. Temperature measurement using LM35 (analogRead
& digitalWrite)
int baselineTemp = 0; int celsius = 0; int fahrenheit = 0;
void setup() { pinMode(A0, INPUT); Serial.begin(9600);
pinMode(2, OUTPUT); pinMode(3, OUTPUT);
pinMode(4, OUTPUT); } void loop() { baselineTemp = 40;
int reading = analogRead(A0);
float voltage = reading * (5.0 / 1024.0); float celsius = voltage * 100;
fahrenheit = ((celsius * 9) / 5 + 32); Serial.print(celsius);
Serial.print(" C, "); Serial.print(fahrenheit); Serial.println(" F");
if (celsius < baselineTemp) { digitalWrite(2, LOW); digitalWrite(3, LOW);
digitalWrite(4, LOW); }
if (celsius >= baselineTemp && celsius < baselineTemp + 10)
{ digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); }
if (celsius >= baselineTemp + 10 && celsius < baselineTemp + 20)
{ digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, LOW); }
if (celsius >= baselineTemp + 30) { digitalWrite(2, HIGH);
digitalWrite(3, HIGH); digitalWrite(4, HIGH); } delay(1000); }
6. Smoke Detection using MQ-2 Gas Sensor (analogRead & digitalWrite)
const int gas_input = A0; int gas = 0; const int led = 6; const int buzzer = 12;
void setup() { pinMode(led, OUTPUT); pinMode(buzzer, OUTPUT);
Serial.begin(9600); } // Function runs repeatedly. void loop()
{ gas = analogRead(gas_input); Serial.println(gas); if (gas > 300)
{ digitalWrite(led, HIGH); digitalWrite(buzzer, HIGH);
} else { digitalWrite(led, LOW); digitalWrite(buzzer, LOW); } delay(100); }
7. Motion Detection using PIR Sensor and Arduino Genuino UNO
(digitalRead & digitalWrite)
int sensorval=0; int pir=3; int buzzer=7; int led = 8; void setup()
{ Serial.begin(9600); pinMode(pir, INPUT); pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT); void loop() { sensorval=digitalRead(3);
if (sensorval == HIGH) { tone(7,800); digitalWrite(8, HIGH);
Serial.println("Motion of object Detected"); } else {
noTone(7); digitalWrite(8, LOW);
Serial.println("No Motion of object"); } delay(1000); }
8. Distance Measurement using Ultrasonic sensor and
Arduino Genuino UNO (digitalWrite)
int trigger = 12; int echo = 13; int led = 8; int buzzer=9;
long duration = 0; int distance = 0; void setup() { Serial.begin(9600);
pinMode(trigger, OUTPUT); pinMode(echo, INPUT);
pinMode(led, OUTPUT); } void loop() { digitalWrite(12, LOW); delay(2);
digitalWrite(12, HIGH); delay(10); digitalWrite(12, LOW);
duration = pulseIn(echo, HIGH); distance = duration * 0.0344 / 2;
Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm");
if(distance100) { digitalWrite(8,HIGH); tone(buzzer,500); } else {
digitalWrite(8,LOW); noTone(buzzer); }delay(1000); }
9. Servo motor (SG-90) control using Arduino Genuino UNO
(#include ; Servo myservo; myservo.attach(9); myservo.write(pos);)
#include <Servo.h> Servo myservo; int pos =0; void setup() {
myservo.attach(9); Serial.begin(9600); } void loop() {
degree for (pos = 0; pos <= 180; pos += 1) { myservo.write(pos);
Serial.print("The Current Position is "); Serial.print(pos);
Serial.println(" Degrees"); delay(500); }
for (pos = 180; pos >= 0; pos -= 1) { myservo.write(pos);
Serial.print("The Current Position is "); Serial.print(pos);
Serial.println(" Degrees"); delay(500); } }