Example-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)