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));
}
}
4 Replies
Dark AI
Dark AI5mo ago
@Dtynin Is your motor connected properly to the PWM pin on the microcontroller? Sometimes wiring issues can cause these kinds of problems and have you checked if the PWM peripheral is correctly defined in your device tree source file? Can you share that part of your dts file?
RED HAT
RED HAT5mo ago
@Dtynin what kind of values are you getting back from the ADC? Are they consistently off by a certain amount, or do they seem random?
Dtynin
Dtynin5mo ago
@RED HAT They seem pretty random. Sometimes they’re way higher than expected, other times much lower. It’s all over the place.
Dtynin
Dtynin5mo ago
@Dark AI Yeah, I have double-checked the wiring, and the motor is connected properly. don't know what seems to be the issue.
Want results from more Discord servers?
Add your server