Dtynin
Dtynin
DIIDevHeads IoT Integration Server
Created by Dtynin on 7/11/2024 in #code-review
Why isn't my AVR microcontroller controlling motor speed with PWM using Zephyr?
@Middleware & OS Hello guys, I'm attempting to control a motor's speed using PWM on my AVR microcontroller running Zephyr. I've set up the PWM peripheral and device tree configuration, but the motor does not respond to PWM signals. The PWM peripheral has been configured in prj.conf. I also created the device bindings in the device tree source file (dts), then wrote a code to generate PWM signals to control the motor. I expect the motor to change its speed in response to the PWM signals. Specifically, varying the duty cycle should result in corresponding changes in motor speed. The motor I'm using is a Mabuchi RS-540 series. The issue I'm having is that the motor is not responding to the PWM signals. What could be the reason for this, and how can I debug it? this is my code snippet:
#include <zephyr.h>
#include <device.h>
#include <drivers/pwm.h>
#include <logging/log.h>

LOG_MODULE_REGISTER(main);

#define MOTOR_PWM DT_NODELABEL(pwm0)

void main(void) {
const struct device *pwm_dev = DEVICE_DT_GET(MOTOR_PWM);

if (!device_is_ready(pwm_dev)) {
LOG_ERR("PWM device not ready");
return;
}

while (1) {
int ret = pwm_pin_set_usec(pwm_dev, PWM_PIN, 1000, 500, PWM_POLARITY_NORMAL);
if (ret) {
LOG_ERR("PWM set failed: %d", ret);
return;
}
k_sleep(K_MSEC(1000));
}
}
#include <zephyr.h>
#include <device.h>
#include <drivers/pwm.h>
#include <logging/log.h>

LOG_MODULE_REGISTER(main);

#define MOTOR_PWM DT_NODELABEL(pwm0)

void main(void) {
const struct device *pwm_dev = DEVICE_DT_GET(MOTOR_PWM);

if (!device_is_ready(pwm_dev)) {
LOG_ERR("PWM device not ready");
return;
}

while (1) {
int ret = pwm_pin_set_usec(pwm_dev, PWM_PIN, 1000, 500, PWM_POLARITY_NORMAL);
if (ret) {
LOG_ERR("PWM set failed: %d", ret);
return;
}
k_sleep(K_MSEC(1000));
}
}
5 replies