You are on page 1of 12

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

SHARE THIS

37 likes

RELATED ARTICLES

How to Run a Stepper Motor with an Arduino + L293D IC+

How to Run a Stepper Motor with an Arduino + L293D IC

Hi Guys This article is about something every electronics enthusiast would want to learn and master. Its
name might...

How to run small brushed motor for mini quadcopter with Arduino+

How to run small brushed motor for mini quadcopter with Arduino

Hi Guys, I have read the question on many forms, How to run small brushed motor for mini
quadcopter....

Arduino and Bluetooth HC-06 to Control the LED with Android Device+

Arduino and Bluetooth HC-06 to Control the LED with Android Device

Hi everyone, In this project i will show you how to control LED light with your Android device along...
Ultrasonic Distance sensor HC-04 with Arduino - DIY Distance Meter project+

Ultrasonic Distance sensor HC-04 with Arduino – DIY Distance Meter project

DIY distance meter with an Arduino and ultrasonic distance sensor Hi Folks, Ones again I am going to
write...

Control DC Motor CW/CCW with MPU-6050 Gyro/Accelerometer + Arduino+

Control DC Motor CW/CCW with MPU-6050 Gyro/Accelerometer + Arduino

In this article you will get the code and circuit diagram to control the DC Motor CW/CCW using GY-521
gyroscope and...

PIR Motion Sensor with Arduino

Hi folks I am back ones again to share with you my simple but useful project and step by...

List of Arduino Robot Kits for Beginners

If you are a beginner and want to enter the world of Robotics and want to build your own...

How to Run Stepper Motor with Arduino using L298N Driver Module

Hi Guys, In this post, I will demonstrate how you can run or control 6 wired unipolar stepper motor...

How to program an Arduino - Tutorial

Best Cheap DIY 3D Printer Kits in 2018

12 COMMENTS
CrystalRunner

2 years ago

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: “Arduino/Genuino Uno”

C:\Users\User\Desktop\SKETCHES\DualHBridgeDCMotor_Sketch\DualHBridgeDCMotor_Sketch.ino: In
function ‘void demoTwo()’:

DualHBridgeDCMotor_Sketch:61: error: ‘i’ was not declared in this scope

analogWrite(enA, i);

exit status 1

‘i’ was not declared in this scope

This report would have more information with

“Show verbose output during compilation”

option enabled in File -> Preferences.

REPLY

Author

admin 2 years ago


Hi, Thanks for informing me. I have update the code, Please copy and past the code again it will work
now. And Please let me know if you are still facing any problem.

REPLY

subham mohanty 2 years ago

yep its showing errors in code

how can i correct it..//

REPLY

Abhishek Gupta 3 months ago

remove the spaces behind the scope or in other words remove the indentations. to verify the keywords
should be colored, check that

REPLY

Matey Ankit

2 years ago

I want to run 24v dc motor having 10kg torque, 30 rpm with the help of aurdino uno

The motor must run only in one direction continuously

Plz suggests me what to use and how to do programing

REPLY

md.intishar ul islam
2 years ago

how to contro??

by joystic???

REPLY

Shashi Kiran

2 years ago

My DC motor spins one direction only when connected thru the L298. I have adapted the code above. I
dont think the problem is with the code or the motor as I tested the motor by connecting + and – to 12V
directly and changed polarities and it spins both directions. But when I connect thru the L298 then only
1 direction works.

How can I debug the L298. At this time I have only 1 DC geared 100 rpm motor. Have not connected
another motor.

The Power is thru a 12V DC wall adapter. The controls to the L298 are thru the Arduino Pins 6 and 7. Pin
9 is set to analogWrite of PWM 255. ( of course the jumper on EN-A Enable A is removed and controlled
thru the Arduino.

REPLY

Vinod 11 months ago

I also have the same problem if you get the solution please let me know

REPLY

Emon
12 months ago

How can I do the same thing with Arduino & L293D motor shield ?

REPLY

Vinod

11 months ago

I also have the same problem if you get the solution please let me know

REPLY

Harshala

4 months ago

hi

I am not getting any error but the motor is not running please help

REPLY

Abhishek Gupta

3 months ago

that’s some awesome code, nice variations. Loved it !!!

REPLY
Leave a reply

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

Profile photo of admin

Admin

Follow4

37 likes

78339 Views30th January 2018

SALE STILL NOT OVER

Find us on Facebook
Members

Newest | Active

Profile picture of admin

admin

active 3 weeks ago

Profile picture of manjunath s

manjunath s

active 3 weeks, 1 day ago

Profile picture of Mandeep Singh

Mandeep Singh

active 3 weeks, 2 days ago

Profile picture of Kristen

Kristen

active 4 months, 1 week ago

Profile picture of beipsychsebi

beipsychsebi

active 7 months ago

Recent Posts

ANYCUBIC 3D Printer Parts

CREALITY 3D Printer Ender-3 Part list

List of Arduino Robot Kits for Beginners

3 Amp Stepper motor Driver TB6560 with Arduino

Temperature Sensor with Arduino – K Type Thermocouple Sensor MAX6675 Module

Recent Comments

Gwyneth on PIR Motion Sensor with Arduino


Nitheesh Murikipudi on Control DC Motor CW/CCW with MPU-6050 Gyro/Accelerometer + Arduino

Jp on Control DC Motor CW/CCW with MPU-6050 Gyro/Accelerometer + Arduino

Jp on Control DC Motor CW/CCW with MPU-6050 Gyro/Accelerometer + Arduino

Abhishek Gupta on How to control DC motor with L298N driver and Arduino.

Categories

3d Printer

Arduino

Circuits

Cool Projects

technology

Search

Categories

3d Printer (5)

Arduino (19)

Circuits (1)

Cool Projects (2)

technology (1)

Tag Cloud

3d PrinterArduinoCircuitsCool Projectstechnology

notification icon

You might also like