Common Library Examples

This document provides example programs for commonly used modules in the PinPong library, demonstrating how to control devices such as servo motors, buzzers, ultrasonic sensors, temperature and humidity sensors, and WS2812 LEDs using Python.

Modules in common library examples are imported through the board library.

Example Program - Servo

Servo: Servo Motor

This Python code demonstrates how to control a servo motor. The Arduino main control board is connected to a Windows or Linux computer, and a servo motor is connected to the D4 pin of the main control board.

# -*- coding: UTF-8 -*-
# Experiment effect: Servo control
# Connection: Connect an Arduino main control board to a Windows or Linux computer, and connect a servo to D4
import time
from pinpong.board import Board,Pin,Servo # Import Servo library

Board("uno").begin()  # Initialization, select the board type (uno, microbit, RPi, handpy) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization by specifying the port under Mac

s1 = Servo(Pin(Pin.D4)) # Initialize the servo pin by passing Pin into Servo

while True:
    #s1.angle(0) # Control the servo to turn to the 0 degree position Method 1
    s1.write_angle(0)  # Control the servo to turn to the 0 degree position Method 2
    print("0")
    time.sleep(1)

    #s1.angle(90) # Control the servo to turn to the 90 degree position
    s1.write_angle(90)  # Control the servo to turn to the 90 degree position Method 2
    print("90")
    time.sleep(1)

    #s1.angle(180) # Control the servo to turn to the 180 degree position
    s1.write_angle(180)  # Control the servo to turn to the 180 degree position Method 2
    print("180")
    time.sleep(1)

    #s1.angle(90) # Control the servo to turn to the 90 degree position
    s1.write_angle(90)  # Control the servo to turn to the 90 degree position Method 2
    print("90")
    time.sleep(1)

Example Program - Tone

Tone: Buzzer

This Python code demonstrates how to control a buzzer to make a sound. The Arduino main control board is connected to a Windows or Linux computer, and a buzzer module is connected to the D8 pin of the main control board.

# -*- coding: UTF-8 -*-
# Experiment effect: Control the buzzer to make a sound
# Connection: Connect an Arduino main control board to a Windows or Linux computer, and connect a buzzer module to D8 of the main control board
import time
from pinpong.board import Board,Pin,Tone # Import Tone class to control the buzzer

Board("uno").begin()  # Initialization, select the board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization by specifying the port under Mac

tone = Tone(Pin(Pin.D8)) # Implement analog output by passing Pin into Tone
tone.freq(200) # Play according to the set frequency

'''
while True:
  tone.tone(200,500)  # Play according to the set frequency and time until completed
  time.sleep(1)
'''

while True:
  print("freq=",tone.freq()) # Read and print the frequency
  tone.on()  # Turn on the buzzer
  time.sleep(1)
  tone.off() # Turn off the buzzer
  time.sleep(1)
  tone.freq(tone.freq()+100) # Play according to the set frequency

Example Program - HCSR04

SR04_URM10: Ultrasonic Sensor

This Python code demonstrates how to read ultrasonic waves using an SR04 or URM10 ultrasonic sensor. The Arduino main control board is connected to a Windows or Linux computer, the Trig pin of the sensor is connected to D7, and the Echo pin is connected to D8 of the main control board.

# -*- coding: UTF-8 -*-
# Experiment effect: Read ultrasonic waves
# Connection: Connect an Arduino main control board to a Windows or Linux computer, use SR04 or URM10 ultrasonic sensor, connect Trig to D7, and Echo to D8
import time
from pinpong.board import Board,Pin,SR04_URM10 # Import SR04_URM10

Board("uno").begin()  # Initialization, select the board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization by specifying the port under Mac

TRIGER_PIN = Pin(Pin.D7)
ECHO_PIN = Pin(Pin.D8)

sonar = SR04_URM10(TRIGER_PIN,ECHO_PIN)

while True:
  dis = sonar.distance_cm() # Get the distance, unit in centimeters (cm)
  print("distance = %d cm"%dis)
  time.sleep(0.1)

Example Program - DHT

DHT: Temperature and Humidity Sensor

This Python code demonstrates how to read temperature and humidity data from DHT11 and DHT22 sensors. The Arduino main control board is connected to a Windows or Linux computer, the DHT11 sensor is connected to D6, and the DHT22 sensor is connected to D7 of the main control board.

# -*- coding: UTF-8 -*-
# Experiment effect: Read dht temperature and humidity sensor
# Connection: Connect an Arduino main control board to a Windows or Linux computer, connect dht11 to D6, and dht22 to D7
import time
from pinpong.board import Board,Pin,DHT11,DHT22 # Import dht library

Board("uno").begin()  # Initialization, select the board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization by specifying the port under Mac

dht11 = DHT11(Pin(Pin.D6))
dht22 = DHT22(Pin(Pin.D7))

while True:
  temp = dht11.temp_c() # Read Celsius temperature
  humi = dht11.humidity() # Read humidity
  print("dht11 temperature=",temp," humidity=",humi)

  temp = dht22.temp_c() # Read Celsius temperature
  humi = dht22.humidity() # Read humidity
  print("dht22 temperature=",temp," humidity=",humi)
  time.sleep(1)

Example Program - Neopixel

Neopixel: WS2812 Light Strip

This Python code demonstrates how to control a WS2812 single-line RGB LED light strip. The Arduino main control board is connected to a Windows or Linux computer, and the WS2812 light strip is connected to the D9 port of the main control board.

# -*- coding: UTF-8 -*-
# Experiment effect: Control WS2812 single-line RGB LED light
# Connection: Connect an Arduino main control board to a Windows or Linux computer, connect the WS2812 light to the D9 port

import time
from pinpong.board import Board,Pin,NeoPixel # Import the NeoPixel class

Board("uno").begin()  # Initialization, select the board type (uno, leonardo, xugu) and port number. If no port number is entered, automatic recognition will be performed
#Board("uno","COM36").begin()  # Initialization by specifying the port under Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization by specifying the port under Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization by specifying the port under Mac

NEOPIXEL_PIN = Pin(Pin.D9)
PIXELS_NUM = 4 # Number of lights

np = NeoPixel(NEOPIXEL_PIN,PIXELS_NUM)

while True:
    np[0] = (0, 255 ,0) # Set the RGB brightness of the first light
    np[1] = (255, 0, 0) # Set the RGB brightness of the second light
    np[2] = (0, 0, 255) # Set the RGB brightness of the third light
    np[3] = (255, 0, 255) # Set the RGB brightness of the fourth light
    print("color 1")
    time.sleep(1)
    np[1] = (0, 255, 0)
    np[2] = (255, 0, 0)
    np[3] = (255, 255, 0)
    np[0] = (0, 0, 255)
    print("color 2")
    time.sleep(1)

Was this article helpful?

TOP