1/20/14 ¡ DC MOTORS 101 Servos, DC motors, Stepper motors, motor drivers Agenda ¨ Start with PWM (Pulse Width Modulation) ¤ “analog” output feature of Arduinos – equivalent to varying output voltage ¤ Use for dimming LEDs or speed control of DC motors ¨ DC motors ¤ Basic operation ¤ Driving with transistors ¤ Bi-directional drive with H-Bridge ¨ Stepper motors ¤ Unipolar and Bipolar versions ¤ Stepper driving circuits ¤ Example of Arduino-based stepper object library 1 ¡
1/20/14 ¡ Analog vs. Digital Input ¨ digitalRead(pin); ¤ Returns true/false, 1/0, off/on ¤ Great for switches ¨ analogRead(pin); ¤ Returns a range of values ¤ Actual voltage on the input should be between 0-5v ¤ Converted to 0-1023 discrete steps using ADC ¤ ADC is why the analog pins are special… Analog vs. Digital Output ¨ Digital Out = on/off, up/down, left/right, black/white, etc ¨ Analog Out = how hot, how fast, how bright, how loud, how grey? etc. ¨ As with digital output, we have current considerations, this time, how can we generate enough energy to control analog devices? ¨ Can we generate an analog voltage between 0-5v on the output? 2 ¡
1/20/14 ¡ ADC and DAC Arduino version is a 10-bit ADC Arduino has no built-in DAC… Pulse Width Modulation 3 ¡
1/20/14 ¡ Arduino PWM ¨ The Arduino has 6 PWM pins (3, 5, 6, 9, 10, 11) that can receive an analogWrite() command. ¤ analogWrite(pin, pulsewidth); ¨ 0 = 0 volts, 255 = 5V ¤ a number in between will provide a specific PWM signal. ¤ 128 will be seen as 2.5v, for example Knob Fade int knobPin = A0; // the analog input pin from the potentiometer int ledPin = 9; // pin for LED (a PWM pin) int val; // Variable to hold light sensor value void setup () { pinMode(ledPin, OUTPUT); // declare ledPin as output pinMode(knobPin, INPUT); // knobPin is an (analog) input } void loop () { val = analogRead(knobPin); //read the value from the pot val = map(val, 0, 1023, 100, 255); // map to reasonable values val = constrain(val, 0, 255); // Make sure it doesn’t go out of range analogWrite(ledPin, val); // write it to the LED using PWM } 4 ¡
1/20/14 ¡ Arduino PWM Alternative ¨ It is also possible to create a pwm signal by simply ‘pulsing’ a digital out pin very quickly: ¤ digitalWrite(pin, HIGH); delayMicroseconds(pulsewidth); // note: “Micro…!” digitalWrite(pin, LOW); delayMicroseconds(pulsewidth); ¨ Or, use a Motor Shield… Dimming Lights ¨ Incandescent blubs ¤ Vary the voltage, or use PWM ¨ LEDs ¤ Use PWM ¨ Fluorescents (compact or regular) ¤ In general you can’t dim them… ¤ There are a few “dimmable” compact fluorescents 5 ¡
1/20/14 ¡ Dimming Lights ¨ Fixed resistance and varying voltage = varying current ¤ V = IR, V/R = I ¨ Pay attention to the lamp ratings ¤ Remember P = IV ¨ For high current devices, you will need to electrically isolate the Arduino from the high current circuit. Activity: Arduino “flickering candle” ¨ Connect an LED to the Arduino on a PWM output pin ¤ Always remember current-limiting resistor… ¨ Write a program to make that LED “flicker” like a candle ¤ Chose between random brightness values ¤ Choose random times to change the value 6 ¡
1/20/14 ¡ Servos Also Use PWM ¨ From Tod Kurt, totbot.com Servos Also Use PWM ¨ From Tod Kurt, totbot.com 7 ¡
1/20/14 ¡ Servo Innards Servo Specs ¨ From Tod Kurt, totbot.com 8 ¡
1/20/14 ¡ Servo Control ¨ From Tod Kurt, totbot.com Servo Control ¨ From Tod Kurt, totbot.com 9 ¡
1/20/14 ¡ Programming Servo Motors ¨ The first thing to do when programming servos is work out the pulse range of the specific servo you are using. ¤ With the arduino, this can be between 0.5 and 2.5 ms. ¤ Servos also need ‘refresh’ time between pulses -- usually 20ms ¨ The frequency of the Arduino’s built-in pwm pins is too high for servos, so we have to use the pseudo-pwm method instead: digitalWrite(servoPin, HIGH); delayMicroseconds(pulse); digitalWrite(servoPin, LOW); delayMicroseconds(refreshTime); ¨ Or alternatively, use the Arduino servo library ¨ Black = gnd ¨ Red = +5v ¨ Yellow = signal Servo Example Program #include <Servo.h> // include the built-in servo library Servo myservo; // create a servo object to control the servo (one per servo) int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attach servo control to pin 9 } void loop() { for (pos = 0; pos < 180; pos++) { // go from 0 to 180 degrees myservo.write(pos); // move the servo delay(20);l // give it time to get there } for (pos = 180; pos>=1; pos--) { // wave backwards myservo.write(pos); delay(20); } } 10 ¡
1/20/14 ¡ Servo Functions ¨ Servo is a class ¤ Servo myservo; // creates an instance of that class ¨ myservo.attach(pin); ¤ attach to an output pin (doesn’t need to be PWM pin!) ¤ Servo library can control up to 12 servos on our boards ¤ but a side effect is that it disables the PWM on pins 9 and 10 ¨ myservo.write(pos); ¤ moves servo – pos ranges from 0-180 ¨ myservo.read(); ¤ returns the current position of the servo (0-180) Servo Object (class instance) .write 0 to 180 Pin 9 Name: myservo .attach Pin number position: 67 deg Attached to: pin 9 return current .read deg position 11 ¡
1/20/14 ¡ Moving on… ¨ Write a program to control the position of the servo from a pot, or from a photocell ¤ remember analogRead(); values are from 0-1023 ¤ measure the range of values coming out of the photocell first? ¤ use Serial.print(val); for example ¤ use map(val, in1, in2, 0, 180); to map in1-in2 values to 0-180 ¤ Can also use constrain(val, 0, 180); Side Note - Power ¨ Servos can consume a bit of power ¤ We need to make sure that we don’t draw so much power out of the Arduino that it fizzles ¤ If you drive more than a couple servos, you probably should put the servo power pins on a separate power supply from the Arduino ¤ Use a wall-wart 5v DC supply, for example 12 ¡
1/20/14 ¡ Moving on: Electromagnetism ¨ Permanent magnets have magnetic fields that flow from North to South poles of the magnet Electromagnets ¨ Current flowing through a conductor creates a magnetic field ¤ Right hand rule: Point your thumb in direction of current, and your fingers curl in the direction of the magnetic field 13 ¡
1/20/14 ¡ Electromagnets ¨ Current flowing through a coiled conductor creates a magnetic field ¤ Right hand rule: Point your thumb in direction of current, and your fingers curl in the direction of the magnetic field ¤ Alternately, curl your fingers in the direction of the current, your thumb points to North magnetic pole Electromagnets ¨ Current flowing through a coiled conductor creates a magnetic field ¤ Important implication: When the current it reversed, so is the magnetic field! 14 ¡
1/20/14 ¡ Aside: Current dir vs. Carrier dir ¨ Current (amps) flows from positive to negative ¤ “positive current” ¨ BUT – the charge carriers are (usually) electrons ¤ Electrons have negative charge ¤ So – the electrons move from negative to positive ¨ Charge carriers move in opposite direction from current! ¤ Yes, it’s confusing… ¤ We’ll tackle this again talking about transistors! Electromagnets and Motors ¨ Motors use the reversing feature of an electromagnet, and magnetic attraction ¤ Permanent magnets on one side, electromagnets that can switch polarity on the other 15 ¡
1/20/14 ¡ Basic DC Motor Behavior ¨ Electrical energy -> mechanical motion ¨ Motors have fixed parts (stator) and moving parts (rotor) ¨ A DC motor consists of: ¤ Commutator n Rotary switch n Reverses current twice every cycle ¤ Electromagnetic coils n Opposing polarities switched by commutator n Inertia causes them to continue rotating at the moment of polarity switching ¤ Fixed Magnets n Opposing polarities Basic DC Motor Behavior ¨ Electrical energy -> mechanical motion ¨ Motors have fixed parts (stator) and moving parts (rotor) ¨ A DC motor consists of: ¤ Commutator n Rotary switch n Reverses current twice every cycle ¤ Electromagnetic coils n Opposing polarities switched by commutator n Inertia causes them to continue rotating at the moment of polarity switching ¤ Fixed Magnets n Opposing polarities 16 ¡
Recommend
More recommend