Skip to main content

Subsection 11.5.2 Part 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.

Activity 11.5.2. Predict the trace.

You are about to wire the photointerrupter as in Figure 11.5.2: power, ground, its signal wire to a row of its own on the breadboard, and a 10 kΩ resistor from that row up to 3.3 V. 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. The motor is running, so slots are going past the beam. What would the trace look like, and why?
The photointerrupter has three connections, and all three are in Figure 11.5.2. Its ground goes to the breadboard’s ground rail. Its power is 5 V, taken from the regulator, the same node that already feeds the motor driver’s VM pin, because the EE-SX67 series datasheet gives the part’s supply voltage as 5 to 24 V DC. Its signal wire goes to a row of its own on the breadboard, and a 10 kΩ resistor goes from that row up to 3.3 V. So the sensor runs from 5 V and the resistor is tied to 3.3 V. We’ll see below why those are two different rails.
Figure 11.5.2. The circuit for today’s measurement: Wednesday’s circuit with the photointerrupter and one resistor added. The sensor’s power goes to the regulator’s 5 V, the same node that feeds the driver’s VM pin. Its signal wire goes to a row of its own, with the 10 kΩ pull-up from that row to 3.3 V.
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.
Figure 11.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).

Activity 11.5.3. Wire the sensor, and watch it while the motor ramps.

(a)

Wire the photointerrupter’s power and ground. Its power is 5 V, taken from the regulator, just as the motor’s is. Run its signal wire to a row of its own on the breadboard, and put the 10 kΩ resistor between that same row and 3.3 V.

(b)

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.

(c)

Run TTmotor_ramp.c and watch the trace as the motor speeds up and slows down again.
The square wave you are looking at runs between 0 V and 3.3 V, and that 3.3 V does not come from the sensor at all: the sensor runs on 5 V. The phototransistor inside the part is a switch to ground and nothing else. In one of the two states it conducts and pulls the signal wire down to 0 V; in the other it stops conducting and lets go of the line, at which point the wire is pulled up through the 10 kΩ resistor to 3.3 V. So the rail the resistor is tied to is what sets the HIGH level, and we tied it to 3.3 V because 3.3 V is what the STM32C031C6’s pins expect. Figure 11.5.6 draws both states.
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.
Figure 11.5.6. Why the sensor’s output needs a pull-up resistor. In the left panel the beam reaches the phototransistor and it conducts, pulling OUT down; in the right panel a spoke blocks the beam, the transistor stops conducting, and the 10 kΩ resistor is the only thing left holding the line up. Whatever that resistor is connected to is what sets the HIGH level, here 3.3 V, which is what the STM32C031C6’s pins expect.
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.
You have seen this behavior before under a different name. The two I2C pins were configured as open-drain outputs (Subsection 10.3.6), which can pull a line down and cannot drive it up, and the bus needed pull-up resistors for exactly the reason above. The photointerrupter’s output does the same job with a different kind of transistor (its datasheet calls it an open collector), and the one difference that matters to you is that there is no OTYPER bit to set here. The behavior is built into the part and cannot be configured away, so the resistor is not optional. The resistor’s value is a trade-off with a wide middle, and 10 kΩ is comfortably inside it; Subsection 11.6.6 works both ends of that trade-off out, and gives the internal-pull-up alternative Lab 6 allows.
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.
Table 11.5.8. Three traces, and where to look
What you see Where to look
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.