Python machine.PWM Examples

The following are 30 code examples of machine.PWM(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module machine , or try the search function .
Example #1
Source File: __init__.py    From platypush with MIT License 6 votes vote down vote up
def pwm_off(self, pin: Union[int, str], **kwargs):
        """
        Turn off a PWM PIN.

        :param pin: GPIO PIN number or configured name.
        :param kwargs: Parameters to pass to :meth:`platypush.plugins.esp.EspPlugin.execute`.
        """
        device = self._get_device(**kwargs)
        pin = device.get_pin(pin)
        code = '''
import machine
pin = machine.PWM(machine.Pin({pin}))
pin.deinit()
'''.format(pin=pin)

        self.execute(code, **kwargs) 
Example #2
Source File: servo.py    From ulnoiot-upy with MIT License 6 votes vote down vote up
def __init__(self, name, pin,
                 ignore_case=True, on_change=None,
                 report_change=False,
                 turn_time_ms=700,
                 freq=50, min_us=600, max_us=2400, angle=180):
        self.min_us = min_us
        self.max_us = max_us
        self.us = 0
        self.freq = freq
        self.angle = angle
        self.angle_list = None
        self.turn_time_ms = turn_time_ms
        self.turn_start = None
        Device.__init__(self, name, PWM(pin, freq=self.freq, duty=0),
                        setters={"set": self.turn}, ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change)
        self._init() 
Example #3
Source File: servo.py    From ulnoiot-upy with MIT License 6 votes vote down vote up
def __init__(self, name, pin,
                 ignore_case=True, on_change=None,
                 report_change=False,
                 turn_time_ms=700,
                 freq=50, min_us=600, max_us=2400, angle=180):
        self.min_us = min_us
        self.max_us = max_us
        self.us = 0
        self.freq = freq
        self.angle = angle
        self.angle_list = None
        self.turn_time_ms = turn_time_ms
        self.turn_start = None
        Device.__init__(self, name, PWM(pin, freq=self.freq, duty=0),
                        setters={"set": self.turn}, ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change)
        self._init() 
Example #4
Source File: servo.py    From ulnoiot-upy with MIT License 6 votes vote down vote up
def __init__(self, name, pin,
                 ignore_case=True, on_change=None,
                 report_change=False,
                 turn_time_ms=700,
                 freq=50, min_us=600, max_us=2400, angle=180):
        self.min_us = min_us
        self.max_us = max_us
        self.us = 0
        self.freq = freq
        self.angle = angle
        self.angle_list = None
        self.turn_time_ms = turn_time_ms
        self.turn_start = None
        Device.__init__(self, name, PWM(pin, freq=self.freq, duty=0),
                        setters={"set": self.turn}, ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change)
        self._init() 
Example #5
Source File: servo.py    From ulnoiot-upy with MIT License 6 votes vote down vote up
def __init__(self, name, pin,
                 ignore_case=True, on_change=None,
                 report_change=False,
                 turn_time_ms=700,
                 freq=50, min_us=600, max_us=2400, angle=180):
        self.min_us = min_us
        self.max_us = max_us
        self.us = 0
        self.freq = freq
        self.angle = angle
        self.angle_list = None
        self.turn_time_ms = turn_time_ms
        self.turn_start = None
        Device.__init__(self, name, PWM(pin, freq=self.freq, duty=0),
                        setters={"set": self.turn}, ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change)
        self._init() 
Example #6
Source File: motor.py    From 1ZLAB_PyEspCar with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, idx, is_debug=False):
        
        gpio_a, gpio_b, self.motor_install_dir = Motor.MOTOR_LIST[idx]
        # A相PWM
        self.pwm_a = PWM(
            Pin(gpio_a, Pin.OUT),
            freq = Motor.MOTOR_PWM_FREQUENCY,
            duty = 0)
        
        # B相PWM
        self.pwm_b = PWM(
            Pin(gpio_b, Pin.OUT),
            freq = Motor.MOTOR_PWM_FREQUENCY,
            duty = 0)
        
        # 电机安装方向
        if not self.motor_install_dir:
            self.pwm_a, self.pwm_b = self.pwm_b, self.pwm_a
        
        # 电机速度信号 取值范围: -1023 - 1023 
        self._pwm = 0
        # 设置电机的PWM
        # self.pwm(self._pwm) 
Example #7
Source File: __init__.py    From platypush with MIT License 6 votes vote down vote up
def pwm_on(self, pin: Union[int, str], freq: Optional[int] = None, duty: Optional[int] = None, **kwargs):
        """
        Set the specified PIN to HIGH.

        :param pin: GPIO PIN number or configured name.
        :param freq: PWM PIN frequency.
        :param duty: PWM PIN duty cycle.
        :param kwargs: Parameters to pass to :meth:`platypush.plugins.esp.EspPlugin.execute`.
        """
        device = self._get_device(**kwargs)
        pin = device.get_pin(pin)
        code = '''
import machine
pin = machine.PWM(machine.Pin({pin}))

if {freq}:
    pin.freq({freq})
if {duty}:
    pin.duty({duty})

pin.on()
'''.format(pin=pin, freq=freq, duty=duty)

        self.execute(code, **kwargs) 
Example #8
Source File: __init__.py    From platypush with MIT License 6 votes vote down vote up
def pwm_duty(self, pin: Union[int, str], duty: Optional[int] = None, **kwargs) -> Optional[int]:
        """
        Get/set the duty cycle of a PWM PIN.

        :param pin: GPIO PIN number or configured name.
        :param duty: Optional duty value to set.
        :param kwargs: Parameters to pass to :meth:`platypush.plugins.esp.EspPlugin.execute`.
        """
        device = self._get_device(**kwargs)
        pin = device.get_pin(pin)
        code = '''
import machine
pin = machine.PWM(machine.Pin({pin}))
pin.duty({duty})
'''.format(pin=pin, duty=duty if duty else '')

        ret = self.execute(code, **kwargs).output
        if not duty:
            return int(ret) 
Example #9
Source File: __init__.py    From platypush with MIT License 6 votes vote down vote up
def pwm_freq(self, pin: Union[int, str], freq: Optional[int] = None, **kwargs) -> Optional[int]:
        """
        Get/set the frequency of a PWM PIN.

        :param pin: GPIO PIN number or configured name.
        :param freq: If set, set the frequency for the PIN in Hz.
        :param kwargs: Parameters to pass to :meth:`platypush.plugins.esp.EspPlugin.execute`.
        """
        device = self._get_device(**kwargs)
        pin = device.get_pin(pin)
        code = '''
import machine
pin = machine.PWM(machine.Pin({pin}))
pin.freq({freq})
'''.format(pin=pin, freq=freq if freq else '')

        ret = self.execute(code, **kwargs).output
        if not freq:
            return int(ret) 
Example #10
Source File: servo.py    From ulnoiot-upy with MIT License 6 votes vote down vote up
def __init__(self, name, pin,
                 ignore_case=True, on_change=None,
                 report_change=False,
                 turn_time_ms=700,
                 freq=50, min_us=600, max_us=2400, angle=180):
        self.min_us = min_us
        self.max_us = max_us
        self.us = 0
        self.freq = freq
        self.angle = angle
        self.angle_list = None
        self.turn_time_ms = turn_time_ms
        self.turn_start = None
        Device.__init__(self, name, PWM(pin, freq=self.freq, duty=0),
                        setters={"set": self.turn}, ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change)
        self._init() 
Example #11
Source File: esp_8266Full.py    From python_banyan with GNU Affero General Public License v3.0 6 votes vote down vote up
def digital_write(self, payload):
        """
        Write to a digital gpio pin
        :param payload:
        :return:
        """
        pin = payload['pin']
        mode = Pin.OUT
        if 'drain' in payload:
            if not payload['drain']:
                mode = Pin.OPEN_DRAIN
        pin_object = Pin(pin, mode)
        pwm = PWM(pin_object)
        pwm.deinit()
        Pin(pin, mode, value=payload['value'])
        self.input_pin_objects[pin] = None 
Example #12
Source File: buzzer.py    From developer-badge-2018-apps with Apache License 2.0 6 votes vote down vote up
def playnotes(self, title, length=150, duty=64):
        # Init
        p = Pin(27, Pin.OUT)
        self.pwm = PWM(p)
        self.pwm.duty(0)

        if title not in self.notes:
          print('unknown title: {}'.format(title))
          return

        melody = self.notes[title]
        print('Play', title)
        for i in melody:
            if i == 0:
                self.pwm.duty(0)
            else:
                self.pwm.freq(i)
                self.pwm.duty(duty)
            time.sleep_ms(length)

        # deinit
        self.pwm.deinit() 
Example #13
Source File: buzzer.py    From developer-badge-2018-apps with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self.notes = {
            'cdef': [
                self.C6, self.D6, self.E6, self.F6, self.G6, self.A6, self.B6, self.C7, self.D7, self.E7, self.F7, self.G7, self.A7, self.B7, self.C8, 0
            ],
            'mario': [
                self.E7, self.E7,  0, self.E7,  0, self.C7, self.E7,  0, self.G7,  0,  0,  0, self.G6,  0,  0,  0,
                self.C7,  0,  0, self.G6,  0,  0, self.E6,  0,  0, self.A6,  0, self.B6,  0, self.AS6, self.A6, 0,
                self.G6, self.E7,  0, self.G7, self.A7,  0, self.F7, self.G7,  0, self.E7,  0, self.C7, self.D7, self.B6,  0,  0,
                self.C7,  0,  0, self.G6,  0,  0, self.E6,  0,  0, self.A6,  0, self.B6,  0, self.AS6, self.A6, 0,
                self.G6, self.E7,  0, self.G7, self.A7,  0, self.F7, self.G7,  0, self.E7,  0, self.C7, self.D7, self.B6,  0,  0
            ],
            'starwars': [
                self.A4,  0,  0,  0, self.A4,  0,  0,  0, self.A4,  0,  0,  0, self.F4,  0,  0, self.C5,
                self.A4,  0,  0,  0, self.F4,  0,  0, self.C5, self.A4,  0,  0,  0,  0,  0,  0,  0,
                self.E5,  0,  0,  0, self.E5,  0,  0,  0, self.E5,  0,  0,  0, self.F5,  0,  0, self.C5,
                self.GS4, 0,  0,  0, self.F4,  0,  0, self.C5, self.A4,  0,  0,  0,  0,  0,  0,  0,
            ],
        }

        # Init
        self.pwm = PWM(Pin(27, Pin.OUT))
        self.pwm.duty(0) 
Example #14
Source File: buzzer.py    From developer-badge-2018-apps with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        self.notes = {
            'cdef': [
                self.C6, self.D6, self.E6, self.F6, self.G6, self.A6, self.B6, self.C7, self.D7, self.E7, self.F7, self.G7, self.A7, self.B7, self.C8, 0
            ],
            'mario': [
                self.E7, self.E7,  0, self.E7,  0, self.C7, self.E7,  0, self.G7,  0,  0,  0, self.G6,  0,  0,  0,
                self.C7,  0,  0, self.G6,  0,  0, self.E6,  0,  0, self.A6,  0, self.B6,  0, self.AS6, self.A6, 0,
                self.G6, self.E7,  0, self.G7, self.A7,  0, self.F7, self.G7,  0, self.E7,  0, self.C7, self.D7, self.B6,  0,  0,
                self.C7,  0,  0, self.G6,  0,  0, self.E6,  0,  0, self.A6,  0, self.B6,  0, self.AS6, self.A6, 0,
                self.G6, self.E7,  0, self.G7, self.A7,  0, self.F7, self.G7,  0, self.E7,  0, self.C7, self.D7, self.B6,  0,  0
            ],
            'starwars': [
                self.A4,  0,  0,  0, self.A4,  0,  0,  0, self.A4,  0,  0,  0, self.F4,  0,  0, self.C5,
                self.A4,  0,  0,  0, self.F4,  0,  0, self.C5, self.A4,  0,  0,  0,  0,  0,  0,  0,
                self.E5,  0,  0,  0, self.E5,  0,  0,  0, self.E5,  0,  0,  0, self.F5,  0,  0, self.C5,
                self.GS4, 0,  0,  0, self.F4,  0,  0, self.C5, self.A4,  0,  0,  0,  0,  0,  0,  0,
            ],
        }

        # Init
        self.pwm = PWM(Pin(27, Pin.OUT))
        self.pwm.duty(0) 
Example #15
Source File: sparkfun_esp32_thing.py    From webthing-upy with MIT License 5 votes vote down vote up
def __init__(self, ledPin):
        Thing.__init__(
            self,
            'urn:dev:ops:blue-led-1234',
            'Blue LED',
            ['OnOffSwitch', 'Light'],
            'Blue LED on SparkFun ESP32 Thing'
        )
        self.pinLed = machine.Pin(ledPin, machine.Pin.OUT)
        self.pwmLed = machine.PWM(self.pinLed)
        self.ledBrightness = 50
        self.on = False
        self.updateLed()

        self.add_property(
            Property(self,
                     'on',
                     Value(self.on, self.setOnOff),
                     metadata={
                         '@type': 'OnOffProperty',
                         'title': 'On/Off',
                         'type': 'boolean',
                         'description': 'Whether the LED is turned on',
                     }))
        self.add_property(
            Property(self,
                     'brightness',
                     Value(self.ledBrightness, self.setBrightness),
                     metadata={
                         '@type': 'BrightnessProperty',
                         'title': 'Brightness',
                         'type': 'number',
                         'minimum': 0,
                         'maximum': 100,
                         'unit': 'percent',
                         'description': 'The brightness of the LED',
                     })) 
Example #16
Source File: led.py    From 1ZLAB_MicroPython_ESP32_Tutorial with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, led_id):
        # LED字典 
        # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
        led_list = [(2, False, True),(13, True, False)]

        if led_id >= len(led_list) or led_id < 0:
            print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
            return None
        
        gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
        self.pin = Pin(gpio_id, Pin.OUT)
        self.pwm = PWM(self.pin, freq=1000) 
Example #17
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def __init__(self, name, pin, freq=50, duty=0,
                 ignore_case=True, on_change=None,
                 report_change=False):
        self._duty = 0
        self._freq = freq
        Device.__init__(self, name, machine.PWM(pin, freq=freq, duty=duty),
                        setters={"freq/set": self.set_freq, "duty/set": self.set_duty},
                        getters={"freq": self.get_freq, "duty": self.get_duty},
                        ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change) 
Example #18
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def set_duty(self, d):
        try:
            d = int(d)
        except:
            print("PWM: received invalid duty value:", d)
        else:
            self._duty = d
            self.port.duty(d) 
Example #19
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def set_freq(self, f):
        try:
            f = int(f)
        except:
            print("PWM: received invalid frequency value:", f)
        else:
            self._freq = f
            self.port.freq(f) 
Example #20
Source File: esp32_wrover_kit_rgb.py    From webthing-upy with MIT License 5 votes vote down vote up
def __init__(self, rPin, gPin, bPin):
        Thing.__init__(
            self,
            'urn:dev:ops:esp32-rgb-led-1234',
            'ESP32-RGB-LED',
            ['OnOffSwitch', 'Light', 'ColorControl'],
            'RGB LED on ESP-Wrover-Kit'
        )
        self.pinRed = machine.Pin(rPin, machine.Pin.OUT)
        self.pinGreen = machine.Pin(gPin, machine.Pin.OUT)
        self.pinBlue = machine.Pin(bPin, machine.Pin.OUT)
        self.pwmRed = machine.PWM(self.pinRed)
        self.pwmGreen = machine.PWM(self.pinGreen)
        self.pwmBlue = machine.PWM(self.pinBlue)
        self.redLevel = 50
        self.greenLevel = 50
        self.blueLevel = 50
        self.on = False
        self.updateLeds()

        self.add_property(
            Property(self,
                     'on',
                     Value(True, self.setOnOff),
                     metadata={
                         '@type': 'OnOffProperty',
                         'title': 'On/Off',
                         'type': 'boolean',
                         'description': 'Whether the LED is turned on',
                     }))
        self.add_property(
            Property(self,
                     'color',
                     Value('#808080', self.setRGBColor),
                     metadata={
                         '@type': 'ColorProperty',
                         'title': 'Color',
                         'type': 'string',
                         'description': 'The color of the LED',
                     })) 
Example #21
Source File: rgb.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def __init__(self, name, pinr, ping, pinb,
                 ignore_case=True, on_change=None,
                 report_change=False):
        port = (PWM(pinr), PWM(ping), PWM(pinb))
        RGB_Base.__init__(self, name, port,
                          ignore_case=ignore_case,
                          on_change=on_change,
                          report_change=report_change) 
Example #22
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def set_duty(self, d):
        try:
            d = int(d)
        except:
            print("PWM: received invalid duty value:", d)
        else:
            self._duty = d
            self.port.duty(d) 
Example #23
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def set_freq(self, f):
        try:
            f = int(f)
        except:
            print("PWM: received invalid frequency value:", f)
        else:
            self._freq = f
            self.port.freq(f) 
Example #24
Source File: led.py    From 1ZLAB_MicroPython_ESP32_Tutorial with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, led_id):
        # LED字典 
        # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
        led_list = [(2, False, True),(13, True, False)]

        if led_id >= len(led_list) or led_id < 0:
            print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
            return None
        
        gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
        self.pin = Pin(gpio_id, Pin.OUT)
        self.pwm = PWM(self.pin, freq=1000) 
Example #25
Source File: rgb.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def __init__(self, name, pinr, ping, pinb,
                 ignore_case=True, on_change=None,
                 report_change=False):
        port = (PWM(pinr), PWM(ping), PWM(pinb))
        RGB_Base.__init__(self, name, port,
                          ignore_case=ignore_case,
                          on_change=on_change,
                          report_change=report_change) 
Example #26
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def __init__(self, name, pin, freq=50, duty=0,
                 ignore_case=True, on_change=None,
                 report_change=False):
        self._duty = 0
        self._freq = freq
        Device.__init__(self, name, machine.PWM(pin, freq=freq, duty=duty),
                        setters={"freq/set": self.set_freq, "duty/set": self.set_duty},
                        getters={"freq": self.get_freq, "duty": self.get_duty},
                        ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change) 
Example #27
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def set_freq(self, f):
        try:
            f = int(f)
        except:
            print("PWM: received invalid frequency value:", f)
        else:
            self._freq = f
            self.port.freq(f) 
Example #28
Source File: rgb.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def __init__(self, name, pinr, ping, pinb,
                 ignore_case=True, on_change=None,
                 report_change=False):
        port = (PWM(pinr), PWM(ping), PWM(pinb))
        RGB_Base.__init__(self, name, port,
                          ignore_case=ignore_case,
                          on_change=on_change,
                          report_change=report_change) 
Example #29
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def __init__(self, name, pin, freq=50, duty=0,
                 ignore_case=True, on_change=None,
                 report_change=False):
        self._duty = 0
        self._freq = freq
        Device.__init__(self, name, machine.PWM(pin, freq=freq, duty=duty),
                        setters={"freq/set": self.set_freq, "duty/set": self.set_duty},
                        getters={"freq": self.get_freq, "duty": self.get_duty},
                        ignore_case=ignore_case,
                        on_change=on_change, report_change=report_change) 
Example #30
Source File: pwm.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def set_duty(self, d):
        try:
            d = int(d)
        except:
            print("PWM: received invalid duty value:", d)
        else:
            self._duty = d
            self.port.duty(d)