Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

Click the above “Mushroom Cloud Creation” to follow us!

1. Overview

A button switch, also known as a push button switch, was previously called a sensitive switch and is widely used in lights, socket switches, doorbells, and car central consoles. The introduction of button switches adds a layer of protection for electrical safety, making it easier to control electrical appliances while further protecting components.

During the learning process of Arduino, we will encounter various input devices, among which the button switch is the simplest and most widely used. Here, we will use Arduino to control an LED light, achieving the effect of turning it on when the button is pressed and off when pressed again.

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

2. Project Implementation

(1) Using a Button to Light Up a Small Lamp

Hardware Preparation:

Controller: Arduino UNO, IO Sensor Expansion Board V7.1

Modules: LED Light Module, Button Module

Connection Cable: Type A to B USB Cable

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

Program Writing:

1. Find button.py digital input in the example program and open it with IDLE.

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

2. Press F5 to run the program and check the effect. When the button is pressed, the onboard LED light will turn on (and print 1), and it will turn off (print 0) when released.

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

(2) Button Switch Light

In the previous step’s functionality, we can achieve “press the button – LED on”, “release the button – LED off”, but the actual switch is “first press to turn on, press again to turn off”. We will implement this functionality in this step.

Hardware Preparation:

Controller: Arduino UNO, IO Sensor Expansion Board V7.1

Modules: LED Light Module, Button Module

Connection Cable: Type A to B USB Cable

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

Program Writing

import time

from pinpong.board import Board,Pin

board = Board(“uno”).begin() # Initialize, select board type and port number, if the port number is not entered, it will automatically identify

#board = Board(“uno”,”COM36″).begin() # Windows specified port initialization

#board = Board(“uno”,”/dev/ttyACM0″).begin() # Linux specified port initialization

#board = Board(“uno”,”/dev/cu.usbmodem14101″).begin() # Mac specified port initialization

btn = Pin(Pin.D8, Pin.IN) # Initialize pin as digital input

led = Pin(Pin.D13, Pin.OUT)

i=0 # Set variable i=0

while True:

v = btn.read_digital() # Read pin level

#print(v) # Print the read level state in the terminal

if (v == 1):

if (i == 1):

i=0

led.write_digital(0) # Set the button state to the led pin

print(“LED off”)

else:

i=1

led.write_digital(1) # Set the button state to the led pin

print(“LED on”)

time.sleep(0.5) # Add delay to eliminate bounce

Run the code, pressing the button can toggle the LED light on and off.

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

3. Code Analysis

1. Import necessary packages and initialize settings.

import time

from pinpong.board import Board,Pin

board = Board(“uno”).begin() # Initialize, select board type and port number, if the port number is not entered, it will automatically identify

#board = Board(“uno”,”COM36″).begin() # Windows specified port initialization

#board = Board(“uno”,”/dev/ttyACM0″).begin() # Linux specified port initialization

#board = Board(“uno”,”/dev/cu.usbmodem14101″).begin() # Mac specified port initialization

btn = Pin(Pin.D8, Pin.IN) # Initialize pin as digital input

led = Pin(Pin.D13, Pin.OUT)# Initialize pin as digital output

2. We need a button to get two different results, so we need to set an intermediate value to implement the switching state. Therefore, we first set a variable i=0.

i=0 # Set variable i=0

3. Next, add a judgment to determine the button press state. For convenience, define variable v as the button state.

while True:

v = btn.read_digital() # Read pin level

#print(v) # Print the read level state in the terminal

if (v == 1):

4. Then add a judgment, using i to distinguish the state. As shown in the logic diagram, each time the button is pressed, the value of i will toggle between 0 and 1, and based on the change of i’s value, we can determine the LED light’s on and off state according to the logic to complete the code.

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

if (i == 1):

i=0

led.write_digital(0) # Set the button state to the led pin

print(“LED off”)

else:

i=1

led.write_digital(1) # Set the button state to the led pin

print(“LED on”)

time.sleep(0.5)

What is button bounce?

We imagine the switch circuit as “press the button – immediately conduct” “press again – immediately disconnect”, but this is not the case.

Buttons usually use mechanical elastic switches, and during the moment when the mechanical contacts open and close (usually around 10ms), due to elastic action, a series of bounces occur, causing the button switch to not immediately stabilize the circuit when closed, nor to completely disconnect instantly when opened.

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

So how do we eliminate button bounce?

Common methods to eliminate bounce are divided into two types: software methods and hardware methods. Here, we will focus on the simple software method.

We already know that the bounce time caused by elastic inertia is around 10ms. By delaying the command execution time with a delay command, we can achieve the effect of eliminating bounce.

Therefore, we added a 0.5-second delay in the code to achieve the button debounce function.

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial
——————— End of Text ———————

Controlling Hardware with Pinpong Library: Arduino UNO Button and LED Tutorial

Recommended Reading:
02【Pinpong Library Control Hardware】 Arduino UNO – LED Blinking – 1
01【Pingpong Library Control Hardware】 Virtual Valley – 1
Mind+: A domestic programming software for teenagers developed over 8 years, supporting AI and IoT features
00s college students successfully launched a homemade rocket! Netizens collectively worship…
What you dare to imagine but not dare to do, let AI help you realize!
What is the difference between course design and teaching design?

Leave a Comment

Your email address will not be published. Required fields are marked *