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

Practice Arduino Codes

Uploaded by

shaikrezwana8812
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)
29 views

Practice Arduino Codes

Uploaded by

shaikrezwana8812
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/ 7

Controlling of two DC Motors alternatively with L293D Driver module – Clockwise 3 Sec, Anticlockwise 3

sec and stops for 1 sec.

void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}
void loop()
{
//clockwise, Anticlockwise & Stop of M1
digitalWrite(4, HIGH);digitalWrite(5, LOW); delay(3000);
digitalWrite(4, LOW);digitalWrite(5, HIGH); delay(3000);
digitalWrite(4, LOW);digitalWrite(5, LOW); delay(1000);

//clockwise, Anticlockwise & Stop of M2


digitalWrite(7, HIGH);digitalWrite(6, LOW); delay(3000);
digitalWrite(7, LOW);digitalWrite(6, HIGH); delay(3000);
digitalWrite(7, LOW);digitalWrite(6, LOW); delay(1000);
}

A robotic system using a DC motor that detects objects and halts automatically.

#include <NewPing.h>

#define TRIG_PIN 9
#define ECHO_PIN 10
#define MOTOR_A_FWD 5
#define MOTOR_B_FWD 7

NewPing sonar(TRIG_PIN, ECHO_PIN, 200);

void setup() {
pinMode(MOTOR_A_FWD, OUTPUT);
pinMode(MOTOR_B_FWD, OUTPUT);
}

void loop() {
delay(50);
if (sonar.ping_cm() > 15) {
digitalWrite(MOTOR_A_FWD, HIGH);
digitalWrite(MOTOR_B_FWD, HIGH);
} else {
digitalWrite(MOTOR_A_FWD, LOW);
digitalWrite(MOTOR_B_FWD, LOW);
delay(500); // Pause before stopping
}
}

Rotation of a stepper motor in a specific direction and reverse it

#include <Stepper.h>

const int stepsPerRevolution = 2048;

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
myStepper.setSpeed(15);
Serial.begin(9600);
Serial.println("Stepper motor control initialized");
}

void loop() {
Serial.println("Rotating clockwise...");
myStepper.step(stepsPerRevolution);
delay(1000);

Serial.println("Rotating counterclockwise...");
myStepper.step(-stepsPerRevolution);
delay(1000);

Serial.println("Cycle complete, waiting for next rotation...");


delay(2000); // Additional delay between cycles
}
Control Servo Motor from 0 to 180 degrees and reverse with a increment of 1 degree

#include <Servo.h>
Servo myservo;
void setup()
{
myservo.attach(9);
Serial.begin(9600);
}
void loop()
{
int pos;
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
Serial.print("The Current Position is ");
Serial.print(pos);
Serial.println(" Degrees");
delay(500);
}
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos);
Serial.print("The Current Position is ");
Serial.print(pos);
Serial.println(" Degrees");
delay(500);
}
}
Smart Parking Barrier System using a Servo Motor that opens and closes the parking barrier

#include <Servo.h>

Servo barrierServo;

const int trigPin = 9;


const int echoPin = 10;
const int servoPin = 6;

long duration;
int distance;
int thresholdDistance = 30;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
barrierServo.attach(servoPin);
barrierServo.write(90);
Serial.begin(9600);
}

void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);


distance = duration * 0.034 / 2;

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

if (distance < thresholdDistance) {


barrierServo.write(0);
delay(5000);
barrierServo.write(90);
}

delay(500);
}
Display the message "FIS" on the first line and "KL University" on the second line of an LCD screen.

#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
}
void loop()
{
lcd.setCursor(0,0);
lcd.print("FIS");
lcd.setCursor(2,1);
lcd.print("KL University");
}
Servo Angle control with Potentiometer and display on LCD

#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

Servo myServo;

const int potPin = A0;


const int servoPin = 9;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
myServo.attach(servoPin);
lcd.begin();
lcd.backlight();
lcd.print("Servo Position:");
}

void loop() {
int potValue = analogRead(potPin);
int servoAngle = map(potValue, 0, 1023, 0, 180);
myServo.write(servoAngle);

lcd.setCursor(0, 1);
lcd.print("Angle: ");
lcd.print(servoAngle);
lcd.print(" ");

delay(15);
}
Potentiometer-Controlled Differential Drive Robot using DC motors

const int potPin = A0;


const int leftMotorPin1 = 9;
const int leftMotorPin2 = 10;
const int rightMotorPin1 = 11;
const int rightMotorPin2 = 12;

void setup() {
pinMode(leftMotorPin1, OUTPUT);
pinMode(leftMotorPin2, OUTPUT);
pinMode(rightMotorPin1, OUTPUT);
pinMode(rightMotorPin2, OUTPUT);
}

void loop() {
int potValue = analogRead(potPin);
int motorSpeed = map(potValue, 0, 1023, -255, 255);

if (motorSpeed > 0) {
analogWrite(leftMotorPin1, motorSpeed);
analogWrite(rightMotorPin1, motorSpeed);
analogWrite(leftMotorPin2, 0);
analogWrite(rightMotorPin2, 0);
} else {
analogWrite(leftMotorPin1, 0);
analogWrite(rightMotorPin1, 0);
analogWrite(leftMotorPin2, -motorSpeed);
analogWrite(rightMotorPin2, -motorSpeed);
}

delay(100);
}
Movement base controlled Stepper Motor with LCD Display

#include <Stepper.h>
#include <LiquidCrystal_I2C.h>

const int stepsPerRevolution = 200;


Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int pirPin = 2;
int motionDetected = 0;

void setup() {
pinMode(pirPin, INPUT);
myStepper.setSpeed(60);
lcd.begin();
lcd.backlight();
lcd.print("Waiting for");
lcd.setCursor(0, 1);
lcd.print("motion...");
}

void loop() {
motionDetected = digitalRead(pirPin);
if (motionDetected == HIGH) {
lcd.clear();
lcd.print("Motion Detected");
myStepper.step(stepsPerRevolution);
lcd.clear();
lcd.print("Stepping...");
delay(2000);
lcd.clear();
lcd.print("Waiting for");
lcd.setCursor(0, 1);
lcd.print("motion...");
}
delay(500);
}

You might also like