Microcontroller Basics, Lesson 4 File
Download
Report
Transcript Microcontroller Basics, Lesson 4 File
Microcontroller basics
Embedded systems for
mortals
Lesson 4
• PWM
• Transistors and Fets
• Motors
• Servo
• DC
• Stepper
FETs
• FET = Field Effect Transistor
• 2 different types with 2 different modes:
• N-FET and P-FET
• Enhancement and depletion mode
Enhancement Mode
Depletion Mode
FET Applications
• Most commonly used as an electric switch
• High power and switching speeds possible
• N-Fet can be used to “cut” the ground wire
and P-Fet to “cut” the voltage source wire
PWM (Pulse Width Modulation)
• Basic terms
• Duty cycle = Pulse Width / Wave Period
Duty cycle
• 100 %
• 75 %
• 50 %
• 25 %
• 0%
←Ideal Square Wave
PWM Applications
•
•
•
•
•
Servomotor angle control
DC motor speed control
LED Dimming
Audio generation
Digitally generating analog voltages (requires
filtering)
PWM with Teensy
• Total of 9 pins with PWM capability
• 4 different PWM modes in total:
•
•
•
•
CTC
Fast PWM
Phase correct PWM
Phase and frequency correct PWM
• Requires using of internal interrupts and timers if
you want to modify the frequency of PWM
• Lot of libraries and examples available for
multiple applications for normal PWM
Servo motor
• Simple feedback motor that obtains a certain
position based on the input signal
• Position is determined by the duty cycle (=
pulse width)
• Without feedback a servo motor is basically a
normal motor, but speed is then controlled
with the PWM signal
Example 12 – Breadboard setup
Using servo motor
Orange= sign
Brown = GND
Power supply and Teensy need
to be in the same ground!
Red = + Volt
External power
supply
Example 12: Code
#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(16);
}
// attaches the servo on pin 9 to the servo object
void loop()
{
for(pos = 0; pos <= 120; pos += 1) // goes from 0 degrees to 120 degrees
{
// in steps of 1 degree
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 = 120; pos>=0; pos-=1)
// goes from 120 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
}
}
Turn the Servo
• Use a potentiometer to determine the postion
of the servo
• Servo still has the same wiring
Turning the Servo
#include <Servo.h>
Servo myservo;
// create servo object to control a servo
int potpin = 38; // analog pin used to connect the potentiometer (F0/A0)
int val;
// variable to read the value from the analog pin
void setup()
{
myservo.attach(16);
}
// attaches the servo on pin 16 (C6) to the servo object
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 120); // scale it to use it with the servo (value between 0 and 120)
myservo.write(val);
// sets the servo position according to the scaled value
delay(15);
// waits for the servo to get there
}
DC motor control
• Can change speed according to voltage
(PWM) or direction
• Requires an H-bridge for direction control
• Build yourself
• Get a DC motor driver that has it
DVR8833 Dual motor driver
Example 13: Breadboard setup
Example 13: Simple DC
int a1 = 15; //First directrion pin C5
int a2 = 16; //Second directrion pin C6
int spd = 100;
void setup(){
//Set both PWM pins as outputs
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
}
void loop(){
//First direction
digitalWrite(a1, LOW);
analogWrite(a2, spd);
delay(2000);
//Second direction
digitalWrite(a2, LOW);
analogWrite(a1, spd);
delay(2000);
}
DC – adjustable 1/2
int a1 = 15; //First directrion pin C5
int a2 = 16; //Second directrion pin C6
int spd = 0;
void setup(){
//Set both PWM pins as outputs
pinMode(a1, OUTPUT);
pinMode(a2, OUTPUT);
Serial.begin(9600);
}
void loop(){
for(spd=0; spd <= 250; spd += 1) //first dircetion, acceleration to 150
{
digitalWrite(a1, LOW);
analogWrite(a2, spd);
Serial.println(spd);
delay(20);
}
DC – adjustable 2/2
for(spd=250; spd >= 0; spd -= 1){ //first direction, deceleration to 0
digitalWrite(a1, LOW);
analogWrite(a2, spd);
Serial.println(spd);
delay(20);
}
for(spd=0; spd <= 250; spd += 1) //Second dircetion, acceleration to 150
{
digitalWrite(a2, LOW);
analogWrite(a1, spd);
Serial.println(spd);
delay(20);
}
for(spd=250; spd >= 0; spd -= 1){ //Second direction, deceleration to 0
digitalWrite(a2, LOW);
analogWrite(a1, spd);
Serial.println(spd);
delay(20);
}
}
Stepper motor
• Our motor is a 12 Volt 200 step motor
• With a stepper you can control speed,
direction and step amount
• 200 steps / 360 deg = 1 step is 1.8 deg
• Bipolar motor: two windings
• Red/Yellow
• Green/Grey
• No polarity! (no ”+” or ”-”)
http://bildr.org/2011/06/easydriver/
Example 14:
Breadboard setup
Exmaple 14: Stepper motor 1/2
//Controlling a Stepper mtor
#define DIR_PIN 24 //Dir pin to B4
#define STEP_PIN 25 //Step pin to B5
void setup() {
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
}
void loop(){
//rotate a specific number of degrees
rotateDeg(360, 1);
delay(1000);
rotateDeg(-360, .1);
delay(1000);
}
//reverse
Exmaple 14: Stepper motor 2/2
void rotateDeg(float deg, float speed){
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
if (deg > 0){
// define direction according to deg (negative for reverse)
digitalWrite(DIR_PIN, HIGH);
}
else{
digitalWrite(DIR_PIN, LOW);
}
int steps = abs(deg)*(1/0.225); //How many steps we want to take? e.g.
// 360*(1/0.225) = 1600 microsteps
float usDelay = (1/speed) * 80; //Speed defines the delay between pulses
for(int i=0; i < steps; i++){ //Toggle the step pin ON and OFF with the desired
// delay = speed
digitalWrite(STEP_PIN, HIGH); //Step pin ON
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW); //Step pin OFF
delayMicroseconds(usDelay);
}
}
Comparison of motors
Servo
- Angle is adjustable
- Active feedback
Servo motor 6 V: 10 €
- No need for a driver
DC
- Speed is adjustable
- No feedback
- Dual motor driver DRV8833: 5€
DC motor we used: 2 €
Stepper
- Speed and steps (=length of the move) are adjustable
- No feedback
Stepper motor 12 V 200 steps: 15 €
Stepper motor Driver: 15 €
The bigger the motor,
The more it cost,
The more power
Caution: Know what type of motor you need
and do not pay more for attributes or power
that you don’t need!