Skip to content
RoboLearn
Not completed

Buttons, switches and bump sensors

SensorsBeginner25 minTry a button in the simulator ↓Before this: What is a sensor?

What you’ll learn

  • Why a floating input reads random values
  • How pull-up and pull-down resistors fix it
  • How to debounce a button
  • How robots use switches as bump and limit sensors

The floating input problem

An input pin connected to nothing “floats”: it picks up electrical noise and flickers between HIGH and LOW. A button on its own only connects the pin to something while pressed, so it needs a resistor to hold the pin steady the rest of the time.

Pull-up and pull-down

Wiring Not pressed Pressed
Pull-up: resistor from pin to 5 V, button from pin to GND HIGH LOW
Pull-down: resistor from pin to GND, button from pin to 5 V LOW HIGH

The Arduino has built-in pull-up resistors. pinMode(pin, INPUT_PULLUP) turns one on, so you only need the button between the pin and GND.

Bounce

Inside a button, metal contacts bounce for a few milliseconds when pressed, so one press can read as several. Fix it by ignoring changes for about 20–50 ms after the first one (debouncing).

Switches on robots

  • Bump switches (microswitches with a lever) on the front bumper tell the robot it has hit something.
  • Limit switches stop a motor at the end of its travel, like an arm reaching its top position.
  • Microswitches have three terminals: COM (common), NO (normally open: connects when pressed) and NC (normally closed: disconnects when pressed). For a bump sensor, wire COM to GND and NO to the input with INPUT_PULLUP.

Finished this lesson?