Four programs. All four are correct. Every one of them has been run on a board that does the wrong thing anyway.
Every debug challenge you have done so far had the bug in the code. These have the bug in the wiring — which is a different kind of hunt, because reading the program harder will never find it.
For each one, answer two questions: what is wrong on the board, and how would you confirm it without just rewiring everything and hoping.
Read the symptom carefully first. A part that does nothing at all is telling you something different from a part that does the wrong thing, and both are different again from a part that works but badly.
Symptom: the program runs. No error appears. The LED never lights at all, not even faintly. A different LED swapped into the same holes works fine.
from machine import Pinimport time
led = Pin(28, Pin.OUT)
while True: led.value(1) time.sleep(0.5) led.value(0) time.sleep(0.5)The last sentence of the symptom is the clue. If a fresh LED works in the same holes, the holes are fine and the pin is fine. So what is different about the first one?
Symptom: prints a mixture of 0s and 1s at random with nobody touching the button. Pressing it does make 1s more likely, but releasing it does not reliably give 0.
from machine import Pinimport time
button = Pin(33, Pin.IN)
while True: print(button.value()) time.sleep(0.2)This one is half a code problem and half a wiring problem, which is why it is here. Compare line 4 against how you created every button in Activity 2.7, then ask what is holding that pin at a known value when the button is open. Nothing is.
Symptom: all five LEDs light in turn, as intended. But they are dim, and each one gets dimmer as the next one joins. By the fifth they are barely visible.
from machine import Pinimport time
leds = [ Pin(28, Pin.OUT), Pin(29, Pin.OUT), Pin(30, Pin.OUT), Pin(31, Pin.OUT), Pin(32, Pin.OUT),]
for led in leds: led.value(1) time.sleep(0.4)Nothing is broken and nothing is backwards — the circuit works, it just works badly. The behavior changing as more LEDs come on is the whole clue. What could five LEDs be sharing that they should each have had one of?
Symptom: the program runs and finishes. No error. The motor does absolutely nothing — no movement, no sound, no twitch. The battery pack is fresh and the motor works when touched directly to it.
from machine import Pin, PWMimport time
a_in1 = Pin(31, Pin.OUT)a_in2 = Pin(32, Pin.OUT)a_pwm = PWM(Pin(33))a_pwm.freq(1000)
a_in1.value(1)a_in2.value(0)a_pwm.duty_u16(40000)time.sleep(2)a_pwm.duty_u16(0)The motor is fine and the battery is fine, so the fault is between them. There are two classic culprits on this driver, and both produce exactly this silence: one is a pin that has to be HIGH before the chip will listen to anything, and the other is a connection two separate parts of the circuit both need to share.
For each of the four, write down:
That last question is the point of the whole exercise. Getting used to asking is this even a code problem? will save you more time than any single fix.