Basic Library Examples
Example programs can help you quickly verify the use of modules. Simply copy and paste the code into a Python editor and modify the Board initialization model to the model of the board you are using.
Modules in common library examples are imported through the board library.
Example Program - Blink
Blink: Digital Output
This Python code controls the onboard LED of the Arduino UNO to blink once per second. The Arduino main control board is connected to a Windows or Linux computer.
# -*- coding: UTF-8 -*-
# Experiment effect: Control the onboard LED of the Arduino UNO to blink once per second
# Connection: Use a Windows or Linux computer to connect to an Arduino main control board
import time
from pinpong.board import Board, Pin
Board("uno").begin() # Initialization, select board type (uno, microbit, RPi, handpy) and port number. If no port number is entered, automatic detection will be performed
# Board("uno", "COM36").begin() # Initialization with specified port on Windows
# Board("uno", "/dev/ttyACM0").begin() # Initialization with specified port on Linux
# Board("uno", "/dev/cu.usbmodem14101").begin() # Initialization with specified port on Mac
led = Pin(Pin.D13, Pin.OUT) # Initialize the pin for digital output
while True:
# led.value(1) # Output high level Method 1
led.write_digital(1) # Output high level Method 2
print("1") # Print information in the terminal
time.sleep(1) # Wait for 1 second to maintain the state
# led.value(0) # Output low level Method 1
led.write_digital(0) # Output low level Method 2
print("0") # Print information in the terminal
time.sleep(1) # Wait for 1 second to maintain the state
Example Program - Button
Button: Digital Input
This Python code uses a button to control the on and off state of the onboard LED of an Arduino UNO. The Arduino main control board is connected to a Windows or Linux computer, and the button module is connected to the D8 pin of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: Use a button to control the Arduino UNO onboard light on and off
# Connection: Use a Windows or Linux computer to connect to an Arduino main control board, the main control board D8 is connected to a button module
import time
from pinpong.board import Board, Pin
Board("uno").begin() # Initialization, select board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic detection will be performed
# Board("uno", "COM36").begin() # Initialization with specified port on Windows
# Board("uno", "/dev/ttyACM0").begin() # Initialization with specified port on Linux
# Board("uno", "/dev/cu.usbmodem14101").begin() # Initialization with specified port on Mac
btn = Pin(Pin.D8, Pin.IN) # Initialize the pin for digital input
led = Pin(Pin.D13, Pin.OUT)
while True:
# v = btn.value() # Method 1 to read pin level
v = btn.read_digital() # Method 2 to read pin level
print(v) # Print the read level status in the terminal
# led.value(v) # Set the button status to the LED pin Output level Method 1
led.write_digital(v) # Set the button status to the LED pin Output level Method 2
time.sleep(0.1)
Example Program - ADC
ADC: Analog Input
This Python code prints the analog value of the A0 port of the Arduino UNO board. The Arduino main control board is connected to a Windows or Linux computer, and a knob module is connected to the A0 port of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: Print the analog value of the UNO board A0 port
# Connection: Use a Windows or Linux computer to connect to an Arduino main control board, the main control board A0 is connected to a knob module
import time
from pinpong.board import Board, Pin
Board("uno").begin() # Initialization, select board type (uno, microbit, RPi, handpy) and port number. If no port number is entered, automatic detection will be performed
# Board("uno", "COM36").begin() # Initialization with specified port on Windows
# Board("uno", "/dev/ttyACM0").begin() # Initialization with specified port on Linux
# Board("uno", "/dev/cu.usbmodem14101").begin() # Initialization with specified port on Mac
# adc0 = ADC(Pin(Pin.A0)) # Pass Pin into ADC to realize analog input Analog input method 1
adc0 = Pin(Pin.A0, Pin.ANALOG) # Initialize the pin for analog input Analog input method 2
while True:
# v = adc0.read() # Read the analog signal value of A0 port Analog input method 1
v = adc0.read_analog() # Read the analog signal value of A0 port Analog input method 2
print("A0=", v)
time.sleep(0.5)
Example Program - PWM
PWM: Analog Output
This Python code performs a PWM (Pulse Width Modulation) output experiment to control the brightness of an LED light. The Arduino main control board is connected to a Windows or Linux computer, and the LED light is connected to the D6 pin of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: PWM output experiment, control the brightness change of the LED light
# Connection: Use a Windows or Linux computer to connect to an Arduino main board, the LED light is connected to the D6 pin
import time
from pinpong.board import Board, Pin
Board("uno").begin() # Initialization, select board type (uno, microbit, RPi, handpy) and port number. If no port number is entered, automatic detection will be performed
# Board("uno", "COM36").begin() # Initialization with specified port on Windows
# Board("uno", "/dev/ttyACM0").begin() # Initialization with specified port on Linux
# Board("uno", "/dev/cu.usbmodem14101").begin() # Initialization with specified port on Mac
# pwm0 = PWM(Pin(board, Pin.D6)) # Pass the pin into PWM for initialization Analog output method 1
pwm0 = Pin(Pin.D6, Pin.PWM) # Initialize the pin as PWM mode Analog output method 2
while True:
for i in range(255):
print(i)
# pwm0.duty(i) # PWM output Method 1
pwm0.write_analog(i) # PWM output Method 2
time.sleep(0.05)
Example Program - IRQ
IRQ: Pin Interrupt
This Python code demonstrates the pin interrupt function. The Arduino main control board is connected to a Windows or Linux computer, and a button module is connected to the D8 pin of the main control board.
# -*- coding: UTF-8 -*-
# Experiment effect: Pin interrupt function test
# Connection: Use a Windows or Linux computer to connect to an Arduino main control board, the main control board D8 is connected to a button module
import time
from pinpong.board import Board, Pin
Board("uno").begin() # Initialization, select board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic detection will be performed
# Board("uno", "COM36").begin() # Initialization with specified port on Windows
# Board("uno", "/dev/ttyACM0").begin() # Initialization with specified port on Linux
# Board("uno", "/dev/cu.usbmodem14101").begin() # Initialization with specified port on Mac
btn = Pin(Pin.D8, Pin.IN)
def btn_rising_handler(pin): # Interrupt event callback function
print("\n--rising---")
print("pin = ", pin)
def btn_falling_handler(pin): # Interrupt event callback function
print("\n--falling---")
print("pin = ", pin)
def btn_both_handler(pin): # Interrupt event callback function
print("\n--both---")
print("pin = ", pin)
btn.irq(trigger=Pin.IRQ_FALLING, handler=btn_falling_handler) # Set the interrupt mode to trigger on falling edge
# btn.irq(trigger=Pin.IRQ_RISING, handler=btn_rising_handler) # Set the interrupt mode to trigger on rising edge, and callback function
# btn.irq(trigger=Pin.IRQ_RISING+Pin.IRQ_FALLING, handler=btn_both_handler) # Set the interrupt mode to trigger when the level changes
while True:
time.sleep(1) # Keep the program running continuously
Was this article helpful?
