def angle(pin, angle): angle_to_ns = int(MIN + ((angle / 180.0) * (MAX - MIN))) if pin == SV1: SV1_obj.duty_ns(angle_to_ns) elif pin == SV2: SV2_obj.duty_ns(angle_to_ns) elif pin == SV3: SV3_obj.duty_ns(angle_to_ns) else: servo_tmp = PWM(pin, freq=50) servo_tmp.freq(50) servo_tmp.duty_ns(angle_to_ns)
class Servo(object): def __init__(self, gpio, freq=50): self.gpio = gpio self.pwm = PWM(Pin(gpio, Pin.IN)) self.pwm.freq(freq) self.pwm.duty_ns(0) def set_value(self, value): self.pulse_us = 500 + 20*max(0, min(100, value)) self.pwm.duty_ns(int(1000*self.pulse_us)) def off(self): self.pwm.duty_ns(0)
s = PWM(Pin(15)) s.freq(50) led.on() while True: reading = sensor_temp.read_u16() * conversion_factor temperature = 27 - (reading - 0.706)/0.001721 print(temperature) #Oculto if button.value() == 0: led.on() ton = ((105 + 45) * 100000) / 9 s.duty_ns(int(ton)) time.sleep(0.1) #Normal else: led.off() ton = ((70 + 45) * 100000) / 9 s.duty_ns(int(ton)) time.sleep(0.1)
dsp1.setCursor(0, 0) dsp1.print("Hello, World") dsp1.setCursor(0, 1) dsp1.print("Display 1") ## setup rotary angle sensors knob0 = Knob(26) knob1 = Knob(27) ## setup ultra-sonic distance sensor on Pin 20 dst = Pin(20) ## set up servo motor pwm = PWM(Pin(16)) pwm.freq(50) pwm.duty_ns(1000*1500) start = time.time() ball_position = 0 while time.time() - start < 20: # read distance # send pulse dst.init(Pin.OUT) dst.value(0) time.sleep_us(2) dst.value(1) time.sleep_us(10) dst.value(0)
# PWM (pulse width modulation)¶ from machine import Pin, PWM pwm0 = PWM(Pin(0)) # create PWM object from a pin freq = pwm0.freq() # get current frequency (default 5kHz) pwm0.freq(1000) # set PWM frequency from 1Hz to 40MHz duty = pwm0.duty() # get current duty cycle, range 0-1023 (default 512, 50%) pwm0.duty(256) # set duty cycle from 0 to 1023 as a ratio duty/1023, (now 25%) duty_u16 = pwm0.duty_u16() # get current duty cycle, range 0-65535 pwm0.duty_u16(2 ** 16 * 3 // 4) # set duty cycle from 0 to 65535 as a ratio duty_u16/65535, (now 75%) duty_ns = pwm0.duty_ns() # get current pulse width in ns pwm0.duty_ns(250_000) # set pulse width in nanoseconds from 0 to 1_000_000_000/freq, (now 25%) pwm0.deinit() # turn off PWM on the pin pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go print(pwm2) # view PWM settings # ADC (analog to digital conversion)¶ from machine import ADC adc = ADC(Pin(32)) # create ADC object on ADC pin adc.read() # read value, 0-4095 across voltage range 0.0v - 1.0v
from machine import Pin, PWM import utime MID = 1500000 MIN = 1000000 MAX = 2000000 pwm = PWM(Pin(20)) pwm.freq(50) while(True): pwm.duty_ns(MIN) utime.sleep(1) pwm.duty_ns(MID) utime.sleep(1) pwm.duty_ns(MAX) utime.sleep(1)
import time from machine import u2if, Pin, PWM # Connect a servo on GP6 MIN_DUTY_MS = 1000 MAX_DUTY_MS = 2000 servo_pwm = PWM(Pin(u2if.GP6)) servo_pwm.freq(50) duty = MIN_DUTY_MS direction = 1 while True: duty += direction if duty > MAX_DUTY_MS: duty = MAX_DUTY_MS direction = -direction elif duty < MIN_DUTY_MS: duty = MIN_DUTY_MS direction = -direction servo_pwm.duty_ns(duty*1000) time.sleep(0.005)
sleep_ms(tiempo) InitValues[5] = i getValues() def runAll(): #corre todas las rutinas defValues() rutm1() rutm2() rutm3() rutm4() rutm5() rutm6() defValues() #valores iniciales de los servos servo1.duty_ns(Map(90, 0, 180, 500000, 2500000)) sleep_ms(200) servo2.duty_ns(Map(60, 0, 180, 500000, 2500000)) sleep_ms(200) servo3.duty_ns(Map(120, 0, 180, 500000, 2500000)) sleep_ms(200) servo4.duty_ns(Map(150, 0, 180, 500000, 2500000)) sleep_ms(200) servo5.duty_ns(Map(110, 0, 180, 500000, 2500000)) sleep_ms(200) servo6.duty_ns(Map(60, 0, 180, 500000, 2500000)) sleep_ms(200)
# Dev by Sonthaya Nongnuch from machine import Pin, PWM SV1 = Pin(18) SV2 = Pin(19) SV3 = Pin(20) SV1_obj = PWM(SV1) SV1_obj.freq(50) SV1_obj.duty_ns(0) SV2_obj = PWM(SV2) SV2_obj.freq(50) SV2_obj.duty_ns(0) SV3_obj = PWM(SV3) SV3_obj.freq(50) SV3_obj.duty_ns(0) MIN = 500000 MAX = 2500000 def angle(pin, angle): angle_to_ns = int(MIN + ((angle / 180.0) * (MAX - MIN))) if pin == SV1: SV1_obj.duty_ns(angle_to_ns) elif pin == SV2: SV2_obj.duty_ns(angle_to_ns) elif pin == SV3:
from machine import Pin, PWM, ADC import time print("setting servo") servo = PWM(Pin(1)) print("Setting up LED") led = PWM(Pin(25)) print("setting pot") potentiometer = ADC(26) print("setting servo frequency") freq = 50 servo.freq(freq) led.freq(1000) while True: # print("reading pot") pot = potentiometer.read_u16() duty = pot print('pot ', pot, ' duty:', duty, 'Freq:', freq) servo.duty_ns(int(duty)) led.duty_u16(int(duty)) time.sleep(0.001)