Interfase trouble with the raspberry pi 3 (Analog output)



  • Hello,

    I am having trouble with the analog output. I did supply the PWM signal at 400Hz from GPIO18. This PWM signal was measured to make sure that it was there and I tried differed duty cycles. Is there something else that I am missing. Should the jumper wires be in a specific setting or maybe something else?


  • administrators

    Hello @Jelle,
    can you please share how exactly did you proceed with setting up the PWM signal?

    Thanks,
    Martin



  • I use the wiringPi library so to run this code you should install wiringPi

    #include <stdio.h>
    #include <unistd.h>
    #include <stdint.h>
    
    #include "wiringPi.h"
    
    void gpioSetupCostum()
    {
    
      printf("gpioSetup has started\n");
      wiringPiSetupGpio ();
      usleep(1000);
      printf("wiringPiSetupGpio was a succes\n");
    
      pinMode(18, PWM_OUTPUT);
      usleep(1000);
      printf("pinMode was a succes\n");
    
      pwmSetMode(PWM_MODE_MS);
      usleep(1000);
      printf("pwmSetMode was a succes\n");
    
      pwmSetRange(1024);
      usleep(1000);
      printf("pwmSetRange was a succes\n");
    
      pwmSetClock(47);
      usleep(1000);
      printf("pwmSetClock was a succes\n");
      
    }
    
    void gpioClose()
    {
      pwmSetMode(PWM_MODE_BAL);
      usleep(1000);
      pinMode(18, INPUT);
      usleep(1000);
    }
    
    
    int main()
    {
      int GPIOCounter, SampleCounter;
      double PWM[11]={0, 1 , 2 , 3, 4, 5, 6, 7, 8, 9, 10};
      const int len = 11;	
      int AmountOfSamples = 200;  
      printf("Start setup\n");
      gpioSetupCostum();
      printf("Finish setup\n");
      
      printf("Start reading input ports\n");
      for(SampleCounter =1; SampleCounter <= AmountOfSamples; SampleCounter++){
        printf("Sample = %d\n", SampleCounter);
    	for(GPIOCounter = 0; GPIOCounter < len; GPIOCounter++){
    	  printf("PWM input = %.1f\n", PWM[GPIOCounter]);
    	  pwmWrite(18, (int)((PWM[GPIOCounter] * 1024)/10));
          usleep(1000000);
        }	
      }	
      
      gpioClose();
    }