L293D Speed Control doesn't work

I’m stumped. Done a lot of searching and I think I am wiring and coding this correctly, but controlling the speed doesn’t seem to work. I have a 12V external motor supply. I’ve attached the wiring diagram. Here is my code;

int in1Pin = 3;
int in2Pin = 4;

void setup()
{
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);

}

void loop()
{
setMotor(225);
}

void setMotor(int speed)
{
analogWrite(2, speed);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, HIGH);
}

If I set the speed for < 130 the motor doesn’t start. Anything over that and it runs at a constant speed regardless of the speed variable. Any ideas?

Thanks,
Scott

try this.
Connect a pot to A0 analog input.
Run this code.
Adjust speed with pot .
If it doesn't work there is something wrong with your hardware because I tested it 5 minutes ago.

 int in1Pin = 3;
int in2Pin =4;
int pot=0;
int ENA =2;
int val= 0;
int mspeed;

void setup()
{
  pinMode(in1Pin, OUTPUT);
  pinMode(in2Pin, OUTPUT);
  pinMode(ENA, OUTPUT);
  digitalWrite(ENA, HIGH);

}

//void setMotor(int mspeed)
//{
//  val = analogRead(pot); 
//  mspeed = map(val,0,1023, 0,255);
//  digitalWrite(in1Pin, LOW);
//  digitalWrite(in2Pin, HIGH);
//}

void loop()
{
  //setMotor(mspeed);
  val = analogRead(pot); 
   mspeed = map(val,0,1023, 0,255);
  digitalWrite(in1Pin, LOW);
  digitalWrite(in2Pin, HIGH);
  analogWrite(ENA,mspeed);
}

Pin 2 isn’t a PWM pin (on an Uno anyway) … so analogWrite is giving either on or off.

uno pin2.PNG

Dooh ! I didn’t even think of that ! I feel like such an idiot ! That is such a freshman error. …
Oh well, at least the OP can use the sketch I uploaded.

Here’s a pinout for the UNO that might help you.
FYI,
pins 0,1,2,4,7,8,12 & 13 are NOT PWM pins

DING DING DING!!! We have a winner. So THAT's what those tildes mean :roll_eyes:

Moved pin 2 to pin 5 and works perfectly. THANKS!

Hi everybody and special thanks to alexands! :slight_smile:
I was looking for an easy code for my first arduino project to understand some basic rules.
With alexands code i could change the speed of my dc motor but i still have one question.
I want to make my dc also change direction so i changed the LOW/HIGH but there was no reverse of the direction like i had with other codes when i changed LOW to HIGH and HIGH to LOW.
So my question is: How can i change the code so my dc motor also spins in the other direction?
My goal is that my dc motor drives with a changeable speed forward and back.
Can anybody give me a hint or help?
Thank you!