# Controlling a 12v 80 watt dc motor with an Arduino

Status
Not open for further replies.

#### Raymond3231976

##### Member
thanks for the help

Last edited:

#### Pommie

##### Well-Known Member
I'm guessing that the UNO doesn't use A0 as an analog in pin. Change A0 at the top (#define) to whatever you have the pot connected to.

Mike.
BTW, I can't read whats on those screen shots as they're too small.

#### Pommie

##### Well-Known Member
I broke it with the edit, try,
Code:
#define analogInPin A0
#define pwmOutPin 2

uint16_t sensorValue,outputValue,percentValue;

void setup() {
pinMode(pwmOutPin,OUTPUT);
}

void loop(){
// Read the PWM value of the input potentiometer.
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// map pump speed percent of full scale
percentValue = map (outputValue, 0, 255, 0, 100);
// change the analog out value:
analogWrite(pwmOutPin, outputValue);
}
Mike.

Last edited:

##### Well-Known Member
Pommie,

<CODE>
#define pwmOutPin 2
</CODE>

With the Arduino UNO I don't think we can use Pin 2 as a PWM out pin. Pins 3, 5, 6, 7, 9, 10, or 11 can be used. I tried it and while the code loads Pin 2 won't out a PWM. The other pins I mentioned do. Also, only pins 5 & 6 have a 980 Hz PWM frequency I believe the others are 490 Hz. I would use 5 or 6 for the PWM out pin and if when the project gets going the motor hums worry about using code to increase the PWM frequency.

Also, for some reason my options on my task bar are greyed out so I can't use a quote or code or anything up there. Weird!

Ron

#### dr pepper

##### Well-Known Member
I havent actually run the code so I cant say it#ll do what its sposed to, however I got it to compile, the pinmodes where in the wrong place.

Code:
#define analogInPin A0
#define pwmOutPin 2

uint16_t sensorValue,outputValue,percentValue;

void setup() {
pinMode(pwmOutPin,OUTPUT);
}

void loop(){
// Read the PWM value of the input potentiometer.
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// map pump speed percent of full scale
percentValue = map (outputValue, 0, 255, 0, 100);
// change the analog out value:
analogWrite(pwmOutPin, outputValue);
}

##### Well-Known Member
Guys, you can't use Pin 2 as a PWM out. With the Arduino UNO it goes as I mentioned in my previous post. Yes, the code using pin 2 will compile and load but all you will get is a logic high level or logic low on pin 2, depending on the input to A0. On my UNO anything below 1/2 Vref I get a Low out and anything above Vref I get a high out. When you actually run the code. Pins 3, 5, 6, 7, 9, 10, or 11 can be used for PWM and only pins 5 & 6 will give you a 940 Hz PWM out, the remainder will only do a 490 Hz PWM frequency.

When I use pin 5 or 6 I get the 940 Hz PWM and things work the way they should. Also, depending on how the motor likes or dislikes the PWM frequency code can be used to cahnge the Arduino UNO PWM frequencies.

Ron

#### Raymond3231976

##### Member
Im getting this error now

##### Well-Known Member
I can't read the error? The text is too small for me to make out. Also, as I have mentioned, you can't use pin 2 as a PWM output pin. However, that will not throw an error.

Ron

#### Raymond3231976

##### Member
The arduino took this code without any error

#define pwmOutPin 2

uint16_t sensorValue,outputValue,percentValue;

void setup() {
pinMode(pwmOutPin,OUTPUT);
}

void loop(){
// Read the PWM value of the input potentiometer.
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// map pump speed percent of full scale
percentValue = map (outputValue, 0, 255, 0, 100);
// change the analog out value:
analogWrite(pwmOutPin, outputValue);
}

#### Raymond3231976

##### Member
So should i change the PWM pin 2 to pin 5 or 6?

#### Raymond3231976

##### Member
i was noticing today when i was putting it all together there are some differences with the uno i have and the one in the pictures ive posted i got the photo from the website i ordered it from everything looks the same exept the pins on the POWER pins the one in the old pics had 2 5v pins on the rail but this one has a IOREF were there was a 5v there so when i wired it up i used the other 5v at the bottom of the board

#### Raymond3231976

##### Member
im still waiting on my other resistors to come in the mail the 20k ohm

#### Raymond3231976

##### Member
I was testing my 10k ohm resistors and they are saying there 10.12k ohm will these still work or did they send me the wrong ones im testing them with a ( 12864 Mega328 ESR Transistor Resistor Diode Capacitor Mosfet Tester )

#### Raymond3231976

##### Member
here is what i got so far until i move PWM pin 2 to 5 or 6

#### dr pepper

##### Well-Known Member
Yes move the pwm pin as you said, Rons right.
And the 'duino you have looks like a compatible device, there might be some extra bits.

#### Raymond3231976

##### Member
ok so ill move pin 2 to pin 5 thank you also i should be getting my resistors on the 20th of this month

##### Well-Known Member
First this link, How to spot a counterfeit Arduino should help you identify your board. My read in this forum and others is for the most part even the "counterfeit" boards, with a few exceptions, work just fine. My guess is you have a clone type board. The Power section pin out doesn't match as you mentioned so I don't know what to tell you on that note.

Back in your post #127 you were having problems loading the code onto the board. Did you get that resolved?

On your 3:1 divider I likely would have used a small 10K trim pot rather than fixed resistors so you could apply a known voltage and adjust the output for a good known 3:1 value.

My guess is both of your motors, fan and pump will be used only in one direction so on your motor controller board both Direction 1 and Direction 2 would be tied to either 5 volts or ground. Unless you have any plans to PWM the fan, the fan PWM input to the motor controller would be tied to 5 volts so it just runs full speed all of the time. I never used this motor controller board.

Also, in my earlier code example I wrongly assumed the motor driver board had an enable pin which it doesn't. The code I used will need modified so when the bettery voltage is below a set threshold the PWM out is disabled or zero and the PWM out is only enabled when battery voltage is above a set point value. That is unless someone else has a suggestion for monitoring battery voltage and allowing the pump to run or not run.

Ron

#### Raymond3231976

##### Member
Back in my post #127 I was having problems loading the code onto the board. I Did get that resolved . Yes by using a Windows 10 laptop....
both motors will only run one direction the fan will only spin when the system is on for cooling

Last edited: