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

FDP Arduino Programs

The document contains four problem statements related to Arduino programming. Each statement includes code snippets for controlling LEDs, measuring distance with an ultrasonic sensor, and controlling a DC motor based on distance readings. The problems range from simple LED blinking to more complex motor control using PWM and ultrasonic sensor data.

Uploaded by

ripfoc92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

FDP Arduino Programs

The document contains four problem statements related to Arduino programming. Each statement includes code snippets for controlling LEDs, measuring distance with an ultrasonic sensor, and controlling a DC motor based on distance readings. The problems range from simple LED blinking to more complex motor control using PWM and ultrasonic sensor data.

Uploaded by

ripfoc92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Problem Statement - 1

(Blinking LEDs in combination with some delay)

Code:
// Variable and pins declared and initialized

int led_red = 8; // the red LED is connected to Pin 8 of the Arduino


int led_yellow = 9; // the yellow LED is connected to Pin 9 of the Arduino
int led_green = 10; // the green LED is connected to Pin 10 of the Arduino

void setup()
// This loop repeats only once
{
// set up all the LEDs as OUTPUT
pinMode(led_red, OUTPUT);
pinMode(led_yellow, OUTPUT);
pinMode(led_green, OUTPUT);

Serial.begin(9600);// begin communication


}

void loop()
// This loop repeats continuously
{
// turn the green LED on and the other LEDs off
digitalWrite(led_red, LOW);
digitalWrite(led_yellow, LOW);
digitalWrite(led_green, HIGH);
Serial.println("Green light is ON");
delay(2000); // wait 2 seconds
// turn the yellow LED on and the other LEDs off
digitalWrite(led_red, LOW);
digitalWrite(led_yellow, HIGH);
digitalWrite(led_green, LOW);
Serial.println("Yellow light is ON");
delay(1000); // wait 1 second

// turn the red LED on and the other LEDs off


digitalWrite(led_red, HIGH);
digitalWrite(led_yellow, LOW);
digitalWrite(led_green, LOW);
Serial.println("Red light is ON");
delay(3000); // wait 3 seconds
}

Link- Problem_Statements_1
Problem Statement - 2
(Measure the distance of an object/wall using the ultrasonic sensor)

Code:
// Pin
const int trigPin = 4; // Trigger pin of ultrasonic sensor
const int echoPin = 5; // Echo pin of ultrasonic sensor

// Variables to store the duration and distance


long duration;
int distance;

void setup() {
// Start serial communication for monitoring
Serial.begin(9600);

// Set the pin modes


pinMode(trigPin, OUTPUT); // Set the trigger pin as output
pinMode(echoPin, INPUT); // Set the echo pin as input
}

void loop() {
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Send a pulse to the trigger pin


digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Pulse duration
digitalWrite(trigPin, LOW);

// Read the pulse duration from the echo pin


duration = pulseIn(echoPin, HIGH);

// Calculate the distance in centimeters


distance = duration * 0.0344 / 2;

// Print the distance to the Serial Monitor


Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}

Link- Problem_statements_2
Problem Statement - 3
(Control the DC motor using the Ultrasonic sensor data (using Digital PIN))

Note: For real-time, we have to use an H-bridge motor driver L298N to control the motor.

Code:
// pin numbers
const int triggerPin = 2; // Trigger pin for ultrasonic sensor
const int echoPin = 4; // Echo pin for ultrasonic sensor
const int motorPin = 12; // Pin to control the DC motor

void setup()
{
// pin modes
pinMode(motorPin, OUTPUT); // Set motor control pin as output
pinMode(triggerPin, OUTPUT); // Set trigger pin as output
pinMode(echoPin, INPUT); // Set echo pin as input

// Start serial communication


Serial.begin(9600);
}

void loop()
{
long duration; // Duration of the pulse from the sensor
long distance; // Calculated distance in cm

// Trigger the ultrasonic sensor


digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

// Read the pulse duration from the echo pin


duration = pulseIn(echoPin, HIGH);

// Convert duration to distance in centimeters


distance = microsecondsToCentimeters(duration);

// If the object is more then 40 cm, turn motor ON


if (distance >= 40)
{
digitalWrite(motorPin, HIGH); // Turn on the motor
}
else
{
digitalWrite(motorPin, LOW); // Turn off the motor
}

// Print the distance


Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");

delay(500); // Delay
}

// Function to convert microseconds to centimeters


long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

Link- Problem_Statements_3
Problem Statement - 4
(Control the DC motor using the Ultrasonic sensor data (using PWM PIN))

Code:
// pin numbers
const int triggerPin = 2; // Trigger pin for ultrasonic sensor
const int echoPin = 4; // Echo pin for ultrasonic sensor
const int motorPin = 9; // PWM pin to control the DC motor speed

void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop()
{
long duration;
long distance;

// Trigger the ultrasonic sensor


digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

// Read echo duration


duration = pulseIn(echoPin, HIGH);
distance = microsecondsToCentimeters(duration);

// Map distance (40–100 cm) to PWM value (0–255)


int motorSpeed = 0;
if (distance >= 30) {
motorSpeed = map(distance, 30, 100, 100, 255); // Increase speed with distance
motorSpeed = constrain(motorSpeed, 0, 255); // Ensure within 0–255
} else {
motorSpeed = 0; // Too close, stop motor
}

analogWrite(motorPin, motorSpeed); // Control motor speed via PWM

Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm | Motor Speed: ");
Serial.println(motorSpeed);

delay(500);
}

long microsecondsToCentimeters(long microseconds)


{
return microseconds / 29 / 2;
}

Link- Problem_statements_4

You might also like