Python RPi.GPIO.PWM Examples
The following are 30
code examples of RPi.GPIO.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
RPi.GPIO
, or try the search function
.
Example #1
Source File: move.py From Adeept_RaspTank with MIT License | 6 votes |
def setup():#Motor initialization global pwm_A, pwm_B GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(Motor_A_EN, GPIO.OUT) GPIO.setup(Motor_B_EN, GPIO.OUT) GPIO.setup(Motor_A_Pin1, GPIO.OUT) GPIO.setup(Motor_A_Pin2, GPIO.OUT) GPIO.setup(Motor_B_Pin1, GPIO.OUT) GPIO.setup(Motor_B_Pin2, GPIO.OUT) motorStop() try: pwm_A = GPIO.PWM(Motor_A_EN, 1000) pwm_B = GPIO.PWM(Motor_B_EN, 1000) except: pass
Example #2
Source File: kalipi.py From Kali-Pi with GNU General Public License v3.0 | 6 votes |
def screen_off(backlightControl): screen.canvas.fill(black) pygame.display.update() if backlightControl == "3.5r": process = subprocess.call("echo '0' > /sys/class/backlight/soc\:backlight/brightness", shell=True) elif backlightControl == "4dpi-24": process = subprocess.call("echo '0' > /sys/class/backlight/24-hat-pwm/brightness", shell=True) elif backlightControl == "pi70": process = subprocess.call("echo '1' > /sys/class/backlight/rpi_backlight/bl_power", shell=True) elif backlightControl == "hyperpixel4": process = subprocess.call("echo '1' > /sys/class/backlight/rpi_backlight/brightness", shell=True) else: backlight = GPIO.PWM(18, 0.1) backlight.start(0) process = subprocess.call("setterm -term linux -back black -fore white -clear all", shell=True) return() # Input loop for touch event
Example #3
Source File: three_color_led_gui.py From Mastering-GUI-Programming-with-Python with MIT License | 6 votes |
def __init__(self, red, green, blue, pinmode=GPIO.BOARD, freq=50): GPIO.setmode(pinmode) self.pins = { "red": red, "green": green, "blue": blue } for pin in self.pins.values(): GPIO.setup(pin, GPIO.OUT) # Turn all on and all off for pin in self.pins.values(): GPIO.output(pin, GPIO.HIGH) GPIO.output(pin, GPIO.LOW) self.pwms = dict([ (name, GPIO.PWM(pin, freq)) for name, pin in self.pins.items() ]) for pwm in self.pwms.values(): pwm.start(0)
Example #4
Source File: __init__.py From OctoPrint-Enclosure with GNU General Public License v3.0 | 6 votes |
def write_pwm(self, gpio, pwm_value, queue_id=None): try: if queue_id is not None: self._logger.debug("running scheduled queue id %s", queue_id) for pwm in self.pwm_instances: if gpio in pwm: pwm_object = pwm[gpio] old_pwm_value = pwm['duty_cycle'] if 'duty_cycle' in pwm else -1 if not self.to_int(old_pwm_value) == self.to_int(pwm_value): pwm['duty_cycle'] = pwm_value pwm_object.start(pwm_value) #should be changed back to pwm_object.ChangeDutyCycle() but this # was causing errors. self._logger.debug("Writing PWM on gpio: %s value %s", gpio, pwm_value) self.update_ui() if queue_id is not None: self.stop_queue_item(queue_id) break except Exception as ex: self.log_error(ex) pass
Example #5
Source File: 02_rgb_led.py From SunFounder_SensorKit_for_RPi2 with GNU General Public License v2.0 | 6 votes |
def setup(Rpin, Gpin, Bpin): global pins global p_R, p_G, p_B pins = {'pin_R': Rpin, 'pin_G': Gpin, 'pin_B': Bpin} GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location for i in pins: GPIO.setup(pins[i], GPIO.OUT) # Set pins' mode is output GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led p_R = GPIO.PWM(pins['pin_R'], 2000) # set Frequece to 2KHz p_G = GPIO.PWM(pins['pin_G'], 1999) p_B = GPIO.PWM(pins['pin_B'], 5000) p_R.start(100) # Initial duty Cycle = 0(leds off) p_G.start(100) p_B.start(100)
Example #6
Source File: squid.py From squid with MIT License | 6 votes |
def __init__(self, red_pin, green_pin, blue_pin): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) self.RED_PIN, self.GREEN_PIN, self.BLUE_PIN = red_pin, green_pin, blue_pin GPIO.setup(self.RED_PIN, GPIO.OUT) self.red_pwm = GPIO.PWM(self.RED_PIN, 500) self.red_pwm.start(0) GPIO.setup(self.GREEN_PIN, GPIO.OUT) self.green_pwm = GPIO.PWM(self.GREEN_PIN, 500) self.green_pwm.start(0) GPIO.setup(self.BLUE_PIN, GPIO.OUT) self.blue_pwm = GPIO.PWM(self.BLUE_PIN, 500) self.blue_pwm.start(0)
Example #7
Source File: squid.py From squid with MIT License | 6 votes |
def __init__(self, red_pin, green_pin, blue_pin): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) self.RED_PIN, self.GREEN_PIN, self.BLUE_PIN = red_pin, green_pin, blue_pin GPIO.setup(self.RED_PIN, GPIO.OUT) self.red_pwm = GPIO.PWM(self.RED_PIN, 500) self.red_pwm.start(0) GPIO.setup(self.GREEN_PIN, GPIO.OUT) self.green_pwm = GPIO.PWM(self.GREEN_PIN, 500) self.green_pwm.start(0) GPIO.setup(self.BLUE_PIN, GPIO.OUT) self.blue_pwm = GPIO.PWM(self.BLUE_PIN, 500) self.blue_pwm.start(0)
Example #8
Source File: squid.py From squid with MIT License | 6 votes |
def __init__(self, red_pin, green_pin, blue_pin): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) self.RED_PIN, self.GREEN_PIN, self.BLUE_PIN = red_pin, green_pin, blue_pin GPIO.setup(self.RED_PIN, GPIO.OUT) self.red_pwm = GPIO.PWM(self.RED_PIN, 500) self.red_pwm.start(0) GPIO.setup(self.GREEN_PIN, GPIO.OUT) self.green_pwm = GPIO.PWM(self.GREEN_PIN, 500) self.green_pwm.start(0) GPIO.setup(self.BLUE_PIN, GPIO.OUT) self.blue_pwm = GPIO.PWM(self.BLUE_PIN, 500) self.blue_pwm.start(0)
Example #9
Source File: rpi_gpio.py From pcaspy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def write(self, reason, value): status = True if reason == 'OUT': GPIO.output(PIN_OUT, value) elif reason == 'TRIG': GPIO.output(PIN_OUT, GPIO.HIGH) time.sleep(0.1) GPIO.output(PIN_OUT, GPIO.LOW) value = 0 elif reason == 'PWM': if value == 0: self.pwm.stop() else: self.pwm.start(self.getParam('DC')) elif reason == 'DC': self.pwm.ChangeDutyCycle(value) elif reason == 'FREQ': self.pwm.ChangeFrequency(value) else: status = False if status: self.setParam(reason, value) return status
Example #10
Source File: squid.py From squid with MIT License | 6 votes |
def __init__(self, red_pin, green_pin, blue_pin): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) self.RED_PIN, self.GREEN_PIN, self.BLUE_PIN = red_pin, green_pin, blue_pin GPIO.setup(self.RED_PIN, GPIO.OUT) self.red_pwm = GPIO.PWM(self.RED_PIN, 500) self.red_pwm.start(0) GPIO.setup(self.GREEN_PIN, GPIO.OUT) self.green_pwm = GPIO.PWM(self.GREEN_PIN, 500) self.green_pwm.start(0) GPIO.setup(self.BLUE_PIN, GPIO.OUT) self.blue_pwm = GPIO.PWM(self.BLUE_PIN, 500) self.blue_pwm.start(0)
Example #11
Source File: 23_ircontrol.py From SunFounder_SensorKit_for_RPi2 with GNU General Public License v2.0 | 6 votes |
def setup(): global p_R, p_G, p_B GPIO.setmode(GPIO.BCM) GPIO.setup(Rpin, GPIO.OUT) GPIO.setup(Gpin, GPIO.OUT) GPIO.setup(Bpin, GPIO.OUT) p_R = GPIO.PWM(Rpin, 2000) # Set Frequece to 2KHz p_G = GPIO.PWM(Gpin, 2000) p_B = GPIO.PWM(Bpin, 2000) p_R.start(100) p_G.start(100) p_B.start(100) err = pylirc.init("ircontrol", "./lircrc", blocking) print(err) if (err == 0): raise IOError("IR init error!")
Example #12
Source File: move.py From RaspberryCar with MIT License | 6 votes |
def __init__(self): GPIO_motor_1 = 18 # GPIO setting (BCM coding) GPIO_motor_4 = 23 GPIO_motor_5 = 24 GPIO_motor_6 = 25 GPIO.setup(GPIO_motor_1, GPIO.OUT) # GPIO input/output definiation GPIO.setup(GPIO_motor_4, GPIO.OUT) GPIO.setup(GPIO_motor_5, GPIO.OUT) GPIO.setup(GPIO_motor_6, GPIO.OUT) self.motor_1 = GPIO.PWM(GPIO_motor_1, 500) # PWM initialization: 500 Hz self.motor_4 = GPIO.PWM(GPIO_motor_4, 500) self.motor_5 = GPIO.PWM(GPIO_motor_5, 500) self.motor_6 = GPIO.PWM(GPIO_motor_6, 500) self.motor_1.start(0) # motors start self.motor_4.start(0) self.motor_5.start(0) self.motor_6.start(0)
Example #13
Source File: rc_control1.py From AutoRun-Car with GNU Lesser General Public License v3.0 | 6 votes |
def __init__(self): global pwm_ENA global pwm_ENB global delaytime GPIO.setup(ENA,GPIO.OUT,initial=GPIO.HIGH) GPIO.setup(IN1,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(IN2,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(ENB,GPIO.OUT,initial=GPIO.HIGH) GPIO.setup(IN3,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(IN4,GPIO.OUT,initial=GPIO.LOW) #设置pwm引脚和频率为2000hz pwm_ENA = GPIO.PWM(ENA, 2000) pwm_ENB = GPIO.PWM(ENB, 2000) pwm_ENA.start(0) pwm_ENB.start(0) #小车右转
Example #14
Source File: buzzer.py From rainbow-hat with MIT License | 6 votes |
def setup(): """Setup piezo buzzer.""" global _is_setup, pwm if _is_setup: return GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(BUZZER, GPIO.OUT) # Set up the PWM and then set the pin to input # to prevent the signal from being output. # Since starting/stopping PWM causes a segfault, # this is the only way to manage the buzzer. pwm = GPIO.PWM(BUZZER, 1) GPIO.setup(BUZZER, GPIO.IN) pwm.start(50) _is_setup = True
Example #15
Source File: __init__.py From explorer-hat with MIT License | 6 votes |
def blink(self, on=1, off=-1): """Blinks an LED by working out the correct PWM frequency/duty cycle @param self Object pointer. @param on Time the LED should stay at 100%/on @param off Time the LED should stay at 0%/off""" self.stop() if off == -1: off = on off = float(off) on = float(on) total = off + on duty_cycle = 100.0 * (on/total) # Use pure PWM blinking, because threads are ugly self.frequency(1.0/total) self.duty_cycle(duty_cycle) self.blinking = True return True
Example #16
Source File: control.py From rpi-film-capture with MIT License | 6 votes |
def __init__(self): GPIO.setmode(GPIO.BCM) GPIO.setup(self.dir_pin, GPIO.OUT) GPIO.setup(self.pulse_pin, GPIO.OUT) GPIO.setup(self.ms1_pin, GPIO.OUT) GPIO.setup(self.ms2_pin, GPIO.OUT) GPIO.setup(self.sleep_pin, GPIO.OUT) GPIO.setup(self.reset_pin, GPIO.OUT) dir=False GPIO.output(self.dir_pin, dir) GPIO.output(self.pulse_pin, False) GPIO.output(self.ms1_pin, True) GPIO.output(self.ms2_pin, False) GPIO.output(self.sleep_pin, False) GPIO.output(self.reset_pin, True) self.p1 = GPIO.PWM(self.pulse_pin, self.pulse_freq)
Example #17
Source File: 05_rgb.py From SunFounder_Super_Kit_V3.0_for_Raspberry_Pi with GNU General Public License v2.0 | 6 votes |
def setup(): global p_R, p_G, p_B # Set the GPIO modes to BCM Numbering GPIO.setmode(GPIO.BCM) # Set all LedPin's mode to output, # and initial level to High(3.3v) for i in pins: GPIO.setup(pins[i], GPIO.OUT, initial=GPIO.HIGH) # Set all led as pwm channel, # and frequece to 2KHz p_R = GPIO.PWM(pins['Red'], 2000) p_G = GPIO.PWM(pins['Green'], 2000) p_B = GPIO.PWM(pins['Blue'], 2000) # Set all begin with value 0 p_R.start(0) p_G.start(0) p_B.start(0) # Define a MAP function for mapping values. # Like from 0~255 to 0~100
Example #18
Source File: spm_manager.py From autopi-core with Apache License 2.0 | 6 votes |
def led_pwm_handler(frequency=None, duty_cycle=None): """ Change PWM frequency and/or duty cycle for LED. Optional arguments: - frequency (float): Change to frequency in Hz. - duty_cycle (float): Change to duty cycle in percent. """ ret = {} if frequency != None: led_pwm.ChangeFrequency(frequency) # Hz if duty_cycle != None: led_pwm.ChangeDutyCycle(duty_cycle) # % return ret
Example #19
Source File: sample_pwm.py From raspberry-gpio-emulator with Apache License 2.0 | 6 votes |
def main(): import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 50) try: p.start(0) for dc in range(0, 101, 5): p.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): p.ChangeDutyCycle(dc) time.sleep(0.1) finally: p.stop() GPIO.cleanup()
Example #20
Source File: drive.py From SDRC with GNU General Public License v3.0 | 6 votes |
def __init__(self): GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(self.MotorFront1, GPIO.OUT) GPIO.setup(self.MotorFront2, GPIO.OUT) GPIO.setup(self.MotorFront, GPIO.OUT) GPIO.output(self.MotorFront, 0) GPIO.setup(self.MotorBack1, GPIO.OUT) GPIO.setup(self.MotorBack2, GPIO.OUT) GPIO.setup(self.MotorBack, GPIO.OUT) GPIO.output(self.MotorBack, 0) self.BackPWM = GPIO.PWM(self.MotorBack,100) self.BackPWM.start(0) self.BackPWM.ChangeDutyCycle(0) self.direction = 0
Example #21
Source File: __init__.py From OctoPrint-Enclosure with GNU General Public License v3.0 | 5 votes |
def handle_pwm_linked_temperature(self): try: for pwm_output in list(filter(lambda item: item['output_type'] == 'pwm' and item['pwm_temperature_linked'], self.rpi_outputs)): gpio_pin = self.to_int(pwm_output['gpio_pin']) if self._printer.is_printing(): index_id = self.to_int(pwm_output['index_id']) linked_id = self.to_int(pwm_output['linked_temp_sensor']) linked_data = self.get_linked_temp_sensor_data(linked_id) current_temp = self.to_float(linked_data['temperature']) duty_a = self.to_float(pwm_output['duty_a']) duty_b = self.to_float(pwm_output['duty_b']) temp_a = self.to_float(pwm_output['temperature_a']) temp_b = self.to_float(pwm_output['temperature_b']) try: calculated_duty = ((current_temp - temp_a) * (duty_b - duty_a) / (temp_b - temp_a)) + duty_a if current_temp < temp_a: calculated_duty = 0 except: calculated_duty = 0 self._logger.debug("Calculated duty for PWM %s is %s", index_id, calculated_duty) elif self.print_complete: calculated_duty = self.to_int(pwm_output['duty_cycle']) else: calculated_duty = 0 self.write_pwm(gpio_pin, self.constrain(calculated_duty, 0, 100)) except Exception as ex: self.log_error(ex)
Example #22
Source File: driver.py From l293d with MIT License | 5 votes |
def drive_motor(self, direction=1, duration=None, wait=True, speed=100): """ Method called by other functions to drive L293D via GPIO """ self.check() if not speed: speed = 0 if isinstance(speed, int): # If speed is an integer, change it to a tuple speed = (speed, speed) # Unpack speed into PWM, this works even if a PWM tuple was passed in speed = PWM(*speed) if self.reversed: direction *= -1 if not Config.test_mode: if direction == 0: # Then stop motor self.pwm.stop() else: # Spin motor # Create a PWM object to control the 'enable pin' for the chip self.pwm = GPIO.PWM(self.motor_pins[0], speed.freq) # Set first direction GPIO level GPIO.output(self.motor_pins[direction], GPIO.HIGH) # Set second direction GPIO level GPIO.output(self.motor_pins[direction * -1], GPIO.LOW) # Start PWM on the 'enable pin' self.pwm.start(speed.cycle) # If duration has been specified, sleep then stop if duration is not None and direction != 0: stop_thread = Thread(target=self.stop, args=(duration,)) # Sleep in thread stop_thread.start() if wait: # If wait is true, the main thread is blocked stop_thread.join()
Example #23
Source File: PWMOut.py From Adafruit_Blinka with MIT License | 5 votes |
def frequency(self): """Get or set the PWM's output frequency in Hertz. Raises: PWMError: if an I/O or OS error occurs. TypeError: if value type is not int or float. :type: int, float """ return self._frequency
Example #24
Source File: grove_servo.py From grove.py with MIT License | 5 votes |
def __init__(self, channel): IO.setup(channel,IO.OUT) self.pwm = IO.PWM(channel,50) self.pwm.start(GroveServo.INIT_DUTY)
Example #25
Source File: PWMOut.py From Adafruit_Blinka with MIT License | 5 votes |
def duty_cycle(self): """Get or set the PWM's output duty cycle which is the fraction of each pulse which is high. 16-bit Raises: PWMError: if an I/O or OS error occurs. TypeError: if value type is not int or float. ValueError: if value is out of bounds of 0.0 to 1.0. :type: int, float """ return int(self._duty_cycle * 65535)
Example #26
Source File: motor_driver.py From autonomous-rc-car with MIT License | 5 votes |
def get_pwm_imstance(): """Returns a PWM instance""" return GPIO.PWM(BACK_MOTOR_ENABLE_PIN, PWM_FREQUENCY)
Example #27
Source File: kalipi.py From Kali-Pi with GNU General Public License v3.0 | 5 votes |
def screen_on(retPage, backlightControl): pygame.quit() if backlightControl == "3.5r": process = subprocess.call("echo '1' > /sys/class/backlight/soc\:backlight/brightness", shell=True) elif backlightControl == "4dpi-24": process = subprocess.call("echo '80' > /sys/class/backlight/24-hat-pwm/brightness", shell=True) elif backlightControl == "pi70": process = subprocess.call("echo '0' > /sys/class/backlight/rpi_backlight/bl_power", shell=True) elif backlightControl == "hyperpixel4": process = subprocess.call("echo '0' > /sys/class/backlight/rpi_backlight/brightness", shell=True) else: backlight = GPIO.PWM(18, 1023) backlight.start(100) GPIO.cleanup() if os.environ["KPPIN"] != "1": launch_bg=os.environ["MENUDIR"] + "launch-bg.sh" process = subprocess.call(launch_bg, shell=True) if os.environ["KPPIN"] == "1": page=os.environ["MENUDIR"] + "menu-pin.py" args = [page, retPage] else: page=os.environ["MENUDIR"] + retPage args = [page] os.execvp("python", ["python"] + args) # Turn screen off
Example #28
Source File: __init__.py From explorer-hat with MIT License | 5 votes |
def _setup_gpio(self): if self._gpio_is_setup: return self._gpio_is_setup = True setup_gpio(self.pin_fw, GPIO.OUT, initial=GPIO.LOW) setup_gpio(self.pin_bw, GPIO.OUT, initial=GPIO.LOW) self.pwm_fw = GPIO.PWM(self.pin_fw, 100) self.pwm_fw.start(0) self.pwm_bw = GPIO.PWM(self.pin_bw, 100) self.pwm_bw.start(0)
Example #29
Source File: __init__.py From explorer-hat with MIT License | 5 votes |
def _setup_gpio(self): if self._is_gpio_setup: return True setup_gpio(self.pin, self.mode) self.gpio_pwm = GPIO.PWM(self.pin, PULSE_FREQUENCY) self.gpio_pwm.start(0)
Example #30
Source File: 10_passive_buzzer.py From SunFounder_SensorKit_for_RPi2 with GNU General Public License v2.0 | 5 votes |
def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(Buzzer, GPIO.OUT) # Set pins' mode is output global Buzz # Assign a global variable to replace GPIO.PWM Buzz = GPIO.PWM(Buzzer, 440) # 440 is initial frequency. Buzz.start(50) # Start Buzzer pin with 50% duty ration