After running the program, I observed the following behavior:
```
Before PWM setup: 43
After PWM setup: 0
```
This output suggests that GPIO 18 was initially in `ALT5` (PWM mode) as configured in the device tree. However, it was reset to `OUTPUT` mode (`func=0`) after calling `GPIO.setup(PWM_PIN, GPIO.OUT)`.
Here’s the relevant portion of my code:
```python
def setup_gpio():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# Print GPIO 18 state before initialization
print("Before PWM setup:", GPIO.gpio_function(PWM_PIN)) # Check GPIO function state
# Setup laser switch pin
GPIO.setup(RELAY_PIN, GPIO.OUT)
GPIO.output(RELAY_PIN, False) # Initial state is OFF
# Initialize PWM
global pwm
GPIO.setup(PWM_PIN, GPIO.OUT) # Setting this seems to overwrite ALT5 mode
pwm = GPIO.PWM(PWM_PIN, PWM_FREQUENCY)
pwm.start(0) # Start PWM with 0% duty cycle
# Print GPIO 18 state after initialization
print("After PWM setup:", GPIO.gpio_function(PWM_PIN)) # Check GPIO function state
```
I suspect the issue lies with `GPIO.setup(PWM_PIN, GPIO.OUT)`. Removing this line causes a `RuntimeError`, while including it resets the PWM configuration.
How can I ensure GPIO 18 remains in `ALT5` mode for PWM without triggering an error during initialization? Any advice would be appreciated!
```
Before PWM setup: 43
After PWM setup: 0
```
This output suggests that GPIO 18 was initially in `ALT5` (PWM mode) as configured in the device tree. However, it was reset to `OUTPUT` mode (`func=0`) after calling `GPIO.setup(PWM_PIN, GPIO.OUT)`.
Here’s the relevant portion of my code:
```python
def setup_gpio():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# Print GPIO 18 state before initialization
print("Before PWM setup:", GPIO.gpio_function(PWM_PIN)) # Check GPIO function state
# Setup laser switch pin
GPIO.setup(RELAY_PIN, GPIO.OUT)
GPIO.output(RELAY_PIN, False) # Initial state is OFF
# Initialize PWM
global pwm
GPIO.setup(PWM_PIN, GPIO.OUT) # Setting this seems to overwrite ALT5 mode
pwm = GPIO.PWM(PWM_PIN, PWM_FREQUENCY)
pwm.start(0) # Start PWM with 0% duty cycle
# Print GPIO 18 state after initialization
print("After PWM setup:", GPIO.gpio_function(PWM_PIN)) # Check GPIO function state
```
I suspect the issue lies with `GPIO.setup(PWM_PIN, GPIO.OUT)`. Removing this line causes a `RuntimeError`, while including it resets the PWM configuration.
How can I ensure GPIO 18 remains in `ALT5` mode for PWM without triggering an error during initialization? Any advice would be appreciated!
Statistics: Posted by dgy411852 — Fri Jan 03, 2025 11:06 pm