Extended Library Examples

Modules from the extension library examples can be imported through the libs library. You can enter 'pinpong' in the terminal to query the support list and usage methods. All example program codes can be found in the 'examples' folder from the installation directory.

Example Program - LCD1602

2-05-lcd1602: 1602 Display Screen

Here is the Python code to control an I2C LCD1602 display:

# -*- coding: UTF-8 -*-
# Experiment effect: I2C LCD1602 control
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the LCD1602 display screen to the I2C port SCL and SDA
import time
from pinpong.board import Board
from pinpong.libs.lcd1602 import LCD1602_I2C # Import the lcd1602_i2c library from libs

Board("uno").begin()               # Initialization, choose the board type (uno, leonardo, xugu) and port number. If the port number is not entered, automatic recognition 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

lcd = LCD1602_I2C(i2c_addr=0x20) # Initialize the I2C address of the LCD
print("I2C LCD1602 TEST...")

lcd.backlight(True)  # Turn on the backlight
lcd.clear()   # Clear the screen

lcd.set_cursor(0,0)  # Set the cursor position
lcd.print("Hello World") # Display "Hello World". The 1602 screen has few pixels and cannot display Chinese characters
lcd.set_cursor(1,1)  # Set the cursor position
lcd.print(1234) # Display the number 1234

while True:
    time.sleep(1)
    lcd.scroll_left() # Scroll display

Example Program - OLED2864

I2C OLED2864 Screen Control

Here is the Python code to control an I2C OLED2864 display:

# -*- coding: UTF-8 -*-
# Experiment effect: I2C OLED2864 screen control
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the OLED2864 display screen to the I2C port SCL and SDA

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_ssd1306 import SSD1306_I2C # Import the ssd1306 library

Board("uno").begin()               # Initialization, choose the board type (uno, leonardo, xugu) and port number. If the port number is not entered, automatic recognition 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

oled=SSD1306_I2C(width=128, height=64) # Initialize the screen, pass in the number of screen pixels

while True:
    oled.fill(1) # Fill all for display
    oled.show() # Display takes effect
    print("1")
    time.sleep(1)

    oled.fill(0) # Fill all to turn off, clear the screen
    oled.show() # Display takes effect
    print("0")
    time.sleep(1)

    oled.text("0") # Display the number
    oled.text("Hello PinPong",8,8) # Display text at the specified position
    oled.show()  # Display takes effect
    time.sleep(2)

Example Program - TCS34725

Reading Values from I2C TCS34725 Color Sensor

Here is the Python code to read values from an I2C TCS34725 color sensor:

# -*- coding: UTF-8 -*-
# Experiment effect: Read the values of the I2C TCS34725 color sensor
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the TCS34725 color sensor to the I2C port SCL and SDA

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_tcs34725 import TCS34725 # Import the tcs34725 library from libs

Board("uno").begin()               # Initialization, choose the board type (uno, leonardo, xugu) and port number. If the port number is not entered, automatic recognition 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

tcs = TCS34725() # Sensor initialization

print("Color View Test!");
while True:
  if tcs.begin(): # Look for the sensor, return True if found
    print("Found sensor")
    break # If found, break the loop
  else:
    print("No TCS34725 found ... check your connections")
    time.sleep(1)

while True:
  r,g,b,c = tcs.get_rgbc() # Get the RGBC data
  print(r,g,b,c)
  print("C=%d\tR=%d\tG=%d\tB=%d\t"%(c,r,g,b))

  '''
  # Data conversion
  r /= c
  g /= c
  b /= c
  r *= 256
  g *= 256
  b *= 256;
  print("------C=%d\tR=%d\tG=%d\tB=%d\t"%(c,r,g,b))
  '''
  time.sleep(1)

Example Program - Urm09

Reading Values from I2C Ultrasonic Sensor (URM09)

Here is the Python code to read values from an I2C URM09 ultrasonic sensor:

# -*- coding: UTF-8 -*-
# Experiment effect: Read the values of the I2C URM09 ultrasonic sensor
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the URM09 sensor to the I2C port SCL and SDA

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_urm09 import URM09 # Import the URM09 library from libs

Board("uno").begin()               # Initialization, choose the board type (uno, leonardo, xugu) and port number. If the port number is not entered, automatic recognition 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

urm = URM09(i2c_addr=0x11) # Initialize the sensor and set the I2C address
urm.set_mode_range(urm._MEASURE_MODE_AUTOMATIC, urm._MEASURE_RANG_500) # Set the URM09 mode to automatic detection, with a maximum measurement distance of 500cm

while True:
  dist = urm.distance_cm() # Read the distance data in centimeters (cm)
  temp = urm.temp_c() # Read the sensor temperature in Celsius (℃)

  print("Distance is %d cm         "%dist)
  print("Temperature is %.2f .c    "%temp)
  time.sleep(0.5)

Example Program - RGB1602

Controlling I2C RGB LCD1602 Display

Here is the Python code to control an I2C RGB LCD1602 display:

# -*- coding: UTF-8 -*-
# Experiment effect: Control I2C RGB LCD1602 display
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the LCD1602 to the I2C port SCL and SDA

import time
from pinpong.board import Board
from pinpong.libs.rgb1602 import RGB1602 # Import the rgb1602 library from libs

Board("uno").begin()               # Initialization, choose the board type (uno, leonardo, xugu) and port number. If the port number is not entered, automatic recognition 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

lcd = RGB1602()

print("I2C RGB1602 TEST...")

lcd.set_rgb(0,50,0);       # Set RGB values
lcd.set_cursor(0,0)                 # Set the cursor to the origin
lcd.print("PinPong")       # Display "PinPong"

lcd.set_cursor(1,1)        # Set the cursor position
lcd.print(1234)            # Display a number

while True:
    time.sleep(1)
    lcd.scroll_left()           # Scroll the display

Example Program - MLX90614

Reading Values from I2C MLX90614 Infrared Temperature Sensor

Here is the Python code to read values from an I2C MLX90614 infrared temperature sensor:

# -*- coding: UTF-8 -*-
# Experiment effect: Read values from the I2C MLX90614 infrared temperature sensor
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the infrared temperature sensor to the I2C port SCL and SDA

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_mlx90614 import MLX90614 # Import the mlx90614 library from libs

Board("uno").begin()               # Initialization, choose the board type (uno, leonardo, xugu) and port number. If the port number is not entered, automatic recognition 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

irt = MLX90614()

while True:
  print("Object  %s *C"% irt.obj_temp_c())     # Read the object temperature in Celsius (℃)
  print("Object  %s *F"% irt.obj_temp_f())     # Read the object temperature in Fahrenheit (℉)
  print("Ambient %s *C"% irt.env_temp_c())     # Read the ambient temperature in Celsius (℃)
  print("Ambient %s *F"% irt.env_temp_f())     # Read the ambient temperature in Fahrenheit (℉)
  print() # Empty line
  time.sleep(1)

Example Program - NFC

Here are the Python codes to read and write card information with an NFC near field communication module connected to an Arduino main control board via I2C port (SCL and SDA).

Reading Card Information

# -*- coding: utf-8 -*-
# Experiment effect: NFC near field communication module IIC reads card information
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the NFC near field communication module to the I2C port SCL and SDA
import time
from pinpong.board import Board
from pinpong.libs.dfrobot_pn532 import PN532

Board("uno").begin()  # Initialization, choose the board type and port number. If the port number is not entered, automatic recognition 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

nfc = PN532()

while not nfc.begin():
  print("initial failure")
  time.sleep(1)
print("Please place the info card/tag on module..... ")

while True:
  if nfc.scan():
    info = nfc.get_information()
    if info != None:
      print("----------------NFC card/tag information-------------------")
      print("UID Length: %d"%info.length)
      print("UID: %x %x %x %x"%(info.uid[0],info.uid[1],info.uid[2],info.uid[3] ))
      print("AQTA: %x %x"%(info.AQTA[0], info.AQTA[0]))
      print("SAK: 0x%x"%(info.sak))
      print("Type: %s"%(info.types))
      print("Manufacturer: %s"%(info.manu))
      print("RF Technology: %s"%(info.RF))
      print("Memory Size: %d bytes(total)/%d bytes(available)"%(info.size_total, info.size_available))
      print("Block/Page Size: %d bytes"%(info.block))
      print("Number of Blocks/pages: %d"%(info.num_block))
  time.sleep(1)

Reading Card Internal Information

# -*- coding: utf-8 -*-
# Experiment effect: NFC near field communication module IIC reads card internal information
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the NFC near field communication module to the I2C port SCL and SDA
import time
from pinpong.board import Board
from pinpong.libs.dfrobot_pn532 import PN532

Board("uno").begin()  # Initialization, choose the board type and port number. If the port number is not entered, automatic recognition 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

nfc = PN532()

def print_data(block):
  value = nfc.read_data(block)
  if  value != None:
    for i in value:
      print("0x%x "%(i), end="")
    print("")
  else:
    print_data(block)

while not nfc.begin():
  print("initial failure")
  time.sleep(1)
print("Waiting for a card......")

while True:
  if nfc.scan():
    NFC = nfc.get_information()
    if NFC != None:
      if NFC.length == 0x02 or NFC.length == 0x04:
        print("----------------Here is the card information to read-------------------")
        for i in range(NFC.num_block):
          if i == 0:
            print("Block %d:UID0-UID3/MANUFACTURER--------->"%(i), end="")
            print_data(i);
          elif (i+1)%4==0 and i < 128:
            print("Block %d:KEYA/ACCESS/KEYB--------------->"%(i), end="")
            print_data(i)
          elif (i+1)%16==0 and i > 127:
            print("Block %d:KEYA/ACCESS/KEYB--------------->"%(i), end="")
            print_data(i)
          else:
            print("Block %d:DATA   ------------------------>"%(i), end="")
            print_data(i)
      else:
        print("The card type is not mifareclassic...")
  time.sleep(3)

Reading and Writing Card Information

# -*- coding: utf-8 -*-
# Experiment effect: NFC near field communication module IIC reads and writes card information
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the NFC near field communication module to the I2C port SCL and SDA
import time
from pinpong.board import Board
from pinpong.libs.dfrobot_pn532 import PN532

Board("uno").begin()  # Initialization, choose the board type and port number. If the port number is not entered, automatic recognition 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

nfc = PN532()

write_data = "DFRobot NFC"
#write_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#write_data = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

block_num = 2

while not nfc.begin():
  print("initial failure")
  time.sleep(1)
print("Waiting for a card......")

def parse_data(read_data):
  if read_data != None:
    print("read success! data is ", end=" ")
    print(read_data)
  else:
    print("read failure!")

while True:
  if nfc.scan():
    info = nfc.get_information()
    if info != None:
      if info.length == 0x02 or info.length == 0x04:
        if not nfc.write_data(block_num, write_data):
          print("write failure!")
        else:
          print("write success! data is", end=" ")
          print(write_data)
        read_data= nfc.read_data(block_num)
        parse_data(read_data)
      else:
        print("The card type is not mifareclassic...")
  time.sleep(2)

Was this article helpful?

TOP