= 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(http://heroicuniverse.com/wp-content/uploads/2015/05/x-men-days-of-future-past-michael-fassbender-magneto-post-hugh-jackman-x-men-who-should-be-the-new-face-of-the-x-men-movies.jpg, align=right, width=200)]] 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: **Exercise**: use the 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). == 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. **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.