= Move It! = There are only two more lessons before you're ready to build a mini-bot! This lesson covers motors (and a few other things) which are at the core of most robot movement. To understand motors and how they work, you need to learn a little about electromagnetism or, more specifically, electrodynamics. == Magnetism == [[Image(magneto.jpg, align=right, width=200, margin=10)]] Magnetism was discovered by the ancient Greeks (and also by the vikings, Chinese, etc.) who noted that lodestone (magnetite), a naturally occurring iron ore was attracted to iron. They also discovered that a block of lodestone suspended from a string would point to magnetic North and the compass was born. **Trivia**: It's called a magnet because lodestone was found in Magnesia, Turkey. == Electromagnetism == The relationship between magnetism and electricity was studied by Hans Christian Oersted, Andre-Marie Ampere and tied together by Michael Faraday and James Clerk Maxwell and polished by Oliver Heaviside and Heinrich Hertz (note: Heaviside should have fired his publicist...all the rest have major units of measure named after them). **Exercise**: make an electromagnet by coiling up a lot of wire and passing a current through it. Observe its effects on a compass and/or on iron shavings Whenever current moves through a conductor (like a wire), it generates a magnetic field. The direction of that magnetic field is perpendicular to the direction of the current flow and can be remembered using the right hand rule. If the wire is coiled up, the magnetic field is enhanced. It can be further enhanced and directed by inserting a ferrous material inside the coil. Much of the early work on electricity and magnetism was done by Michael Faraday. They discovered that when a conductor is carrying a current, and therefore generating a magnetic field is placed inside another magnetic field, an **electromotive force** results that applies force to push the conductor. The direction of that force is often described using the left-hand rule. * [https://www.youtube.com/watch?v=Dc8Vay7mRss British electromagnetism tutorial] Electro-motive force lets us use electricity to move things. One of the simplest applications of electromotive force is the solenoid. A solenoid actuator is an electromagnet (coil of wire) that is energized by passing electric current through it; the resulting magnetic field attracts an iron or steel rod causing it to move. When the electric current is turned off, a spring typically returns the rod to its original position. You can use your [http://playground.arduino.cc/Learning/SolenoidTutorial arduino to control a solenoid], but there are some important tricks to be aware of. 1. Electromagnets are formed by coils of wire. Wire has very low resistance, so it will conduct a lot of current...far more than your Arduino can supply directly. So the Arduino must control the solenoid indirectly: the arduino will activate an electronic switch that takes a small amount of current to activate and can then pass a large amount of current. These switches can be electro-mechanical (called relays) or solid state (transistors or similar). 2. When a current is passed through a coil a magnetic field builds up around the coil. When the current is removed (when the electricity is turned off), the field collapses back into the coil inducing a flow of current. If the current has no place to go, the voltage will build, potentially to quite high levels which can destroy the electronic switch. This phenomenon is called **[https://learn.digilentinc.com/Documents/390 inductive flyback]** So an escape path is needed for the current; typically a protection diode (aka flyback diode) is used. == DC Motors == By far the most common application is the motor. There are actually many types of motors, but in our experiments we will be using common DC motors. * [https://www.youtube.com/watch?v=OpL0joqJmqY Retro DC motor tutorial] * [https://www.youtube.com/watch?v=LAtPHANEfQo a little more modern] * [https://www.youtube.com/watch?v=ZAY5JInyHXYv Brushless DC Motors] == Motor Control == Like solenoids, the electromagnets in motors require a great deal of current; far more than the Arduino itself can source or sink. External switches are required to control the flow of electricity through each motor in a robot. Your kit contains a switching transistor; you can use the transistor to control the flow of electricity through a motor: [[Image(bjt.png, align=right, width=200, margin=10, title=BJT Transistor)]] There are many different types of transistors, but most are three-terminal devices where the current or voltage at one terminal controls the flow of electricity through the other two terminals. The most common transistors used today are either bipolar-junction transistors (BJTs) and field-effect transistors (FETs). BJTs are an older technology, but are very inexpensive and are still in common use. You can use a BJT as a switch for motor control, but metal-oxide-semiconductors (MOSFETs) offer many advantages. You can learn about BJTs for motor control in this [https://www.youtube.com/watch?v=sRVvUkK0U80 brief video tutorial]; you should watch this before moving on. A primary advantage of the MOSFET is that it has a very low resistance when on (when the control voltage on the gate terminal allows current to flow between the other two terminals (source and drain). Because there is very little resistance, the transistor can pass large amounts of current without getting hot. If you recall Ohms law, E=IR, if the resistance is very low (a few tenths or hundredths of an ohm), a lot of current can pass without the voltage drop getting large. This is important because if you passed 10Amps (a lot of current) through a transistor that offered 10 ohms of resistance, it would drop 100 volts and the power dissipated (as heat) would be E*I = 1000 Watts! [[Image(https://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Lateral_mosfet.svg/2000px-Lateral_mosfet.svg.png, align=left, width=200, title=MOSFET transistor)]] **Exercise**: use the IRL3715Z N-FET transistor in your kit to allow your arduino to control the flow of electricity through a motor. Turn the motor on or off in response to a switch or to your ultrasonic controller. Use a protection diode (also in your kit) to prevent the back EMF from the motor from destroying the transistor (and potentially you Arduino). You can use the micro-motors provided (see a mentor if you don't have one) or you can use one of the gear-motors from the mini-bot kits (also ask a mentor). The first step in working with any electronic component is to review its [http://www.irf.com/product-info/datasheets/data/irl3715z.pdf datasheet]. The datasheet will give you all of the component's specifications and limits and will often give you examples of how to use it. The IRL3175Z datasheet tells you that it turns on when the voltage between the gate and source terminals is above 4.5 volts (our Arduino uses 5v), that the absolute maximum voltage that can flow between the source and drain (to the motor) is 20v and above that, the device will be damaged. It also tells you that the maximum resistance between the source and drain pins when the transistor is 'on' is 11 milli-ohms (very low), so if you used this transistor to switch 10A (a lot of current) to a motor, it would drop at most 10A * 0.011ohms = 0.11 volts and dissipate 0.11 volts * 10A = 1.1W as heat which is quite manageable. With a heat sink (metal fins attached to the transistor to give it more surface area for heat transfer), it can switch as much as 50A at room temperature. You can learn more about MOSFETs [https://www.youtube.com/watch?v=GrvvkYTW_0k here] how to use MOSFETs for motor control [https://www.youtube.com/watch?v=3tHIXPoKPNc here]. This [https://www.youtube.com/watch?v=I1h-HZmIJ4E video tutorial] shows an N-Channel MOSFET being used to turn a large motor on and off and includes a good deal of information about MOSFETs and how they work. == Direction Control == In robots, we want to be able to run the motor in either direction (forward or reverse). To reverse the direction of a DC motor, we must reverse the direction of current through it. Direction control is typically achieved using a circuit called an H-Bridge. An H-bridge can be formed by a DPDT switch (or two SPDT switches), but for computerized direction control, the H-Bridge is formed using electronic switches (transistors). * [http://www.dprg.org/tutorials/1998-04a/ Good tutorial] * [https://itp.nyu.edu/physcomp/labs/motors-and-transistors/dc-motor-control-using-an-h-bridge/ Detailed tutorial] == Speed Control == If you don't consider the load on a motor, the higher the voltage you apply, the faster the motor will spin (at least for an ideal DC motor). However, it's not easy for a digital device like the Arduino to adjust the analog voltage to a motor; instead, motor speed control is achieved using a clever technique called [https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM pulse-width modulation or PWM]. You may have used PWM with your Arduino to vary the brightness of an LED in the "Fade" sketch (if you didn't, [https://www.arduino.cc/en/Tutorial/PWM please try it now]. **Exercise**: Observe the PWM signal with an oscilloscope (ask a mentor for help) We will use an [https://www.adafruit.com/products/81 Arduino motor shield] to control the direction and speed of the DC motors in our mini-bots. Although you can program the shield yourself, a convenient [https://learn.adafruit.com/adafruit-motor-shield/library-install library] is available for download. The motor shields are based on the [http://www.rakeshmondal.info/L293D-Motor-Driver L293D motor driver]. The L293D is an integrated circuit (IC) designed for DC motor control made by Texas Instruments; you can see the datasheet [http://www.ti.com/lit/ds/symlink/l293.pdf here]. The L293D allows the 5v outputs of your Arduino to control the flow of electricity to motors up to 36v and 600mA (with suitable heat sink). We use the L293D shields because they are very inexpensive, however they use BJT technology, not MOSFETs so they can easily overheat. An easy upgrade to the motor shields we have is to replace the socketed L293D chips with a more modern TI [http://www.ti.com/lit/ds/symlink/sn754410.pdf SN754110] (available [https://store.ti.com/SN754410NE.aspx here]) which can supply 1A vs. 600mA (see [http://webbot.org.uk/WebbotLibDocs2/44212.html here]). Note that for either chip, if running at high current, it would probably be a good idea to attach a heat sink to the IC package using thermally conductive glue to help with heat dissipation. **Exercise**: Connect a motor shield to an arduino and use it to control the speed and direction of one of the mini-bot gear motors. If your USB port can't power the motor (very possible), use an AA battery pack (see a mentor) to provide a separate power source for the motors via the motor shield power connections.