10 : Output Devices<
Hero Shot<
Learning Outcomes<
What did I learn this week ?
- Electronics Theory :
- Physics behind semiconductors and diodes
- Physics behind a MOSFET transistor
- How to solve a circuit with a MOSFET transistor
- How to use PWM to control current or voltage
- H-bridges
- Stepper Motors :
- How does a stepper motor work
- Different kind of stepper motor
- How does a stepper motor driver work
- Advantages and Inconvenients of each stepper motor type
Assignments<
This tenth week's asignments are :
- Group :
- Measure the power consumption of an output device
- Individual :
- Add an output device to a microcontroller board you've designed, and program it to do something
1. Theory<
1.1. Semiconductors Theory<
How do matter conduct electricity ?<
Let's first look at how a conductor material works. All materials are made of atoms. Atoms are made of a positively charged nucleus and a cloud of electrons more or less attracted by this nucleus which stops them from travelling freely inside the material. Indeed, to detach a electron from an atom, it requires an energy greater than the bonding energy between the electron and its nucleus.
However, conductor materials' atoms have an interesting property : they have a weekly bound electron around them. It implies that the thermal energy of the material is sufficient to detach this electron from their nucleus. They are therefore free to move inside the material and generally have a quite chaotic motion between the atoms. This motion can however be guided in a direction by applying an electric field inside the material (this is done by applying a voltage, I will pass the details). That's what we call electrical current !
Before going through the semiconductors theory, let's do two important remarks. First, the conductors atoms are neutral by default which means that the positive charge of their nucleus is equal to the sum of the negative charges of all their electrons. Therefore when an weekly bound electrons is detached and moves away from its atom, the latter becomes positively charged.
Second, conductor materials atoms are arranged in a crystal structure, i.e. strongly bound 3D grid structure. The strength of the bounding between the atoms implies that the free electrons motion does not make the material atoms move.
Semiconductor materials<
Semiconductor materials atoms are also arranged in a crystal structure but their external electrons are involved in the atoms to atoms bonding. In a way, they play a cement role and removing them require way more energy than in conductor materials, the thermal energy is not sufficient in livable temperatures.
Furthermore, removing them would create a hole in the bonding structure which will attract the temporarily free electron to its initial spot.
The semiconductor materials become interesting when one insert intruders inside them. Here, intruders are atoms similar to the semiconductor ones but that do not have exactly the same number of external electrons. For example an atom that have a supplementar external electron will fit correctly in the crystal structure but its supplementar electron would be a third wheel. It then becomes a free electron and the intruder becomes a fixed positively charged atom (as it was neutral at first).
We call that N-type doped semiconductor.
One can imagine the opposite scenario where the intruder has one less electron than the semiconductor atoms, a hole then appears in the bonding structure. Any close electron could fill that hole and make the intruder a negatively charged atom. However it would let a hole behind itself and create what we call free positive charges (that are actually the holes).
We call that P-type doped semiconductor.
Diodes<
Let's now consider a P-type and N-type semiconductors close together.
If one stick them, it creates what is called a PN junction, the free electrons of the N-type will move towards the P-type to fill the holes close to the junction.
This process will keep going until an electrostatic balance is achieved and creates a region around the junction where there are no free charges anymore (indeed all freee electrons of this region found a hole to fill).
One the N-type side of the junction, the intruder atoms are positively charged (since they gave one of their external electron) and on the P-type side, the intruder atoms are negatively charged (since they accepted an supplementar electron). These charged atoms creates a potential difference (i.e. a voltage) that blocks new free charges to go through junction.
This blocking mechanisme can be used to create an electrical component that let current flows only in one way, i.e. a diode. Indeed if you imagine a PN junction with conductive extremity inside a circuit containing a tension source, the voltage of the source will be added to the tension of the PN junction. If they are in opposite directions, the tension superposition will diminish the PN junction barrier :
The inner tension of the PN junction depends on the semiconductor material. The most common is Silicium and has a 0.6 V inner tension which means that if the power source creates an opposite tension greater than 0.6 V, the barrier will disappear and the free charges of the semiconductors will manage to go through the junction.
On the other hand, if the power source creates a tension in the same direction as the junction inner tension, the barrier will get larger and the free charges will still be unable to go trough the junction.
1.2. Transistors Theory<
MOSFET Transistor<
The PN junction properties can be used in more complex structure such as the MOSFET transistor which is composed of two small N-type areas and a large P-type (or the opposite) in between as shown below. The two N-types have a conductive tip and can be connected to a circuit (respectively on the points referred as "S" and "D") as well as the lower part of the P-type (on the point referred as "B"). On the other hand, the upper part of the P-type is isolated from the "G" part of the circuit. Therefore no current can pass from G to the semiconductor but the electrostatic effects will play as we will explain further.
If each area is connected to the ground, two barriers form between the P-type and each one of the N-types and nothing more happens. One can also imagine a scenario where the lower central part (referred as "B" on the picture) is connected to a tension source. In this case, it simply corresponds to two diodes stuck together, nothing new would happen and we would go back to the diodes part of this documentation.
This structures becomes interesting when one imagine that the upper "G" part is connected to a positive voltage source. The latter will electrostatically repulse the holes of the P-type semiconductor and let negative fixed ions behind and create a thin negative barrier along the isolant.
If the tension connected to "G" becomes greater than the semicondutor threshold tension (i.e. 0.6 V for Silicium), free electrons of the N-type semiconductors will go through their barrier and distribute along the isolant border. A thin channel of free electrons created by the "G" tension thus connect "S" to "D".
The greater the "G" tension is compared to the "S" and "D" ones, the larger the channel will be.
This channel can be used to transport electrons and hence create current. Indeed, let's now imagine that a positive tension is applied to "D". The voltage between "D" and "S" creates a current in the channel.
The fixed ions in the channel acts like a resistor therefore the current is proportionnal to the voltage between "D" and "S" but on the other side, the resistance value depends on the channel width and therefore and the voltage between "G" and "S".
If the tension of "D" becomes to close to the "G" one, the channel width becomes non-uniform and start to narrow close to the right semiconductor. Therefore, increasing this tension has to effect : it pushes stronger the free charges and it increases the resistance.
These to effect will balance when the voltage between "D" and "G" goes below the semiconductor threshold tension. This is called the saturation region.
The required voltage to get to this saturation obviously depends on the initial width of the channel hence on the voltage between "G" and "S".
Most of the time, a transistor is used as a valve by activating and deactivating the "G" tension which opens and closes the channel. The tension applied at "G" determines the current that will go through the MOSFET. Two different formulas can be used to compute the current determined by the "G" tension depending on if it is in the triode or the saturation region.
To make it short, MOSFET transistor acts like :
- A resistor whos value is electrically controlled (for low voltage)
- A varying resistor whose range of value is electrically controlled (just before the saturation region)
- An electrically controlled valve (in the saturation region)
Transistor with a resistance<
Usually a transistor is used in a circuit hence is connected to another device which most of the time is resistive. In the circuit below, we can choose VDD but the voltage between "D" and "S" is not a free parameter. It is completely determined by the "G" tension which determines the current and hence the tension trhough the resistor. Therefore the voltage between "D" and "S" is the difference between VDD and the voltage through the resistor.
Graphically, it corresponds to the intersection point of the green line and the transistor characteristic curve of the corresponding grid tension. The green line slope is inversely proportional to the resistor value.
Transistor with a resistance and an inductance<
H-Bridge<
1.3. Stepper Motors Theory<
General Principle<
A stepper motor is a discrete step motor. It is made of two distincts part : a rotor composed of permanent magnets and a stator containing coils of wire that will sometimes contain current hence create a magnetic field that will cause the rotor teeh to move in their direction as we will explain further. Each coil is controlled by a transistor (not exactly but we will se that further).
Each coil is divided in two small antipodal coils that are called phases.
To make it simple, let's imagine a 4-teeth stepper motor with 6 phases (hence 3 separate coils). When one of the phase is energized, the permanent magnet poles will allign with the phase induced magnetic field. One can then turn off the energized phase and turn on the next one : the rotor will sense a torque tending to align its poles with the new magnetic field and making it turn by a small step angle (60° in our example but much smaller in real cases). By repeating these step, the rotor turns.
In real cases, the rotor has a lot more than 4 teeths like one can see on the first stepper motor picture and each phase top also has small teeth. When a coil is powered, the teeth of the rotor align with the phase's ones. The teeth of the next phase are a bit shifted in order to make the rotor rotate by a small angle during the next phase. This angle is the motor step angle and is completely determined by the phase teeth offset and the rotor's number of teeth.
Earlier I said that antipodal phases are two halves of a same coil. One may be concerned about how the rotor could make a full turn because if the top phase creates an upper magnetic field, what about the bottom phase induced magnetic field ? Is it wrapped in a direction such that it induces a magnetic field pointing in the same way as its antipodal phase ? Then, what will happen after the rotor poles make half a turn ? There are actually two ways to make a full rotation possible.
In unipolar stepper motors, antipodal phases have opposite induced magnetic field but can be energized independently thanks to a central tap. Each phase can then almost be considered as an independant coil. The driver circuit is thus quite simple but these motors have a quite small torque and precision.
In bipolar stepper motors, antipodal phases have winding directions such as their respective induced magnetic fields are poiting in the same way. Therefore to perform a complete rotation of the rotor, the current must be inverted in the coils after half a rotation. The torque is thus higher than in unipolar motors since the two whole coil is contributing to the magnetic field at each step.
Unipolar Stepper Motor<
Mechanism<
As an example, I will use a 28BYJ-48 stepper motor which is four phases unipolar stepper motor. According to its datasheet, it has a 5.625° angle step which means its rotor has 32 teeth (if my computations are good).
Since it is a unipolar, phases can be energized independentely thanks to a central tap shown on the diagram below. This diagram representation may be confusing, to make it clear, remember that each half of a coil (orange and purple for example) are antipodal around the rotor (if the orange half is the upper phase then the purple half would be the bottom phase).
The tension at both of each coil tips can be controlled separately but both coils centers are connected to the power tension via a central tap. If the orange coil tip is grounded, current will flow from the power tension to the orange tip and the orange phase will induce a magnetic field. One must then ground each coil tip in the good order to make the rotor turn. There are three valid sequences to make it turn.
Wave Mode<
In wave mode, one phase is energized at a time resulting in a full step rotation with the rotor poles always tending to align with the energized phase magnetic field. However only one phase contributes to the magnetic field at a time resulting in a quite low torque.
| Color | Step 1 | Step 2 | Step 3 | Step 4 |
|---|---|---|---|---|
| Orange | 0 | 1 | 1 | 1 |
| Yellow | 1 | 0 | 1 | 1 |
| Pink | 1 | 1 | 0 | 1 |
| Blue | 1 | 1 | 1 | 0 |
| Red | 1 | 1 | 1 | 1 |
Full-Step Mode<
In full-step mode, two phases are always energized at a time resulting in a full step rotation with the rotor poles always between the two magnetic fields direction induces by the two energized phases. Two phase cotributing to the magnetic field implies a better torque (even if they are not perfectly aligned).
| Color | Step 1 | Step 2 | Step 3 | Step 4 |
|---|---|---|---|---|
| Orange | 0 | 1 | 1 | 0 |
| Yellow | 0 | 0 | 1 | 1 |
| Pink | 1 | 0 | 0 | 1 |
| Blue | 1 | 1 | 0 | 0 |
| Red | 1 | 1 | 1 | 1 |
Half-Step Mode<
Half-step mode is an alternance of the two previous ones resulting in a half-step rotation. It produces a more fluid motion alterning between lower and higher torque.
| Color | Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | Step 6 | Step 7 | Step 8 |
|---|---|---|---|---|---|---|---|---|
| Orange | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |
| Yellow | 1 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
| Pink | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 |
| Blue | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 |
| Red | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Wires<
The grounding sequence must be done by a microcontroller connected to the motor thanks to the wires shown below.
Each coil tip is connected to a wire and both coils center to a common power tension hence the motor has 5 wires. Some unipolar stepper motors have 6 wires because the two central taps can be connected at two different power source.
Driver<
Naively (like me before asking Michel), one could think that the motor wires could directly be connected a microcontroller pins and one could directly send signals to the stator phases with the correct sequence.
However, directly applying the microcontroller inner voltage to a phase would imply a large current (since the copper coils resistance is low) which is not supported by the microcontroller. One could think (like me before asking Michel) that putting a large resistor between the microcontroller pin and the phase could solve the problem but then the induced magnetic field in the phases hence the motor's torque would be too low.
Ideally, we would like the microcontroller to send signals to open and close current valve directed towards the motor phases : transistors (what a plot twist... I did not introduce transistors only for fun). The circuit connecting the microcontrollers to the motor through transistors is called a driver.
A typical unipolar stepper motor driver circuit is on the picture below.
It may seem complicated because of the diodes but the latter are here to allow the transistor to work with an inductance. Indeed the voltage through an inductance is proportional to the derivative of the current thus when one close the transistor's channel, the current will rapidly go to zero and the voltage through the inductance will be really high. Since the transistor is between the ground and the inductance, it may destroy it.
Bipolar Stepper Motor<
Mechanism<
In bipolar stepper motor, antipodal phases are always energized at the same time but their polarisation must be inverted each half sequence. This can be done with a driver containing H-bridges which is a bit more complicated that a unipolar motor driver.
On the other side the circuit diagram of the bipolar stepper motor itself is simpler since it has no central tap, they thus have four wires to be connected. Note that a unipolar stepper motor can be used as a bipolar simply by not using the central tap and using a bipolar motor driver.
Wave Mode<
In wave mode, one pair of antipodal phase is energized at a time resulting in a full step rotation with the rotor poles always tending to align with the energized pair magnetic field.
| Phase | Step 1 | Step 2 | Step 3 | Step 4 |
|---|---|---|---|---|
| A+ | + | 0 | - | 0 |
| B- | 0 | + | 0 | - |
| A- | - | 0 | + | 0 |
| B+ | 0 | - | 0 | + |
Full-Step Mode<
In full-step mode, two pairs are always energized at a time resulting in a full step rotation with the rotor poles always between the two magnetic fields direction induces by the two energized pairs. Two pairs contributing to the magnetic field implies a better torque (even if they are not perfectly aligned).
| Phase | Step 1 | Step 2 | Step 3 | Step 4 |
|---|---|---|---|---|
| A+ | + | - | - | + |
| B- | + | + | - | - |
| A- | - | + | + | - |
| B+ | - | - | + | + |
Half-Step Mode<
Half-step mode is an alternance of the two previous ones resulting in a half-step rotation. It produces a more fluid motion alterning between lower and higher torque.
| Phase | Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | Step 6 | Step 7 | Step 8 |
|---|---|---|---|---|---|---|---|---|
| A+ | + | + | 0 | - | - | - | 0 | + |
| B- | 0 | + | + | + | 0 | - | - | - |
| A- | - | - | 0 | + | + | + | 0 | - |
| B+ | 0 | - | - | - | 0 | + | + | + |
Driver<
As the unipolar stepper motor, a bipolar requires a driver to invert the circuit polarization. Such a circuit is called a H-bridge and its working principle is detailed higher in this documentation.
Pulse Width Modulation<
2. Individual Assignment<
Since I spent almost the whole week on theory, my individual assignment will be quite short : make a stepper motor turn without using a stepper library.
I used a 28BYJ-48 (unipolar stepper motor with 4 phases) with a ULN2003 driver. Below you may find
The motor is the one I used for an example earlier :
and below you may find how I connected the motor and my board to the driver. The white socket holds the motor cables (you may recognize the colors) while the other are connected to the board.
Here is how they are connected to the board (on the right is the FTDI converter to connect my board to my laptop USB port, check week 8 for more details about my board).
The code I used is a code that we made with a classmate last year for a class project. The most interesting part of the code is how the rotation sequence is translated :
void turn() {
switch(counter % 8) {
case 0:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
case 1:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
break;
case 2:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
break;
case 3:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
break;
case 4:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
break;
case 5:
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
break;
case 6:
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
case 7:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
default:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
}
}
You may have recognized the half-step mode sequence. Here is a video of the motor rotating :
And you may find the full code below.
Full Arduino Code
int pin1 = 2;
int pin2 = 3;
int pin3 = 4;
int pin4 = 5;
float break_time;
int counter = 0;
unsigned long previous_time;
bool is_on = false;
void start(int minute) {
break_time = (minute * 60000) / 4096;
counter = 0;
previous_time = millis();
is_on = true;
}
void turn() {
switch(counter % 8) {
case 0:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
case 1:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
break;
case 2:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, HIGH);
break;
case 3:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
break;
case 4:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
break;
case 5:
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
break;
case 6:
digitalWrite(pin1, LOW);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
case 7:
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
default:
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
break;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
start(1) // test for 1 minute
}
void loop() {
// put your main code here, to run repeatedly:
if ((is_on) and (counter < 4096)){
unsigned long current_time = millis();
if (current_time - previous_time >= break_time) {
previous_time = current_time;
turn();
counter++;
}
}
}