1 Microcontroller basicsEmbedded systems for mortals
2 Transistors PWM Motors: Servo motor DC motor Stepper motorLesson 4 Transistors PWM Motors: Servo motor DC motor Stepper motor
3 Transistor types Transistor = switching element,Control a larger current with a smaller current (voltage) Used as a switch, in amplifiers, logic, memory etc. BJT (Bipolar junction transistor) FET (Field-effect transistor) Base, emitter, collector Gate, drain, source Cheaper, more noisy, lower switching speed, wastes current, robust, More expensive, less noisy, higher switching speed, easily damaged if improperly connected Low power applications High power applications Current controlled Voltage controlled Current limiting resistor Pull-down (up) resistor Darlington=two BJTs together (e.g. TIP120) MOSFET= Metal oxide semiconductor field effect transistor JFET= Junction field effect transistor
4 Transistor types BJT FET
5 PNP and NPN P=positive, semiconductor doped to have ”holes”N=negative, semiconductor doped to have electrons NPN (N-channel) connect the ground wire, when base/gate HIGH PNP (P-channel) connect the voltage source, when base/gate LOW
6
7 PWM (Pulse Width Modulation)Basic terms Duty cycle = Pulse Width / Wave Period
8 Duty cycle 100 % 75 % 50 % ←Ideal Square Wave 25 % 0 %
9 PWM Applications Servomotor angle control DC motor speed controlLED Dimming Audio generation Digitally generating analog voltages (requires filtering)
10 PWM with Uno Total of 6 pins with PWM capability (~ symbol)PWM frequency Pins 5 & 6: default: 980Hz ~1kHz (62.5kHz base with 64 as prescaler) Pins 3,9,10,11: default 490Hz ~0.5kHz (31.25 kHz base with 64 as prescaler) Can be adjusted (30Hz-62.5kHz) with setPwmFrequency(); CAUTION: Affects the delay() millis() and Servo functions
11 LED fading 220 ohm current limiting resistor!File->Examples->Fading
12 DC motor Can change speed according to voltage (voltage controlled with PWM) Brushed and brushless Requires an H-bridge for direction control (motor drivers are also useful e.g. L9110 or L298N)
13 Diode: Prevent current spikes from going to transistor (inductive kickback)Capacitor: Reduce fast voltage transitions (100pF to 1nF), works without it
14 File->Examples->Fading
15 Servo motor
16 Example 12 – Servo Sweep Using 5 V servo motor
17 Example 12 – Servo Sweep #include
18 Example 13 - Turn the ServoUse a potentiometer to determine the postion of the servo Servo still has the same wiring just add the potentiometer as learned before
19 Example 13 - Turn the Servo#include
20 Stepper motor
21 Stepper motor Our motor is a 12 Volt 200 step motorWith 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 ”-”)
22 Example 15: Stepper Motor
23 Exmaple 15 - Stepper motor 1/2//Controlling a Stepper mtor #define DIR_PIN 2 //Dir pin to 2 #define STEP_PIN 3 //Step pin to 3 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); //reverse
24 Exmaple 15 - Stepper motor 2/2void 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
25 Accelstepper http://www.airspayce.com/mikem/arduino/AccelStepper/Install library, try example Bounce
26 Comparison of motors (Hobby) ServoAngle is adjustable, speed is constant Active feedback No need for a driver DC Speed is adjustable No feedback Needs a driver (or an H-bridge / transistor) Stepper Speed and steps (=length of the move) are adjustable Needs a driver Closed loop DC-motors (=servos) Active feedback (e.g. encoder) Drivers are recommandable Pricy but precise
27 The bigger the motor, The more it costs, The more powerCaution: Know what type of motor you need and do not pay more for attributes or power that you don’t need!