arduino compatibility mode - جامعة نزوى · 2016-04-16 · • /*fade: this example shows...

45
4/16/2016 1 ARDUINO Dr. Munaf Salim 1 Lesson Lesson Lesson Lesson 1 Getting Started with Arduino Dr. Munaf Salim 2

Upload: others

Post on 24-Apr-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

1

ARDUINO

Dr. Munaf Salim 1

Lesson Lesson Lesson Lesson 1111Getting Started with Arduino

Dr. Munaf Salim 2

Page 2: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

2

Overview

Dr. Munaf Salim 3

• In this lesson, you will learn how to setup your computer to use Arduino and how to set about the lessons that follow.

What is Arduino

Dr. Munaf Salim 4

Page 3: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

3

What is Arduino

Dr. Munaf Salim 5

The Arduino Uno can be powered via the USB connection or with an external

power supply

VIN. The input voltage to the Arduino board when it's using an external power

source (as opposed to 5 volts from the USB connection or other regulated power

source). You can supply voltage through this pin, or, if supplying voltage via the

power jack, access it through this pin.

5V. This pin outputs a regulated 5V from the regulator on the board. The board

can be supplied with power either from the DC power jack (7 - 12V), the USB

connector (5V), or the VIN pin of the board (7-12V). Supplying voltage via the 5V

or 3.3V pins bypasses the regulator, and can damage your board. We don't advise

it.

3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current

draw is 50 mA.

GND. Ground pins.

What is Arduino

Dr. Munaf Salim 6

• Each of the 14 digital pins on the Uno can be used as an input or output, using pinMode(), digitalWrite(), and digitalRead()functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA.

• Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.

• External Interrupts: 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.

• PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.

Page 4: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

4

What is Arduino

Dr. Munaf Salim 7

• The Uno has 6 analog inputs, labeled A0 through A5, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and the analogReference() function

Dr. Munaf Salim 8

Getting Started with ARDUINO

• Get an Arduino board and USB cable

• Download the Arduino environment (http://arduino.cc/en/Main/Software)

• Connect the board

• Install the drivers

• Launch the Arduino application

• Open your code

• Select your board

• Select your serial port

• Upload the program

Page 5: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

5

The Arduino IDE

The main features you need to know about are:

• Code area: This is where you will type all your code

• Info panel: This will show any errors during compiling or uploading

code to your Arduino

• Verify: This allows you to compile your code to code the Arduino

understands. Any mistakes you have made in the syntax of your

code will be show in the info pannel

• Upload: This does the same as verify but will then send your code

to your Arduino if the code is verified successfully

• Serial Monitor: This will open a window that allows you to send

text to and from an Arduino. We will use this feature in later

lectures.

Dr. Munaf Salim 9

The Arduino IDE

By far one of the most valuable part of the Arduino software

is its vast library of example programs. All features of the

Arduino are demonstrated in these.

Optional libraries usually add their own examples on how to

use them.

Arduino shields will often come with their own libraries and

therefore their own examples.

If these examples don’t cover what you need….Google it!

Dr. Munaf Salim 10

Page 6: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

6

Before we begin coding

Dr. Munaf Salim 11

Structure of an Arduino “sketch”

void setup()

{

// put your setup code here, to run once:

}

void loop()

{

// put your main code here, to run repeatedly:

}

NB: A copy of this can be found in File>Examples>1. Basics>BareMinimum

Dr. Munaf Salim 12

Page 7: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

7

My first sketch

int onBoardLED;

void setup()

{

//Arduinos have an on-board LED on pin 13

onBoardLED = 13;

pinMode(onBoardLED, OUTPUT);

}

void loop()

{

digitalWrite(onBoardLED, HIGH);

delay(500); //delay measured in milliseconds

digitalWrite(onBoardLED, LOW);

delay(500);

}

Dr. Munaf Salim 13

Breadboard

Dr. Munaf Salim 14

Page 8: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

8

LEDs

Dr. Munaf Salim 15

External LEDs

Try make an LED pin blink in a pattern on a pin of your choice

Dr. Munaf Salim 16

Page 9: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

9

PWM – Pulse width modulation

PWM allows you to create a fake analogue signal by toggling a pin high and low. The amount of overall time the pin spends high effects the average voltage of the signal.

This works well for dimming LEDs so long as the frequency of pulses is faster than the eye can pick up

An Arduino UNO can only do PWM on pins:

3, 5, 6, 9, 10 and 11

Dr. Munaf Salim 17

PWM EXAMPLE

int ledPin;

void setup()

{

ledPin = 10;

//Note that PWM doesn't need a pinMode

}

void loop()

{

analogWrite(ledPin, 50);

delay(500);

analogWrite(ledPin, 255);

delay(500);

}

Dr. Munaf Salim 18

Page 10: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

10

loop

for (int counter = 0; counter<10; counter+=1)

{

//Do a barrel roll

}

For loop: Allows you to loop a certain number of times

• Counter initialisation

• Counter condition

• What to do when loop iteration finishes

Dr. Munaf Salim 19

Loop

While loop: Allows you to loop until a condition is met

• Condition

while(digitalRead(10) == LOW)

{

//Such loop, many iteration, WOW!, much condition met

}

Dr. Munaf Salim 20

Page 11: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

11

First Example Blinking LED• This example shows the simplest thing you can do with an Arduino to see physical

output: it blinks an LED.

• Arduino Board

• LED

• Resistor, anything between 220 ohm to 1K ohm

First Example Blinking LED

Page 12: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

12

First Example Blinking LED• /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code

is in the public domain. */

// Pin 13 has an LED connected on most Arduino boards.

// give it a name:

// the setup routine runs once when you press reset:

void setup() {

// initialize the digital pin as an output.

pinMode(13, OUTPUT);

}

// the loop routine runs over and over again forever:

void loop() {

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(13, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

First Example Blinking LED• In the program below, the first thing you do is to initialize pin 13 as an output pin

with the line

pinMode(13, OUTPUT);

• In the main loop, you turn the LED on with the line:

digitalWrite(13, HIGH);

• This supplies 5 volts to pin 13. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line:

digitalWrite(13, LOW);

• That takes pin 13 back to 0 volts, and turns the LED off. In between the on and the off, you want enough time for a person to see the change, so the delay() commands tell the Arduino to do nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else happens for that amount of time

Page 13: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

13

First Example Blinking LED• /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code

is in the public domain. */

// Pin 13 has an LED connected on most Arduino boards.

// give it a name:

int led = 13;

// the setup routine runs once when you press reset:

void setup() {

// initialize the digital pin as an output.

pinMode(led, OUTPUT);

}

// the loop routine runs over and over again forever:

void loop() {

digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(led, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

Example Two Fading• Demonstrates the use of the analogWrite() function in fading an LED off and on. AnalogWrite uses

pulse width modulation (PWM), turning a digital pin on and off very quickly, to create a fading effect.

• Arduino board

• Breadboard

• A LED

• A 220 ohm resistor

Page 14: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

14

Example Two Fading

Example Two Fading• /* Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This

example code is in the public domain. */

int led = 9; // the pin that the LED is attached to

int brightness = 0; // how bright the LED is

int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:

void setup() {

// declare pin 9 to be an output:

pinMode(led, OUTPUT);

}

Page 15: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

15

Example Two Fading• // the loop routine runs over and over again forever:

void loop() {

// set the brightness of pin 9:

analogWrite(led, brightness);

// change the brightness for next time through the loop:

brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:

if (brightness == 0 || brightness == 255) {

fadeAmount = -fadeAmount ;

}

// wait for 30 milliseconds to see the dimming effect

delay(30);

}

Example Two Fading• After declaring pin 9 to be your ledPin, there is nothing to do in the setup() function of your code.

• The analogWrite() function that you will be using in the main loop of your code requires two

arguments: One telling the function which pin to write to, and one indicating what PWM value to

write.

• In order to fade your LED off and on, gradually increase your PWM value from 0 (all the way off) to

255 (all the way on), and then back to 0 once again to complete the cycle. In the sketch below, the

PWM value is set using a variable calledbrightness. Each time through the loop, it increases by the

value of the variable fadeAmount.

• If brightness is at either extreme of its value (either 0 or 255), then fadeAmount is changed to its

negative. In other words, if fadeAmount is 5, then it is set to -5. If it's 55, then it's set to 5. The next

time through the loop, this change causesbrightness to change direction as well.

• analogWrite() can change the PWM value very fast, so the delay at the end of the sketch controls the

speed of the fade. Try changing the value of the delay and see how it changes the program.

Page 16: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

16

Example Two Seven Segments• This project is absolutely for beginners who prefers to run than taking a walk,I am going to control 7

segment display to automatically count numbers from 1-9 using Arduino,well I hope you know the

basics of 7 segment display.

• Arduino Uno

• Breadboard

• 7 segment display (common anode or cathode)

• 220ohm resistor – 8 pieces

• some wire stripes

Example Two Seven Segments

Page 17: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

17

Example Two Seven Segments

Example Two Seven Segments

Page 18: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

18

Example Two Seven Segments

Example Two Seven Segments• /* Program for Common Anode Single Digit Decimal COunter using arduino */

• const int a=7;

• const int b=6;

• const int c=4;

• const int d=2;

• const int e=1;

• const int f=9;

• const int g=10;

Page 19: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

19

Example Two Seven Segments

void setup()

{

pinMode(a, OUTPUT);

pinMode(b, OUTPUT);

pinMode(c, OUTPUT);

pinMode(d, OUTPUT);

pinMode(e, OUTPUT);

pinMode(f, OUTPUT);

pinMode(g, OUTPUT);

};

Example Two Seven Segmentsvoid loop ()

{

for (int i=0;i<10;i++)

{

LightLed(i);

delay (1000);

}

};

Page 20: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

20

void LightLed(int n)

{

switch(n)

{

case 0:

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, HIGH);

break;

case 1:

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

break;

case 2:

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, HIGH);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, HIGH);

digitalWrite(g, LOW);

break;

case 3:

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, HIGH);

digitalWrite(f, HIGH);

digitalWrite(g, LOW);

break;

case 4:

digitalWrite(a, HIGH);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

break;

case 5:

digitalWrite(a, LOW);

digitalWrite(b, HIGH);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, HIGH);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

break;

case 6:

digitalWrite(a, LOW);

digitalWrite(b, HIGH);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, HIGH);

break;

case 7:

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, HIGH);

digitalWrite(e, HIGH);

digitalWrite(f, HIGH);

digitalWrite(g, HIGH);

break;

case 8:

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, LOW);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

break;

case 9:

digitalWrite(a, LOW);

digitalWrite(b, LOW);

digitalWrite(c, LOW);

digitalWrite(d, LOW);

digitalWrite(e, HIGH);

digitalWrite(f, LOW);

digitalWrite(g, LOW);

break;

}

};

Page 21: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

21

Digital Input

digitalRead()

• Description: Reads the value from a specified digital pin,

either HIGH or LOW.

• Syntax: digitalRead(pin)

• Parameters

• pin: the number of the digital pin you want to read (int)

• Returns: HIGH or LOW

Digital Input

Page 22: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

22

Digital Input

Digital Input

Page 23: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

23

Digital Inputconst int buttonPin = 2; // the number of the pushbutton pin

const int ledPin = 9; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

void setup() {

pinMode(ledPin, OUTPUT);

pinMode(buttonPin, INPUT);

}

void loop(){

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);

}

else {

digitalWrite(ledPin, LOW);

}

}

The Serial MonitorIn this lesson, you will build on lesson 4, adding the facility to control the LEDs from

your computer using the Arduino Serial Monitor. The serial monitor is the 'tether'

between the computer and your Arduino - it lets you send and receive text

messages, handy for debugging and also controlling the Arduino from a keyboard!

Page 24: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

24

The Serial Monitor

void setup() // run once, when the sketch starts

{

Serial.begin(9600); // set up Serial library at 9600 bps

Serial.println("Hello world!"); // prints hello with ending line break

}

void loop() // run over and over again

{

// do nothing!

}

The Serial Monitor

void setup() // run once, when the sketch starts

{

Serial.begin(9600); // set up Serial library at 9600 bps

}

void loop() // run over and over again

{

Serial.println("Hello world!"); // prints hello with ending line break

delay(1000);

}

Page 25: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

25

The Serial Monitor

The Serial Monitorconst int ledpin =9;

int input;

void setup()

{pinMode(ledpin, OUTPUT);

Serial.begin(9600);

Serial.flush();

}

void loop()

{

while(Serial.available()>0)

{input = Serial.read();

Serial.println(input);

if(input == 49)

{digitalWrite(ledpin,HIGH);}

else if(input==48)

{digitalWrite(ledpin,LOW);}

delay(500);

}

}

Page 26: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

26

Analog InputAnalogue sensors are those that have more than 2 output options. They don't

just turn on or off; they have intermediary steps. Analogue sensors can't be read

from the digital pins as they require analogue to digital signal conversion. The

voltage needs to be translated into a number.

The Arduino has a 10 bit ADC (analogue to digital converter), so it can receive 0

to 5 V and map it to values of 0 to 1023 (so 5/1024 = 0.0049 V per value).

analogRead() is the method used to read in analog signals.

Analog Input

Page 27: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

27

Analog Inputvoid setup() {

// open the serial connection at 9600 BAUD

Serial.begin(9600);

}

void loop() {

// store the value read from pin 2 into a variable

int sensorValue = analogRead(2);

// print that variable over the serial connection Serial.println(sensorValue);

}

Temperature Sensing

In this project, we will demonstrate how to build temperature sensor circuit

using a LM35 sensor. As a temperature sensor, the circuit will read the

temperature of the surrounding environment and relay thi temperature to

us back in degrees Celsius.

The IC we will use to measure the temperature is the LM35 IC. We will

integrate this with the arduino to measure the temperature. The

arduino will then read this measured value from the LM35 and

translate into degrees Fahrenheit and Celsius, which we will be able to

read from the computer from the arduino serial monitor.

Page 28: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

28

Temperature Sensing

Temperature Sensing

Page 29: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

29

Small direct current (DC) electric motors can be found in a wide range of

devices, including radio-controlled cars and boats, electric car windows,

DVD players, and handheld electric fans. Many of these can be

repurposed for use with an Arduino.

Voltages for small DC motors normally range from 1.5 to 30 volts, delivered through two wires; each motor manufacturer provides a recommended voltage. Exceeding the recommended voltage by too much will cause the motor to burn out; delivering too little voltage will result in the motor not turning at all. To make a motor reverse, you normally just need to reverse the two wires connected to it. If you’re using a motor to power a small robot, it’s often connected to a gearbox.

Why a gearbox? A small DC motor normally produces high speed and low torque. A gearbox converts this to low speed, high torque, making it more suitable for powering a small robot. A gearbox can normally be purchased with the motor. Figure 5.1 shows a typical motor with a gearbox.

The Arduino can only provide a small amount of current—not enough to power a motor—so you’ll need to use an external power supply. You’ll use the Arduino to switch the motor on and off, as well as to provide speed control. Initially, we’ll look at switching a motor on and off, and then we’ll move on to controlling its speed.

Page 30: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

30

The basic DC motor has actually two windings and two permanent magnets. The coils are

powered from the commutator and the brushes. We will see these two later on. For now, you

only need to know that during a full cycle of the rotor, the current that runs through each

winding change direction once. Thus, each electromagnet will change its magnetic polarity.

Moreover, the windings of the two magnets are winded in reversed direction. Thus, when one

electromagnet is North, the other is South and vice versa. Look at the following drawing of the

basic DC motor:

• A motor will often need a higher voltage as well as higher current than can be supplied

directly so an external power supply is normally used to provide this. The simplest way of

driving a motor is directly through a transistor, as shown here:-

Page 31: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

31

• An output pin from the Arduino is connected through a resistor to the base of a transistor, the

motor sits between the collector and the positive external power supply and the emitter is

connected to ground, earth or zero volts (to give the line just three commonly

interchangeable names). Note that the ground has to be common (connected together)

between your external supply and the Arduino. A motor is basically an lectromagnet or coil;

in electronic terms this is an inductor. When you suddenly remove turn off the power, the

magnetic field collapses because there is no current flowing to keep it up. This collapsing field

then produces a “back EMF” or reverse voltage that can be several hundred volts. This has the

potential to damage the electronics connected to the rest of the circuit and so it is normal

practice to place a diode across the motor, in order to short this back-EMF out and protect

you other components. This is most important and any sort of rectifier diode will do, like the

popular 1N4004 or similar. The value of the base resistor is something that troubles some

beginners, however, it is not too critical. It has to limit the base current, but not so much that

the transistor doesn't turn on fully. Basically the base current times the "transistor current

gain" has to equal or exceed the current taken by the motor

Page 32: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

32

void setup() {

pinMode(8, OUTPUT);

}

void loop() {

digitalWrite(8, HIGH); // Turn on motor

delay(5000);

digitalWrite(8, LOW); // Turn off motor

delay(5000);

}

Page 33: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

33

Speed Control

Page 34: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

34

Speed Control

int motorPin = 3;

void setup()

{

pinMode(motorPin, OUTPUT);

Serial.begin(9600);

while (! Serial);

Serial.println("Speed 0 to 255");

}

void loop()

{

if (Serial.available())

{

int speed = Serial.parseInt();

if (speed >= 0 && speed <= 255)

{

analogWrite(motorPin, speed);

}

}

}

H-Bridge

Page 35: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

35

H-Bridge

H-Bridge

int enA = 10;

int in1 = 9;

int in2 = 8;

void setup()

{

pinMode(enA, OUTPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

}

Page 36: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

36

H-Bridge

void loop()

{

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

for (int i = 0; i < 256; i++)

{

analogWrite(enA, i);

delay(20);

}

// decelerate from maximum speed to zero

for (int i = 255; i >= 0; --i)

{

analogWrite(enA, i);

delay(20);

}

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

delay(5000);

H-Bridge

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

for (int i = 0; i < 256; i++)

{

analogWrite(enA, i);

delay(20);

}

// decelerate from maximum speed to zero

for (int i = 255; i >= 0; --i)

{

analogWrite(enA, i);

delay(20);

}

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

delay(5000);

}

Page 37: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

37

Stepper MotorA stepper motor is a motor controlled by a series of electromagnetic coils. The center shaft

has a series of magnets mounted on it, and the coils surrounding the shaft are alternately given

current or not, creating magnetic fields which repulse or attract the magnets on the shaft,

causing the motor to rotate.

This design allows for very precise control of the motor: by proper pulsing, it can be turned in

very accurate steps of set degree increments (for example, two-degree increments, half-degree

increments, etc.). They are used in printers, disk drives, and other devices where precise

positioning of the motor is necessary.

There are two basic types of stepper motors, unipolar steppers and bipolar steppers.

• Unipolar Stepper MotorsThe unipolar stepper motor has five or six wires and four coils (actually two coils divided by

center connections on each coil). The center connections of the coils are tied together and used

as the power connection. They are called unipolar steppers because power always comes in on

this one pole.

Page 38: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

38

• Bipolar stepper motorsThe bipolar stepper motor usually has four wires coming out of it. Unlike unipolar steppers,

bipolar steppers have no common center connection. They have two independent sets of coils

instead. You can distinguish them from unipolar steppers by measuring the resistance between

the wires. You should find two pairs of wires with equal resistance. If you’ve got the leads of

your meter connected to two wires that are not connected (i.e. not attached to the same coil),

you should see infinite resistance (or no continuity).

.

Like other motors, stepper motors require more power than a microcontroller can give them, so

you’ll need a separate power supply for it. Ideally you’ll know the voltage from the

manufacturer, but if not, get a variable DC power supply, apply the minimum voltage (hopefully

3V or so), apply voltage across two wires of a coil (e.g. 1 to 2 or 3 to 4) and slowly raise the

voltage until the motor is difficult to turn. It is possible to damage a motor this way, so don’t go

too far. Typical voltages for a stepper might be 5V, 9V, 12V, 24V. Higher than 24V is less common

for small steppers, and frankly, above that level it’s best not to guess.

.

Page 39: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

39

Like other motors, stepper motors require more power than a microcontroller can give them, so

you’ll need a separate power supply for it. Ideally you’ll know the voltage from the

manufacturer, but if not, get a variable DC power supply, apply the minimum voltage (hopefully

3V or so), apply voltage across two wires of a coil (e.g. 1 to 2 or 3 to 4) and slowly raise the

voltage until the motor is difficult to turn. It is possible to damage a motor this way, so don’t go

too far. Typical voltages for a stepper might be 5V, 9V, 12V, 24V. Higher than 24V is less common

for small steppers, and frankly, above that level it’s best not to guess.

To control the stepper, apply voltage to each of the coils in a specific sequence. The sequence

would go like this:

Step wire 1 wire 2 wire 3 wire 4

1 High low high low

2 low high high low

3 low high low high

4 high low low high

Page 40: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

40

Page 41: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

41

int motorPin1 = 8;

int motorPin2 = 9;

int motorPin3 = 10;

int motorPin4 = 11;

int delayTime = 500;

void setup() {

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(motorPin3, OUTPUT);

pinMode(motorPin4, OUTPUT);

}

void loop() {

digitalWrite(motorPin1, HIGH);

digitalWrite(motorPin2, LOW);

digitalWrite(motorPin3, LOW);

digitalWrite(motorPin4, LOW);

delay(delayTime);

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, HIGH);

digitalWrite(motorPin3, LOW);

digitalWrite(motorPin4, LOW);

delay(delayTime);

Page 42: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

42

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, LOW);

digitalWrite(motorPin3, HIGH);

digitalWrite(motorPin4, LOW);

delay(delayTime);

digitalWrite(motorPin1, LOW);

digitalWrite(motorPin2, LOW);

digitalWrite(motorPin3, LOW);

digitalWrite(motorPin4, HIGH);

delay(delayTime);

}

Servo Motors

• A servomotor is a rotary actuator that allows for precise control of

angular position, velocity and acceleration.[1] It consists of a suitable

motor coupled to a sensor for position feedback. It also requires a

relatively sophisticated controller, often a dedicated module designed

specifically for use with servomotors.

• Servomotors are not a specific class of motor although the

term servomotor is often used to refer to a motor suitable for use in a

closed-loop control system.

Page 43: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

43

Servo Motors

• Servo motors have three wires: power, ground, and signal. The power

wire is typically red, and should be connected to the 5V pin on the

Arduino board. The ground wire is typically black or brown and should

be connected to a ground pin on the Arduino board. The signal pin is

typically yellow, orange or white and should be connected to pin 9 on

the Arduino board.

Page 44: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

44

• include <Servo.h>

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()

{

myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

Page 45: Arduino Compatibility Mode - جامعة نزوى · 2016-04-16 · • /*Fade: This example shows how to fade an LED on pin 9 using the analogWrite() function. This ... • This project

4/16/2016

45

• void loop()

{

for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180

degrees

{ 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

}

}