Session 1 · 90 min
Where do the counts come from?
The encoder: a slotted disc that turns with each wheel, and a photo interrupter (a Sharp GP1A57HRJ00F on SparkFun's breakout: PWR, GND and SIG) whose beam the disc's bars block. SIG goes LOW when the beam is blocked and HIGH through a slot: the datasheet gives the high level as at least 4.9 V and the low level as at most 0.4 V (at 16 mA), with the sensor on the board's 5V (its operating supply is 4.5–17 V). The output drives itself both ways (the datasheet's diagram shows a 15 kΩ resistor in its output stage): it is not open-collector, so pinMode(pin, INPUT) is enough, with no pull-up. It also means a pin set as an OUTPUT and HIGH would be driven against the sensor: a short. So SIG is wired only after a sketch that leaves pins 2 and 3 as inputs is on the board. On the UNO only pins 2 and 3 work with attachInterrupt(), and the rover already uses both (STOP on D2, STBY on D3), so this unit re-plans the pins: encoders D2 (left) and D3 (right), STBY to D8, STOP to A0, an analog pin used as a digital input with its pull-up. Not D13: its on-board LED makes it a poor input.
In the studio
- Steer with two motors (watched only)
Hands-on
In Steer with two motors, find STBY in the driver's table: what do both wheels do while it is LOW? (The lab's wiring view still shows STBY on D3; on this rover it moves to D8.) The studio's labs still put STBY on D3 and STOP on D2: never paste a lab's pin lines into the rover's sketch; take every pin from the plan's pin table. Before the session the adult solders each photo interrupter, its 220 Ω resistor and a 0.1 µF capacitor across PWR and GND onto its breakout, then mounts a slotted disc so it turns with each wheel, and a breakout so the disc passes through its gap without touching, at least 4 mm down into it, with the open side away from windows and lamps. With the motor battery switched off and unplugged and USB unplugged, check that each disc turns freely through its gap. Still unpowered, follow the pin table: move the STOP button's wire from D2 to A0 and the driver's STBY wire from D3 to D8, then wire each breakout's PWR to the 5V rail and GND to GND, and leave both SIG wires unplugged for now. Change your sketch: STBY to 8, STOP to A0 with pinMode(A0, INPUT_PULLUP), pinMode(2, INPUT) and pinMode(3, INPUT), and print digitalRead(2) and digitalRead(3). After the adult's check, plug in USB only and upload it (or Bare Minimum, which sets no pins, if your sketch is not ready): the old sketch on the board still sets D3 as an OUTPUT. Unplug USB, then wire SIG to D2 (left) and D3 (right), routed away from the motor leads. Plug USB in again and turn each wheel slowly by hand: the reading must change from HIGH to LOW and back as bars and slots pass.
Starting code: The learner's own sketch that drives the rover (from R13-U3 or the Rescue Rover course), with STBY set to 8, STOP read from A0, pins 2 and 3 set as INPUT, and the HC-SR04 reading taken out, its Trig pin still set as an OUTPUT and written LOW in setup() so D9 never floats.
Pins
- D2
- left wheel encoder SIG (photo interrupter breakout)
- D3
- right wheel encoder SIG (photo interrupter breakout)
- D4
- driver AIN1 (left motor)
- D5
- driver PWMA (left motor)
- D6
- driver PWMB (right motor)
- D7
- driver AIN2 (left motor)
- D8
- driver STBY moved from D3
- D9
- HC-SR04 Trig (wired, not read; kept OUTPUT and LOW)
- D10
- HC-SR04 Echo (wired, not read in these sketches)
- D11
- driver BIN2 (right motor)
- D12
- driver BIN1 (right motor)
- A0
- STOP button (other side to GND) moved from D2
Evidence to collect
The rover's new pin table, checked against the wiring by the learner, and a short table per wheel: disc position (bar or slot in the gap) → reading, with the STOP button tested on A0.
- Predict
- Before turning a wheel, predict what digitalRead(2) prints while a bar sits in the gap, and while a slot does.
- Debug
- If a reading never changes, unplug USB and check that breakout's SIG wire, then that the disc really passes through the beam.
- Reflect
- Why did STOP and STBY have to move, and why not to D13?
⚠ Adult checks before power-on: The motor battery's power-off switch is off and its holder unplugged: no motor is driven in this session. The motor battery reaches the driver's VM only, through its power-off switch; the board's 5V goes to the driver's VCC only; every GND is joined. Before every upload: setup() never sets pin 2 or 3 as OUTPUT (the encoders drive those pins). Before USB first goes in: each breakout's PWR on 5V and GND on GND, not reversed, its capacitor across them; both SIG wires unplugged; STOP from A0 to GND, never across 5V and GND; STBY on D8; nothing else on D2, D3, D8 or A0. SIG goes in only after the upload, unpowered, to D2 or D3 only. Chassis tools: screwdrivers pushed away from hands; side-cutters cut cable ties flush, eye protection on, sharp ends turned in; hot glue low-temperature or adult-only (it burns); soldering or drilling adult-only. No wire is moved, pulled or plugged in while anything is powered: power-off switch off and battery unplugged first, then USB.
Works towards: Odometry