• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

pwm detach or set to 0?

Q&A Forum › Category: ESP32 › pwm detach or set to 0?
0 Vote Up Vote Down
anders jepsen asked 5 years ago

Hey.
I’m planning on trying to get an esp32 to control my beer-brewing machine.
It’s got 12 pinch valves in there, which are controlled by servos (mg996r).
They are set by a pwm signal, and originally – when opening or closing the valves, it runs a short 1-2 second period of providing 5v to the servo, and sending the pwm signal. After this, everything goes back down – both the 5v supply, and also the pwm signal. The open/close positions of the servo are neither in a fully open or fully closed position. (5833 is the closed posistion)
I’m trying to figure out what the best way is, to mimic this behaviour :
A:

  ledcAttachPin(16,3);
  ledcWrite(3,5833);
  delay(1000);
  ledcWrite(3,0);

Or B:

  ledcAttachPin(16,3);
  ledcWrite(3,5833);
  delay(1000);
  ledcDetachPin(16);

with B I do have to reattach the pin with every call, but does it use less memory or power??

3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Anders.

I’m sorry for taking so long to get back to you.

Your question was lost in so many other question we have here. I apologize for that.

I never did that experiment. But for what I read, servo motors are supposed to receive PWM signals continuously. If you detach PWM, it may hold the position, go to some random position or other unpredictable behavior.

However, I suggest that you experiment both codes and see how your motors behave. But I think the most “correct” way is option A.

Can you test it and then, tell me the results?

Regards,
Sara

0 Vote Up Vote Down
anders jepsen answered 5 years ago

Hey – thanks for your reply.
What I was afraid of, was that if I just use option A, then the servo would go to a a third position (corresponding to “0”).
I think what I will end up doing, is use another pin to turn the 5V supply for the servos on/off – then I should be safe.
Anyways, again thanks for your input 🙂
Regards
Anders

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Great!
Then, then tell me the results.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.