Find a course

Module 5: Beyond the BasicsLesson 10 of 1012 min

Switches, Sensors and Microcontrollers

Wire buttons with pull-up resistors, read sensors, control LED brightness with PWM and plan, build and troubleshoot your first microcontroller project.

Step 1 of 12Learn

The button that pressed itself

Alex wires a push button to a microcontroller input and writes a program that turns on an LED when the button is pressed. It works, sort of. The LED also flickers on randomly when nobody touches anything, and sometimes when Alex just waves a hand near the wires.

The problem is a floating input. When the button isn't pressed, the input pin isn't connected to anything. Microcontroller inputs are extremely sensitive, so an unconnected pin picks up tiny stray voltages from the air, nearby wires and even your body, and reads them as random HIGHs and LOWs.

The fix is a pull-up resistor (or pull-down resistor):

  • A pull-up connects the input to the supply voltage through a resistor, often 10 kΩ. When the button is open, the pin reads HIGH. The button connects the pin to ground when pressed, so it reads LOW. Pressed = LOW may feel backward, but it's extremely common.
  • A pull-down connects the input to ground through a resistor, so it reads LOW until the button connects it to the supply.

The resistor is large enough that only a tiny current flows when the button is pressed (5 V ÷ 10 kΩ = 0.5 mA) but small enough to hold the pin firmly at a known level. Many microcontrollers have built-in pull-up resistors you can switch on in code, so you can connect a button straight between the pin and ground.

Step 2 of 12Practice · True or false

With a pull-up resistor and a button connected to ground, the input reads LOW when the button is pressed.

Step 3 of 12Diagram

A button with a pull-up resistor

The pull-up keeps the input at a known level when the button is open.

A button with a pull-up resistorProcess diagram: Supply (5 V or 3.3 V) → Pull-up resistor (about 10 kΩ) → Input pin → Push button → Ground (0 V).Supply (5 V or 3.3 V)Pull-up resistor(about 10 kΩ)Input pinPush buttonGround (0 V)

Tap a shape with to see more.

Supply (5 V or 3.3 V)
Pull-up resistor (about 10 kΩ)

Gently holds the input HIGH while nothing else is connected.

Input pin

Reads HIGH when the button is open, LOW when it is pressed.

Push button

When pressed, it connects the input straight to ground.

Ground (0 V)

Step 4 of 12Learn

Switch types and bounce

Switches come in many forms:

  • Push buttons (momentary) are closed only while pressed.
  • Toggle and slide switches stay in the position you set: good for power switches.
  • Limit switches are pressed by a moving part, such as a robot arm reaching the end of its travel.
  • Reed switches close when a magnet comes near, often used to detect open doors.
  • Tilt switches close when tipped.

Switches are described by poles and throws. A single-pole single-throw (SPST) switch simply opens or closes one connection. A single-pole double-throw (SPDT) switch connects one common terminal to either of two others, useful for choosing between two circuits.

Mechanical switches have a hidden quirk: bounce. When the metal contacts close, they physically bounce for a few milliseconds, making and breaking contact several times. A fast microcontroller can read one press as five or ten presses, so a counter jumps by several instead of one.

Debouncing fixes it. In software, the program waits a short time, often 10 to 50 ms, after a change and checks again before accepting it. In hardware, a small capacitor and resistor smooth out the bounces, using the same RC timing you met in the previous lesson.

Step 5 of 12Practice · What would you do?

What would you do?

Your project counts button presses on a display. Each single press makes the count jump by 3 or 4. The wiring includes a pull-up resistor. What's the most likely cause and fix?

Step 6 of 12Learn

Microcontrollers: electronics that think

A microcontroller board connects your circuits to code. Its pins can act as:

  • Digital outputs: set HIGH (the board's supply voltage, often 5 V or 3.3 V) or LOW (0 V) to turn on an LED (with its resistor) or signal a transistor.
  • Digital inputs: read whether a button or switch is HIGH or LOW.
  • Analog inputs: read a voltage through the built-in ADC, from sensors like potentiometers, photoresistors in a voltage divider or temperature sensors.
  • PWM outputs: switch rapidly on and off to create an average voltage between 0 and the supply.

PWM (pulse-width modulation) is how a digital pin dims an LED or controls motor speed. The pin switches on and off hundreds or thousands of times per second, too fast for your eye to see. The duty cycle is the percentage of time it's on:

average voltage = duty cycle × supply voltage

At 25% duty cycle on a 5 V pin, the average is 1.25 V, and an LED looks dim. At 100%, it's fully on.

Mind the limits: check whether your board uses 5 V or 3.3 V logic and never feed an input more than its supply voltage. Keep each pin's current low, often under 20 mA, and use transistors for anything bigger. All grounds in a project must be connected together, or signals have no common reference.

Step 7 of 12Diagram

What a microcontroller pin can do

What a microcontroller pin can doHierarchy diagram: Pin, Inputs, Outputs, Digital input, Analog input, Digital output, PWM output.PinInputsOutputsDigital inputAnalog inputDigital outputPWM output

Tap a shape with to see more.

Pin
Inputs
Outputs
Digital input

HIGH or LOW: buttons, switches, motion sensors.

Analog input

A number, such as 0–1023: light sensors, potentiometers.

Digital output

On or off: LEDs, buzzers, transistor switches.

PWM output

Fast on/off pulses: dimming LEDs, motor speed.

Step 8 of 12Practice · Work it out

A 3.3 V microcontroller pin outputs PWM at a 40% duty cycle. What is the average output voltage?

Step 9 of 12Explore

PWM: duty cycle sets the average voltage

The horizontal axis is the duty cycle (percent of the time the pin is HIGH). Slide between a 3.3 V and a 5 V board.

012345 020406080100
Average at 25%
1.25 V
Average at 75%
3.75 V

Step 10 of 12Tap to reveal

Your first project plan: a night light that turns on in the dark

  1. 1. Define it

    Goal: an LED turns on automatically when the room gets dark and off when it gets light. Power: USB 5 V.

  2. 2. Choose parts

    A microcontroller board, a photoresistor and a 10 kΩ resistor as a voltage divider, an LED with a 220 Ω resistor, jumper wires and a breadboard.

  3. 3. Build in stages

    First blink the LED from code. Then wire the divider and print the analog readings in bright and dark conditions. Only then combine them.

  4. 4. Calibrate

    Record readings in a lit room and in darkness, and set the threshold halfway between, with a small gap (hysteresis) so the LED doesn't flicker at dusk.

  5. 5. Improve

    Use PWM to fade the LED in and out, add a button to override it or swap in a transistor to drive a brighter LED strip.

Step 11 of 12Note

Step 12 of 12Practice · Choose one

A night-light project reads random values from its button input when the button isn't pressed. Which fix addresses the cause?