Can VIM4 PWM output the width of pulses like the output of a servo tester?

Which version of system do you use? Khadas official images, self built images, or others?

No certain

Please describe your issue below:

Referring to the tutorial (VIM4 PWM [Khadas Docs]), I added “fdt_overlays=pwm_f”. Then, I input

echo 1 | sudo tee /sys/class/pwm/pwmchip4/export
echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period
echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle
echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable

on terminal. However, " Device or resource busy" always appears…

I want to use the Pin35 to output the width of pulses like a servo tester, e.g., 800 us. Could anyone teach me how to do the calculation? I have tried to search online, but I cannot find any clues for the units of period and duty cycle. I have no oscilloscope…

Post a console log of your issue below:


tee: /sys/class/pwm/pwmchip4/export: Device or resources busy

Hello @Athenachc

@Electr1 Can you help to check ?

Hello @Athenachc

The purpose of the export sysfs attribute, is to enable the PWM controller.

echo 1 | sudo tee /sys/class/pwm/pwmchip4/export

It only needs to be enabled once.

To enable or disable on the fly, you need to write to the enable attribute.
To enable,

echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable

To disable,

echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable

you can follow this.

khadas@Khadas:~$ # enable the pwm controller
khadas@Khadas:~$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/export
[sudo] password for khadas: 
1
khadas@Khadas:~$ # set the period time
khadas@Khadas:~$ echo 1000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period
1000000
khadas@Khadas:~$ # set the duty cycle
khadas@Khadas:~$ echo 500000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle
500000
khadas@Khadas:~$ # enable output from the pwm pin
khadas@Khadas:~$ echo 1 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
1
khadas@Khadas:~$ 
khadas@Khadas:~$ 
khadas@Khadas:~$ # modify the duty cycle
khadas@Khadas:~$ echo 200000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/duty_cycle
200000
khadas@Khadas:~$ # modify the period time
khadas@Khadas:~$ echo 2000000 | sudo tee /sys/class/pwm/pwmchip4/pwm1/period
2000000
khadas@Khadas:~$ # disable the output from the pwm pin
khadas@Khadas:~$ echo 0 | sudo tee /sys/class/pwm/pwmchip4/pwm1/enable
0
2 Likes

the units of duty period is the percent in ten-thousand-th percent
ie. 500000 as duty cycle corresponds to 500000/10000 = 50% duty cycle.
to find the value for duty cycle, multiply % with 10000.

The units of pwm period is in nanoseconds,
ie. 1000000 as the period time means frequency is 10^9/1000000 = 1000 Hz PWM
to find the value for the pwm period, divide 10^9 with desired frequency.

1 Like

Thank you so much for your help! I successfully read the PWM values from pin 35 by an ESP32 microcontroller.

#define pwm_cmd 2           //pwm input from vim4
int pwm_val;  //pwm reading from vim4

void setup() {
  Serial.begin(9600);
  pinMode(pwm_cmd, INPUT);
  Serial.print("start");

}

void loop() {
  pwm_val = pulseIn(pwm_cmd, HIGH);
  Serial.print("PWM input: ");
  Serial.println(pwm_val);
}

By adjusting the duty cycle, different “pwm_val” can be read. For instance, if 1999000 is the duty cycle, then pwm_val = 1999 (or 1998).

Thanks again for your kind help!