Basic Electronics 10, Chapter 10: Challenge 3 solution (level shifting, safe motors and choosing a board) Formulas: V(out) = V(in) x R2 / (R1 + R2) I = V / (R1 + R2) R(source) = R1 || R2 rise time = 2.2 R(source) C sense voltage = I x R(sense) counts = V / 3.3 x 4096 duty = DEAD + (100 - DEAD) x knob V(avg) = D x V (a) HC-SR04 echo to a Raspberry Pi pin, R1 = 1.8 k (series), R2 = 3.3 k (to ground) V(out) = 5 x 3300 / (1800 + 3300) = 5 x 3.3 / 5.1 = 3.235 V (above the 2.3 V threshold, below the 3.6 V limit) Divider current = 5 / 5100 = 0.980 mA Source impedance = 1800 || 3300 = 1800 x 3300 / 5100 = 1165 ohm Time constant with 30 pF: 1165 x 30e-12 = 34.9 ns rise time = 2.2 x 34.9 ns = 76.9 ns Why not 3.3 V up to 5 V: a divider's output is always lower than its input (k is below 1), so it can only shift a signal DOWN. Going up needs a gain stage: a MOSFET shifter module, a 74HCT buffer powered from 5 V, or just an Uno input, whose threshold of about 3.0 V accepts 3.3 V directly. (b) Current sense, R(sense) = 0.22 ohm in the motor's ground return (motor M1: stall 1.09 A, running 0.25 A) Running 0.25 A: V = 0.25 x 0.22 = 0.055 V counts = 0.055 / 3.3 x 4096 = 68.3 -> 68 Stall 1.09 A: V = 1.09 x 0.22 = 0.2398 V counts = 0.2398 / 3.3 x 4096 = 297.6 -> 297 Trip at 0.8 A: V = 0.8 x 0.22 = 0.176 V counts = 0.176 / 3.3 x 4096 = 218.5 -> 218 One count = 0.806 mV / 0.22 ohm = 3.66 mA (so the 0.8 A trip is about 218 counts and 1 count is under half a per cent of it) Power in the resistor at stall: I^2 R = 1.09^2 x 0.22 = 0.261 W -> use a 0.5 W resistor (a 0.25 W one would run hot) The resistor also takes up to 0.24 V of the motor's supply, and lifts the motor ground by that amount: acceptable for a 4.8 V pack, a poor trade at 1.5 V. Program logic: if the count stays above 218 for 25 loops (25 x 20 ms = 0.5 s), set the duty to 0 and stop; the brief start-up peak does not count. (c) Ramp and dead-zone mapping from Circuit 2 (DEAD = 18, STEP = 2 per loop, loop = 20 ms) Knob 0.5: duty = 18 + 82 x 0.5 = 59 % Average on a 4.8 V pack: 0.59 x 4.8 = 2.832 V (the driver drops a little more) Ramp from 0 to 100 %: 100 / 2 = 50 steps x 20 ms = 1.0 s Ramp from 0 to 59 %: 59 / 2 = 29.5, so 30 steps = 0.6 s The motor does not move until about 18 % (0.86 V on 4.8 V), which is the first step of the dead-zone map, so the knob has no dead region. (d) Choosing a board (i) Line-following robot with five 5 V analogue sensors: Arduino Uno. 5 V logic and ADC range match the sensors; six ADC pins; simple, forgiving and well simulated. (A Pico would need its inputs protected or divided to 3.3 V; a Raspberry Pi has no ADC at all.) (ii) Robot driven by a phone over Wi-Fi: ESP32. Wi-Fi and Bluetooth are on the chip, with many PWM and ADC channels. (An Uno or a Pico has no radio; a Raspberry Pi has one but is heavier, costlier and slower to start.) (iii) Robot identifying objects with a camera: Raspberry Pi. It runs Linux, Python and the vision libraries and has a camera connector. Add a Pico or an Arduino for the motors and sensors, since the Pi has no ADC and loose timing. (iv) Motor controller at 20 kHz with a TB6612 and a battery monitor: Raspberry Pi Pico. 3.3 V logic matches the TB6612, hardware PWM at 20 kHz on any pin, a 12-bit ADC for the divider, a watchdog, and MicroPython for fast testing. Expected readings (a) 3.23 V at the pin with a meter when the echo is high (the 5 V pulse is brief, so a meter sees an average: use a scope or a steady 5 V test) (b) with a meter on the sense resistor: 55 mV running, 240 mV stalled for a second at most (c) printed duty 59 %, motor average about 2.8 V after the ramp Wiring checklist - Divider on the echo: 1.8 k in series, 3.3 k to GND, pin at the junction; never connect the echo straight to the pin. - Sense resistor between the driver's GND (the motor's ground return) and the common ground; the ADC reads the voltage across it, so the sense lead goes to the resistor's driver side. - Fuse in the pack lead rated above the running current and below the wire rating (a 2 A fast-blow fuse for M1). - 100 k pull-downs on every driver input so the motor is off while the board boots; a switch in the pack lead as the hard stop.