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;
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);
}
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
Hi everybody and special thanks to alexands!
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!