Skip to content

Commit

Permalink
Added output PWM example
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Oct 23, 2023
1 parent a8dac48 commit 50586cd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
7 changes: 7 additions & 0 deletions examples/modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Two Outputs](#two-outputs)
- [Multiple Outputs](#multiple-outputs)
- [Actioned Output](#actioned-output)
- [PWM Output](#pwm-output)
- [LED Strip Module](#led-strip-module)
- [Single Strip](#single-strip)
- [Multiple Strips](#multiple-strips)
Expand Down Expand Up @@ -74,6 +75,12 @@ A cycling pattern will be played on the attached outputs.
How to control a powered output from a Dual Output Module connected to Slot1, using a monitor action.


### PWM Output
[dual_output/pwm_output.py](dual_output/pwm_output.py)

How to control a powered output from a Dual Output Module connected to Slot1, using PWM.


## LED Strip Module

### Single Strip
Expand Down
41 changes: 41 additions & 0 deletions examples/modules/dual_output/pwm_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from machine import PWM
from pimoroni_yukon import Yukon
from pimoroni_yukon import SLOT1 as SLOT
from pimoroni_yukon.modules import DualOutputModule

"""
How to control a powered output from a Dual Output Module connected to Slot1, using PWM.
"""

# Constants
OUTPUT = DualOutputModule.OUTPUT_1 # The output from the Dual Switched Module to use
VOLTAGE_LIMIT = 12.1 # The voltage to not exceed, to protect the output
FREQUENCY = 8 # The frequency to run the PWM at (8 is the lowest supported)
# The highest is a result of the output's rise time of 2V/ms (e.g. 6ms for 12V)
PERCENT = 0.5 # The duty cycle percent of the PWM signal
SLEEP = 1.0 # The time to sleep between each reading

# Variables
yukon = Yukon(voltage_limit=VOLTAGE_LIMIT) # Create a new Yukon object, with a lower voltage limit set
module = DualOutputModule() # Create a DualOutputModule object

# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt)
try:
yukon.register_with_slot(module, SLOT) # Register the DualOutputModule object with the slot
yukon.verify_and_initialise() # Verify that a DualOutputModule is attached to Yukon, and initialise it

yukon.enable_main_output() # Turn on power to the module slots
module.enable(OUTPUT) # Enable a single output switch

pwm = PWM(module.outputs[OUTPUT], freq=FREQUENCY) # Create a PWM object to control the output
pwm.duty_u16(int(min(max(PERCENT, 0), 1) * 65535)) # Convert the duty cycle percent to a u16 value and set it

# Loop until the BOOT/USER button is pressed
while not yukon.is_boot_pressed():

# Monitor sensors for a number of seconds, recording the min, max, and average for each
yukon.monitored_sleep(SLEEP)

finally:
# Put the board back into a safe state, regardless of how the program may have ended
yukon.reset()
4 changes: 2 additions & 2 deletions lib/pimoroni_yukon/modules/dual_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ def is_enabled(self, output=None):

@property
def output1(self):
return self.output[0]
return self.outputs[0]

@property
def output2(self):
return self.output[1]
return self.outputs[1]

def read_power_good1(self):
return self.__power_good[0].value() == 1
Expand Down

0 comments on commit 50586cd

Please sign in to comment.