Base Input

๐Ÿ”˜ Button

๐Ÿ“Œ Description

A button switch is a simple part that turns something on or off when you press it. Itโ€™s like a light switch, but smaller and made for electronic projects.

๐ŸŽฏ Use

In our projects, buttons are used to:

  • Start or stop actions.
  • Control lights, sounds, or motors.
  • Let the robot react when someone presses it.

๐Ÿ‘‰ Think of the button as the robotโ€™s hands โ€” when you press it, the robot feels it and responds.

Module parameters

Pin nameDescription
GGND (Power Input Negative)
VVCC (Power Input Cathode)
SThe signal is digitally output, low when pressed and high when released
  • Supply voltage: 3.3V/5V

  • Connection: PH2.0 terminal wire

  • Installation method: double screw fixing


๐Ÿ–ฅ๏ธ Code Explanation

import machine
import time

pin26 = machine.Pin(26, machine.Pin.OUT)   # LED connected to pin 26
pin17 = machine.Pin(17, machine.Pin.IN)    # Button connected to pin 17

while True:
    if pin17.value() == 0:   # If button is pressed
        pin26.value(1)       # Turn LED ON
        time.sleep(1)        # Keep it on for 1 second
    else:
        pin26.value(0)       # Turn LED OFF

Step by Step

  1. import machine & import time โ†’ Tools to control pins and add pauses.
  2. pin26 = machine.Pin(26, machine.Pin.OUT) โ†’ Pin 26 controls the LED (output).
  3. pin17 = machine.Pin(17, machine.Pin.IN) โ†’ Pin 17 reads the button (input).
  4. while True: โ†’ Loop that repeats forever.
  5. if pin17.value() == 0: โ†’ Checks if the button is pressed.
  6. pin26.value(1) โ†’ Turns the LED on.
  7. time.sleep(1) โ†’ Keeps the LED on for 1 second.
  8. else: โ†’ If the button is not pressedโ€ฆ
  9. pin26.value(0) โ†’ Turns the LED off.

๐Ÿ‘‰ Together, this makes the LED light up only when the button is pressed.


โœ… Conclusion of the Test

  • If pressing the button makes the LED turn on for 1 second, the program is working correctly.
  • This test shows how the board can read input (button press) and control output (LED).
  • Later, we can use buttons to trigger more complex actions, like starting motors or changing modes.
On this page