Arduino Codes Marked
Arduino Codes Marked
void setup() {
pinMode(3, OUTPUT);
void loop() {
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3,LOW);
delay(1000);
}
02) Arduino Program to print “Hello World” in the Serial
Monitor.
void setup() {
Serial.begin(9600);
void loop() {
Serial.println("Hello World");
delay(5000);
}
03) Arduino Program to understand the operation of the
push button.
int button_state = 0;
void setup() {
pinMode(3, INPUT); // Button Pin
pinMode(4, OUTPUT); // LED Pin
void loop() {
int val;
int mapping;
void setup() {
pinMode(A0, INPUT); // Potentiometer Pin
pinMode(4, OUTPUT); // LED Pin
void loop() {
}
05) Arduino Program to print temperature reading from
LM35 Sensor.
int voltage;
int x;
int temp;
void setup() {
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop() {
voltage = analogRead(A0);
x = (5/1023)*voltage;
temp = 100*x;
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("C");
}
06) Arduino Program to control a servo motor.
#include <Servo.h>
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
07) Arduino Program to read the value from a PIR
(Motion Sensor)
int val;
void setup() {
Serial.begin(8600);
pinMode(2,INPUT);
}
void loop() {
val = digitalRead(2);
if (val == 0)
{
Serial.print("Motion Detected");
val = 1;
}
else if (val == 1)
{
Serial.print("Motion Ended");
val = 0;
}
}
08) Arduino Program to measure distance using an
ultrasonic sensor.
// Define the pins for the ultrasonic sensor
int trigPin = 9; // Trigger pin
int echoPin = 10; // Echo pin
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(trigPin, OUTPUT); // Set the trigger pin as an output
pinMode(echoPin, INPUT); // Set the echo pin as an input
}
void loop() {
// Trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the time taken for the sound wave to return
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
float distance_cm = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
delay(1000); // Wait for a moment before taking the next measurement
}
09) Arduino program to read the values from a soil moisture
sensor.
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read the soil moisture sensor value
int moistureValue = analogRead(soilMoisturePin);
// Map the sensor value to a moisture percentage (adjust the mapping according to your
sensor)
int moisturePercentage = map(moistureValue, 0, 1023, 0, 100);
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(irSensorPin, INPUT); // Set the IR sensor pin as an input
}
void loop() {
// Read the state of the IR sensor
int obstacleDetected = digitalRead(irSensorPin);
void setup() {
pinMode(motorPin, OUTPUT); // Set the motor pin as an output
}
void loop() {
// Vary the motor speed from minimum to maximum
for (int speed = 0; speed <= 255; speed++) {
analogWrite(motorPin, speed); // Set the motor speed
delay(10); // Adjust the delay to control the speed ramp-up rate
}
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read the value from the LDR sensor
int ldrValue = analogRead(ldrPin);
}
14) Arduino program for interfacing 16x2 LCD display and
printing the word “EEE” in the LCD Display.
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
lcd.begin(16,2);
void loop() {
lcd.setCursor(2,0);
lcd.print("EEE");
}
15) Arduino Program for interfacing a 7-segment display with
Arduino and showing the digit 0 to 9 in the 7-segment display.
// Define the delay time between displaying each digit
#define del 1000
void setup() {
// Initialize segment pins as outputs
pinMode(segmentA, OUTPUT);
pinMode(segmentB, OUTPUT);
pinMode(segmentC, OUTPUT);
pinMode(segmentD, OUTPUT);
pinMode(segmentE, OUTPUT);
pinMode(segmentF, OUTPUT);
pinMode(segmentG, OUTPUT);
}
void loop() {
// Display digits 0 to 9 sequentially
zero();
one();
two();
three();
four();
five();
six();
seven();
eight();
nine();
}
void three() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, LOW);
digitalWrite(segmentF, LOW);
digitalWrite(segmentG, HIGH);
delay(del);
}
void four() {
digitalWrite(segmentA, LOW);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, LOW);
digitalWrite(segmentE, LOW);
digitalWrite(segmentF, HIGH);
digitalWrite(segmentG, HIGH);
delay(del);
}
void five() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, LOW);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, LOW);
digitalWrite(segmentF, HIGH);
digitalWrite(segmentG, HIGH);
delay(del);
}
void six() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, LOW);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, HIGH);
digitalWrite(segmentF, HIGH);
digitalWrite(segmentG, HIGH);
delay(del);
}
void seven() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, LOW);
digitalWrite(segmentE, LOW);
digitalWrite(segmentF, LOW);
digitalWrite(segmentG, LOW);
delay(del);
}
void eight() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, HIGH);
digitalWrite(segmentF, HIGH);
digitalWrite(segmentG, HIGH);
delay(del);
}
void nine() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, LOW);
digitalWrite(segmentF, HIGH);
digitalWrite(segmentG, HIGH);
delay(del);
}