Top Banner

of 15

Arduino Microcontroller Workshop

Apr 05, 2018

Download

Documents

Ramani6540
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/31/2019 Arduino Microcontroller Workshop

    1/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Microcontroller Introduction

    A microcontroller is a device that allows you to program it to do various things. The most basic thing is

    turning a digital output on or off. When a digital output is on it has a digital value of 1 and outputs 5V.

    When a digital output is off, it has a digital value of 0 and outputs 0V. Digital pins can also be used asinputs. So when 5V is applied to a digital input pin, it will read as a 1. Otherwise it will read as a 0. In

    digital circuitry, there is nothing between 0V and 5V, it is one or the other. Digital circuits can be used

    for things like turning LEDs on and off. You can program the LED to turn on and off according to some

    pattern.

    Some microcontrollers like the Arduino have other stuff besides the digital output , these features are

    shown below.

    Components Needed

    Arduino with USB cable and free software 4 x LEDs, any colour Potentiometers (5K) Breadboard Phototransistor Power Transistor (TIP41C, but could be any) Various resistors Servo DC motor (12V) Diode (1N4002, but any will do) Multi-meter and oscilloscope would be nice but not needed Various wires to connect stuff together

    Arduino Introduction

    The picture on the right is the Arduino.

    The Arduino is the name of the board

    that has everything you see in the picture

    as well as the microcontroller. The name

    of the microcontroller is the ATMega328,

    but you do not need to know that. I'll

    refer to the entire thing as the Arduino.

    The Arduino is programmed through the

    USB jack; it also receives all of its power

    (5V) through the USB jack.

  • 7/31/2019 Arduino Microcontroller Workshop

    2/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    The Arduino is programmed in C. In addition to the standard For-

    loops and If-statements the Arduino comes with a library of functions

    that make life easier. We will look at these later on.

    To program it, all you do is install the free software

    (http://www.arduino.cc), then plug in the USB and hit program. TheArduino Software is shown on the left. It is not very complicated. You

    just type instructions into the box and hit the second last button on

    the top to upload it to the Arduino. The key is what to type into the

    box. The thing about microcontrollers is that programming is usually

    the easy part, you also have to know how to hook up the circuitry to

    go with it. We will be doing both throughout this workshop. We

    won't be doing any actual projects, the idea is to introduce the concepts so that you will be able to do

    this stuff on your own.

    Note: The Arduino software comes with about 30 examples of common applications (File -> Examples),

    you should start most projects from one of these and possibly combine several examples to get what

    you want.

    Flash the On-Board LED

    We'll start with just programming the Arduino, we won't use any circuitry. The Arduino

    comes with a small LED already on the board. It is connected to digital pin 13 on one

    side and GND on the other. The schematic shown on the left is already built onto the

    Arduino board. All you have to do is turn pin 13 on and off.

    There is already an example that does this, so let's just load that up. Go to File ->

    Examples -> Digital -> Blink. Make sure the Arduino is plugged in and hit Upload (not

    the play button, the arrow pointing to the right, second last at the top). You should be

    seeing the little LED turning on and off every second on the board. Looking at the code,

    you'll see most of the lines are comments, but the comments basically explain

    everything. The code is shown below.

    The KeyTo become good at this electronics, there are certain basic circuits you must

    memorize. Most projects involve mixing and matching these basic circuits to get

    what you want. This workshop will show you several of these basic circuits. None of

    them are complicated.

    http://www.arduino.cc/http://www.arduino.cc/http://www.arduino.cc/http://www.arduino.cc/
  • 7/31/2019 Arduino Microcontroller Workshop

    3/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Summary of code:

    There are always at least two functions in each program. setup() is run once when theArduino first powers up; after that loop() is run over and over again...forever.

    The int ledPin = 13; line is outside both functions at the top. This means that both functionswill be able to use the variable ledPin.

    Most digital pins can be configured as inputs or outputs. An input means the value of the pinis read by the program, and output means the value of the pin is set by the program. Here

    we want the program to turn the pin on and off, so it needs to be set to digital 1 (High) and

    then 0 (low) repeatedly. To do this is must be an output.

    The loop section turns the pin on and off every second. This means digital 1 (5V) and digital0 (0V).

    /*

    Blink

    Turns on an LED on for one second, then off for one second, repeatedly.

    The circuit:

    * LED connected from digital pin 13 to ground.

    * Note: On most Arduino boards, there is already an LED on the board

    connected to pin 13, so you don't need any extra components for this example.

    Created 1 June 2005

    By David Cuartielles

    http://arduino.cc/en/Tutorial/Blink

    based on an orginal by H. Barragan for the Wiring i/o board

    */

    int ledPin = 13; // LED connected to digital pin 13

    // The setup() method runs once, when the sketch starts

    void setup() {

    // initialize the digital pin as an output:

    pinMode(ledPin, OUTPUT);

    }

    // the loop() method runs over and over again,

    // as long as the Arduino has power

    void loop()

    {

    digitalWrite(ledPin, HIGH); // set the LED on

    delay(1000); // wait for a second

    digitalWrite(ledPin, LOW); // set the LED off

    delay(1000); // wait for a second

    }

  • 7/31/2019 Arduino Microcontroller Workshop

    4/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Try playing around with the code to make it faster or slower.

    Digital LED Chain

    Quick Sidebar - What is a breadboard?

    A breadboard lets you connect stuff together temporarily. This lets you

    quickly build a circuit, and not worry about whether it will need to be

    changed. The holes on a breadboard are 0.1" (inches) apart. This is a

    standard which means that most components

    will easily fit into the holes. Each hole has a

    clip which is shown on the right. When you

    stick a wire in the hole, the two pieces of

    metal move apart and then clamp the wire in place. This way the wire

    stays secure but can still easily be removed. You can see the clip under

    each hole on the left.

  • 7/31/2019 Arduino Microcontroller Workshop

    5/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    At the top you can see a breadboard showing its internal

    connections. There are many vertical strips of 5-holes (green). Each

    of those 5 holes are connected together. There is one set on top,

    and one on the bottom. They are separated by the bridge. This

    bridge is perfectly sized so that a standard chip can fit between two

    different 5-hole blocks.

    Finally there is the power strip. The power strip usually has a red or

    blue line running next to it. The power strip allows you to distribute

    power along the breadboard. The holes are connected from left to right as shown in the pictures above.

    Normally you would connect a positive voltage to the red strip and GND to the blue strip. Then you can

    easily place wires between the strip and any 5-hole block.

    Note: On some breadboards, the power strip is split into two pieces as shown above (yellow and

    orange), on other breadboards, the power strip is connected the entire way

    through. To find out which way yours is connected, use a multi-meter in

    "continuity" mode. In Continuity mode, the multi-meter will beep when the

    two leads are touching. This is a useful way to make sure parts are connectedthe way they should be. The symbol for continuity mode is usually a diode or

    a sound wave. To do the continuity check, first touch the two leads together

    to make sure you hear a sound. Then place two wires on opposite ends of

    the same power strip. Touch the leads of the multi-meter to each wire and

    listen for the sound. Move the wires around to other spots on the

    breadboard so that you understand how the board is connected internally. This will be very important in

    the next sections.

    Back to the LED ChainThe LED blink circuit used digital pin 13, which is already connected to an LED. We will now

    manually connect several LEDs and use some of the other pins. As shown in the schematic,

    we will need to connect the LED to GND and the resistor to a digital pin. Since each LED is

    connected to the same GND, we will use a power strip. Run a wire between the GND pin (in

    the 'power' section of the Arduino, opposite the

    digital pins) and the blue power strip. Then connect

    one end of the resistor to any of the digital pins on the

    Arduino and the other end to any 5-hole block on the breadboard.

    Finally connect the LED to the same 5-hole block as the resistor and

    the blue power strip (long side positive, short side negative). Now

    open up the example code for the blink program above and change

    the ledPin variable to the correct digital pin. Upload the code and

    see what happens.

    Once you get that working try modifying the circuit and the code to add 4 LEDs. Have them turn on one

    after another after a short delay. Do not disconnect this circuit, we will use all 4 LEDs in the next section.

  • 7/31/2019 Arduino Microcontroller Workshop

    6/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Analog InputSo far we have been using the digital inputs and outputs. Remember that digital is either 0 or 1. Here we

    will be using the analog inputs. Analog can be anything between 0 and 5V. The analog input on the

    Arduino is basically a voltmeter. When you apply some voltage between 0V and 5V, it will tell you

    exactly how much voltage you have.

    The confusing part here is remembering that the chip itself is digital. This means the Arduino has an

    analog to digital converter (ADC). In analog, there is an infinite number of voltages between 0V and 5V.

    Of course, you can't have infinitely small numbers on a computer so that means you can only have

    increments. In this case the Arduino has a 10-bit ADC. So that means that there are 210

    = 1024 possible

    values between 0V and 5V. Looking at the diagram should help. What happens is that if you put 0V on

    the analog input, you will get a 0; if you put 5V

    you will get 1023 (it starts counting from 0, so 0

    to 1023 is 1024 possibilities). This implies that if

    you put 2.5V you will get 512.

    From a math point of view, the easiest way of thinking about it is like a percentage. 2.5V is

    50% of 5V, so what is 50% of 1023? The answer is 512 (rounded). Thinking of it like a

    percentage will allow you to easily calculate the digital value of 1.37V ((1.37/5)*1023 = 280).

    Once you know how to do that, you can use an analog input to control anything. Let do this

    using a variable resistor. Variable resistor are called potentiometers or pots for short. Pots

    have 2 regular terminals, just like a resistor, but they also have a third terminal called a

    wiper. You can see the wiper in the middle of R2 in the diagram to the left. The wiper

    Remember: Digital PinsYou don't need to remember any code, you just need to remember that you can set

    a digital pin to either output a 0 or 1 or read the value fed into it as either 0 or 1.

    Every time you need to do this, just look at the example and start from there.

    Usage Examples

    LEDs on and off Relays (switches)

  • 7/31/2019 Arduino Microcontroller Workshop

    7/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    physically moves back and forth along the resistor. This in effect creates two resistors, one from GND to

    the wiper, and another from the wiper to 5V. By moving the wiper you can change the ratio between

    the two resistors.

    What we will do is connect the wiper to the analog input of the Arduino. So what will happen is that

    when the wiper is close to the GND side, the voltage will be almost 0V. When the wiper is close to the5V side, the voltage will be almost 5V. Or course the voltage can also be anything in between.

    So take a pot and then connect two sides of it to 5V and 0V on the Arduino, and then connect the

    middle wire to analog input 0 on the Arduino. Now as you turn the knob, the voltage at the analog input

    will vary. Open up the analog input example from File -> Examples -> Analog -> AnalogInput. This

    example should make the onboard LED flash, but the time between flashes should vary based on the

    position of the knob. Read through the comments because they explain exactly what is happening. The

    key to the whole thing is this line:

    sensorValue = analogRead(sensorPin);

    analogRead() will read the voltage at any of the 5 analog input pins (in this case sensorPin = 0), and then

    give back a value between 0 and 1023.

    Hopefully you still have the LEDs connected from before, so let's make LED 0 turn on only when the

    knob is past a certain value.

    Try to modify the example code yourself, but here's a hint:

    if (sensorValue >= 512) {

    digitalWrite(0, HIGH);

    }else {

    digitalWrite(0, LOW);

    }

    Once you get that working, change the code again, to make the LED chain into some kind of a bar graph.

    Make the first LED turn on at 256, then turn the first and second on at 512, then turn the first second

    and third on... What you should have is as you turn the knob, the LEDs turn on one after another, and

    then when you turn the other way they turn off one after another. You now have a very crude bar

    graph that tells you when the input voltage is at a certain level.

    In this case, the analog input is coming from manual input of the knob, but it is also very common to get

    input from a sensor. The key to making this work is to make sure the sensor will give a voltage between

    0V and 5V. That way you can always read the voltage and do whatever you want. We will do this next

    with a phototransistor. You can disconnect the pot, but leave the LEDs connected.

  • 7/31/2019 Arduino Microcontroller Workshop

    8/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Phototransistor

    First a quick introduction to transistors. A transistor has three terminals. In the

    schematic to the left, the base of the transistor is on the left side. The terminal

    at the top is the collector and the terminal with the arrow is the emitter. In a

    normal transistor, when you apply a small current to the base, you get a larger

    current that flows between the collector and emitter (the current flows in the

    direction of the arrow).

    A phototransistor is basically a normal transistor that has its base exposed. So instead of

    applying a small current to the base, you would instead rely on light to create a small

    current at the base. So when light hits the base of the transistor, it creates a small

    current there. That means that a much larger current will now flow between the

    collector and emitter. Note that anytime you have current, you must have voltage. So

    you can expect the voltage across the transistor to change as the current flow changes. If

    you have not yet learned about transistors don't worry, this is all you need to know for

    now.

    What we will do is set up the phototransistor to change its voltage when there is light

    and when there isn't. We will then monitor this change in voltage using the Arduino.First connect the phototransistor as shown in the schematic on the right. Note that the

    long lead is the emitter and the short lead is the collector. Once you have it hooked up,

    use a multi-meter to measure the voltage between GND and the collector. Move so that

    the light is shining directly on the phototransistor and check the voltage. Then place your

    Remember: Analog InputYou don't even need to remember the code here. You just need to remember that

    you can measure voltages between 0V and 5V and then have the Arduino do

    anything you want. Every time you need to use it, just look it up in the example.

    Usage Examples

    Read output from a sensor (light / dark, distance, accelerometer)

  • 7/31/2019 Arduino Microcontroller Workshop

    9/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    hand on top of the phototransistor to block the light and check the voltage. You should be able to see

    the voltage changing.

    To adjust the sensitivity, change the value of the resistor. If you put a small resistor like a 1K, the voltage

    will always be very low, but if you put a very high resistor the voltage will always be very high. The key is

    to find a resistor that allows you to easily change the voltage by moving your hand on top of thephototransistor. The actual value of the resistor will depend on the amount of lighting in the room you

    are in. 68K is what worked for me, it will most likely be different for everyone, but it should be pretty

    high, close to 50K.

    You may have already guessed that what we will now

    to is connect the collector pin to the analog input of

    the Arduino. So connect it to analog pin 0. Now,

    looking at the analog input example and the code for

    the LED bar graph, try writing a program that turn on

    the LEDs depending on how bright it is. Below is an

    excerpt from mine. There are many ways to do it and

    the numbers are mostly random, so try to do it on

    your own. The way I picked the numbers (235, 430...) is by looking at the multi-meter and checking what

    the largest and smallest voltages were as I moved my hand over the phototransistor. Let's say it was

    0.3V - 4V. That would translate to 81 - 818. I then made it so that a value slightly higher than 81 will turn

    on the first LED (235). That way all the LEDs would be off by default, but when I bring my hand close to

    the phototransistor, the first LED will turn off. Then as I bring my hand closer, the other LEDs would

    being to turn off.

    val = analogRead(0);

    digitalWrite(0, LOW);

    digitalWrite(2, LOW);

    digitalWrite(5, LOW);

    digitalWrite(7, LOW);

    if (val >= 235) {

    digitalWrite(0, HIGH);

    }

    if (val >= 430) {

    digitalWrite(2, HIGH);

    }

    if (val >= 624) {

    digitalWrite(5, HIGH);

    }

    if (val >= 780) {

    digitalWrite(7, HIGH);

    }

    You can now disconnect the 4 LEDs, but do not disconnect the phototransistor circuit.

  • 7/31/2019 Arduino Microcontroller Workshop

    10/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Servos

    A servo is a type of motor that allows you to select its position. Most servos move between 0 and 179.

    You can use an Arduino to tell the servo to go to any angle, say

    37, and then have it stay there. If you make it stay at 37, then

    even if you try rotating it manually, it will push back and stay

    at 37.

    The way servos actually work is a bit complicated, but you

    don't need to know it because the Arduino has a built in

    function where all you have to do is tell it what angle to go to.

    Servos have three wires. Two are for power; red is for 5V and

    the darker one is for GND. The third wire is for the signal, this is how it knows what position to go to. So

    to get started, connect the power wires to the power strip on the breadboard. Then connect the signal

    wire to digital pin 9 on the Arduino. Notice that next to digital pin 9 and 10 on the Arduino, it says PWM.

    That stands for Pulse Width Modulation. PWM is how the position of the Arduino is controlled. You do

    not need to know how it works, but you need to know that only some of the pins on the Arduino can do

    PWM. The pins that can have PWM written next to them. For now just stick to pin 9. Now open up the

    servo sweep example from File -> Examples -> Servo -> Sweep. Upload the code.

    You should see the servo arm moving back and forth. If you look at the code you should see a For-loop

    that goes form 0 - 179 and then from 179 - 0. The key is the write line.

    myservo.write(pos);

    Remember: PhototransistorThe important thing to remember here is the circuit for using the phototransistor.

    You will probably never make an LED bar graph like this, but what you will do is use

    it for something else. As long as you can get the analog voltage to the Arduino, you

    will be able to use it for anything you want.

    Usage Examples

    Make a light / dark sensor

  • 7/31/2019 Arduino Microcontroller Workshop

    11/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    What that does is set the angle to the servo. Note that you can attach more than one servo at a time.

    That is why at the top you have to create a servo object and tell it what pin it is attached to. After that

    you just use it by saying its name; in this case 'myservo'. If you want, you could connect a second servo

    to pin 10 and call it uberServo. Then by saying uberServo.write(37), it would go to 37. To make it stay at

    a certain angle, what you do is make a loop and keep writing the same angle over and over. That way if

    you try to move it, it will try to move back to 37.

    Now let's try using the analog input of the phototransistor to control the position of the servo. First you

    need to learn about a function called map(). As I mentioned above, when using the analog input, the

    ADC will input a voltage between 0V and 5V and output a number between 0 and 1023. In mathematical

    terms this is called a map. Values between 0-5 get mapped linearly to 0-1023. So 2.5V input is 512

    output.

    In this case, we have an analog input that goes between 0-1023 (actually it's more like 80-818 as

    mentioned above) and we have a servo that can only go between 0 and 179. We need to map input

    value between 0-1023 to output values between 0-179. So we use the map function:

    output = map(input, In-Low, In-High, Out-Low, Out-High);

    So what happens is that

    you take variable called

    input that can vary

    between In-Low and In-

    High. The function will then

    map it to a variable

    between Out-Low and Out-

    High. The result will besaved in output.

    To see this in action, open up the servo knob example. This example is designed to be used with a

    potentiometer. What happens is that as you rotate the knob, the servo moves between 0 and 180.

    Instead of the knob we will be using the phototransistor. Upload the code and see what happens. You

    should see that the servo does not move the full 180 degrees. That is because as you saw above, the

    phototransistor does not vary from 0V to 5V. Try to modify the code to make the servo move the full

    180. This is what I got:

    val = map(val, 40, 818, 0, 179);

    If you want you could connect a pot to analog input 2 and use that to control the position of the servo.

    You can disconnect everything from the breadboard now, because we will not be using it again.

  • 7/31/2019 Arduino Microcontroller Workshop

    12/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Motor Speed ControllerIf you are using a standard DC motor to make something like a car, you will

    probably need to be able to adjust the speed at which the motor rotates.

    The simples and cheapest motor you can get looks like the motor on the

    left. It has one wire for positive voltage and another wire for GND. Most

    motors require 12V to operate, but you should read the label to be sure

    because if you use a higher voltage, the motor will burn out. Also if you use

    a lot less it won't start.

    As you saw with the phototransistor, when you apply a small current to the base of the transistor, you

    get a larger current that flows between the collector and emitter. So if we connect the motor so that the

    current flowing between the collector and emitter also flows through the motor, then we can control

    the amount of current flowing through the motor. We will use the Arduino to control the base current,

    and therefore use the Arduino to control the speed of the motor.

    First we need some basic electrical theory. If you have a voltage across a resistor, there will also be

    current flowing across that resistor. In fact the amount of voltage is equal to the amount of current

    multiplied by the size of the resistor. This is known as Ohm's law: V = IR. V is voltage, I is current, R is

    resistance.

    We know the Arduino can output 5V (digital 1)from its digital outputs. Now if we apply 5V

    across a 470 resistor we will get 0.0106A

    (5/470) of current. If we could vary the voltage

    output, to say 2.5V instead of 5V, we would then

    have 0.00532A. This would make the motor

    slower, but how do we vary the voltage?

    Remember: ServosServos are useful for making anything that must be rotated precisely. All you have

    to remember is what a servo does. Then when you need one, you will know what

    to use. The servo examples are the best place to start with the code. Also note that

    servos can only be attached to Arduino pins marked with PWM.

    Usage Examples

    A base that rotates Arm / joint Control surfaces of an airplane or boat Steering of a car Electronic door lock (move a dead bolt back and forth)

  • 7/31/2019 Arduino Microcontroller Workshop

    13/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    To do this we use a technique called Pulse Width Modulation (PWM). You may remember that PWM is

    how the servo is actually controlled. Lets first talk about PWM.

    PWM basically takes a voltage source (like the digital output of the Arduino which is 5V) and turns it on

    and off very quickly. In the graph above, when the digital output is on, the graph is at Ymax, when the

    output is off it is at Ymin. By turning the voltage on and off, what happens is that the voltage appears tobe somewhere between Ymax and Ymin. If you turned the output on for 50ms and then off for 50ms over

    and over again, the voltage would appear to be 2.5V. This is because the voltage is on for 50% of the

    time and off for 50% of the time. The result is that the voltage appears to be 50% of the Y max. The

    percentage of time the output is on is called the duty cycle. To get 2.5V you would need a 50% duty

    cycle. To get 1V you would need a 20% duty cycle (20% of 5V = 1V).

    Since we can program when the digital output is on and when it is off, we can set the duty cycle to

    whatever we want. But the Arduino makes it really easy. Instead of manually turning the outputs on and

    off, there is a function called analogWrite(). analogWrite() takes a number between 0 and 255. 255

    represents a 100% duty cycle (5V), 127 represents a 50% duty cycle (2.5V) and 0 represents a 0% duty

    cycle (0V). So a number between 0 and 255 gives you any duty cycle you want.

    To see this in action, open the

    fading example in File ->

    Examples -> Analog ->Fading. If

    you want you can connect an LED

    and a resistor to pin 9 like it says

    in the example, but we just need

    to see the PWM in action. To do

    this, connect an oscilloscope to

    pin 9 (be sure to connect the

    reference to GND as well). You should see the width of the pulses getting

    smaller and then getting bigger again.

    Now that we can vary the voltage, we can create a varying current. We will

    now apply the varying current to the base of a power transistor and use it

    to change the speed of a motor.

    First remember that motors need 12V and digital circuits only use 5V. Therefore to connect the motor

    you will need a power supply that can supply 12V. This lab has a 15V power supply. We have not learned

    how to step down voltages yet, so we will just use this 15V. Note, that if you use 15V for a long time on a12V motor, it will burn out. Do not keep the motor running for more than 10 seconds at a time is using

    15V. Also by using a separate power supply, we can have very large currents going through the motor.

    This power transistor can multiply current by about 100 times. So that means it can draw a huge current

    from the 12V supply, while only using a small amount of current from the Arduino. This is good.

  • 7/31/2019 Arduino Microcontroller Workshop

    14/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Connect the motor as shown in the top part of the diagram to the left. Note that the PWM on the

    diagram is pin 9 on the Arduino. Run the fading example and you should be able to hear the motor

    changing speed.

    Note, the motor requires a certain minimum current in order to run, lower than that and it will just not

    work. So change the fading program to vary the PWMbetween 128 - 255 instead of 0 - 255. This way the motor

    should stay on and then keep changing speed.

    To make this more useful, connect a pot to analog pin 0 and

    use it to control the speed of the motor. So when the

    analog input is 0V the motor should be very slow, or off.

    When the input is 5V, the motor should be very fast. The

    pot is connected like the bottom of the diagram on the left.

    Here is the code I used to make it work:

    val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)val = map(val, 0, 1023, 128, 255); // scale it to use it with the servo (value between 0 and 180)

    analogWrite(ledPin, val);

    delay(15); // waits for the servo to get there

    As you turn the knob, you should be able to hear the motor going faster and slower.

    Here we used PWM to vary the effective voltage being output by the Arduino. The way the servo works

    is the time between two pulses determines the angle. So if you make two pulses spaced 50ms apart, it

    will go to some angle. If you then change the pulses to 100ms apart, it will go to some other angle. The

    problem is knowing what time corresponds to what angle. That is why the Arduino makes it easy and

    just has a servo write() function.

    Remember: PWMPulse width modulation can be used to make any voltage between the min and

    max. So with the Arduino, any voltage between 0V and 5V can be produced using

    PWM.

    Usage Examples

    Variable voltage output Variable current output Servo control

  • 7/31/2019 Arduino Microcontroller Workshop

    15/15

    Carleton IEEE, 2010 Microcontroller Workshop Series

    Author: Laxman Pradhan

    Remember: Motor Speed ControllerTo control the speed of a motor, you use a power transistor and then vary the

    current at the base using PWM. Also since motors work at 12V, not the 5V of digital

    circuits, you will need another power supply to use motors. This is good because

    motors require very large currents. This way the large currents stay separate from

    your Arduino.

    Usage Examples

    Car wheels (control speed) Fan speed (most fans use regular DC motors just like this one)

    Note

    To make a car, you need some other stuff to control your motor.Specifically you need an H-bridge. A H-bridge lets you change the direction

    of current going through the motor so that way you can go in reverse. You

    can buy a single chip that will control motor speed, current direction and

    other stuff. Lookup "H-bridge" on Wikipedia for more information.

    Remember: Varying CurrentsCurrent can be made by applying voltage across a resistor. If you can change the

    voltage across the resistor, you can change the current. Warning: The Arduino can

    only output 40mA max from all the outputs combined. Do not try to output more

    or the Arduino will simply turn off.