design with microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/pmp/lecture/c8.pdf · design...

42
Design with Microprocessors Year III Computer Science 1-st Semester Lecture 9: Microcontroller based applications: usage of sensors and actuators (motors)

Upload: dangmien

Post on 18-Jan-2019

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Design with Microprocessors

Year III Computer Science

1-st Semester

Lecture 9: Microcontroller based applications: usage of sensors and actuators (motors)

Page 2: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

DC motor control

Diligent MT motor/gearbox

• 1/19 or 1/53 ratio gearbox

• Variable speed (value of the average voltage applied)

• Direction control (voltage polarity)

• Speed and direction signals (SA, SB) (quadrature encoder) Motor speed can

be determined by the frequency and motor rotation direction can be determined

by the phase relationship of the two signals.

Page 3: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

DC motor control

Digilent Pmod HB5TM 2A H-Bridge Module (HB5)

J3 - motor power (up to 12 V)

EN = 0 (current flows through the H

bridge)

DIR – rotation direction

Motor speed is controlled by pulse

width modulating the EN pin

The direction of the motor should not

be reversed while the Enable pin is

active. This could damage the bridge

transistors.

Page 4: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

DC motor control

H bridge functioning

Page 5: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

DC motor control

Pulse Width Modulation and Motor Speed Control

In an analog circuit, motor speed is controlled by varying the input voltage to a

circuit. In a digital circuit, there are only two ways to control a motor digitally:

• use a variable resistance circuit to control the motor voltage (expensive,

complicated, and wastes much energy in the form of heat)

• pulse the power to the motor by pulse width modulation (PWM).

• While voltage is being applied, the motor is driven by the changing magnetic forces.

• When voltage is stopped, momentum causes the motor to continue spinning a while.

• At a high enough frequency, this process of powering and coasting enables the motor to

achieve a smooth rotation that can easily be controlled through digital logic.

Page 6: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

DC motor control

PWM driving issues

PWM has two important effects on DC motors:

• Inertial resistance is overcome more easily at startup because short bursts

of maximum voltage achieve a greater degree of torque than the equivalent

DC voltage.

• Higher level of heat generation inside the motor.

• If a pulsed motor is used for an extended time, heat dissipation systems may

be needed to prevent damage to the motor. Because of these effects, PWM

is best used in high-torque infrequent-use applications such as airplane flap

servos and robotics.

• PWM circuits can also create radio frequency interference (RFI) that can be

minimized by locating motors near the controller and by using short wires.

• Line noise created by continually powering up the motor may also need to be

filtered to prevent interference with the rest of the circuits. Placing small

ceramic capacitors directly across the motor terminals and between the

motor terminals and the motor case can be used to filter RFI emissions from

the motor.

Page 7: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Robotic Kits

Digilent: CerebotII_RSK_StartupDesign

http://digilentinc.com/Products/Detail.cfm?NavPath=2,403,543&Prod=RSK

Robofun: http://www.robofun.ro/kit-roboti

Page 8: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Sensors

Fairchild QRB 1134 - IR Sensor•Detects surface reflectivity

• IR LED + IR phototransistor (same frequency)

•The phototransistor responds by varying the collector current depending on

the incident reflected light

•The phototransistor response depends on the surface reflectivity and

distance between the sensor and the surface

Page 9: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Sensors

PMod LS1 – IR sensors interface

• To use with IR sensors (up to 4)

• Analog response of each sensor compared with a reference voltage digital

output

• Reference voltage is adjustable (IR sensor sensibility setting)

Page 10: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Sonar range finder

LV-MaxSonar®-EZ0™ High Performance

Sonar Range Finder (see C7)

AN – Outputs analog voltage with a scaling

factor of (Vcc/512) per inch. A supply voltage

of 3.3V yields ~6.4mV/in 2.56 mV/cm

Sonar range: 6-in (15 cm) … 254 in ( 645 cm)

with 1-inch resolution. For objects from 0 .. 6in range

as 6-inches.

][][56.2

1024][56.21024cmd

V

cmdmV

V

VADC

REF

IN

ADC_Init:

ldi r16, 0b11000011 ; Vref=2,56 V internal, ADLAR=0 (Data Shift right), ADC3

single ended

out ADMUX, r16

ldi r16, 0b10000000 ; Activate ADC, max. speed

out ADCSRA, r16

ret

ADC_read:

in r20, ADCL //ADC access to data registers is blocked

in r21, ADCH //ADC access to the ADCH and ADCL Registers is re-enabled

// r21:r20 = d[cm] (in r20 range = 15 cm …. 256 cm)

ret

Page 11: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Autonomous car

Cerebot II

Pmod Hb5

Pmod Hb5

Pmod

LS1

Mt motor

Mt motor

IR Sensors

Pmod

BTN

Lane (path)

US sensor

Heading

direction

Features:

• Move Forward / Left / Right with

adjustable speed

• Detect and follow a freeform black line

on a white background

• Detect objects on the path and adjust

speed accordingly or even stop the

vehicle

Page 12: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Autonomous car

Page 13: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Autonomous car

Detailed flowchart description:

• The application entry point at address 0x0000 determines a jump to the

initialization module which sets up all the required resources

• After setup is complete, execution enters the infinite main loop

• The control from the main loop is taken away by interrupt requests

generated by the two timers which have been configured in the initialization

step

• When Timer1 overflow occurs, an interrupt request is generated and the

Timer1 Overflow ISR is called.

• This routine calls in order the following procedures : IR sensor read,

Lanekeep, Ultra sonic sensor read, Automatic cruise control

• After all these procedures have been called control is returned to the main

procedure/loop

• Timer0 will generate both compare match and overflow interrupts which are

responsible for pulse width modulation (global speed variation) of the

motors

• Also Timer0 overflow procedure calls the 7segment display procedure

which is responsible for controlling the two digit seven segment Pmod

which displays the read data (US sensor value)

Page 14: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Autonomous car

http://www.youtube.com/watch?v=8797GgIC1WI

Page 15: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Servo motor control

PWM control: pulse width rotation amplitude

•1 ms – maximum left (0 deg.)

•1.5 ms – neutral position (90 deg)

•2 ms – maximum right 180 deg)

PWM signal frequency: 30 … 60 Hz

Continuous rotation servo (feedback sensor disabled) low speed / high torque

continuous rotation motorhttps://digilentinc.com/Data/Products/GWSSERVO/GWS%20Servo%20360%20Modification.pdf

Other servos: http://www.robofun.ro/mecanice/servo

GWS Servo Kit : https://digilentinc.com/Products/Detail.cfm

?NavPath=2,403,635&Prod=GWSSERVO

Provides 0.23 second/60 degree

rotation with 47 oz-in of Torque

at 4.8V

Page 16: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Servo library

Servo library supports:

• up to 12 motors on most Arduino boards

• 48 motors on the Arduino Mega.

• the use of the library disables analogWrite() (PWM) functionality on pins 9

and 10, whether or not there is a Servo on those pins (except Mega).

• on Mega, up to 12 servos can be used without interfering with PWM

functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.

Servo connection (3 wire): power, ground, and signal.

• The power wire, 5V pin on the Arduino board.

• The ground wire (black or brown) ground pin on the Arduino board.

• The signal pin (yellow, orange or white) and should be connected to a

digital pin on the Arduino board.

Note: servos draw considerable power! To drive >2 servos: power them from a

separate supply (i.e. not the +5V pin on your Arduino). Be sure to connect the

grounds of the Arduino and external power supply together.

Page 17: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Servo library

Servo library methods:

servo.attach(pin) / servo.attach(pin, min, max) – attach the servo variable to a

pin

• servo: a variable of type Servo

• pin: the number of the pin that the servo is attached to

• min (optional): the pulse width, in microseconds, corresponding to the

minimum (0-degree) angle on the servo (defaults to 544)

• max (optional): the pulse width, in microseconds, corresponding to the

maximum (180-degree) angle on the servo (defaults to 2400)

servo.detach() - detach the Servo variable from its pin.

boolean val servo.attached() - check whether the Servo variable is attached to a

pin. Returns value is true / false

servo.write(angle) - writes a value (0 .. 180) to the servo, controlling the shaft

accordingly:

• standard servo set the angle of the shaft [deg] moving it to that orientation.

• continuous rotation servo set the speed of the servo (0: full speed in one

direction; 180: full speed in the other; 90: no movement)

int val = servo.read() - read the current angle of the servo (the value passed to

the last call to write()). Val - angle of the servo, from 0 to 180 degrees.

Page 18: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Servo library

Example: Sweeps the shaft of a RC servo motor back and forth across 180

degrees (http://arduino.cc/en/Tutorial/Sweep)

#include <Servo.h>

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

// a maximum of eight servo objects can be created

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

void setup()

{

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

}

void loop()

{

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

{

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for servo to reach the position

}

for(pos = 180; pos>=1; pos-=1) // goes from 180 to 0 degrees

{

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for servo to reach the position

}

}

Page 19: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Servo library

Example: Control the position of a RC (hobby) servo motor with your

Arduino and a potentiometer (http://arduino.cc/en/Tutorial/Knob)

#include <Servo.h>

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

int potpin = 0; // analog pin used to connect the potentiometer

int val; // variable to read the value from the analog pin

int angle; // angle for the servo

void setup()

{

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

}

void loop()

{

val = analogRead(potpin); // reads the value of the potentiometer

angle = map(val, 0, 1023, 0, 179); // scale it to use it with the servo

// (value between 0 and 180)

myservo.write(angle); // sets the servo position accordingly

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

}

Page 20: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Explorer vehicle

Page 21: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Explorer vehicleRemote control & sensing module

Page 22: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Explorer vehicleActuators and sensors module

Electronic Speed Controller (ESC)

Brushless DC motor

(propulsion)

Servo motor

(steering)

50Hz, 1 – 2 ms puls width:

1 ms - lowest level

2 ms - highest level

1.5 ms – neutral (start up)

Page 23: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Explorer vehicle

Page 24: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Stepper motors

• Motor controlled by a series of electromagnetic coils.

• Build: center magnetic shaft surrounded by coils

• The coils 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 preset 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.

• Two phase & higher count phase stepper motors

• There are 2 basic types of two phase stepper motors:

- unipolar steppers (simple commutation circuit: 1 transistor / winding)

- bipolar steppers (more complicated driving circuit – usually H bridge)

Refrences

[1] http://www.tigoe.net/pcomp/code/circuits/motors/stepper-motors/

[2] http://en.wikipedia.org/wiki/Stepper_motor

[3] M. Margolis, Arduino Cookbook, 2-nd Edition, O’Reilly, 2012.

Page 25: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Stepper motors

Unipolar steppers [2]

Wave drive or Full step drive (one phase on)

- Low torque / rarely used (ex: 25 teeth / 4 steps to

rotate by one teeth position 25*4 = 100 steps per

full rotation each step will be 360/100 = 3.6

Full step drive (two phases on)

- Maximum rated torque (most used)

Half stepping

- Lower torque (70%) / x2 resolution (ex: 8 steps to

rotate by 1 tooth position 25*8 = 200 steps per

full rotation each step 360/200 = 1.8

Microstepping

- Increased resolution / smoother operation

Ex: wave drive motor (3.6 /step)

Page 26: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Stepper motors

Arduino Stepper Library (http://arduino.cc/en/reference/stepper)

- allows you to control unipolar or bipolar stepper motors. To use it you will need a

stepper motor, and the appropriate hardware (driver) to control it.

To create a new instance of the Stepper motor class

Stepper(steps, pin1, pin2) - ex: Stepper myStepper = Stepper(100, 5, 6);

Stepper(steps, pin1, pin2, pin3, pin4)

int steps: the number of steps / revolution (e.g. 360 / 3.6 = 100 steps)

int pin1, pin2: two pins that are attached to the driver (2 pin setup)

int pin3, pin4: optional the last two pins attached to the driver (4 pin setup)

2 pin/wire setup

Step wire 1 wire 2

1 low high

2 high high

3 high low

4 low low

Control sequence (2 wire setup):

Page 27: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Stepper motors

4 pin / wire setup

Control sequence (4 wire setup):

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

With the use of the Arduino Stepper Library, the

control signals are generated by the library !!!!

Example of motor driver: U2004 Darlington Array

- high voltage, high current Darlington

arrays each containing seven open collector

Darlington pairs with common emitters. Each

channel rated at 500 mA and can withstand peak

currents of 600 mA.

Page 28: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Stepper motors

Arduino Stepper Library (http://arduino.cc/en/reference/stepper)

setSpeed(long rpms) - Sets the motor speed in rotations per minute (RPMs). This

function doesn't make the motor turn, just sets the speed at which it will when you

call step().

step(int steps) - Turns the motor a specific number of steps, at a speed

determined by the most recent call to setSpeed().

• int steps: the number of steps to turn the motor – positive(+) to turn one

direction, negative(-) to turn the other

• This function is blocking: it will wait until the motor has finished moving to pass

control to the next line in your sketch. (Ec: speed = 1 RPM and called

step(100) on a 100-step motor the function would take a full minute to run.

• For better control, keep the speed high and only go a few steps with each call

to step().

Page 29: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Stepper motors

Example: Stepper Motor Knob (http://arduino.cc/en/Tutorial/MotorKnob) –

unipolar stepper (4 wire driver setup) controlled by a potentiometer

#include <Stepper.h>

#define STEPS 100

Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input

int previous = 0;

void setup()

{

// set the speed of the motor to 30 RPMs

stepper.setSpeed(30);

}

void loop()

{

// get the sensor / potentiometer reading

int val = analogRead(0);

// move a number of steps equal to the change in the sensor reading

stepper.step(val - previous);

// remember the previous value of the sensor

previous = val;

}

Page 30: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Stepper motorsExample: unipolar stepper (4 wire driver setup) controlled using commands from

the serial port: numeric_value followed by ‘+’ steps in one direction; followed by ‘-’

steps in the other (M. Margolis, Arduino Cookbook, 2-nd Edition, O’Reilly, 2012)

#include <Stepper.h>

#define STEPS 24

Stepper stepper(STEPS, 2, 3, 4, 5);

int steps = 0;

void setup()

{

stepper.setSpeed(30); // motor speed [RPM]

Serial.begin(9600);

}

void loop()

{

if ( Serial.available())

{

char ch = Serial.read();

if(isDigit(ch)) { // is ch a number?

steps = steps * 10 + ch - '0'; // yes, accumulate the value

}

else if(ch == '+'){

stepper.step(steps);

steps = 0;

}

else if(ch == '-'){

stepper.step(steps * -1);

steps = 0;

}

else if(ch == 's'){

stepper.setSpeed(steps);

Serial.print("Setting speed to ");

Serial.println(steps);

steps = 0;

}

}

}

Page 31: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor Shield

https://www.arduino.cc/en/Main/ArduinoMotorShieldR3

- based on the L298 Dual Full Bridge Driver (H bridge) drive inductive

loads: relays, solenoids, DC and stepping motors (max. 2A / channel)

http://www.st.com/web/en/catalog/sense_power/FM142/CL851/SC1790/SS1

555/PF63147

- drive 2 DC motors (control the speed and direction of each independently)

- drive 1 Stepper

- Functions: free running / stop / brake; measure the motor current absorption

etc.

Simplified schematic of L298:

https://www.robofun.ro/forum/download/file.php?id=121&sid=3c711a1384348b9f

91b32405828a1015

Page 32: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor ShieldPower

The Arduino Motor Shield must be powered only by an external power supply.

External (non-USB) power can come either from an AC-to-DC adapter (7..12V)

or battery.

The adapter can be connected by

• plugging a 2.1mm center-positive plug into the

Arduino's board power jack on which the

motor shield is mounted

• OR by connecting the wires that lead the

power supply to the Vin and GND screw

terminals, taking care to respect the polarities

The shield power pins:

• Vin - is the input voltage to the motor connected to the shield (max. 18V). An

external power supply connected to this pin also provide power to the Arduino

board on which is mounted.

• GND Ground on the screw terminal block.

If your motor require more than 9V we recommend that you separate the power

lines of the shield and the Arduino board on which the shield is mounted. This is

possible by cutting the "Vin Connect" jumper placed on the back side of the shield

Page 33: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor ShieldShield Input and Output

• 2 separate channels, (A and B) - each use 4 of the Arduino

pins to drive or sense the motor.

• can use each channel separately to drive two DC motors

• combine them to drive one bipolar stepper motor.

Function pins per Ch. A pins per Ch. B

Direction D12 D13

PWM D3 D11

Brake D9 D8

Current Sensing A0 A1

The additional sockets on the shield :

• Screw terminal to connect the motors and their power

supply.

• 2 TinkerKit connectors for two Analog Inputs (in white):

connected to A2 , A3

• 2 TinkerKit connectors for two Analog Outputs (in orange

in the middle), connected to PWM outputs on pins D5 and

D6.

• 2 TinkerKit connectors for the TWI interface (in white with

4 pins), one for input and the other one for output.

Note: thinker kit connectors

have different pin layout than

brick-connectors (i.e. brick

temperature sensor). DO

NOT connect brick conectors

directly int-o the thinkerkit

sokets. Use wires !!!

Page 34: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor Shield

Brushed DC motor control

You can drive two Brushed DC motors: connect the two wires of each one in the

(+) and (-) screw terminals for each channel A and B.

• Direction control: set HIGH / LOW the DIR A (D12) / DIR B (D13) pins.

• Speed control: duty cycle of PWM A (D3) and PWM B (D11)

• Break: set HIGH the Brake A (D9) and Brake B (D8) pins

• Measure the current going through the DC motor: read SNS0 (A0) / SNS1

(A1). Use analogRead() on the analog input A0 / A1 (1.65 V/A)

How to use the motor:

1. Set the motor direction (polarity of the power supply) by setting it either

HIGH or LOW.

2. Disengage the brake pin for the motor channel by setting it to LOW.

3. Move the motor: set the speed by sending a PWM command (analogWrite) to

the appropriate pin.

Page 35: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor ShieldArduino Motor Shield Tutorial

http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/?ALLSTEPS

void setup() {

//Setup Channel A

pinMode(12, OUTPUT); //Initiates Motor Direction pin for Channel A

pinMode(9, OUTPUT); //Initiates Brake pin for Channel A

}

Ex. 1 – control one DC motor

• Plug the motor's positive

(red) wire into Channel A's

(+) terminal on the motor

shield, and the motor's

ground (black) wire into

Channel A's (-) terminal on

the shield

• Power the shield with en

external power supply as

explained above

Page 36: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor ShieldEx. 1 – control one DC motor (cont.)

void loop(){

//forward @ full speed

digitalWrite(12, HIGH);//Establishes forward direction of Channel A

digitalWrite(9, LOW); //Disengage the Brake for Channel A

analogWrite(3, 255); //Spins the motor on Channel A at full speed

delay(3000);

digitalWrite(9, HIGH); //Eengage the Brake for Channel A

delay(1000);

//backward @ half speed

digitalWrite(12, LOW); //Establishes backward direction of Channel A

digitalWrite(9, LOW); //Disengage the Brake for Channel A

analogWrite(3, 123); //Spins the motor on Channel A at half speed

delay(3000);

digitalWrite(9, HIGH); //Eengage the Brake for Channel A

delay(1000);

}

Page 37: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor Shield

void setup() {

//Setup Channel A

pinMode(12, OUTPUT); //Initiates Motor Channel A pin

pinMode(9, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B

pinMode(13, OUTPUT); //Initiates Motor Channel B pin

pinMode(8, OUTPUT); //Initiates Brake Channel B pin

}

Ex. 2 – control two DC motor

• Plug motors. positive (red)

wire into Channel A/B's (+)

terminal on the motor shield,

and the motor's ground

(black) wire into Channel

A/B's (-) terminal on the

shield

• Power the shield with en

external power supply as

explained above

Page 38: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor ShieldEx. 2 – control two DC motor (cont.)void loop(){

//Motor A forward @ full speed

digitalWrite(12, HIGH);//Establishes forward direction of Channel A

digitalWrite(9, LOW); //Disengage the Brake for Channel A

analogWrite(3, 255); //Spins the motor on Channel A at full speed

//Motor B backward @ half speed

digitalWrite(13, LOW); //Establishes backward direction of Channel B

digitalWrite(8, LOW); //Disengage the Brake for Channel B

analogWrite(11, 123); //Spins the motor on Channel B at half speed

delay(3000);

digitalWrite(9, HIGH); //Engage the Brake for Channel A

digitalWrite(8, HIGH); //Engage the Brake for Channel B

delay(1000);

//Motor A backeard @ half speed

digitalWrite(12, LOW); //Establishes backward direction of Channel A

digitalWrite(9, LOW); //Disengage the Brake for Channel A

analogWrite(3, 123); //Spins the motor on Channel A at half speed

//Motor B forward @ full speed

digitalWrite(13, HIGH); //Establishes forward direction of Channel B

digitalWrite(8, LOW); //Disengage the Brake for Channel B

analogWrite(11, 255); //Spins the motor on Channel B at full speed

delay(3000);

digitalWrite(9, HIGH); //Engage the Brake for Channel A

digitalWrite(9, HIGH); //Engage the Brake for Channel B

delay(1000);

}

Page 39: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor Shield

Stepper motor control

Ex.3 – bipolar stepper motor control (4 pins 2 coils).

• To use a stepper, you need to power these two coils in phase with alternating

polarity.

• To reverse the motor direction of a bipolar stepper, simply reverse the polarity

of the second coil.

How to use the motor:

1. Power the first coil.

2. Next power the second coil with reverse polarity.

3. Then power the first coil with reverse polarity.

4. Finally, power the second coil.

int delaylegnth = 30;

void setup() {

//establish motor direction toggle pins

pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards

pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards

//establish motor brake pins

pinMode(9, OUTPUT); //brake (disable) CH A

pinMode(8, OUTPUT); //brake (disable) CH B

}

Page 40: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor ShieldEx. 3a – bipolar stepper motor control – spin in one direction

void loop(){

digitalWrite(9, LOW); //ENABLE CH A

digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, HIGH);//Sets direction of CH A

analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A

digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, LOW); //Sets direction of CH B

analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

digitalWrite(9, LOW); //ENABLE CH A

digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, LOW); //Sets direction of CH A

analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A

digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, HIGH); //Sets direction of CH B

analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

}

Page 41: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor ShieldEx. 3b – bipolar stepper motor control – spin in the other direction

void loop(){

digitalWrite(9, LOW); //ENABLE CH A

digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, HIGH);//Sets direction of CH A

analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A

digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, HIGH); //Sets direction of CH B

analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

digitalWrite(9, LOW); //ENABLE CH A

digitalWrite(8, HIGH); //DISABLE CH B

digitalWrite(12, LOW); //Sets direction of CH A

analogWrite(3, 255); //Moves CH A

delay(delaylegnth);

digitalWrite(9, HIGH); //DISABLE CH A

digitalWrite(8, LOW); //ENABLE CH B

digitalWrite(13, LOW); //Sets direction of CH B

analogWrite(11, 255); //Moves CH B

delay(delaylegnth);

}

Page 42: Design with Microprocessors - users.utcluj.rousers.utcluj.ro/~tmarita/PMP/Lecture/C8.pdf · Design with Microprocessors ... #include  Servo myservo; ... Brushless DC

Arduino Motor Shield

Variante alternative:

https://www.robofun.ro/mecanice/driver/driver_dc/shield-

motoare-l298-v2

https://www.robofun.ro/forum/viewtopic.php?f=16&t=493

https://www.robofun.ro/forum/download/file.php?id=121&sid=3c

711a1384348b9f91b32405828a1015