IoT and Smart Systems Engineer · Focus area 2: Talking to hardware

IO5: Pins, analog readings and driving things: GPIO, ADC and PWM

Goal: read switches and drive outputs with digital pins, take good analog readings, control brightness and speed with PWM, drive loads such as relays and motors safely, understand a sensor's accuracy and calibration, and protect people and electronics.

You'll need: IO1, IO2, IO4, Module 1 (How Computers Work), Module 22 (Loops and Algorithms), and the JalSetu practice kit (docs/lv20-datasheet.md, capstone/design-notes-template.md).

Where this is used: every device that senses or moves something: a doorbell button, a soil moisture probe, a fan speed control, a pump relay, a buzzer. Getting the electrical side right decides whether readings can be trusted and whether the device is safe.

How it differs: in software, a value is what you set it to. In electronics, a pin's voltage depends on wiring, noise, resistors and the parts connected to it, and a mistake can damage a board or hurt someone.

Time: reading about 3 hours · practice (the Try it tasks) about 3 hours · assignment about 4 hours.

Lessons

  1. 1Digital pins: inputs and outputsReading 15–25 minutes, plus practice✓
  2. 2Analog readings with the ADCReading 15–25 minutes, plus practice✓
  3. 3PWM and driving loadsReading 15–25 minutes, plus practice✓
  4. 4Sensors and how far to trust themReading 15–25 minutes, plus practice✓
  5. 5Protecting people and electronicsReading 15–25 minutes, plus practice✓

🛠️Assignment: JalSetu's hardware additions

  1. Design a float-switch input for overflow, with its pull-up and debouncing.
  2. Work out JalSetu's battery measurement through a voltage divider and a 12-bit ADC, with averaging.
  3. Plan a low-voltage pump control with a driver and a flyback diode (on paper only).
  4. Write the LV-20's range, resolution and timing, two sanity checks and a calibration method.
  5. Write a protection plan for the rooftop, marking what needs expert review.

Self-check before you finish:

✅Module check

Explain in a few sentences, in your own words:

How do you connect sensors and loads to a microcontroller safely and reliably?

Show a good answer

A good answer mentions: inputs with pull-ups or pull-downs, voltage levels and current limits respected; ADC readings converted with the reference and averaged, with dividers for larger voltages; PWM for brightness and speed, with drivers, shared grounds and flyback diodes for coils; sensor range, resolution, accuracy, calibration and sanity checks; and protection from water, heat, static, reversed power and battery hazards, with mains work left to qualified electricians and certification checked.

📚Official references

Official documentation and guidance for this module's topics, chosen on 2 November 2026. Tools, laws and standards change, so check the current version before you rely on a detail. If a link has moved, search the same site.

📝Module quiz

10 questions · 10 marks each · 100 marks in all · pass mark 70% · retry as often as you like ·

1. A button input reads randomly when nobody presses it. What's the most likely fix?

Why each option is right or wrong

  • “Use a longer wire to the button so the signal is stronger” (Incorrect): Longer wires pick up more noise.
  • “Read the input a hundred times faster so that the noise averages out” (Incorrect): Faster reading doesn't give the input a defined state.
  • “Connect the button to 5 V instead of 3.3 V, to make it stronger” (Incorrect): That can damage a 3.3 V chip.
  • “Turn on a pull-up resistor, so the input has a clear state when the button is open” is correct.

    A floating input reads randomly.

    • Then: the button pulls it low.

📖 Revise: Lesson 1: Digital pins: inputs and outputs

Question ID sp-iot-05-q1 · Report a mistake in this question
2. A 12-bit ADC with a 0 to 3.3 V range reads 2,048. Roughly what's the voltage?

Why each option is right or wrong

  • “2.048 V, since the reading is in millivolts” (Incorrect): The reading is a count, not millivolts.
  • “3.3 V, since any reading over 2,000 is the top of the range” (Incorrect): 4,095 is the top.
  • “12 V, because it's a 12-bit converter” (Incorrect): Bits set resolution, not voltage.
  • “About 1.65 V, half the range” is correct.

    2,048 ÷ 4,095 × 3.3 ≈ 1.65 V.

    • Then: apply the sensor's formula.

📖 Revise: Lesson 2: Analog readings with the ADC

Question ID sp-iot-05-q2 · Report a mistake in this question
3. A transistor switching a relay coil keeps failing after a few hours. What's the most likely missing part?

Why each option is right or wrong

  • “A larger resistor in series with the relay's contacts” (Incorrect): The contacts aren't what fails.
  • “A second relay in parallel, so the load is shared between them” (Incorrect): The spike would still hit the transistor.
  • “A faster microcontroller to switch the transistor more quickly” (Incorrect): Speed doesn't stop the spike.
  • “A flyback diode across the coil” is correct.

    It absorbs the spike when the coil switches off.

    • Or: a driver module with one built in.

📖 Revise: Lesson 3: PWM and driving loads

Question ID sp-iot-05-q3 · Report a mistake in this question
4. A sensor reports to 1 mm, but its datasheet says ±10 mm. How precise is a reading?

Why each option is right or wrong

  • “To 1 mm, since that's how many digits the sensor shows on each reading” (Incorrect): Resolution isn't accuracy.
  • “Exactly right, since digital sensors don't have errors” (Incorrect): All sensors have some error.
  • “To 0.1 mm, after averaging ten readings together on the device” (Incorrect): Averaging reduces noise, not a steady error.
  • “Within about ±10 mm: resolution is 1 mm, accuracy is ±10 mm” is correct.

    The datasheet's accuracy is what counts.

    • Improve: calibrate against a reference.

📖 Revise: Lesson 4: Sensors and how far to trust them

Question ID sp-iot-05-q4 · Report a mistake in this question
5. An LED dimmed with PWM flickers visibly. What should change?

Why each option is right or wrong

  • “Lower the duty cycle to zero so the LED is off most of the time” (Incorrect): It would then be off, not dimmed.
  • “Use a larger LED so the flicker is spread over a wider area” (Incorrect): Size doesn't remove flicker.
  • “Switch to an analog pin, since PWM doesn't work on LEDs” (Incorrect): PWM is the usual way to dim LEDs.
  • “Raise the PWM frequency” is correct.

    Hundreds of hertz or more avoid visible flicker.

    • Duty cycle: still sets the brightness.

📖 Revise: Lesson 3: PWM and driving loads

Question ID sp-iot-05-q5 · Report a mistake in this question
6. Battery readings drift up and down as the battery itself runs down, though the battery voltage is measured through a divider. Why?

Why each option is right or wrong

  • “The divider's resistors are getting hotter as the battery runs down” (Incorrect): Heat isn't the usual cause here.
  • “Batteries change voltage randomly, so readings can't be trusted” (Incorrect): Battery voltage changes predictably.
  • “The ADC can only read batteries when they're fully charged” (Incorrect): It reads any voltage in its range.
  • “The ADC's reference depends on the supply, which falls with the battery” is correct.

    A moving reference moves every reading.

    • Fix: a stable reference, or measure it.

📖 Revise: Lesson 2: Analog readings with the ADC

Question ID sp-iot-05-q6 · Report a mistake in this question
7. A rooftop sensor's enclosure is sealed, but water still gets inside in the monsoon. What's the most likely path?

Why each option is right or wrong

  • “Through the plastic itself, since plastic lets water through over time” (Incorrect): A rated enclosure doesn't let water through its walls.
  • “Through the battery, which collects water from the air around it” (Incorrect): Batteries don't draw in water.
  • “Through the sensor's radio waves, which carry moisture inside” (Incorrect): Radio doesn't carry water.
  • “Along the cables, through entries without glands or drip loops” is correct.

    Water runs down cables into the box.

    • Fix: cable glands and drip loops.

📖 Revise: Lesson 5: Protecting people and electronics

Question ID sp-iot-05-q7 · Report a mistake in this question
8. A learner wants their device to switch a 230 V water heater. What's the right advice?

Why each option is right or wrong

  • “Use a pin directly, since the heater only needs a signal to switch on” (Incorrect): A pin can't safely switch mains.
  • “Use any relay from an online shop and wrap the wires in tape” (Incorrect): That's unsafe.
  • “Use a transistor rated for 230 V and connect it to the heater's wires” (Incorrect): It's still mains work, which needs a qualified electrician.
  • “Mains wiring is for qualified electricians with certified parts; keep the project low-voltage” is correct.

    Mains voltage can kill.

    • Instead: a certified smart switch, installed properly.

📖 Revise: Lesson 3: PWM and driving loads

Question ID sp-iot-05-q8 · Report a mistake in this question
9. A sensor reads 20 mm more than a dipstick at two different levels. What's the simplest correction?

Why each option is right or wrong

  • “Replace the sensor, since any error means it's faulty” (Incorrect): A steady offset is normal and correctable.
  • “Send both the raw and dipstick values in every reading” (Incorrect): The dipstick isn't there every day.
  • “Average ten readings, which removes the 20 mm difference” (Incorrect): Averaging doesn't remove a steady error.
  • “Store an offset of −20 mm and apply it to each reading” is correct.

    A steady error at both levels is an offset.

    • Record: the calibration date.

📖 Revise: Lesson 4: Sensors and how far to trust them

Question ID sp-iot-05-q9 · Report a mistake in this question
10. Why use a level shifter between a 5 V sensor and a 3.3 V microcontroller?

Why each option is right or wrong

  • “So the sensor's readings are converted into millimetres before the chip sees them” (Incorrect): It changes voltage levels, not units.
  • “Because 3.3 V chips can only talk to other 3.3 V chips over Wi-Fi” (Incorrect): They connect directly with matching levels.
  • “To make the sensor faster, since lower voltages are slower” (Incorrect): Speed isn't the reason.
  • “To stop 5 V signals from damaging pins that aren't 5 V tolerant” is correct.

    It translates between the two levels.

    • Check: the chip's datasheet for tolerant pins.

📖 Revise: Lesson 1: Digital pins: inputs and outputs

Question ID sp-iot-05-q10 · Report a mistake in this question

ProLearners Assist