User Tools

Site Tools


courses:intro.prototyping.spring.2014.mar18

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
courses:intro.prototyping.spring.2014.mar18 [2014/03/20 02:01]
rickard
courses:intro.prototyping.spring.2014.mar18 [2014/03/20 02:03] (current)
rickard
Line 1: Line 1:
 +=== Transistors and motors ===
 +
 +==Transistors==
 +The transistor is a great component for letting an Arduino control a circuit it can't power itself. The component has three legs called Base, Collector and Emitter. ​
 +
 +{{:​courses:​arduino:​tip120.png?​nolink&​600|}}
 +
 +
 +Exactly which leg is which depends on which model of transistor one is using (here the TIP120), but in any case you basically splice the component into the circuit to be controlled so that the collector is connected towards plus and the emitter is connected towards minus and the base is connected to an Arduino pin. For the Arduino to be able to control the separate circuit, they need a common reference to what 0V is. Because of this you should connect a cable between the Arduinos'​ ground and the minus of the circuit you're controlling. As you can see in the picture below.  ​
 +
 +**HOWEVER**,​ remember that controlling a **DC motor** this way is both dangerous for the Arduino, since there is a risk of current to surge back when a DC motor is slowing down from spinning, and you're also only able to control it's spin in one direction. Which is why I brought up the Motorshield,​ further down, for motors specifically.
 +
 +{{:​courses:​arduino:​5_dclamptran.jpg?​nolink&​600|}}
 +
 +
 +==Servos==
 +A servo is an electric motor usually used in radio controlled vehicles and contraptions. Useful because of the high degree of control you can have over it's position. They come in different sizes and strengths, but mostly, you get to choose between two functions. ​
 +
 +The normal function is that a servo will be able to turn about 180 degrees and in your programming you can decide what degree the servo should position itself on, and it will move there as fast as it can. Which is not that fast.
 +
 +To control the servo you use functions from a prepared library of functions called servo.h that now is a part of the Arduino programming environment. ​
 +The functions in this library are:
 +
 +attach(pin#​) == specify which pin controls your servo
 +
 +write(degrees) == position the servo 
 +
 +writeMicroseconds(microSeconds) == another way to position
 +
 +read() == gives you the current angle 
 +
 +attached() == checks if a servo is attached
 +
 +detach() == release the servo
 +
 +There are also "full rotation"​ or "​continous"​ servos, that has no stop and will potentially turn forever. You can use the same commands as for a normal servo, but instead of a position in degrees, your number will decide the speed and direction your servo will move. 90 will mean standing still, 0 full speed one way, and 180 full speed the other. And numbers in between....well you get it. Because servos are based on analog electronics,​ standing still might not be EXACTLY 90. You might have to tweak that number to find what stands still for your servo.
 +
 +What's neat about servos is that you can run them off the arduino itself.
 +
 +{{:​courses:​2012.servo.png|}}
 +
 +A simple example of just making the servo move degree by degree:
 +<code java>
 +#include <​Servo.h> ​
 + 
 +Servo myservo; ​ // create servo object to control a servo 
 +                // a maximum of eight servo objects can be created ​
 + 
 +int pos = 0;    // variable to store the servo position ​
 + 
 +void setup() ​
 +
 +  myservo.attach(9); ​ // attaches the servo on pin 9 to the servo object ​
 +
 + 
 + 
 +void loop() ​
 +
 +  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees ​
 +  {                                  // in steps of 1 degree ​
 +    myservo.write(pos); ​             // tell servo to go to position in variable '​pos' ​
 +    delay(15); ​                      // waits 15ms for the servo to reach the position ​
 +  } 
 +  for(pos = 180; pos>=1; pos-=1) ​    // goes from 180 degrees to 0 degrees ​
 +  {                                ​
 +    myservo.write(pos); ​             // tell servo to go to position in variable '​pos' ​
 +    delay(15); ​                      // waits 15ms for the servo to reach the position ​
 +  } 
 +
 +</​code>​
 +
 +
 +
 +==DC motors== ​
 +Another means of motion is the regular DC motor that comes with many toys. You can't really control them in detail, but you get speed! In the workshop we also have DC motors connected to small gearboxes that will give you slower but powerful action. Running DC motors uses more power than is healthy to squeeze out of an arduino. Luckily we have a bunch of [[https://​www.adafruit.com/​products/​81|motorshields]] to use. 
 +
 +Motorshields can help you run motors (and also servos) but can make use of an external powersource without punishing the arduino. Another good reason to use the motorshield is that to make a DC motor turn the other way you physically have to change polarity (reverse plus and minus), which is difficult if you're not doing it yourself. But the motorshield does that FOR you.
 +
 +{{:​courses:​dcmotor_w_mshield.jpg|}}
 +
 +To make it easier to use servos and motors with the motorshield,​ there is a {{:​courses:​2012.afmotor.lib.zip|library}} with prepared functions. Download the zipfile and extract it in a folder called "​libraries"​ in the folder you keep your projects in.
 +
 +Here's a how to make a DC motor do..well...pretty much all you can do with it. You try!
 +<code java>
 +#include <​AFMotor.h>​
 +
 +AF_DCMotor motor(2, MOTOR12_64KHZ);​ // create motor #2, 64KHz pwm
 +
 +void setup() {
 +  Serial.begin(9600); ​          // set up Serial library at 9600 bps
 +  Serial.println("​Motor test!"​);​
 +  ​
 +  motor.setSpeed(200); ​    // set the speed to 200/255
 +}
 +
 +void loop() {
 +  Serial.print("​tick"​);​
 +  ​
 +  motor.run(FORWARD); ​     // turn it on going forward
 +  delay(1000);​
 +
 +  Serial.print("​tock"​);​
 +  motor.run(BACKWARD); ​    // the other way
 +  delay(1000);​
 +  ​
 +  Serial.print("​tack"​);​
 +  motor.run(RELEASE); ​     // stopped
 +  delay(1000);​
 +}
 +
 +</​code>​
 +
 +==Stepper motor==
 +There is a third kind of motor called Stepper motor which is sort of inbetween. You can control it's movement in individual steps, but you have less information of the exact position than with a servo. The tricky thing with using stepper motors is that the cables need to be used in pairs. Luckily you can often find which cables go together by datasheet, webinfo or combining touching cables in pairs, and when you feel a little extra resistance, you've got it right.
 +
 +Here's an example to try out a stepper motor:
 +<code java>
 +#include <​AFMotor.h>​
 +
 +// Connect a stepper motor with 48 steps per revolution (7.5 degree)
 +// to motor port #2 (M3 and M4)
 +AF_Stepper motor(32, 1);
 +
 +void setup() {
 +  Serial.begin(9600); ​          // set up Serial library at 9600 bps
 +  Serial.println("​Stepper test!"​);​
 +
 +  motor.setSpeed(100); ​ // 10 rpm   
 +}
 +
 +void loop() {
 +  Serial.println("​Single coil steps"​);​
 +  motor.step(100,​ FORWARD, SINGLE); ​
 +
 +}
 +</​code>​
 +
 +
 +
 +==Resources==
 +Here is the full info of the motor Shields:
 +
 +[[http://​learn.adafruit.com/​adafruit-motor-shield/​overview|Motorshield]]
 +
 +[[http://​arduino.cc/​en/​Tutorial/​Knob#​.Uyqsb-mPIdU|Example using Servo with Arduino]]
 +
 +[[http://​www.youtube.com/​playlist?​list=PLBcrWxTa5CS2eHsNXcfdCML4syi7t34p1|Sparkfuns'​ video series on robotics]]
 +
 +[[http://​www.societyofrobots.com/​actuators_servos.shtml|Servos]]
 +
 +[[http://​www.societyofrobots.com/​actuators_dcmotors.shtml|DC-Motors]]
 +
 +[[http://​www.societyofrobots.com/​actuators_steppers.shtml|Stepper motors]]
  
courses/intro.prototyping.spring.2014.mar18.txt · Last modified: 2014/03/20 02:03 by rickard