H-bridges, motor direction and stepper motors
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A DC motor turns the way the current through it flows. An H-bridge, four switches around the motor, chooses that direction, and must never close both switches on one side. Follow an Arduino Uno R3 driving a TI DRV8833: two input pins pick forward, reverse, coast or brake, and analogWrite() on one input sets the speed. Then drive a two-coil stepper one coil at a time and watch it move in exact steps. The lesson “Driving a servo and a one-way DC motor with a MOSFET” switched a motor one way; “PWM: dimming an LED” explains duty cycle; for a speed that holds under load, see the Control lesson “DC motor speed control”.
Current sets the direction
A DC motor turns one way when current enters its left lead, and the other way when current enters from the right. One transistor, as in the servo and MOSFET lesson, only switches current on and off: one direction.
DC motor current left -> right : turns forward
current right -> left : turns in reverse
one transistor: on or off, always the same way round
to reverse, the current must cross the motor the other wayاقرأ الدرس خطوة بخطوة
Current sets the direction
A DC motor turns one way when current enters its left lead, and the other way when current enters from the right. One transistor, as in the servo and MOSFET lesson, only switches current on and off: one direction.
DC motor current left -> right : turns forward current right -> left : turns in reverse one transistor: on or off, always the same way round to reverse, the current must cross the motor the other wayFour switches in an H
An H-bridge puts the motor in the crossbar of an H. Each side has a high-side switch to the supply (S1, S3) and a low-side switch to ground (S2, S4). Closing two of them picks the path.
supply (+) S1 ─┬──────┬─ S3 high side └─ M ──┘ the motor is the crossbar S2 ─┴──────┴─ S4 low side ground (-)Forward: close S1 and S4
Close S1 and S4, one high and one low on opposite sides. Current leaves the 6 V battery, crosses the motor from left to right and returns through S4 to ground. The motor turns forward.
closed S1 + S4 open S2, S3 path battery + -> S1 -> motor (left to right) -> S4 -> battery - motor forwardReverse: close S3 and S2
Now close the other diagonal, S3 and S2. Current crosses the motor from right to left, so it turns the other way. Same battery, same motor: only the switches changed.
closed S3 + S2 open S1, S4 path battery + -> S3 -> motor (right to left) -> S2 -> battery - motor reverseNever both on one side
Close S1 and S2 together and the battery is shorted through two switches, missing the motor. That is shoot-through: a huge current that can burn the switches. Drivers leave a short dead time, all off, between turning one off and the other on.
closed S1 + S2 (or S3 + S4) path battery + -> S1 -> S2 -> battery - no motor in the way shoot-through: only the switches and wires limit the current DRV8833 dead time 450 ns (VM = 5 V), built inA driver chip does it
The DRV8833 holds two H-bridges made of transistors. The Uno only sets two pins: AIN1 = 1 and AIN2 = 0 means forward, so the chip closes S1 and S4. The motor current comes from the battery, not the Uno.
AIN1 AIN2 OUT1 OUT2 switches motor 0 0 Z Z none coast 0 1 L H S2+S3 reverse 1 0 H L S1+S4 forward 1 1 L L S2+S4 brake VM 2.7 to 10.8 V · 1.5 A RMS, 2 A peak per bridge // Arduino Uno R3 + TI DRV8833 (nSLEEP tied high, VM = 6 V) const int AIN1 = 9, AIN2 = 10; // bridge A: the DC motor void drive(int in1, int in2) { digitalWrite(AIN1, in1); digitalWrite(AIN2, in2); } void setup() { pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); } void loop() { drive(HIGH, LOW); delay(2000); // forward drive(LOW, LOW); delay(1000); // coast drive(LOW, HIGH); delay(2000); // reverse drive(HIGH, HIGH); delay(1000); // brake analogWrite(AIN1, 128); // forward, about half digitalWrite(AIN2, LOW); delay(2000); }Stop: coast or brake
Inputs 0 0 open all four switches: the motor coasts slowly to a stop. Inputs 1 1 close both low-side switches, shorting the motor. Its own spin now drives a current that fights the turning, so it brakes fast.
0 0 coast all open, the motor spins down on its own 1 1 brake S2 + S4 short the motor's leads the spinning motor acts as a generator; its current brakes itSpeed: PWM on one input
analogWrite(AIN1, 128) switches AIN1 on and off 490 times a second, on for 128 of every 255 parts: about half. Off means 0 0, coast. The motor is too heavy to follow each pulse, so it runs at roughly half speed.
line 3 analogWrite(AIN1, 128); AIN2 stays LOW PWM 490 Hz on Uno pin 9: one cycle every 2.04 ms duty 128 / 255 = 50.2 % on 1 0 forward off 0 0 coast (fast decay) the drawing slows the switching right downA stepper motor
A stepper has two coils, A and B, around a magnet rotor. Current through a coil makes a magnetic field; the rotor turns to line up with it and holds. Each coil needs its own H-bridge: the DRV8833 has two.
stepper 2 coils (A, B) + a magnet rotor coil A on bridge A (AIN1, AIN2) coil B on bridge B (BIN1, BIN2) current in a coil -> a field -> the rotor lines up and holdsOne coil at a time
Energise coil A forward, then B forward, then A reversed, then B reversed. Each change turns the field a quarter turn and the rotor follows: one step. Repeat to keep turning; run the order backwards to turn the other way.
step AIN1 AIN2 BIN1 BIN2 coil rotor 1 1 0 0 0 A+ 0° 2 0 0 1 0 B+ 90° 3 0 1 0 0 A- 180° 4 0 0 0 1 B- 270° // wave drive: one coil at a time. Bridge A = coil A, bridge B = coil B const int PIN[4] = {9, 10, 7, 8}; // AIN1 AIN2 BIN1 BIN2 const int SEQ[4][4] = { {1, 0, 0, 0}, // A+ {0, 0, 1, 0}, // B+ {0, 1, 0, 0}, // A- {0, 0, 0, 1} // B- }; void stepTo(int n) { for (int i = 0; i < 4; i++) digitalWrite(PIN[i], SEQ[n % 4][i]); } void setup() { for (int i = 0; i < 4; i++) pinMode(PIN[i], OUTPUT); } void loop() { for (int n = 0; n < 200; n++) { stepTo(n); delay(10); } // 1 turn }Count steps, know the angle
Our drawing needs 4 steps per turn. A common real stepper has a toothed rotor and needs 200: 1.8 degrees each, so 50 steps is exactly 90 degrees, with no sensor. Step it too fast and it misses steps, and the count is wrong.
drawing 4 steps per turn, 90° each real 200 steps per turn, 1.8° each 50 steps 50 x 1.8° = 90° open loop: no sensor checks the angle too fast -> missed steps -> the count is wrong
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In an H-bridge, which pair of switches makes the motor turn forward (current left to right)?
S1 connects the left lead to the supply and S4 connects the right lead to ground, so current crosses the motor from left to right. S3 and S2 send it the other way.
02What happens if S1 and S2 (both switches on the left side) close together?
The current path from the supply to ground skips the motor entirely, so only the switches and wires limit it. Drivers prevent this with dead time; the DRV8833's is 450 ns, built in.
03On the DRV8833, what do inputs AIN1 = 1, AIN2 = 1 do?
The datasheet's truth table gives 1 1 as brake (slow decay): both outputs are driven low, so the motor's leads are shorted and it stops quickly. 0 0 is coast.
04analogWrite(AIN1, 128) with AIN2 LOW on an Uno: what does the motor see?
analogWrite makes a PWM signal at 490 Hz on pin 9; 128 out of 255 is about 50 % on. Between pulses the bridge coasts, and the motor's inertia averages the pulses into roughly half speed.
05A stepper has 200 steps per revolution. How many steps turn it 90 degrees?
Each step is 360 ÷ 200 = 1.8 degrees, and 90 ÷ 1.8 = 50. No sensor is needed, as long as the motor is not stepped faster than it can follow.
المسرد
- H-bridge
- Four switches arranged around a motor like the letter H, so current can be sent through it in either direction.
- High side / low side
- A high-side switch connects a motor lead to the supply; a low-side switch connects it to ground.
- Shoot-through
- Both switches on one side of a bridge on at once: the supply is shorted to ground, with a huge current.
- Dead time
- A short pause, with both switches of one side off, between turning one off and the other on, to prevent shoot-through.
- Coast and brake
- Coast: all switches open, the motor slows on its own. Brake: the motor's leads are shorted, so it stops fast.
- Motor driver
- A chip, such as the DRV8833, that holds the bridge transistors and carries the motor current from its own supply.
- Stepper motor
- A motor with two (or more) coils that turns in fixed steps as the coils are energised in turn.
- Open loop
- Control without measuring the result: a stepper is told how many steps to make, and nothing checks the angle.
الملاحظات والمصادر
- The four switches are drawn as simple switches. Inside the DRV8833 they are N-channel MOSFETs, and its logic only ever selects the four states of its truth table, so it cannot be told to short one side. Its built-in dead time is 450 ns at 5 V.
- With PWM on AIN1 and AIN2 low, the off part of each cycle is the coast state: the datasheet calls it fast decay, and the motor's current dies away through the transistors' body diodes, which the drawing leaves out. Speed is only roughly proportional to the duty cycle; for an exact speed, measure it and correct it (the Control lesson "DC motor speed control").
- Tie the DRV8833's nSLEEP pin high: it has an internal pull-down, and low means sleep with every switch open. Its inputs accept up to 5.75 V, so the Uno's 5 V pins are fine. The Uno and the battery share ground.
- The drawing's rotor is a single two-pole magnet, so 4 steps make a turn. Real hybrid steppers have toothed rotors and stators; 200 steps (1.8°) is common. In the drawing the stepper sits beside the DC motor; really you would connect its coils to the two bridges in place of the motor.
- Arduino’s Stepper library energises both coils at once (its 4-wire sequence is 1010, 0110, 0101, 1001). That gives more torque and parks the rotor between the poles; one coil at a time is easier to see.
- Texas Instruments DRV8833 dual H-bridge motor driver datasheet (SLVSAR1E)
- Arduino language reference: analogWrite() (source)
- Arduino Stepper library: Stepper.cpp (coil sequences)
- Arduino Stepper library example: stepper_oneRevolution
- Adafruit: Stepper motor, NEMA-17, 200 steps/rev (product 324)
- Oriental Motor: Stepper motor overview
- J. W. Valvano, Embedded Systems: Shape the World, ch. 6 (UT Austin)
اختبار يتاح بعد الخطوة الأخيرة.