Home
About Us
Achievement
Home
Raspberry-Pi Projects
_Raspberry Pi 4 Complete Guide
Arduino Project
_All Projects
Esp8266 Project
_All Projects
Esp32 Project
_Automation-with-feedback
Robotics Workshop
Just Do Electronics
June 19, 2020
How To Use Motor Driver L298N
Code :-
void setup() { pinMode(7, OUTPUT); pinMode(8, OUTPUT); } void loop() { digitalWrite(7, HIGH); //clockwise rotation digitalWrite(8, LOW); delay(5000); digitalWrite(8, HIGH); //anticlockwise rotation digitalWrite(7, LOW); delay(5000); }
How to control DC motor with L298N driver
Code :-
//Prateek //www.prateeks.in int enA = 10; int in1 = 9; int in2 = 8; // motor two int enB = 5; int in3 = 7; int in4 = 6; void setup() { // set all the motor control pins to outputs pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void demoOne() { // this function will run the motors in both directions at a fixed speed // turn on motor A digitalWrite(in1, HIGH); digitalWrite(in2, LOW); // set speed to 200 out of possible range 0~255 analogWrite(enA, 200); // turn on motor B digitalWrite(in3, HIGH); digitalWrite(in4, LOW); // set speed to 200 out of possible range 0~255 analogWrite(enB, 200); delay(2000); // now change motor directions digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(2000); // now turn off motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } void demoTwo() { // this function will run the motors across the range of possible speeds // note that maximum speed is determined by the motor itself and the operating voltage // the PWM values sent by analogWrite() are fractions of the maximum speed possible // by your hardware // turn on motors digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); // accelerate from zero to maximum speed for (int i = 0; i < 256; i++) { analogWrite(enA, i); analogWrite(enB, i); delay(20); } // decelerate from maximum speed to zero for (int i = 255; i > 0; --i) { analogWrite(enA, i); analogWrite(enB, i); delay(20); } // now turn off motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } void loop() { demoOne(); delay(1000); demoTwo(); delay(1000); }
DC Motor Speed Controller using Arduino and L298 Motor Driver
Code :-
//Prateek //www.prateeks.in #include
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); #define potentiometer A0 //10k Variable Resistor #define bt_F A1 // Clockwise Button #define bt_S A2 // Stop Button #define bt_B A3 // Anticlockwise Button #define M1_Ena 11 // Enable1 L298 for PWM #define M1_in1 10 // In1 L298 for Clockwise #define M1_in2 9 // In2 L298 for Anticlockwise int read_ADC =0; int duty_cycle; int duty_cycle_lcd; int set = 0; void setup(){ Serial.begin(9600);// initialize serial communication at 9600 bits per second: pinMode(potentiometer, INPUT); pinMode(bt_F, INPUT_PULLUP); pinMode(bt_S, INPUT_PULLUP); pinMode(bt_B, INPUT_PULLUP); pinMode(M1_Ena, OUTPUT); pinMode(M1_in1, OUTPUT); pinMode(M1_in2, OUTPUT); lcd.begin(16,2); lcd.setCursor(0,0); lcd.print(" WELCOME To My "); lcd.setCursor(0,1); lcd.print("YouTube Channel"); delay(2000); // Waiting for a while lcd.clear(); } void loop(){ read_ADC = analogRead(potentiometer); duty_cycle = map(read_ADC, 0, 1023, 0, 255); duty_cycle_lcd = map(read_ADC, 0, 1023, 0, 100); analogWrite(M1_Ena, duty_cycle); lcd.setCursor(0,0); lcd.print("Duty Cycle: "); lcd.print(duty_cycle_lcd); lcd.print("% "); if(digitalRead (bt_F) == 0){set = 1;} if(digitalRead (bt_S) == 0){set = 0;} if(digitalRead (bt_B) == 0){set = 2;} lcd.setCursor(0,1); if(set==0){ lcd.print(" Stop "); digitalWrite(M1_in1, LOW); digitalWrite(M1_in2, LOW); } if(set==1){ lcd.print(" Clockwise "); digitalWrite(M1_in1, HIGH); digitalWrite(M1_in2, LOW); } if(set==2){ lcd.print(" Anticlockwise "); digitalWrite(M1_in1, LOW); digitalWrite(M1_in2, HIGH); } delay(50); }
Arduino Obstacle Avoiding Robot Car
Code :-
Library Link
//Prateek //www.prateeks.in //AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install //NewPing Library https://github.com/livetronic/Arduino-NewPing //Servo Library https://github.com/arduino-libraries/Servo.git #include
#include
#include
#define TRIG_PIN A0 #define ECHO_PIN A1 #define MAX_DISTANCE 200 #define MAX_SPEED 190 // sets speed of DC motors #define MAX_SPEED_OFFSET 20 NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); AF_DCMotor motor1(1, MOTOR12_1KHZ); AF_DCMotor motor2(2, MOTOR12_1KHZ); AF_DCMotor motor3(3, MOTOR34_1KHZ); AF_DCMotor motor4(4, MOTOR34_1KHZ); Servo myservo; boolean goesForward=false; int distance = 100; int speedSet = 0; void setup() { myservo.attach(10); myservo.write(115); delay(2000); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); } void loop() { int distanceR = 0; int distanceL = 0; delay(40); if(distance<=15) { moveStop(); delay(100); moveBackward(); delay(300); moveStop(); delay(200); distanceR = lookRight(); delay(200); distanceL = lookLeft(); delay(200); if(distanceR>=distanceL) { turnRight(); moveStop(); }else { turnLeft(); moveStop(); } }else { moveForward(); } distance = readPing(); } int lookRight() { myservo.write(50); delay(500); int distance = readPing(); delay(100); myservo.write(115); return distance; } int lookLeft() { myservo.write(170); delay(500); int distance = readPing(); delay(100); myservo.write(115); return distance; delay(100); } int readPing() { delay(70); int cm = sonar.ping_cm(); if(cm==0) { cm = 250; } return cm; } void moveStop() { motor1.run(RELEASE); motor2.run(RELEASE); motor3.run(RELEASE); motor4.run(RELEASE); } void moveForward() { if(!goesForward) { goesForward=true; motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly { motor1.setSpeed(speedSet); motor2.setSpeed(speedSet); motor3.setSpeed(speedSet); motor4.setSpeed(speedSet); delay(5); } } } void moveBackward() { goesForward=false; motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly { motor1.setSpeed(speedSet); motor2.setSpeed(speedSet); motor3.setSpeed(speedSet); motor4.setSpeed(speedSet); delay(5); } } void turnRight() { motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); delay(500); motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); } void turnLeft() { motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(FORWARD); motor4.run(FORWARD); delay(500); motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); }
Arduino Obstacle Avoiding Robot Car Using
L298N driver
Code :-
//Prateek //www.prateeks.in //new ping libaray :- https://bitbucket.org/teckel12/arduino-new-ping/downloads/ #include
//Servo motor library. This is standard library #include
//Ultrasonic sensor function library. You must install this library //our L298N control pins const int LeftMotorForward = 7; const int LeftMotorBackward = 6; const int RightMotorForward = 4; const int RightMotorBackward = 5; //sensor pins #define trig_pin A1 //analog input 1 #define echo_pin A2 //analog input 2 #define maximum_distance 200 boolean goesForward = false; int distance = 100; NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function Servo servo_motor; //our servo name void setup(){ pinMode(RightMotorForward, OUTPUT); pinMode(LeftMotorForward, OUTPUT); pinMode(LeftMotorBackward, OUTPUT); pinMode(RightMotorBackward, OUTPUT); servo_motor.attach(10); //our servo pin servo_motor.write(115); delay(2000); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); } void loop(){ int distanceRight = 0; int distanceLeft = 0; delay(50); if (distance <= 20){ moveStop(); delay(300); moveBackward(); delay(400); moveStop(); delay(300); distanceRight = lookRight(); delay(300); distanceLeft = lookLeft(); delay(300); if (distance >= distanceLeft){ turnRight(); moveStop(); } else{ turnLeft(); moveStop(); } } else{ moveForward(); } distance = readPing(); } int lookRight(){ servo_motor.write(50); delay(500); int distance = readPing(); delay(100); servo_motor.write(115); return distance; } int lookLeft(){ servo_motor.write(170); delay(500); int distance = readPing(); delay(100); servo_motor.write(115); return distance; delay(100); } int readPing(){ delay(70); int cm = sonar.ping_cm(); if (cm==0){ cm=250; } return cm; } void moveStop(){ digitalWrite(RightMotorForward, LOW); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW); digitalWrite(LeftMotorBackward, LOW); } void moveForward(){ if(!goesForward){ goesForward=true; digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); } } void moveBackward(){ goesForward=false; digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorForward, LOW); } void turnRight(){ digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorBackward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorForward, LOW); delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); } void turnLeft(){ digitalWrite(LeftMotorBackward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorForward, LOW); digitalWrite(RightMotorBackward, LOW); delay(500); digitalWrite(LeftMotorForward, HIGH); digitalWrite(RightMotorForward, HIGH); digitalWrite(LeftMotorBackward, LOW); digitalWrite(RightMotorBackward, LOW); }
Line Follower Robot using Arduino, L298 Motor Driver and IR Sensor
Code :-
//Prateek //www.prateeks.in #define enA 10//Enable1 L298 Pin enA #define in1 9 //Motor1 L298 Pin in1 #define in2 8 //Motor1 L298 Pin in1 #define in3 7 //Motor2 L298 Pin in1 #define in4 6 //Motor2 L298 Pin in1 #define enB 5 //Enable2 L298 Pin enB #define R_S A0 //ir sensor Right #define L_S A1 //ir sensor Left void setup(){ // put your setup code here, to run once pinMode(R_S, INPUT); // declare if sensor as input pinMode(L_S, INPUT); // declare ir sensor as input pinMode(enA, OUTPUT); // declare as output for L298 Pin enA pinMode(in1, OUTPUT); // declare as output for L298 Pin in1 pinMode(in2, OUTPUT); // declare as output for L298 Pin in2 pinMode(in3, OUTPUT); // declare as output for L298 Pin in3 pinMode(in4, OUTPUT); // declare as output for L298 Pin in4 pinMode(enB, OUTPUT); // declare as output for L298 Pin enB analogWrite(enA, 150); // Write The Duty Cycle 0 to 255 Enable Pin A for Motor1 Speed analogWrite(enB, 150); // Write The Duty Cycle 0 to 255 Enable Pin B for Motor2 Speed delay(1000); } void loop(){ if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){forword();} //if Right Sensor and Left Sensor are at White color then it will call forword function if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){turnRight();} //if Right Sensor is Black and Left Sensor is White then it will call turn Right function if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){turnLeft();} //if Right Sensor is White and Left Sensor is Black then it will call turn Left function if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){Stop();} //if Right Sensor and Left Sensor are at Black color then it will call Stop function } void forword(){ //forword digitalWrite(in1, HIGH); //Right Motor forword Pin digitalWrite(in2, LOW); //Right Motor backword Pin digitalWrite(in3, LOW); //Left Motor backword Pin digitalWrite(in4, HIGH); //Left Motor forword Pin } void turnRight(){ //turnRight digitalWrite(in1, LOW); //Right Motor forword Pin digitalWrite(in2, HIGH); //Right Motor backword Pin digitalWrite(in3, LOW); //Left Motor backword Pin digitalWrite(in4, HIGH); //Left Motor forword Pin } void turnLeft(){ //turnLeft digitalWrite(in1, HIGH); //Right Motor forword Pin digitalWrite(in2, LOW); //Right Motor backword Pin digitalWrite(in3, HIGH); //Left Motor backword Pin digitalWrite(in4, LOW); //Left Motor forword Pin } void Stop(){ //stop digitalWrite(in1, LOW); //Right Motor forword Pin digitalWrite(in2, LOW); //Right Motor backword Pin digitalWrite(in3, LOW); //Left Motor backword Pin digitalWrite(in4, LOW); //Left Motor forword Pin }
Arduino Human Following Robot
Code :-
//Prateek //www.prateeks.in #include
#include
#include
#define RIGHT A2 #define LEFT A3 #define TRIGGER_PIN A1 #define ECHO_PIN A0 #define MAX_DISTANCE 100 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); AF_DCMotor Motor1(1,MOTOR12_1KHZ); AF_DCMotor Motor2(2,MOTOR12_1KHZ); AF_DCMotor Motor3(3,MOTOR34_1KHZ); AF_DCMotor Motor4(4,MOTOR34_1KHZ); Servo myservo; int pos =0; void setup() { // put your setup code here, to run once: Serial.begin(9600); myservo.attach(10); { for(pos = 90; pos <= 180; pos += 1){ myservo.write(pos); delay(15); } for(pos = 180; pos >= 0; pos-= 1) { myservo.write(pos); delay(15); }for(pos = 0; pos<=90; pos += 1) { myservo.write(pos); delay(15); } } pinMode(RIGHT, INPUT); pinMode(LEFT, INPUT); } void loop() { // put your main code here, to run repeatedly: delay(50); unsigned int distance = sonar.ping_cm(); Serial.print("distance"); Serial.println(distance); int Right_Value = digitalRead(RIGHT); int Left_Value = digitalRead(LEFT); Serial.print("RIGHT"); Serial.println(Right_Value); Serial.print("LEFT"); Serial.println(Left_Value); if((Right_Value==1) && (distance>=10 && distance<=30)&&(Left_Value==1)){ Motor1.setSpeed(120); Motor1.run(FORWARD); Motor2.setSpeed(120); Motor2.run(FORWARD); Motor3.setSpeed(120); Motor3.run(FORWARD); Motor4.setSpeed(120); Motor4.run(FORWARD); }else if((Right_Value==0) && (Left_Value==1)) { Motor1.setSpeed(200); Motor1.run(FORWARD); Motor2.setSpeed(200); Motor2.run(FORWARD); Motor3.setSpeed(100); Motor3.run(BACKWARD); Motor4.setSpeed(100); Motor4.run(BACKWARD); }else if((Right_Value==1)&&(Left_Value==0)) { Motor1.setSpeed(100); Motor1.run(BACKWARD); Motor2.setSpeed(100); Motor2.run(BACKWARD); Motor3.setSpeed(200); Motor3.run(FORWARD); Motor4.setSpeed(200); Motor4.run(FORWARD); }else if((Right_Value==1)&&(Left_Value==1)) { Motor1.setSpeed(0); Motor1.run(RELEASE); Motor2.setSpeed(0); Motor2.run(RELEASE); Motor3.setSpeed(0); Motor3.run(RELEASE); Motor4.setSpeed(0); Motor4.run(RELEASE); }else if(distance > 1 && distance < 10) { Motor1.setSpeed(0); Motor1.run(RELEASE); Motor2.setSpeed(0); Motor2.run(RELEASE); Motor3.setSpeed(0); Motor3.run(RELEASE); Motor4.setSpeed(0); Motor4.run(RELEASE); } }
Interfacing Mpu6050 With Arduino
Code :-
//Prateek //www.prateeks.in #include
LiquidCrystal lcd(8,9,10,11,12,13); #include
#include
#define period 10000 MPU6050 mpu; int count=0; char okFlag=0; byte degree[8] = { 0b00000, 0b00110, 0b01111, 0b00110, 0b00000, 0b00000, 0b00000, 0b00000 }; void setup() { lcd.begin(16,2); lcd.createChar(0, degree); Serial.begin(9600); Serial.println("Initialize MPU6050"); while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) { lcd.clear(); lcd.print("Device not Found"); Serial.println("Could not find a valid MPU6050 sensor, check wiring!"); delay(500); } count=0; mpu.calibrateGyro(); mpu.setThreshold(3); lcd.clear(); lcd.print("MPU6050 Interface"); lcd.setCursor(0,1); lcd.print("justdoelectronic"); delay(2000); lcd.clear(); } void loop() { lcd.clear(); lcd.print("Temperature"); long st=millis(); Serial.println("Temperature"); while(millis()
Acceleration Measurement with Accelerometer ADXL335 & Arduino
Code :-
//Prateek //www.prateeks.in #include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); int Xread; int Xrest; int Yread; int Yrest; int Zread; int Zrest; double Gx; double Gy; double Gz; int xpin = 0; int ypin = 1; int zpin = 2; int t1; void setup() { Serial.begin(9600); lcd.begin(16, 2); digitalWrite(13,HIGH); delay(1000); Xrest=analogRead(xpin); Serial.print(Xrest); Yrest=analogRead(ypin); Serial.print(Yrest); Zrest=analogRead(zpin); Serial.print(Zrest); digitalWrite(13,LOW); } void loop() { Serial.print("Time "); t1=millis(); Serial.println(t1*0.001); Xread = analogRead(xpin)-Xrest; Yread=analogRead(ypin)-Yrest; Zread=analogRead(zpin)-Zrest; Gx=Xread/67.584; Gy=Yread/67.584; Gz=Zread/67.584; Serial.print("Acceleration X :"); Serial.print(Gx); Serial.print("Acceleration Y :"); Serial.print(Gy); Serial.print("Acceleration Z :"); Serial.print(Gz); Serial.print("\n"); lcd.setCursor(0, 0); lcd.print("gx:"); lcd.print(Gx); lcd.setCursor(8, 0); lcd.print("gy:"); lcd.print(Gy); lcd.setCursor(0, 1); lcd.print("gz:"); lcd.print(Gz); delay(1000); lcd.clear(); }
Arduino Tilt Angle & Distance Meter with ADXL335 & HC-SR04
Code :-
//Prateek //www.prateeks.in #include
#define ADC_ref 5 // ADC reference Voltage #define zero_x 1.799 #define zero_y 1.799 #define zero_z 1.799 #define echoPin 8 #define trigPin 9 #define selectSwitch 1 #define sensitivity_x 0.4 #define sensitivity_y 0.4 #define sensitivity_z 0.4 unsigned int value_x; unsigned int value_y; unsigned int value_z; float xv; float yv; float zv; float angle; LiquidCrystal lcd(12,11,5,4,3,2); void setup() { analogReference(ADC_ref); pinMode(selectSwitch,INPUT); pinMode(trigPin,OUTPUT); pinMode(echoPin,INPUT); lcd.clear(); lcd.begin(16,2); lcd.print("Tilt & Distance"); lcd.setCursor(0,2); lcd.print(" Measurement "); delay(3000); lcd.clear(); } void loop() { int distance,duration; value_x = analogRead(A0); value_y = analogRead(A1); value_z = analogRead(A2); xv=(value_x/1024.0*ADC_ref-zero_x)/sensitivity_x; yv=(value_y/1024.0*ADC_ref-zero_y)/sensitivity_y; zv=(value_z/1024.0*ADC_ref-zero_z)/sensitivity_z; angle =atan2(-yv,-zv)*57.2957795+180; if(digitalRead(selectSwitch)==HIGH) { lcd.setCursor(0,0); lcd.print("Tilt: "); lcd.print(angle); lcd.print(" deg"); digitalWrite(trigPin, HIGH); delayMicroseconds(1000); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance >= 400 || distance <= 0) { lcd.setCursor(0,1); lcd.print("Out of Range"); } else { lcd.setCursor(0,1); lcd.print("Distance: "); lcd.print(distance); lcd.print(" cm "); } delay(1000); lcd.clear(); } else { lcd.setCursor(0,0); lcd.print("X="); lcd.print(xv); lcd.print(" Y="); lcd.print(yv); lcd.setCursor(0,1); lcd.print(" Z= "); lcd.print(zv); delay(1000); } }
0 Comments
Newer
Older
ESP32 Interfacing With LDR Sensor
How To Make RFID & Keypad Based Door Lock
IoT Based Fingerprint Biometric Attendance System Using NodeMCU (Esp8266)
Esp32 Cam Based Face Unlock
NRF Based Servo Motor Control
Ultrasonic Sensor as a Counter with Arduino
ESP32 Led Blink With Push Button
How to Make a Conveyor Belt System | Counter Machine
IoT Based Patient Health Monitoring System Using Blynk App
Arduino ebook
YouTube
Like on Facebook
Follow on Twitter
Follow on Google+
Follow on Instagram
Subscribe on Youtube
Hi WelCome To My Website If you have a creative mind and want to learn Electronics even without studying it then FRIENDS this Website And Youtube Channel is for you, I am crazy creators who love making Electronics things. I have a bunch of more than 130 Tutorial Videos on My YouTube Channel and I upload a new Tutorial every Sunday.
Tags
Arduino Project
Esp32 Project
Esp8266 Project
0 Comments