0% found this document useful (0 votes)
33 views

Arduino Codes Marked

The document provides 15 Arduino programs covering topics like blinking an LED, printing to the serial monitor, reading inputs from buttons, potentiometers, sensors, and controlling outputs to motors, displays. The programs demonstrate basic Arduino concepts and interfacing with common components.

Uploaded by

Md. Mahruf Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Arduino Codes Marked

The document provides 15 Arduino programs covering topics like blinking an LED, printing to the serial monitor, reading inputs from buttons, potentiometers, sensors, and controlling outputs to motors, displays. The programs demonstrate basic Arduino concepts and interfacing with common components.

Uploaded by

Md. Mahruf Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

01) Arduino Program to Blink an LED.

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

button_state = digitalRead(3); // 0 if the button is not pushed, 1 if the button is pushed


if (button_state == 0)
{
digitalWrite(4, LOW); // Turns off the LED if button is not pushed
}
else
{
digitalWrite(4,HIGH); // Turns on the LED if the button is pushed
}
}
04) Arduino Program to understand the operation of
Potentiometer. (LED Brightness Control with
potentiometer).

int val;
int mapping;
void setup() {
pinMode(A0, INPUT); // Potentiometer Pin
pinMode(4, OUTPUT); // LED Pin

void loop() {

val = analogRead(A0); // Potentiometer Reading


mapping = map(val , 0,1023,0,255);
analogWrite(4, mapping);

}
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>

Servo myservo; // create servo object to control a servo


// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

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.

// Define the pin for the soil moisture sensor


int soilMoisturePin = A0; // Analog pin

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);

// Print the moisture percentage to the Serial Monitor


Serial.print("Moisture Percentage: ");
Serial.print(moisturePercentage);
Serial.println("%");

delay(1000); // Wait for a moment before taking the next measurement


}
10) Arduino program to read the values from a IR sensor.
// Define the pin for the IR sensor
int irSensorPin = 2; // Digital pin

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);

// Print the state to the Serial Monitor


if (obstacleDetected) {
Serial.println("Obstacle detected!");
} else {
Serial.println("No obstacle detected.");
}

delay(1000); // Wait for a moment before taking the next measurement


}
11) Arduino program to control the speed of a dc motor.
// Define the pin for controlling the motor
int motorPin = 9; // PWM pin

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
}

// Wait for a moment at maximum speed


delay(1000);

// Vary the motor speed from maximum to minimum


for (int speed = 255; speed >= 0; speed--) {
analogWrite(motorPin, speed); // Set the motor speed
delay(10); // Adjust the delay to control the speed ramp-down rate
}

// Wait for a moment at minimum speed


delay(1000);
}
12) Arduino program to make dark and light sensor using LDR.
// Define the pin for the LDR sensor
int ldrPin = A0; // Analog pin

void setup() {
Serial.begin(9600); // Initialize serial communication
}

void loop() {
// Read the value from the LDR sensor
int ldrValue = analogRead(ldrPin);

// Print the raw sensor value to the Serial Monitor


Serial.print("LDR Value: ");
Serial.println(ldrValue);

// Check if it's dark or light based on the LDR value


if (ldrValue < 500) {
Serial.println("It's dark.");
} else {
Serial.println("It's light.");
}

delay(1000); // Wait for a moment before taking the next measurement


}
13) Arduino program to connect a Bluetooth module to
Arduino.
#include <SoftwareSerial.h>
// Define the RX and TX pins for the Bluetooth module
int bluetoothRx = 2; // RX pin of Bluetooth module connected to Arduino's digital pin 2
int bluetoothTx = 3; // TX pin of Bluetooth module connected to Arduino's digital pin 3
SoftwareSerial bluetooth(bluetoothRx, bluetoothTx); // Create a SoftwareSerial object
void setup() {
Serial.begin(9600); // Initialize Serial communication for debugging
bluetooth.begin(9600); // Initialize Bluetooth communication
}
void loop() {
// Check if data is available to read from Bluetooth module
if (bluetooth.available()) {
char receivedChar = bluetooth.read(); // Read the incoming byte from Bluetooth

// Print the received character to the Serial Monitor


Serial.print("Received character: ");
Serial.println(receivedChar);

}
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

// Pin mapping for the 7-segment display


int segmentA = 2;
int segmentB = 3;
int segmentC = 4;
int segmentD = 5;
int segmentE = 6;
int segmentF = 7;
int segmentG = 8;

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();
}

// Function to display digit 0


void zero() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, HIGH);
digitalWrite(segmentF, HIGH);
digitalWrite(segmentG, LOW);
delay(del);
}

// Function to display digit 1


void one() {
digitalWrite(segmentA, LOW);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, LOW);
digitalWrite(segmentE, LOW);
digitalWrite(segmentF, LOW);
digitalWrite(segmentG, LOW);
delay(del);
}

// Function to display digit 2


void two() {
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, LOW);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, HIGH);
digitalWrite(segmentF, LOW);
digitalWrite(segmentG, HIGH);
delay(del);
}

// Define functions three() to nine() similarly for digits 3 to 9

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);
}

You might also like