Practical
Practical
Experiment 1
CODE:
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
Loop: The loop ( ) function does precisely what its name suggests ,
and loop consecutively , allowing program to change and respond.It’s
used to actively control the Ardiuno board.
Experiment 2
CODE:
void setup() {
pinMode(13,OUTPUT);
}
void loop () {
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(4000);
}
Het Patel 20031101611039
Experiment 3
LED 1: will be OFF for 80% of time and ON for 20% of time.
LED 2: will be OFF for 20% of time and ON for 80% of time.
Het Patel 20031101611039
CODE:
void setup() {
pinMode(13,OUTPUT);
pinMode(7 ,OUTPUT);
}
void loop () {
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(4000);
Het Patel 20031101611039
digitalWrite(7,HIGH);
delay(4000);
digitalWrite(7,LOW);
delay(1000);
}
Experiment 4
CODE:
int led = 9;
int brightness = 0;
Het Patel 20031101611039
int fadeAmount = 5;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
On which output pins of Arduino UNO can we run program for fading
LED? Explain.
➔ On an Arduino Uno, you can run a program for fading an LED on any
of the digital pins marked with a "~" symbol (e.g., pins 3, 5, 6, 9, 10,
and 11). These pins support Pulse Width Modulation (PWM), which
allows you to control the intensity or brightness of an LED.
Experiment 5
CODE:
int led = 9;
int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(blinkLED, HIGH);
delay(500);
digitalWrite(blinkLED, LOW);
delay(500);
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30);
}
Experiment 6
CODE:
void setup() {
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], HIGH);
}
delay(500);
for (int i = 0; i < numLEDs; i++) {
digitalWrite(ledPins[i], LOW);
}
delay(500);
}
Experiment 7
CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(11, HIGH);
delay(2000);
digitalWrite(9, HIGH);
delay(2000);
digitalWrite(7, HIGH);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9, LOW);
digitalWrite(7, LOW);
delay(2000);
}
Het Patel 20031101611039
Experiment 8
Assume four LEDs are connected to Pins 13, 11, 9 and 7 of Arduino
UNO. Write a
program to turn on one LED at a time.
Second 1: LED on Pin 13 turns on, all other LEDs are off
Second 2: LED on Pin 13 turns off, LED on pin 11 turns on, other
LEDs are off.
Second 3: LED on Pin 11 turns off, LED on pin 9 turns on, other LEDs
are off
Second 4: LED on Pin 9 turns off, LED on pin 7 turns on, other LEDs
are off.
Second 5: LED on Pin 7 turns off, LED on pin 13 turns on, other LEDs
are off.
CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9 , OUTPUT);
pinMode(7 , OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, HIGH);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9 , HIGH);
digitalWrite(7 , LOW);
delay(2000);
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , HIGH);
delay(2000);
digitalWrite(13, HIGH);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
delay(2000);
}
Het Patel 20031101611039
Experiment 9
1. Use IDE program: File > Examples > Digital > Button
2. Connect external LED on Pin 13, use current limiting resister.
3. Connect push button switch on Pin 2 of Arduino UNO.
Connect a register in series with switch.
4. Run program mentioned in step 1 and observe that when switch is
pressed, the LED turns off.
Het Patel 20031101611039
CODE:
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
How will you generate two states (Logic low and logic high) using
push button switch?
➔ To generate two states (logic low and logic high) using a push-button
switch with an Arduino, you can use the internal pull-up resistor and
implement a simple button state detection code. Here's a basic
example:
Het Patel 20031101611039
3) In your Arduino sketch, set the pinMode for the digital pin connected
to the push-button switch as INPUT_PULLUP. This activates the
internal pull-up resistor, which will pull the pin to a HIGH logic level
when the button is not pressed.
5) Based on the state of the pin, you can perform certain actions or
change the state of other components.
Experiment 10
CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9 , OUTPUT);
pinMode(7 , OUTPUT);
pinMode(4 , INPUT_PULLUP);
}
void loop()
{
byte buttonState = digitalRead(4);
if (buttonState == LOW) {
digitalWrite(13, HIGH);
digitalWrite(11, HIGH);
digitalWrite(9 , HIGH);
digitalWrite(7 , HIGH);
}
else {
digitalWrite(13, LOW);
digitalWrite(11, LOW);
digitalWrite(9 , LOW);
digitalWrite(7 , LOW);
}
}
Het Patel 20031101611039
Experiment 11
(c) Measure the value of LDR for various light intensities. Observe how
resistance changes with light intensity.
Het Patel 20031101611039
1. Disconnect the Arduino from any power source and remove any
connections to external components.
2. Set your multimeter to the continuity mode (the symbol for continuity
is usually depicted as a sound wave or a diode).
3. Turn on the multimeter and touch the probe tips together. You should
hear a beep or see the continuity indicator activate. This ensures that
the continuity function is working correctly.
4. Place one probe of the multimeter on the ground (GND) pin of the
Arduino.
5. Use the other probe to touch the suspected pins or traces that may
be involved in the short circuit.
6. If there is a short circuit, the multimeter will indicate continuity by
beeping or showing a "short" indication on the display.
7. Move the probe along the suspected area of the short circuit to
pinpoint the exact location where the short is occurring.
Het Patel 20031101611039
Experiment 12
(A) Connect five resistors in series.
(B) Connect one end to +5 V from Arduino and
other end to Ground Pin of Arduino.
(C) Measure voltage values on different
combinations of Resistors.
Het Patel 20031101611039
Het Patel 20031101611039
Het Patel 20031101611039
Experiment 13
Experiment 14
CODE:
int sensorPin = A0;
int ledPin = 13;
int sensorValue = 0
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin,HIGH);
delay(sensorValue);
digitalWrite(13,LOW);
delay(sensorValue);
}
Het Patel 20031101611039
analogRead: Reads the value from the specified analog pin. Arduino
boards contain a multichannel, 10-bit analog to digital converter. This
means that it will map input voltages between 0 and the operating
voltage(5V or 3.3V) into integer values between 0 and 1023.
Schematic view:
Het Patel 20031101611039
Experiment 15
CODE:
void loop()
{
int sensorValue = analogRead(sensorPin);
int brightness = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, brightness);
}
Het Patel 20031101611039
Experiment 16
CODE:
int sensorPin = A0;
int ledPin = 12;
int sensorValue = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin,HIGH);
delay(sensorValue);
digitalWrite(ledPin,LOW);
delay(sensorValue);
}
Het Patel 20031101611039
➔ In the graph, the y-axis represents resistance (R), and the x-axis
represents light intensity. As light intensity increases, the resistance
of the LDR decreases. Conversely, as light intensity decreases, the
resistance of the LDR increases.
➔ When using an LDR with an Arduino, you can connect it in a voltage
divider circuit, as described in the previous answer. By measuring the
voltage at the junction point of the LDR and the fixed resistor, you can
Het Patel 20031101611039
Experiment 17
CODE:
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin);
int bright = sensorValue/4;
analogWrite(ledPin,bright);
}
Het Patel 20031101611039
Experiment 18 , 19 ,20
Can’t able to perform in Ardiuno IDE.
Het Patel 20031101611039
Experiment 21
CODE:
int a = 6;
int b = 7;
int c = 8;
int d = 3;
int e = 2;
int f = 5;
int g = 4;
void setup() {
pinMode (a, OUTPUT);
pinMode (b, OUTPUT);
pinMode (c, OUTPUT);
pinMode (d, OUTPUT);
pinMode (e, OUTPUT);
pinMode (f, OUTPUT);
pinMode (g, OUTPUT);
}
void loop() {
nine();
delay(2000);
eight();
delay(2000);
seven();
delay(2000);
six();
delay(2000);
five();
delay(2000);
four();
delay(2000);
three();
Het Patel 20031101611039
delay(2000);
two();
delay(2000);
one();
delay(2000);
zero();
delay(2000);
}
void zero() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, LOW);
}
void one() {
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}
void two() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, LOW);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, LOW);
digitalWrite (g, HIGH);
}
Het Patel 20031101611039
void three() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, HIGH);
}
void four() {
digitalWrite (a, LOW);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}
void five() {
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}
void six() {
digitalWrite (a, HIGH);
digitalWrite (b, LOW);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
Het Patel 20031101611039
}
void seven() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, LOW);
digitalWrite (e, LOW);
digitalWrite (f, LOW);
digitalWrite (g, LOW);
}
void eight() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, HIGH);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}
void nine() {
digitalWrite (a, HIGH);
digitalWrite (b, HIGH);
digitalWrite (c, HIGH);
digitalWrite (d, HIGH);
digitalWrite (e, LOW);
digitalWrite (f, HIGH);
digitalWrite (g, HIGH);
}
Experiment 22
Het Patel 20031101611039
CODE:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
Het Patel 20031101611039
Experiment 23
CODE:
Het Patel 20031101611039
int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop() {
int buttonState = digitalRead(pushButton);
Serial.println(buttonState);
delay(1);
}
Experiment 24
Het Patel 20031101611039
CODE:
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
}
Het Patel 20031101611039
Experiment 25
CODE:
int sensorValue = 0;
int outputValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
}
Experiment 26
CODE:
int incomingByte = 0; // for incoming serial data
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop()
{
// send data only when you receive data:
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte - 48, DEC);
}
}
Experiment 27
CODE:
int incomingByte = 0;
int ledPin1=3;
int ledPin2=4;
int ledPin3=5;
void setup(){
Serial.begin(9600);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
}
void loop(){
if(Serial.available()>0);
{
incomingByte = Serial.read();
Serial.print("I recived: ");
Serial.println(incomingByte-48,DEC);
if(incomingByte-48 == 3);
{
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
delay(2000);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
if(incomingByte-48 == 4);
{
digitalWrite(ledPin1,LOW);
Het Patel 20031101611039
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);
delay(2000);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
if(incomingByte-48 == 5);
{
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
delay(2000);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
if(incomingByte-48>5 || incomingByte-48<3);
{
Serial.println("Number is out of Range: ");
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
}
}
Het Patel 20031101611039
Experiment 28
CODE:
int distanceThreshold = 0;
int cm = 0;
int inches = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
distanceThreshold = 350;
cm = 0.01723 * readUltrasonicDistance(7, 6);
inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm, ");
Serial.print(inches);
Serial.println("in");
Het Patel 20031101611039
Experiment 29
CODE:
int distanceThreshold = 0;
int cm = 0;
int inches = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
distanceThreshold = 350;
cm = 0.01723 * readUltrasonicDistance(7, 6);
inches = (cm / 2.54);
Serial.print(cm);
Serial.print("cm, ");
Serial.print(inches);
Serial.println("in");
Het Patel 20031101611039
Experiment 30
CODE:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(){
lcd.begin(16, 2);
lcd.print("19/6/2023");
}
void loop(){
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}
Experiment 32
Het Patel 20031101611039
CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
}
void loop()
{
if (digitalRead(2)== HIGH){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
}
CODE:
void setup()
{
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
}
void loop()
{
if (digitalRead(2)== LOW){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
}