italy
Offline
Newbie
Karma: 0
Posts: 8
/* delay(1000); */
|
 |
« on: March 28, 2011, 05:38:45 pm » |
hi all, it's possible to control the speed of a dc motor with a potentiometer? I started reading this article: http://homepage.mac.com/joester5/art/digitalart2/dcmotor.htmlI've attached all successfully, but i CAN'T ABLE TO set speed! If i turn pot, dc motor run or stop, but at the same speed (fast).  Any idea to fix this issue? This is the used code: int sensorValue = 0; int outputValue = 0; int transistorPin = 2;
void setup() { pinMode(transistorPin, OUTPUT); }
void loop() { sensorValue = analogRead(analogInPin)/4; outputValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(transistorPin, sensorValue); } tks.
|
|
|
|
« Last Edit: March 29, 2011, 04:40:18 pm by elp »
|
Logged
|
-- elp
|
|
|
|
0
Online
Tesla Member
Karma: 73
Posts: 6630
Arduino rocks
|
 |
« Reply #1 on: March 29, 2011, 07:38:20 pm » |
You seem to have a freewheel diode in the wrong place - needs to be across the motor.
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #2 on: March 30, 2011, 01:06:08 am » |
int sensorValue = 0; int outputValue = 0; int transistorPin = 2; int analogInPin = ?; // you must state which analog pin you are using void setup() { pinMode(transistorPin, OUTPUT); }
void loop() { sensorValue = analogRead(analogInPin); // sensorValue = analogRead(analogInPin)/4; // the map function will perform the /4, don't do it twice outputValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(transistorPin, sensorValue); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: March 30, 2011, 02:11:56 am » |
sensorValue = analogRead(analogInPin)/4; outputValue = map(sensorValue, 0, 1023, 0, 255);
Why bother with the map? And yes, why haven't you shown the code you're using? Maybe your speed problem would be solved if you used a PWM pin for the output.
|
|
|
|
« Last Edit: March 30, 2011, 02:13:34 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
italy
Offline
Newbie
Karma: 0
Posts: 8
/* delay(1000); */
|
 |
« Reply #4 on: March 30, 2011, 12:43:43 pm » |
I get pot values.... from 0 to 1023. Sorry for copy/paste code, analogInPin was forgotted. What kind of value i need to pass to pot? I'm very newbie, about coding, about electronics and about english!  Maybe your speed problem would be solved if you used a PWM pin for the output.
What is a PWM pin? You seem to have a freewheel diode in the wrong place - needs to be across the motor.
What is diode correct place? Have you a detailed example for a beginner? thanks for helps!
|
|
|
|
|
Logged
|
-- elp
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #5 on: March 30, 2011, 12:55:55 pm » |
What is a PWM pin? analogWrite() commands only work on arduino pins that support PWM because internal timer support is needed and they are only 'hardwired' to specify arudino pin numbers, depending on what processor chip your board uses (Mega board has more timers then Uno board). The arduino reference page for analogWrite command tells which output pins will support PWM: http://arduino.cc/en/Reference/AnalogWriteWhat is diode correct place? Have you a detailed example for a beginner? Look at the below linked drawing of controlling a solenoid and see where the diode is wired and which direction the diode is to be wired, it's the same for a simple single winding DC motor: http://arduino.cc/playground/uploads/Learning/solenoid_driver.pdf
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 97
Posts: 6376
|
 |
« Reply #6 on: March 30, 2011, 01:00:03 pm » |
PWM = Pulse Width Modulation
Pins 3, 5, 6, 9, 10, and 11 can do PWM "analogWrite()". When you say:
analogWrite(3, 0); // Pin 3 is off all the time analogWrite(3, 64); // Pin 3 is on 1/4 of the time and off 3/4 of the time. analogWrite(3, 128); // Pin 3 is on half the time and off half the time. analogWrite(3, 192); // Pin 3 is on 3/4 of the time and off 1/4 of the time. analogWrite(3, 255); // Pin 3 is on all the time
The pulses are quite fast so for LED's and motors it acts like an analog signal.
If you use a pin that can't do PWM you get LOW for all values below 128 and HIGH for all values above 127. That is why your motor on Pin 2 is doing just OFF (LOW) and ON (HIGH).
|
|
|
|
|
Logged
|
|
|
|
|
italy
Offline
Newbie
Karma: 0
Posts: 8
/* delay(1000); */
|
 |
« Reply #7 on: March 30, 2011, 01:22:20 pm » |
@johnwasser
Right! Now is connected to pin 3 and the speed is controlled!!
Many thanks man!
|
|
|
|
|
Logged
|
-- elp
|
|
|
|
0
Online
Tesla Member
Karma: 73
Posts: 6630
Arduino rocks
|
 |
« Reply #8 on: March 30, 2011, 07:43:39 pm » |
The original article uses pin 9 - its marked in the drawing and in the schematic - now you know why!
|
|
|
|
|
Logged
|
|
|
|
|
Argentina
Offline
Newbie
Karma: 0
Posts: 1
|
 |
« Reply #9 on: January 25, 2013, 11:14:34 pm » |
Hi! I have arduino UNO and Bridge H L298 Conect your DC Motor to Bridge H and conect in1 to PIN11 Arduino. Then, PIN1 of the potenciometer to 5v, PIN2 to A0 and PIN3 to GND and here the programming Good Luck! (Sorry my english, I'm from Argentina) NOTE: YOU MUST CONECT THE GND ARDUINO TO GND BRIDGE H!
int pot = 0; int motorPin = 11;
void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps pinMode(pot, INPUT); pinMode(motorPin, OUTPUT); }
int getPot() { int v; v = analogRead(pot); v /= 4; v = max(v, 60); v = min(v, 255); return v; }
int motorFoward() { analogWrite(motorPin, getPot()); }
int motorBackward() { analogWrite(motorPin, getPot()); }
void loop() // run over and over again { motorFoward(); motorBackward(); }
|
|
|
|
|
Logged
|
|
|
|
|
|