Search code examples
craspberry-pipwmgeanywiringpi

How do I run the PWM with wiringPi?? (Issue with pwmMode PWM)


I am trying to run a PWM programm in geany (C language with a Raspberry 4B). I can compile and build the program; however, when I run it the next erro appears:

pinMode PWM: unable to do this when using /dev(gpiomem. Try sudo?

Does anyone have had experience something like this? How can I fix it?

My program is the following:

 #include <wiringPi.h>

 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>

 #define MFOR_MOTOR 25
 #define MBACK_MOTOR 24 
 #define PWM_MOTOR 1
 #define ENCODER_A 21
 #define ENCODER_B 22


 int main (void){

     if (wiringPiSetup()==-1)
     exit (1) ;

int pwm_user;
wiringPiSetup();
pinMode(MFOR_MOTOR, OUTPUT);
pinMode(MBACK_MOTOR, OUTPUT);
pinMode(PWM_MOTOR, PWM_OUTPUT);
pinMode(ENCODER_A, INPUT);
pinMode(ENCODER_B, INPUT);

printf ("Raspberry Pi wiringPi Motor PWM program\n") ;
printf("PWM from motor: ");
scanf("%d", &pwm_user); 
pwmWrite(PWM_MOTOR, pwm_user);
digitalWrite(MFOR_MOTOR, HIGH);

while(1){
        digitalRead(ENCODER_A);
}

digitalWrite(MFOR_MOTOR, LOW);
return 0;  
}

Solution

  • Thanks @Craig Estey for your help. I "fixed" the problem running the program directly to the terminal in the RaspberryPi so I used the next format for running it

    $ nano pwm_motor.c //for creating the programm
    $ gcc -Waöö pwm_motor.c -lwiring -o pwm_motor // for compile the programm
    $ chmod +x pwm_motor // for build the program
    $ sudo ./pwm_motor //for running the program
    

    Maybe is not the best way but it worked for me. if someone else knows another idea I love reading it.