0

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;  
}
emmanuel
  • 5
  • 4
  • Just a guess: It looks like you don't have write access to `/dev/gpiomem`. Under linux (Raspian), you may have to run the program as `root`. Or, (as root) change the permissions on `/dev/gpiomem` (a security hole, but if you're the only one using the system, it may be okay). Try: `sudo ./myprogram` to run as root. – Craig Estey May 15 '22 at 20:50
  • Hi! thanks for your answer but you mean to run the programm direct in the terminal from the raspberrz pi??? – emmanuel May 15 '22 at 21:37
  • Yes, that's the simple way to start/test the program. When _debugged_, you could start the program from `init` (e.g. SysV init or [more likely] a `systemd` service). But, running under `sudo` lets you `open` the device (which the API calls do internally). That is, probably, `/dev/gpiomem` is _owned_ by root and probably has permissions `-rw-r--r--.` so an ordinary (non-root) user may not access it (Such access can be a sercurity issue, so only "authorized" users should have access). Since it's _your_ system, you can do what you want. – Craig Estey May 15 '22 at 21:43
  • also I read this: "This function has no effect when in Sys mode" but how do I know that? do you know how can I fixx it? – emmanuel May 15 '22 at 21:46
  • Beyond that, I think you'll have to look at some wiringpi docs for PWM (as what I suggested was very general). – Craig Estey May 15 '22 at 21:51
  • Thanks for your answer, yes I am the only one using the system. When I run ls -l /dev/gpiomem. The output is crw-rw---- 1 root gpio 245 0 May 15 23:17 /dev/gpiomem. So how could I change the permision? – emmanuel May 16 '22 at 07:14

1 Answers1

0

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.

emmanuel
  • 5
  • 4