GLOBAL KNOWLEDGE ARK

Electronics · Optional embedded electronics and a sensor logger

Optional first microcontroller program

A microcontroller gives a circuit a stored rule it can repeat. Begin with four supplied numbers and one indicator command. The whole activity can be completed on paper; an already configured board is an optional way to compare the prediction with an observation.

Lesson preparation and course links

This is optional local preparation for the embedded branch. It introduces integer values, millivolt units, conditions, loops and GPIO requests. You can complete the supplied trace on paper; no purchase, installation, programming run or equipment is required.

Back to the Electronics chapter

After reading, you can

Identify the processor, working memory, stored program, clock, peripheral and pin; trace integer values, a condition and repeated work in a complete small program. Separate a computed command from an observed physical response and requested delay from measured time.

01

Give each part of the system a different job

A processor executes instructions. Working RAM holds changing values while the program runs. Nonvolatile program storage retains code without normal operating power. The clock provides timing references, while peripherals implement interfaces such as digital pins, timers and serial communication. These roles cooperate; they are not interchangeable.

A GPIO peripheral lets software request a digital pin state. The pin’s output buffer and the board circuit must then produce the physical response. Assigning a variable does not by itself configure a pin, connect the correct circuit or prove that light was emitted.

The functional diagram is not a wiring schematic. For the optional profile here, the board is the original non-W Raspberry Pi Pico with RP2040, whose onboard LED uses GPIO25. GPIO25 is a logical GPIO number, not an instruction to connect a wire to header pin 25. Other Pico-family boards can have different LED wiring.

Functional roles in a first microcontroller taskStored program and working RAM support the processor; clock supplies timing references. Processor requests pass through GPIO to the onboard LED. This is a functional map, not wiring. The selected original non-W Pico uses GPIO25 for that LED; other boards can differ.A functional map: software values and physical response are distinctStoredprogramProcessorGPIO peripheraland output pinOnboardLEDWorking RAMClockTiming referenceRequestBoard pathOptional profile: original non-W RP2040 Pico • onboard LED is GPIO25Functional roles only; this figure is not a circuit or connector wiring plan.
Stored program and working RAM support processor execution. A clock supplies timing references. The processor issues requests through a GPIO peripheral/output pin to the onboard indicator. These are functional roles, not a wiring schematic. The optional original non-W RP2040 Pico maps its onboard LED to GPIO25; a software assignment and emitted light remain different evidence.
02

Use values, units and an explicit equality rule

An integer is a whole-number value. threshold_mv=1000 names the comparison threshold in millivolts: 1000 mV=1.000 V. readings_mv=(900,1000,1100,950) is a fixed sequence supplied in the program. It is not a set of sensor measurements and this program never reads an ADC.

The suffix _mv reminds the reader of units; Python does not enforce that unit. Comparing 900 mV with a threshold written as 1.0 V without conversion would compare 900 with 1.0 and give the wrong intended decision.

The condition sample_mv >= threshold_mv means “at least the threshold.” Its equality rule makes 1000 mV ON in this task. If it is true, assign command=1; otherwise assign command=0. The equality policy belongs to this specification; it must not be borrowed from another project with a different threshold rule.

The for loop repeats the indented body for each supplied value. enumerate also supplies a sample number starting at 0. Indentation groups the statements belonging to the loop and condition; preserve it when reading or copying the complete program.

Define software values before tracing the program
NameMeaningUnits or values
threshold_mvComparison threshold1000 mV
readings_mvFour supplied input values900, 1000, 1100, 950 mV
sample_numberPosition in the supplied sequence0, 1, 2, 3
commandRequested LED logic value0=OFF, 1=ON for the stated board profile
03

Read the complete finite program

The first two lines import the documented Pin and sleep_ms facilities. Pin(25,Pin.OUT,value=0) selects GPIO25 as an output with initial requested value 0. Within each iteration, the condition computes command, led.value(command) issues the GPIO request, print records the three software values, and sleep_ms(250) requests a delay of at least 250 ms.

The try/finally structure requests LED OFF when control leaves the try block through ordinary completion or an ordinary Python exception/interrupt path that executes finally. It does not guarantee a pin state through power loss, reset, a stalled interpreter or a failed output path.

This is the complete original finite teaching program. Its syntax has been parsed and its supplied-value decisions checked; it has not been executed on hardware or in a MicroPython runtime here. All imports are included, and the stated profile needs no external sensor or extra library.

Complete original MicroPython program; optional original non-W Pico profile

from machine import Pin
from time import sleep_ms

threshold_mv = 1000
readings_mv = (900, 1000, 1100, 950)
led = Pin(25, Pin.OUT, value=0)

try:
    for sample_number, sample_mv in enumerate(readings_mv):
        if sample_mv >= threshold_mv:
            command = 1
        else:
            command = 0
        led.value(command)
        print(sample_number, sample_mv, command)
        sleep_ms(250)
finally:
    led.value(0)
04

Predict the commands before looking at an LED

For each supplied value, apply the comparison and record the command. The expected printed triples are 0 900 0; 1 1000 1; 2 1100 1; 3 950 0. Those are predicted software values, not a captured console log.

The ideal requested-delay timeline begins at the first iteration’s GPIO request and ignores computation, printing and other overhead. It places iteration starts at 0, 250, 500, 750 ms and the end of the four requested delays at 1000 ms. These times are a paper model, not physical timestamps.

At the second and third iterations, command remains 1. There is no intervening OFF request, so the ideal command trace has one continuous high interval of 500 ms, not two separate flashes. Initial/final GPIO requests are 0. The program makes no assertion about the pin before its initialization line.

Supplied-input prediction; nominal starts exclude all execution overhead
SampleSupplied mVComparison with 1000CommandNominal start msRequested delay ms
0900false00250
11000true1250250
21100true1500250
3950false0750250
05

Separate requested delay, measured time and repetition rate

Four 250 ms delay requests sum to 1000 ms. Interpreter work, print/USB delays, interrupts and scheduling can extend the observed duration. sleep_ms specifies a minimum delay; it is not a precision waveform generator or proof of an exact 1000 ms end time.

The processor clock rate does not directly give the loop rate. Instructions, interpreter execution and peripheral activity contribute different delays. A later timer-peripheral investigation will give a hardware counter its own explicitly defined clock and compare its update event with software response.

As a separate reading comparison, the vendor SDK’s periodic LED example requests 2.5 timer callbacks per second and toggles the LED on each callback. Nominal toggle interval=1/2.5=0.4 s. A complete ON/OFF cycle requires two toggles: 0.8 s, or 1.25 complete cycles per second. The callback rate is not the full blink-cycle frequency; actual callback timing can vary.

This finite four-input program uses no timer callback and should not be confused with that vendor example. Neither a requested delay nor a timer API setting is a measured timing record.

06

Use the optional board profile only if it is already available

The optional runnable profile is an original non-W RP2040 Raspberry Pi Pico already running board-matched MicroPython v1.26.0; Thonny with its MicroPython (Raspberry Pi Pico) backend; and an already working USB data/serial connection. Use only the onboard LED. Purchases, firmware installation and external wiring are not part of this lesson.

If that setup is already available, use a separate temporary test file in Thonny and its normal Run action, preserving existing board files and automatic startup. First predict the four printed triples, then compare any actual console and LED observations with that prediction. Record the exact board/runtime and any differences. No board operation is performed by this lesson’s supplied evidence.

If the setup is unavailable, complete the same trace and fault-reasoning exercises on paper. That route establishes the intended software decisions and their limits. It does not produce a claim that a device ran the program.

The selected Pin25 mapping is specific to the original non-W Pico. For a W-series or another board, consult that exact board’s LED and runtime documentation before using a program. A familiar board shape or a successful variable assignment does not establish the pin mapping.

07

Change one rule and explain the evidence you would need

As a supplied logic fault, replace >= with > while keeping all four numbers unchanged. Equality becomes OFF, so the expected commands become 0, 0, 1, 0. The ideal requested-high interval shrinks to 250 ms. This identifies a change in the rule; no failed hardware is needed to derive it.

If printed commands are correct but no light is observed, that does not uniquely locate the fault. Check whether the stated board/runtime and onboard-LED mapping are actually being used, whether the GPIO initialization/request path completed, and whether power and the physical LED path are correct. Separate the recorded software evidence from any untested explanation.

A successful LED indication checks only the named path under the observed conditions. It does not qualify a sensor acquisition system, precise timing, a high-current load, reset defaults or all future embedded functions. Keep the claim as small as the evidence.

Trace equality and the continuous ON interval

Start with threshold 1000 mV. The first supplied 900 is below it, so command 0 is requested for the first 250 ms delay. The second 1000 equals the threshold; >= is true, so command 1 is requested.

The third 1100 is also above the threshold, so the next request remains 1. The two adjacent 250 ms high requests join into one nominal 500 ms high interval. The last 950 requests 0, and finally requests 0 again after ordinary completion.

There are four requested delays, totalling 1000 ms in the overhead-free model. Predicted console triples describe software state. Actual elapsed time and emitted light require separate observations under the named profile; neither follows just from assigning command.

Try the reasoning

  1. What do processor, RAM, stored program, clock and GPIO each contribute?

    Show the answer and reasoning

    The processor executes instructions; RAM holds working state; nonvolatile storage retains code; clocks supply timing references; GPIO implements the requested pin interface. A correct software value still needs the right peripheral, pin mode and physical board path.

  2. Are 900, 1000, 1100, 950 measured ADC values in this program?

    Show the answer and reasoning

    No. They are a fixed tuple of supplied millivolt numbers. The code imports no ADC or sensor driver and performs no sensor acquisition. Treating those values as measurements would invent evidence.

  3. What commands follow the four supplied values with threshold 1000 mV and >=?

    Show the answer and reasoning

    0, 1, 1, 0. Equality is ON because the condition is “at least 1000.” The second and third high commands are adjacent, so the ideal command trace has one continuous high interval.

  4. How much delay and high time does the model request?

    Show the answer and reasoning

    Four 250 ms delays sum to 1000 ms. Two adjacent ON iterations request 500 ms high in the ideal trace. Execution/printing/interrupt time can extend physical timing, so this is not an exact measured duration.

  5. What happens if the condition is changed to > with everything else fixed?

    Show the answer and reasoning

    The 1000 mV equality case becomes OFF. Commands become 0, 0, 1, 0 and the ideal requested-high interval is 250 ms. This is a changed comparison rule, not proof of a hardware fault.

  6. Does command=1 prove that the LED emitted light?

    Show the answer and reasoning

    No. It assigns a software value; led.value(command) requests a GPIO state. Correct board mapping, firmware, mode, power and the physical LED path still matter. Observed light is separate evidence.

  7. What does finally establish, and what does it leave unproved?

    Show the answer and reasoning

    On ordinary completion or an ordinary exception/interrupt exit that executes finally, it requests OFF. It does not guarantee output state during reset, power loss, an interpreter stall or output-path failure.

  8. A separate vendor example toggles at 2.5 callbacks/s. What is the full ON/OFF cycle?

    Show the answer and reasoning

    Nominal callback interval=0.4 s; two toggles give one full 0.8 s cycle, so full-cycle frequency=1.25 Hz, subject to actual callback timing. 2.5 Hz is the toggle-event rate.

  9. Can this activity be completed without equipment? Can the Pin25 program be assumed unchanged on Pico W?

    Show the answer and reasoning

    The full prediction and fault reasoning can be completed on paper. The Pin25 fixture selects the original non-W RP2040 Pico; W-series LED wiring differs and needs its exact documentation. Paper completion is not a hardware execution claim.

What to carry forward

Begin programming with an explicit value-and-unit contract, a complete small program and a predicted trace. Keep computed values, GPIO requests, physical observations and timing measurements distinct. The optional equipment route extends the evidence; it does not define whether you can learn the reasoning.

References

Sources supporting this lesson’s explanations and examples.

  1. Raspberry Pi Ltd2 sources
    1. Raspberry Pi Pico-series Python SDK: A MicroPython environment for Raspberry Pi microcontrollersRaspberry Pi LtdTechnical documentation · PDFdatasheets.raspberrypi.comRead source
    2. RP2040 Datasheet: A microcontroller by Raspberry PiRaspberry Pi LtdTechnical documentation · PDFdatasheets.raspberrypi.comRead source
  2. MicroPython project, runtime maintainer2 sources
    1. Quick reference for the RP2 — MicroPython v1.26.0 documentationMicroPython project, runtime maintainerTechnical documentationdocs.micropython.orgRead source
    2. time – time related functions — MicroPython v1.26.0 documentationMicroPython project, runtime maintainerTechnical documentationdocs.micropython.orgRead source