Page 1 of 1

Hardware PWM

Posted: Fri Jun 19, 2020 1:30 pm
by Syed Abdul Hayi
In my raspberry pi 4b, hardware PWM is not working. Please anyone help me with that.

I used the code below to run dc motor using L298N but it doesn't rotate.



#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//Hardware PWM available on GPIO12, GPIO13, GPIO18, GPIO19


#define dc_dir_foward 19 // IN3

#define dc_dir_backward 4 // IN4

#define dc_speed_pwm 13 //ENB - Hardware PWM

//*

int main()
{
if (wiringPiSetup() == -1)
exit(1);
wiringPiSetupGpio();
printf("1 \n");
pinMode(dc_speed_pwm, PWM_OUTPUT);
pinMode(dc_dir_backward, OUTPUT);
printf("2 \n");
pinMode(dc_dir_foward, OUTPUT);
pwmSetMode(PWM_MODE_MS);
pwmSetClock(192);
pwmSetRange(2000);
printf("3 \n");
int i=0;
delay(2000);
printf("4 \n");
while (i<3)
{
printf("5 \n");
digitalWrite(dc_dir_foward, HIGH);
digitalWrite(dc_dir_backward, LOW);
pwmWrite(dc_speed_pwm, 2000);
printf("Pereiname/n");
printf("6 \n");
delay(8000);
pwmWrite(dc_speed_pwm, 0);
digitalWrite(dc_dir_foward, LOW);
digitalWrite(dc_dir_backward, LOW);delay(3000);
printf("7 \n");
i=i+1;
}
}




Compiled using g++:
g++ -Wall -o try2 /home/pi/Desktop/try2.cpp -lwiringPi
sudo ./try2

Re: Hardware PWM

Posted: Fri Jun 19, 2020 2:14 pm
by B.Goode
Do you have a version of Wiring Pi that has been updated to be compatible with your RPi4B hardware, noting that the library has been deprecated by its own developer 10 months ago. http://wiringpi.com/wiringpi-deprecated/
please look at for alternative GPIO library for on-going projects.