6 Arduino projects with corresponding code snippets

1.LED Blink:

This classic Arduino project blinks an LED connected to a digital pin of the Arduino board. Here’s the code:

const int ledPin = 13; // LED connected to digital pin 13

void setup() {

  pinMode(ledPin, OUTPUT); // Set the LED pin as an output


void loop() {

  digitalWrite(ledPin, HIGH); // Turn the LED on

  delay(1000); // Wait for 1 second

  digitalWrite(ledPin, LOW); // Turn the LED off

  delay(1000); // Wait for 1 second


2.Ultrasonic Distance Measurement:

This project uses an ultrasonic sensor to measure the distance to an object and displays it on the serial monitor. Here’s the code:

const int trigPin = 2; // Trigger pin of the ultrasonic sensor

const int echoPin = 3; // Echo pin of the ultrasonic sensor

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() {

  long duration, distance;


  digitalWrite(trigPin, LOW);


  digitalWrite(trigPin, HIGH); // Send a 10us pulse


  digitalWrite(trigPin, LOW);


  duration = pulseIn(echoPin, HIGH); // Measure the pulse duration from the echo pin


  distance = duration * 0.034 / 2; // Calculate the distance in centimeters


  Serial.print(“Distance: “);


  Serial.println(” cm”);


  delay(500); // Wait for 0.5 seconds before taking the next measurement


3.Servo Motor Control:
This project controls a servo motor’s position using a potentiometer. The servo motor moves based on the potentiometer’s position. Here’s the code:
#include <Servo.h>

const int potPin = A0; // Potentiometer connected to analog pin 0
const int servoPin = 9; // Servo motor control pin

Servo servo;

void setup() {
  servo.attach(servoPin); // Attach the servo to the specified pin

void loop() {
  int angle = map(analogRead(potPin), 0, 1023, 0, 180); // Map the potentiometer value to servo angle range (0-180)
  servo.write(angle); // Set the servo position based on the potentiometer value
  delay(15); // Add a small delay for smooth servo movement
4.Temperature and Humidity Monitoring with DHT11 Sensor:
This project uses a DHT11 sensor to measure temperature and humidity and displays the readings on the serial monitor. Here’s the code:
#include <DHT.h>

#define DHTPIN 2    // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT sensor type


void setup() {
  Serial.begin(9600);   // Initialize serial communication
  dht.begin();          // Initialize DHT sensor

void loop() {
  float temperature = dht.readTemperature();    // Read temperature in Celsius
  float humidity = dht.readHumidity();          // Read relative humidity

  Serial.print(“Temperature: “);
  Serial.print(” °C”);

  Serial.print(”  Humidity: “);
  Serial.println(” %”);

  delay(2000);   // Wait for 2 seconds before taking the next reading
5.PIR Motion Sensor Alarm:
This project uses a PIR (Passive Infrared) motion sensor to detect movement and triggers an alarm using a buzzer or LED. Here’s the code:
int pirPin = 2;       // PIR sensor output pin
int ledPin = 13;      // LED pin for alarm indication

void setup() {
  pinMode(pirPin, INPUT);    // Set PIR pin as an input
  pinMode(ledPin, OUTPUT);   // Set LED pin as an output
  Serial.begin(9600);        // Initialize serial communication

void loop() {
  int pirStatus = digitalRead(pirPin);   // Read the PIR sensor status

  if (pirStatus == HIGH) {
    digitalWrite(ledPin, HIGH);   // Turn on the LED
    Serial.println(“Motion detected!”);
    delay(1000);   // Delay for 1 second
  } else {
    digitalWrite(ledPin, LOW);    // Turn off the LED
6.LCD Display with Button Control:
This project displays a message on an LCD screen and allows users to control the display using buttons. Here’s the code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // Initialize the LCD object

int buttonPin = A0;    // Button pin
int buttonState = 0;   // Current button state
int count = 0;         // Counter variable

void setup() {
  lcd.begin(16, 2);    // Set the LCD dimensions
  pinMode(buttonPin, INPUT);  // Set button pin as an input
  lcd.print(“Press Button”);  // Display initial message

void loop() {
  buttonState = digitalRead(buttonPin);  // Read button state

  if (buttonState == HIGH) {
    count++;   // Increment the counter

    lcd.clear();   // Clear the LCD display
    lcd.setCursor(0, 0);   // Set the cursor to the first row
    lcd.print(“Button Pressed”);   // Display message
    lcd.setCursor(0, 1);   // Set the cursor to the second row
    lcd.print(“Count: ” + String(count));   // Display the counter value
    delay(1000);   // Delay for 1 second