Go Down

### Topic: control current with paralell resistor to 5v motor (Read 5715 times)previous topic - next topic

#### sobratthammer

##### Aug 09, 2011, 10:12 am
/*
project: control motor speed (in free run);

for this project:
8 x 560R resistors
8 green LED
1 blue LED
5v motor,20mA free runn, this one was 13ohm (motor)
Arduino duemilanove
jump wires maybe.

the circuit:
GND =>-motor
motor+ => R1(560) => - LED1+ => digital in (2)
motor+ => R2(560) => - LED2+ => digital in (3)
motor+ => R3(560) => - LED3+ => digital in (4)
motor+ => R4(560) => - LED4+ => digital in (5)
motor+ => R5(560) => - LED5+ => digital in (6)
motor+ => R6(560) => - LED6+ => digital in (7)
motor+ => R7(560) => - LED7+ => digital in (8)
motor+ => R8(560) => - LED8+ => digital in (9)
motor+=-LEDblue+ => -motor

Program
Eenter number between 0 and 8 to control speed of motor.
*/

void setup(){
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);

byte inByte;
void loop()
{
if (Serial.available() > 0) {
if (inByte == '0'){
Serial.println("0 W");
digitalWrite(2,LOW); digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW);
digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW);
}

if (inByte == '1'){// 0.06v 0,00519
Serial.println("0,3114 mW");
digitalWrite(2,HIGH); digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW);
digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW);
}
if (inByte == '2'){//0.12v 0,00997A
Serial.println("1,1964 mW");
digitalWrite(2,HIGH); digitalWrite(3,HIGH); digitalWrite(4,LOW); digitalWrite(5,LOW);
digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW);
}
if (inByte == '3'){//0.18v 0,01128A
Serial.println("2,0304 mW");
digitalWrite(2,HIGH); digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,LOW);
digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW);
}
if (inByte == '4'){//0.23v 0,01242
Serial.println("2,8566 mW");
digitalWrite(2,HIGH); digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,HIGH);
digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW);
}
if (inByte == '5'){//1.31v 0,0135A
Serial.println("17,685 mW");
digitalWrite(2,HIGH); digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,HIGH);
digitalWrite(6,HIGH); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW);
}
if (inByte == '6'){//1,75v 0,0135A
Serial.println("23,625 mW");
digitalWrite(2,HIGH); digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,HIGH);
digitalWrite(6,HIGH); digitalWrite(7,HIGH); digitalWrite(8,LOW); digitalWrite(9,LOW);
}
if (inByte == '7'){//1,89 0,0146A
Serial.println("27,594 mW");
digitalWrite(2,HIGH); digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,HIGH);
digitalWrite(6,HIGH); digitalWrite(7,HIGH); digitalWrite(8,HIGH); digitalWrite(9,LOW);
}
if (inByte == '8'){// 2,00v 0,015A
Serial.println("30,00 mW");
digitalWrite(2,HIGH); digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,HIGH);
digitalWrite(6,HIGH); digitalWrite(7,HIGH); digitalWrite(8,HIGH); digitalWrite(9,HIGH);
}
}
}

#### keeper63

#1
##### Aug 09, 2011, 08:32 pm

1. Use the code button (#) on the editor.
2. This is a poor way to hook up a motor to the Arduino.
3. Try to turn on more than one motor at "full power", and you're likely to burn something out on the Arduino.

The proper way to control a motor is with a relay/transistor/MOSFET (single direction), or via an h-bridge of some sort (once again, composed of relays/transistors/MOSFETs); you also can't neglect the flyback diodes, either (as well as noise suppression capacitors on the motor itself).

If your example hasn't burned out your Arduino yet, it is surely on its way.

Read the following tutorials to gain a better understanding of what to do right (from Mike Cook's - aka Grumpy_Mike's - website):

http://www.thebox.myzen.co.uk/Workshop/Introduction.html
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

#### sobratthammer

#2
##### Aug 09, 2011, 10:32 pm
I use the blue LED as the fly-back diode. I calculated the paralell circut to sum of 0,05A trugh the motor. On full trottle. Any fastere i would've burn out the arduino. The green diodes prevent analog port draw current on LOW pins from HIGH pins.

#### focalist

#3
##### Aug 10, 2011, 12:27 am
Huh.  LED's for flyback?  Never thought of that for some reason.. though I suppose there's no reason it wouldn't work.  I don't know if it would be a good idea on a large coil with a big spike, but I guess a diode is a diode is a diode whether it emits light or not..

#### Grumpy_Mike

#4
##### Aug 10, 2011, 12:38 am
Quote
but I guess a diode is a diode is a diode whether it emits light or not.

Yes but check the data sheet an LED has a poor reverse breakdown and poor reverse current ratings. That is they are quite easy to blow up.

#### sobratthammer

#5
##### Aug 10, 2011, 01:17 am
I  was going to use a real fly-back diode on my breadboard, but the leads on that diode are  thicker than the holes in my breadboard.

#### Grumpy_Mike

#6
##### Aug 10, 2011, 07:42 amLast Edit: Aug 10, 2011, 05:29 pm by Grumpy_Mike Reason: 1
So that's alright then, laws of physics will change because of that.

#### focalist

#7
##### Aug 10, 2011, 05:28 pmLast Edit: Aug 10, 2011, 05:34 pm by focalist Reason: 1
Yeah that's what I was thinking.  They can't have anywhere near the tolerances that a "standard" diode has.. but for something tiny, it would work, at least for a while.  For that matter, I guess it would be fine to use a transistor connected as a diode.. better than nothing but surely going to fail long before using the proper device for the job.

I'm a huge fan of doing things the wrong way just to see if it will work.. I think we file this in the "Works but Not Recommended" column of EE Design.  Come to think of it, I had to check twice that it wasn't me in the first place, being so cruel to those little 'lectrons

#### peertje

#8
##### Aug 10, 2011, 06:53 pm
some recommendations:
use switch case instead of if if if
use transistors and diodes
control the current in a binary way wit R1 = 50 ohm; r2 = 100 ohm; r3 = 200 ohm etc.
or better, use PWM.
Use a H-bridge to control the motor in both directions

#### sobratthammer

#9
##### Aug 11, 2011, 04:01 am
hmm yes this was nicer...
Code: [Select]
`/*project: control motor speed (in free run);for this project:8 x 560R resistors1 x breadboard/circuitboard8 green LED1 blue LED5v motor,20mA free runn, this one was 13ohm (motor) Arduino duemilanovejump wires maybe.the circuit:GND =>-motormotor+ => R1(560) => - LED1+ => digital in (2)motor+ => R2(560) => - LED2+ => digital in (3)motor+ => R3(560) => - LED3+ => digital in (4)motor+ => R4(560) => - LED4+ => digital in (5)motor+ => R5(560) => - LED5+ => digital in (6)motor+ => R6(560) => - LED6+ => digital in (7)motor+ => R7(560) => - LED7+ => digital in (8)motor+ => R8(560) => - LED8+ => digital in (9)motor+=-LEDblue+ => -motor (If you can use a real flyback diode)(LED1 - LED8 can also be a real flyback diode, but use higher resistor for R1 - R8);ProgramEnter number between 0 and 8 to control speed of motor.*/byte ledPins[] = {0,2,3,4,5,6,7,8,9};void setup(){// setup   Serial.begin(9600);  for(byte cnt=1;cnt<9;cnt++)//if cnt is les than 9 then add 1 and execute serial and set pin mode  {Serial.println(cnt,DEC); pinMode(ledPins[cnt],OUTPUT);}  pinMode(13,OUTPUT);//program run indicator}// end of setupbyte inByte;void loop(){// Start of looping programif (Serial.available() > 0) {    inByte = Serial.read();    switch (inByte)     {//switch cases starts here     case '0':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}break;     case '1':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<2;cnt++){digitalWrite(ledPins[cnt],HIGH);}break;     case '2':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<3;cnt++){digitalWrite(ledPins[cnt],HIGH);}break;     case '3':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<4;cnt++){digitalWrite(ledPins[cnt],HIGH);}break;     case '4':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<5;cnt++){digitalWrite(ledPins[cnt],HIGH);}break;     case '5':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<6;cnt++){digitalWrite(ledPins[cnt],HIGH);}break;     case '6':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<7;cnt++){digitalWrite(ledPins[cnt],HIGH);}break;     case '7':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<8;cnt++){digitalWrite(ledPins[cnt],HIGH);}break;     case '8':for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],LOW);}for(byte cnt=1;cnt<9;cnt++){digitalWrite(ledPins[cnt],HIGH);}break; }//switch cases ends here} else {//if no serial trafic, blinkdigitalWrite(13,HIGH);delay(100);digitalWrite(13,LOW);delay(100);}// end of else}//end of loop routine code`

#### Grumpy_Mike

#10
##### Aug 11, 2011, 10:16 pm
See a pattern in those case statements?
You can get rid of them altogether and just have two for() loops.

Hint
1) the first loop happens in every case so just do it
2) use the inByte variable as the limit counter in the second loop.

#### sobratthammer

#11
##### Aug 12, 2011, 09:13 am
Quote

1) the first loop happens in every case so just do it
2) use the inByte variable as the limit counter in the second loop.

1) * that will cause all pinout to LOW, what ever input is
2) * '0' is a string not a number.

#### Grumpy_Mike

#12
##### Aug 12, 2011, 10:34 am
1) No because that is what you do now followed by setting the outputs
2) Convert it to a number. It is a single char so an OR with 0xf is all that is needed

#13
I stick to KISS.

#### Grumpy_Mike

#14
##### Aug 12, 2011, 10:41 pm
Your choice but it makes you look like a rank beginner.

Go Up