Basic Electronics 10, Chapter 10: Challenge 1 solution (pins, pull-ups and the ADC) Formulas: R = (V(pin) - V(LED)) / I I = Vcc / Rp when a pull-up is pressed rise time = 2.2 Rp C LSB = Vref / 2^N code = floor(Vin / Vref x 2^N) divider k = R2 / (R1 + R2) V(bat) = code x LSB / k (a) Red LED, V(LED) = 2.1 V, Uno pin at 5 V, target 12 mA R = (5 - 2.1) / 0.012 = 2.9 / 0.012 = 241.7 ohm Nearest of 220, 270, 330, 390: 220 ohm (21.7 ohm away; 270 is 28.3 away). Actual current with 220 ohm: I = 2.9 / 220 = 13.18 mA (under the 20 mA recommended per pin) (with 270 ohm it would be 10.74 mA, a little dimmer and more conservative: both are acceptable) Total current for N LEDs at 13.18 mA each: 6 LEDs: 6 x 13.18 = 79.1 mA under 200 mA: OK 12 LEDs: 12 x 13.18 = 158.2 mA under 200 mA: OK (and every pin is within its own limit) 20 LEDs: 20 x 13.18 = 263.6 mA OVER the 200 mA chip total: not allowed, even though each pin is fine So 12 LEDs at once is the largest of the three; use 270 ohm and they total 128.9 mA, or add a driver chip beyond that. (b) Button on a 3.3 V board, external pull-up 4.7 k Current when pressed: I = 3.3 / 4700 = 0.702 mA Rise time with 150 pF: 2.2 x 4700 x 150e-12 = 1.55 us Internal pull-up 50 k: I = 3.3 / 50000 = 66 uA rise time = 2.2 x 50000 x 150e-12 = 16.5 us The internal resistor takes a tenth of the current and is ten times slower; both are fast enough for a button on a short wire, and the external 4.7 k is the better choice for a long cable or a noisy place. (c) LSB of each board and a TMP36 reading Uno: LSB = 5 / 1024 = 4.883 mV Pico: LSB = 3.3 / 4096 = 0.806 mV Uno reading 300: V = 300 x 4.883 mV = 1464.8 mV = 1.465 V (adding half an LSB for the centre of the step: 1.467 V) TMP36: T = (1.465 - 0.5) x 100 = 96.5 degrees C (a hot reading: a hand-held soldering iron tip would do it) Pico reading for 1.65 V: code = floor(1.65 / 3.3 x 4096) = 2048 (d) Divider for a 3-cell LiPo with a Pico, R1 = 33 k, R2 = 10 k k = 10 / (33 + 10) = 0.2326 12.6 V: V(pin) = 12.6 x 0.2326 = 2.930 V code = floor(2.930 / 3.3 x 4096) = 3637 11.1 V: V(pin) = 11.1 x 0.2326 = 2.581 V code = floor(2.581 / 3.3 x 4096) = 3204 Source impedance seen by the ADC: 33 k || 10 k = 33 x 10 / 43 = 7.67 k (under 10 k: acceptable; add the 100 nF at the pin) One LSB at the battery: 0.806 mV / 0.2326 = 3.46 mV Divider current at 12.6 V: 12.6 / 43 k = 0.29 mA Expected readings (check by simulation or on the board) (a) 220 ohm: LED current 13.2 mA, 2.9 V across the resistor, 2.1 V across the LED (b) pressed: pin at 0 V, 0.70 mA drawn from the 3.3 V supply (c) Uno at 1.465 V reads 300; Pico at 1.65 V reads 2048 (d) pin 2.930 V at 12.6 V (meter), code 3637 (Pico read_u16 shows about 58 200 = 3637 x 16) Wiring checklist - Check the LED's polarity (long leg to the resistor side) and the resistor value with the colour code or a meter. - For (d), measure the divider output with a meter BEFORE connecting it to the Pico pin: it must be under 3.3 V at the full pack voltage. - Add 100 nF from the divider output to ground; use a fuse in the pack lead; connect the pack's negative to the Pico's GND. - Never put the full pack voltage on a Pico pin: it is not 5 V tolerant, let alone 12 V tolerant.