sync_code_version = '2019.06.27.a' from machine import Pin, PWM import time pwm = PWM(Pin(2), 100) FPS = 60 timeGap_ms = 1000 / FPS def runSync(): led = Pin(2, Pin.OUT) print("Running!!!") saveFile = open('1.txt', 'r') while 1: for line in saveFile: t1 = time.ticks_ms() v = int(line) * 4 pwm.duty(v) t = 0 while t < timeGap_ms: t2 = time.ticks_ms() t = time.ticks_diff(t1, t2)
from machine import Pin, PWM fade = PWM(Pin(2)) # 15 for esp8266 fade.freq(1000) while True: for i in range(10): fade.duty((i / 10) * 1024) delay_ms(100) for i in range(10): fade.duty(((10 - i) / 10) * 1024) delay_ms(100) # fade = PWM(Pin(2), freq=500, duty=(50/100)*1024) #fade.deinit()
def init_pins(self): print('* initializing pins') self.pin_sda = Pin(self.sda) self.pin_scl = Pin(self.scl) self.pin_intr = Pin(self.intr, mode=Pin.IN) self.pin_led = PWM(Pin(self.led, mode=Pin.OUT))
def __init__(self, pin, freq=1000): self.pwm = PWM(pin, freq=freq)
from machine import Pin, PWM, random from rtttl import RTTTL from _buzzer_mapping import buzzer as pin_num import time __pwm = PWM(Pin(pin_num, Pin.OUT), duty=0) __pwm.duty(0) def freq(freq=None): if freq: __pwm.freq(freq) __pwm.duty(50) else: __pwm.duty(0) def tone(freq, duration, pause=None): global pwm freq = round(freq) if not pause: duration = round(duration * 0.9) pause = round(duration * 0.1) if freq < 1: __pwm.duty(0) else: __pwm.freq(freq) __pwm.duty(50) time.sleep_ms(duration) __pwm.duty(0) time.sleep_ms(pause)
from machine import ADC,SPI,PWM,Pin from FlagArcade import * import LCD # 螢幕初始設定 spi = SPI(1, baudrate=40000000) screen = LCD.LCD(spi, 15, 5, 0) screen.init() screen.clearLCD() # 按鈕腳位設定 adc = ADC(0) # 蜂鳴器腳位與強度設定 buzzer = PWM(Pin(2)) amp = 512 # 搖桿音效設計 def toot(): buzzer.freq(494) buzzer.duty(amp) time.sleep(.05) buzzer.duty(0) # 賽車造型設計 c1=( b" 1111111 " b" 11111111111 " b" 111 " b" 1 111 1 " b"1 1 111 1 1"
if recu != b'invalid ID': invalid = False nom = recu[:-2].decode('utf-8') statut = recu[-1] - 48 equipe = recu[-2] - 48 #print(statut, 'statut') if equipe < 5 or equipe > 0: equipe = 'Equipe ' + str(equipe) elif equipe == 0: equipe = 'Coach' elif equipe == 5: equipe = 'Groupe avance' print(nom) #---Buzzer---# beeper = PWM(Pin(26, Pin.OUT), freq=440, duty=512) if invalid: for tone, length in zip(invalid_tone, rhythm): beeper.freq(tones[tone]) time.sleep(tempo / length) else: for tone, length in zip(valid_tone, rhythm): beeper.freq(tones[tone]) time.sleep(tempo / length) beeper.deinit() #---M脿j 茅cran---# if invalid: tft.text((10, 20), 'Invalid ID', TFT.RED, sysfont, 2, nowrap=True) tft.text((20, 60), "(._.)", TFT.RED, sysfont, 3, nowrap=True) else:
def write_analog(self, val): id = int(str(self)[4:-1]) #unsafe! self = PWM(Pin(id), duty=val)
def set_frequency(self, val): id = int(str(self)[4:-1]) self = PWM(Pin(id), freq=val)
def value(self, val): self.val = val PWM(Pin(self.pin)).duty(1023 - self.val)
def setbrightness(n, val): if n in (1, 2): n = 5 * (n - 1) PWM(Pin(n)).duty(1023 - val)
def write_angle(self, angle): id = int(str(self.pin)[4:-1]) PWM(Pin(id), freq=50, duty=int(40 + 75 * angle / 180))
# Demo 4.1 - Blinky PWM version from machine import Pin, PWM BlueLED = PWM(Pin(26), freq=1, duty=512) # pause BlueLED.freq(10) BlueLED.freq(30) BlueLED.freq(60) # pause BlueLED.duty(300) BlueLED.duty(100) BlueLED.duty(700) # pause BlueLED.deinit() # Demo 4.2 - Fading LEDs from machine import Pin, PWM from time import sleep BlueLED = PWM(Pin(26), freq=5000, duty=0) while True: for i in range(1024): BlueLED.duty(i) sleep(0.001) for i in range(1024, 0, -1): BlueLED.duty(i) sleep(0.001) # pause BlueLED.deinit()
# -*- coding: utf-8 -*- """ 程式說明請參閱11-4頁 """ from machine import Pin, PWM, ADC import time adc = ADC(0) ledPin = Pin(2, Pin.OUT) LED = PWM(ledPin,1000) while True: val = adc.read() LED.duty(val) print('POT: ', str(val)) time.sleep(0.5)
import machine from machine import Pin, PWM import time pwm1 = PWM(Pin(5), freq=1000, duty=0) pwm2 = PWM(Pin(4), freq=1000, duty=0) M1 = machine.Pin(0, machine.Pin.OUT) M2 = machine.Pin(2, machine.Pin.OUT) M1.on() # forward M2.on() # forward print("Motor on") speed = 10 while speed < 1030: speed += 10 time.sleep_ms(50) pwm1.duty(speed) pwm2.duty(speed) print("Max speed") while speed > 20: speed -= 10 time.sleep_ms(50) pwm1.duty(speed) pwm2.duty(speed) M1.off() # now backward M2.off()
from machine import Pin, ADC, PWM import time pressed = False def handle(pin): global pressed pressed = True frequency = 10000 button = Pin(12, Pin.IN, Pin.PULL_UP) button.irq(trigger=Pin.IRQ_FALLING, handler=handle) light = ADC(0) led = PWM(Pin(15), frequency) buzzer = PWM(Pin(14), frequency) while True: if pressed: first = button.value() time.sleep_ms(10) second = button.value() light_val = light.read() led.duty(light_val) buzzer.duty(light_val) if not first and second: pressed = False led.duty(0) buzzer.duty(0)
# # Copyright (c) 2006-2019, RT-Thread Development Team # # SPDX-License-Identifier: MIT License # # Change Logs: # Date Author Notes # 2019-06-13 SummerGift first version # from machine import PWM # Import PWM class from machine pwm = PWM(1, 4, 1000, 100) # Create PWM object. Currently, 4 channels of PWM device numbered 1 are used. # The initialization frequency is 1000Hz and the duty ratio value is 100 (duty ratio is 100/255 = 39.22%). pwm.freq(2000) # Set the frequency of PWM object pwm.freq() # Get the frequency of PWM object pwm.duty(200) # sets the duty ratio value of PWM object pwm.duty() # Get the duty ratio value of PWM object pwm.deinit() # close PWM object pwm.init(4, 1000, 100) # open and reconfigure the PWM object
def nvs_get(key, default_value=0): """ Get value for key from NVRAM. Create the key if it does not exist. :param str key: :param int default_value: value to use when creating the key :return value int: (NVRAM can only store integers) """ value = pycom.nvs_get(key) if value is None: pycom.nvs_set(key, int(default_value)) value = int(default_value) return value pwm = PWM(0, frequency=25000) channel0 = pwm.channel(0, pin="P23", duty_cycle=1.0) channel1 = pwm.channel(0, pin="P22", duty_cycle=1.0) lock = _thread.allocate_lock() temp0 = 0 temp1 = 0 dutycycle = 0 # unit %, where 0 if off and 100 is full speed max_dutycycle = nvs_get("max_dutycycle", 100) # unit = % min_dutycycle = nvs_get("min_dutycycle", 20) # unit = % temp_fan_on = nvs_get("temp_fan_on", 40) # unit = degrees celsius temp_fan_max = nvs_get("temp_fan_max", 60) # unit = degrees celsius
# Complete project details at https://RandomNerdTutorials.com from dcmotor import DCMotor from machine import Pin, PWM from time import sleep frequency = 15000 pin1 = Pin(12, Pin.OUT) pin2 = Pin(14, Pin.OUT) enable = PWM(Pin(13), frequency) dc_motor = DCMotor(pin1, pin2, enable) dc_motor.forward(50) sleep(2) dc_motor.stop() sleep(3) dc_motor.backwards(100) sleep(2) dc_motor.forward(5) sleep(5) dc_motor.stop()
from machine import Pin, PWM, ADC from time import sleep frequency = 5000 led = PWM(Pin(5), frequency) pot = ADC(Pin(34)) pot.width(ADC.WIDTH_10BIT) pot.atten(ADC.ATTN_11DB) while True: pot_value = pot.read() print(pot_value) if pot_value < 15: led.duty(0) else: led.duty(pot_value) sleep(0.1)
from machine import PWM pwm_timers = range(3) pwm_channels = range(7) for timer in pwm_timers: for channel in pwm_channels: pwm = PWM(timer, frequency=5000) print(pwm) pwm_c = pwm.channel(channel, pin='P12', duty_cycle=0.5) print(pwm_c) pwm_c.duty_cycle(0.3) print(pwm_c) pwm = PWM(0, frequency=5000) pwm_c = pwm.channel(channel, pin='P12', duty_cycle=-1.0) pwm_c.duty_cycle(0.0) pwm_c.duty_cycle(10.0) pwm_c.duty_cycle(-10.0) pwm_c.duty_cycle(0) try: pwm = PWM(4, frequency=5000) except Exception: print("Exception") try: pwm = PWM(4, frequency=5000) pwm_c = pwm.channel(8, pin='P12', duty_cycle=0.0) except Exception: print("Exception")
from machine import PWM from machine import Pin from machine import Timer from machine import ADC import utime, math dac = DAC(Pin(26,Pin.OUT), bits=12) pwm = PWM(Pin(27),freq=1000) def pulse(dac, period, gears): """呼吸灯函数 Arguments: dac {[DAC]} -- [DAC对象] period {[type]} -- [周期] gears {[type]} -- [亮度档位] """ for i in range(2 * gears): dac.write(int(math.sin(i / gears * math.pi) * 2000) + 2048) pwm.duty(int(math.sin(i / gears * math.pi) * 500) + 523) # 延时 utime.sleep_ms(int(period / (2 * gears))) # 呼吸十次 for i in range(10): pulse(dac, 2000, 100)
def tone(pwm, note, duration): pwm.freq(note) pwm.duty(512) time.sleep_ms(duration) pwm.deinit() def littleBee(pwm): j = 0 for i in range(0, 20): d = int(1000 / duration[i]) p = int(d * 0.5) if note[i] == 0: # led.value(1) # led2.value(0) time.sleep_ms(p) else: # led.value(0) # led2.value(1) tone(pwm, note[i], d) time.sleep_ms(p) time.sleep(2) pwm = PWM(Pin(5)) #while True: littleBee(pwm)
# -*- coding: utf-8 -*- """ 程式說明請參閱17-31頁 """ from machine import Pin, PWM import socket, os, gc lamp = Pin(2, Pin.OUT, value=1) dimmer = PWM(Pin(13), 1000) HOST = '0.0.0.0' PORT = 80 WWWROOT = '/webroot/' httpHeader = '''HTTP/1.0 200 OK Content-type: {} Content-length: {} ''' feedback = b'''\ HTTP/1.1 200 OK OK! ''' mimeTypes = { '.txt': 'text/plain', '.htm': 'text/html', '.html': 'text/html',
# from machine import Pin, PWM import time def ringTone(pwm): for i in range(1, 11): # 10 次迴圈 (1~10) pwm.freq(1000) # 設定頻率為 1KHz pwm.duty(512) # 設定工作週期為 50% time.sleep_ms(50) # 持續時間 50 毫秒 pwm.freq(500) # 設定頻率為 500Hz time.sleep_ms(50) # 持續時間 50 毫秒 while True: pwm = PWM(Pin(23)) ringTone(pwm) pwm.deinit() time.sleep(2)
from machine import Pin, PWM, I2C import time def srv_us(us): srv.duty(us * 1024 * 50 // 1000000) print("Hello!") pin_srv = Pin(5) srv = PWM(pin_srv, freq=50, duty=0) pin_dir = Pin(14) pin_step = Pin(13) pin_en = Pin(0) pin_sda = Pin(12) pin_scl = Pin(16) i2c = I2C(scl=pin_scl, sda=pin_sda, freq=100000) pin_led = Pin(2) #while(1): # print(".") # time.sleep(1) pin_led.init(Pin.OUT) pin_en.init(Pin.OUT) pin_dir.init(Pin.OUT) pin_step.init(Pin.OUT)
# Basic purpose of the app is to take input from serial port and turn it into Morse code. # Then it plays it via sound, light or by acting as a straight key and triggering an external device (radio). # Interfacing with the external device can be done with an optocupler, transistor, relay or similar simple circuit. # There is one memory slot that can be played with a button push, and changed through console. # WPM and sidetone frequency can be changed as well. # Code has many bugs, please fix them :) # 73 import time, sys, utime # import necessary libraries, from machine import Pin, PWM, Timer led = Pin(25, Pin.OUT) # the LED on the Pico is pin 25 digitalOut = Pin(9, Pin.OUT) # output to trigger external device button = Pin(10, Pin.IN, Pin.PULL_UP) buzzer = PWM(Pin(15)) stdin_string = sys.stdin lastInterrupt = 0 timer = Timer(-1) memory1 = "CQ DE YU4HAK" BlinkRate = 0.062 BuzzFrequency = 600 # functions for morse code signal durations def dah(): led.value(1) digitalOut.value(1) buzzer.duty_u16(1000)
# Micro This And Micro That - FCM #167 # ====================================== # For use on the Raspberry Pi Pico # -------------------------------------- # LED Throb 1 # Imports from machine import Pin, PWM from time import sleep pwm = PWM(Pin(25)) pwm.freq(1000) while True: for duty in range(65025): pwm.duty_u16(duty) sleep(0.0001) for duty in range(65025,0, -1): pwm.duty_u16(duty) sleep(0.0001) sleep(0.8)
import machine from machine import Pin, PWM, ADC potenciometro = ADC(Pin(34)) potenciometro.atten(ADC.ATTN_11DB) potenciometro.width(ADC.WIDTH_12BIT) while True: leitura = potenciometro.read() leitura = leitura * 3.3 / 4095 if leitura == 0.0 or leitura == 00.0 or leitura == 0.00: servo = PWM(Pin(2), freq=100) servo.duty(250) elif leitura == 3.3: servo = PWM(Pin(2), freq=100) servo.duty(40) else: servo = PWM(Pin(2), freq=100) servo.duty(0)
import gc # defines header format of image file IMG_HEADER = {"width": 0 | uctypes.UINT16, "height": 2 | uctypes.UINT16} select0 = Pin(14, Pin.IN, Pin.PULL_UP) select1 = Pin(15, Pin.IN, Pin.PULL_UP) dc = Pin(3, Pin.OUT) cs1 = Pin(10, Pin.OUT) cs2 = Pin(11, Pin.OUT) cs3 = Pin(12, Pin.OUT) cs4 = Pin(13, Pin.OUT) res = Pin(2, Pin.OUT) blk = Pin(8, Pin.OUT) blkPWM = PWM(blk) # as all the reset from the displays are wired to one pin of the Pico # the reset shall only be done for the first display tft1 = ST7735B(SPI(0, baudrate=25000000), cs1, dc, res, blk=blkPWM, height=160, width=80, usd=True, reset_tft=True) tft2 = ST7735B(SPI(0, baudrate=25000000), cs2, dc, res,