Subsection11.5.2Part 2: Wire the Sensor and Look at Its Output
We’ll wire the photointerrupter from the reading and put its signal wire on the oscilloscope before reading it with a pin. Before we wire anything, make a prediction.
Now suppose we leave the resistor out. The sensor is powered, the oscilloscope is on its signal wire, and nothing else is connected to that wire at all.
Everything but the sensor and one resistor is what you wired on Wednesday.
Not every kit has the same part. Some have the sensor on a small board with its three connections marked on it, and some have it on a length of four-core cable, where the wires are told apart by color instead (Figure 11.5.4). Either way, put the oscilloscope on the sensor’s row with its minus lead on the shared ground, and turn the wheel slowly by hand before you start the motor. If what you see ever goes above 3.3 V, stop there. Five volts on a Nucleo pin damages it, which is why we check this signal on the oscilloscope before plugging the wire into the header.
Figure11.5.4.The photointerrupter as some kits have it, on a length of four-core cable rather than on a small board. Brown is power, blue is ground, and black is the output. The pink wire is the sensor’s L terminal; leave it unconnected (Subsection 11.6.6).
Put the oscilloscope on that row with Waveforms, and turn the wheel slowly by hand. You are looking for a trace that toggles between 0 V and 3.3 V once for every slot that goes past. If it goes above 3.3 V, stop and check your wiring: 5 V will damage your Nucleo.
Which state is which is worth reading off your own screen. This sensor can be wired to switch on light or on dark, so turn the wheel by hand and watch which way the trace moves as a slot comes into the gap. Nothing in the rpm arithmetic depends on the answer, because either way there is one pulse for every slot, but it matters when you come to choose which edge to count.
The phototransistor can only pull OUT down. Whatever the resistor is tied to is what sets the HIGH level.
That also answers the prediction. With the resistor left out there is nothing to bring the line back up, so between pulses the row is connected to nothing at all: the trace is neither a square wave nor a flat line, but a wandering one, because a floating node picks up whatever is nearby. It can even look as though it is responding to the motor.
If the trace on your screen is not the one described above, work the circuit in the same order every time: power first, then ground, then the signal path. Table 11.5.8 is that order applied to this circuit.
Noise wandering around somewhere between the rails
Check that the pull-up resistor is fitted, and that the end of it really reaches 3.3 V.
A flat line that never moves, at either 0 V or 3.3 V
Nothing is changing the beam, so check first that the wheel’s rim really passes through the gap. If it does, the sensor is not switching: check its power and ground, and check that they are not swapped.
A clean square wave whose frequency does not change when the motor speeds up
The probe is still on Wednesday’s PWM node, which runs at a fixed 1.6 kHz whatever the motor does. Move it to the row the sensor’s signal wire lands in.
Why 3.3 V, and not the sensor’s own 5 V. The resistor’s rail is what sets the HIGH level, and 3.3 V is what the STM32C031C6’s pins expect. Pulled up to 5 V, this line would carry 5 V into whichever pin you wired it to.
Using a much smaller resistor wastes current and makes the transistor work harder to pull the line down. A much larger resistor takes too long to bring the line back up against the stray capacitance of the wiring, and picks up more noise. Ten kilohms is comfortably inside the wide middle.
In Lab 6 you can use this resistor, or enable the pin’s own internal pull-up with PUPDR the way you did for the button. Today we use the external one, because we are putting a scope probe on that node.