Skip to the content.

Arduino Sample Projects

Settings for Arduino IDE:

Contents

LED Lighting Guide

Wiring Diagram

Wiring Instructions:

Sample Code

#define LED_PIN 7  // The digital pin the LED is connected to

void setup() {
  // Set LED_PIN as an output
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  // Turn the LED on
  digitalWrite(LED_PIN, HIGH);
  delay(1000);  // Wait for 1 second

  // Turn the LED off
  digitalWrite(LED_PIN, LOW);
  delay(1000);  // Wait for 1 second
}

Explanation

With this simple circuit and code, you can easily control an LED with an Arduino.

Sending Information to Serial Monitor When Button is Pressed

Wiring Diagram

Wiring Instructions:

Note 1: The GND and resistor should connect to the same side of the button.

Note 2: The pin connection should be on the opposite side of the button.

Sending Information to Serial Monitor When Button is Pressed

Sample Code

#define BUTTON_PIN 2  // The digital pin the button is connected to

void setup() {
  Serial.begin(9600);  // Start serial communication
  pinMode(BUTTON_PIN, INPUT);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == HIGH) {
    Serial.println("Button Pressed!");
    delay(500);  // Short delay to prevent multiple presses
  }
}

Explanation

Buzzer Activated by Button Press

Wiring Diagram

Wiring Instructions:

Note 1: The GND and resistor should connect to the same side of the button.

Note 2: The pin connection should be on the opposite side of the button.

Sample Code

#define BUTTON_PIN 2  // The digital pin the button is connected to
#define BUZZER_PIN 8  // The digital pin the buzzer is connected to

void setup() {
  pinMode(BUTTON_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(BUZZER_PIN, HIGH);  // Activate the buzzer
  } else {
    digitalWrite(BUZZER_PIN, LOW);   // Deactivate the buzzer
  }
}

Explanation

LED Activated by Button Press

Wiring Diagram

Wiring Instructions:

Sample Code

#define BUTTON_PIN 2  // The digital pin the button is connected to
#define LED_PIN 7     // The digital pin the LED is connected to

void setup() {
  pinMode(BUTTON_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  if (digitalRead(BUTTON_PIN) == HIGH) {
    digitalWrite(LED_PIN, HIGH);  // Turn on the LED
  } else {
    digitalWrite(LED_PIN, LOW);   // Turn off the LED
  }
}

Explanation

Turning on a Lamp with a Single Relay

Wiring Diagram

Wiring Instructions:

Sample Code

#define RELAY_PIN 4  // The digital pin the relay is connected to

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
}

void loop() {
  digitalWrite(RELAY_PIN, HIGH);  // Turn on the lamp
  delay(1000);  // Wait for 1 second
  digitalWrite(RELAY_PIN, LOW);   // Turn off the lamp
  delay(1000);  // Wait for 1 second
}

Explanation

Turning on Lamps with a Dual Relay

Wiring Diagram

Wiring Instructions:

Sample Code

#define RELAY1_PIN 4  // The digital pin the 1st relay is connected to
#define RELAY2_PIN 5  // The digital pin the 2nd relay is connected to

void setup() {
  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(RELAY2_PIN, OUTPUT);
}

void loop() {
  digitalWrite(RELAY1_PIN, HIGH);  // Turn on the 1st lamp
  digitalWrite(RELAY2_PIN, HIGH);  // Turn on the 2nd lamp
  delay(1000);  // Wait for

 1 second

  digitalWrite(RELAY1_PIN, LOW);   // Turn off the 1st lamp
  digitalWrite(RELAY2_PIN, LOW);   // Turn off the 2nd lamp
  delay(1000);  // Wait for 1 second
}

Explanation

: Sets LED_PIN as output.

Lightning Effect with 5 LEDs

Circuit Diagram

Connection Instructions:

Sample Code

#define LED1_PIN 2
#define LED2_PIN 3
#define LED3_PIN 4
#define LED4_PIN 5
#define LED5_PIN 6

void setup() {
  pinMode(LED1_PIN, OUTPUT);
  pinMode(LED2_PIN, OUTPUT);
  pinMode(LED3_PIN, OUTPUT);
  pinMode(LED4_PIN, OUTPUT);
  pinMode(LED5_PIN, OUTPUT);
}

void loop() {
  // Lightning effect
  for (int i = 0; i < 5; i++) {
    digitalWrite(LED1_PIN, HIGH);
    delay(100);
    digitalWrite(LED1_PIN, LOW);
    delay(100);
    
    digitalWrite(LED2_PIN, HIGH);
    delay(100);
    digitalWrite(LED2_PIN, LOW);
    delay(100);
    
    digitalWrite(LED3_PIN, HIGH);
    delay(100);
    digitalWrite(LED3_PIN, LOW);
    delay(100);
    
    digitalWrite(LED4_PIN, HIGH);
    delay(100);
    digitalWrite(LED4_PIN, LOW);
    delay(100);
    
    digitalWrite(LED5_PIN, HIGH);
    delay(100);
    digitalWrite(LED5_PIN, LOW);
    delay(100);
  }
}

Explanation

Here’s the translated text:


16x02 LCD Usage: “Hello World” Example

Connection Diagram

Connection Instructions:

Example Code

#include <LiquidCrystal.h>  // Include the LCD library

// Define LCD pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);  // Initialize the LCD with 16 columns and 2 rows
  lcd.print("Hello, World!");  // Print "Hello, World!" to the LCD
}

void loop() {
  // Code can continue here
}

Explanation

This basic example allows you to display messages on your LCD screen.

Displaying Two Button States on the LCD Screen

Connection Diagram

Connection Instructions:

Example Code

#include <LiquidCrystal.h>  // Include the LCD library

// Define LCD pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Define button pins
#define BUTTON1_PIN 2
#define BUTTON2_PIN 3

void setup() {
  lcd.begin(16, 2);  // Initialize the LCD with 16 columns and 2 rows
  pinMode(BUTTON1_PIN, INPUT_PULLUP);  // Set button pins as input with pull-up resistor
  pinMode(BUTTON2_PIN, INPUT_PULLUP);
}

void loop() {
  int button1State = digitalRead(BUTTON1_PIN);  // Read the state of Button 1
  int button2State = digitalRead(BUTTON2_PIN);  // Read the state of Button 2

  lcd.clear();  // Clear the LCD screen
  lcd.setCursor(0, 0);  // Move to the first row
  lcd.print("Button 1: "); 
  lcd.print(button1State == LOW ? "Pressed" : "Released");  // Display the state of Button 1

  lcd.setCursor(0, 1);  // Move to the second row
  lcd.print("Button 2: ");
  lcd.print(button2State == LOW ? "Pressed" : "Released");  // Display the state of Button 2

  delay(500);  // Wait for 0.5 seconds
}

Explanation

This example shows the states of two buttons on the LCD screen. When the buttons are pressed, “Pressed” will be displayed; when not pressed, “Released” will be shown.

Writing MPU6050 Data to the Serial Monitor

Connection Diagram

Connection Instructions:

Example Code

The following code reads data from the MPU6050 sensor and writes this data to the serial monitor. You will need the Wire and MPU6050 libraries. You can download the MPU6050 library here.

#include <Wire.h>            // Include the I2C library
#include <MPU6050.h>         // Include the MPU6050 library

MPU6050 mpu;  // Create an MPU6050 object

void setup() {
  Serial.begin(9600);  // Start serial communication
  Wire.begin();        // Start I2C communication
  mpu.initialize();    // Initialize the MPU6050

  if (!mpu.testConnection()) {
    Serial.println("MPU6050 connection failed!");
    while (1);  // Infinite loop if connection fails
  }
}

void loop() {
  int16_t ax, ay, az;
  int16_t gx, gy, gz;

  mpu.getAcceleration(&ax, &ay, &az);  // Read accelerometer data
  mpu.getRotation(&gx, &gy, &gz);      // Read gyroscope data

  Serial.print("Acceleration: X=");
  Serial.print(ax);
  Serial.print(" Y=");
  Serial.print(ay);
  Serial.print(" Z=");
  Serial.println(az);

  Serial.print("Gyroscope: X=");
  Serial.print(gx);
  Serial.print(" Y=");
  Serial.print(gy);
  Serial.print(" Z=");
  Serial.println(gz);

  delay(1000);  // Wait for 1 second
}

Explanation

This code reads acceleration and gyroscope data from the MPU6050 sensor and prints it to the serial monitor every second.

Writing Rotary Switch Data to the Serial Monitor

Connection Diagram

A rotary switch typically has 3 or 4 pins and is connected as follows:

Connection Instructions:

Example Code

The following code reads the direction of a rotary switch and whether the button

is pressed, and writes this information to the serial monitor. This example is for a rotary encoder and button combination.

#define ENCODER_CLK_PIN 3  // CLK pin connected to this digital pin
#define ENCODER_DT_PIN 4   // DT pin connected to this digital pin
#define BUTTON_PIN 5       // SW pin connected to this digital pin (if present)

volatile int encoderPos = 0;  // Encoder position
int lastEncoderPos = 0;       // Last read encoder position
bool buttonPressed = false;   // Button state

void setup() {
  Serial.begin(9600);          // Start serial communication
  pinMode(ENCODER_CLK_PIN, INPUT);
  pinMode(ENCODER_DT_PIN, INPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(ENCODER_CLK_PIN), handleEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButton, FALLING);
}

void loop() {
  if (encoderPos != lastEncoderPos) {
    if (encoderPos > lastEncoderPos) {
      Serial.println("Turned right");
    } else {
      Serial.println("Turned left");
    }
    lastEncoderPos = encoderPos;
  }

  if (buttonPressed) {
    Serial.println("Pressed");
    buttonPressed = false;
  }

  delay(100);  // Wait for 0.1 seconds
}

void handleEncoder() {
  int dtState = digitalRead(ENCODER_DT_PIN);
  if (dtState == HIGH) {
    encoderPos++;
  } else {
    encoderPos--;
  }
}

void handleButton() {
  buttonPressed = true;
}

Explanation

This code detects the rotary encoder’s direction and button presses, and writes this information to the serial monitor.