logo elektroda
logo elektroda
X
logo elektroda

[Solved] ESP32-C3: SG90 servo and motor PWM conflict at 50Hz and 5000Hz

haslomiwygaslo 213 1
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 21369167
    haslomiwygaslo
    Level 4  
    Code: C / C++
    Log in, to see the code
    .
    Good morning, I'm in 2nd year of high school and I need help to solve a software problem on ESP32-C3 related to PWM and timers. The problem is that when I have the PWM from the servo (50Hz) and the 2×PWM to control the motor (5000Hz) initialized in the program, the servo stops working. As soon as I remove the initialisation and all commands in the program to send these 2 PWM from the motor, the servo works 100%. In the project I use 4 ADC(practically constant reading), 3 PWM(1 to servo and 2 to motor), 1GPIO INPUT(button), and i2c to oled. I tried making a separate program and a separate circuit on a contact board with just the servo and motor and to my surprise the servo works together with the motor, PWM signals are sent correctly to both things. Then following the working makeshift program I re-pasted the way the PWM signals are initialised and sent into the main code where all the rest of the components are and the servo stops working. I tried to remove from the oled code, unnecessary functions so that the signals no longer depend on the readings from the ADC but the servo started working the moment I removed the initialisation of the 2 PWM from the motor. The pins I use are the same in the working makeshift program as in the non-working main program. This allowed me to check if it was a hardware issue simply by uploading the working provisional program and the servo with motor worked. Each module (servo and motor) works fine separately (no initialisation or commands from the other module). When the servo doesn't work sometimes it just starts to oscillate strangely and unpredictably or move into quite random positions. (I'm sure the program is working correctly and not sending several strange messages to the servo at once or anything like that because I did a test where I had a couple of modes of servo setting depending on the button and displayed the current mode on the oledo and everything worked fine) I'm using a SG90 servo which accepts a 50Hz PWM and the filling of this PWM from 0.I am using the ESP32Servo.h library and generating the PWM for the servo using the PWM generating function and not the servo control function, because I want to be sure that the data sent to the servo is correct. I am attaching 2 programs, the main program in which the servo with the motor does not work and a makeshift program on which I tested successfully the simultaneous operation of the servo and the motor. And one more thing, when the servo is together with the motor, only the servo stops working, the motor works fine. I am powering the circuit from a laboratory power supply. Generally the module was supposed to track the light with 4 light sensors but I guess that doesn't matter. Thank you very much in advance for any help.
    AI: Can you provide the code snippets you use to initialise and control the PWM in the main program? .
    #include <ESP32Servo.h> // library initialisation
    ESP32PWM M1_PWM; // initialisation of pwm motor 1
    ESP32PWM M2_PWM; // initialization of pwm motor 2
    ESP32PWM S1; // initialisation of servo pwm

    void setup()
    {
    ESP32PWM::allocateTimer(0);
    ESP32PWM::allocateTimer(1);
    ESP32PWM::allocateTimer(2);
    ESP32PWM::allocateTimer(3);

    S1.attachPin(S_PWM, sfreq, 10);
    M1_PWM.attachPin(MOT_1, freq, 10);
    M2_PWM.attachPin(MOT_2, freq, 10);
    }
    S1.adjustFrequency(sfreq,0.12);//controlling the servo pwm, I know I could use the xx.writeScaled() function; but that was included in the library example program and both work.
    M1_PWM.adjustFrequency(freq, 1.0); //controlling pwm1 of the motor
    M2_PWM.adjustFrequency(freq, 1.0); //controlling pwm2 of the motor
    //I think that's it
    AI: What is the exact power setting for the servo and motor? Are they powered from the same source or separately? .
    They are powered from the same source, on the lab power supply I set 7.4V and the current limit to 500mA the maximum draw when tested was around 200mA. The 7.4V is converted to 5V by an L7805 linear regulator (MAX 1.5A) and from this regulator the servo motor and esp32c3 are powered, which has its own regulator at 3.3v with which its logic and oled are powered.
  • ADVERTISEMENT
  • Helpful post
    #2 21373142
    haslomiwygaslo
    Level 4  
    I managed to solve the problem, it turned out that it was a matter of the order in which the pins were assigned to the PWMs initialised early ie:
    It didn't work like that:

    S1.attachPin(S_PWM, sfreq, 10); //SERVO

    M1_PWM.attachPin(MOT_1, freq, 10); //SERVO
    M2_PWM.attachPin(MOT_2, freq, 10); //SERVO

    This is how it works:

    M1_PWM.attachPin(MOT_1, freq, 10); //SILNIK
    M2_PWM.attachPin(MOT_2, freq, 10); //SILNIK

    S1.attachPin(S_PWM, sfreq, 10); //SERVO

    I don't know why it's like this, I'm assuming there's a bug in the library, I hope this helps someone.
ADVERTISEMENT