Hi, I’m using a ESP32-C3 to control the speed of a model train. The program to get Forward and Reverse using center detent pot is easy. I use a H-Bridge as an interface and this is fed a 0-255 pwm signal but I’m trying to get inertia (Ramping) on increase and decrease of the 255. I’ve scanned the net and none of the “examples” work. Has anyone got any ideas. Thanks
Hi Alan
Yes I have some ideas.
Firstly your use of an H bridge. If you want to creep and ramp up slowly from a standstill I think you are going to have to use a DPDT relay to do the reversing.
There is a reason for this to do with the fact there is never a flywheel diode directly across the motor. One of them always gets to the motor via the power supply. The motor will rush to about 25% full speed as soon as it starts moving. It can then be slowed but that defeats the purpose a bit.
I did generate a thread some years ago on this with the offer to publish oscilloscope pics and provide detail but could not raise any interest. So I lost interest and dropped it. Suffice to say that I know why and that is the main thing. I did bring it up a few months ago in regard to someones problem but never did find out what the solution turned out to be.
I have not got the time right now as I am off to Brisbane until 01/08/25. While up there and if I can get access to a computer I will try to find the thread and link it.
Cheers Bob
PS. Where did you get the centre detent pot. Used to be around years ago but I though they must be pretty extinct now. Used to be able to get reverse log (single and ganged) but I have not sighted one of these for years.
Hi Bob, thanks for your reply. I got the centre detent pot from “HJW Electronics” in England and I’m using a VNH5019 H-Bridge, I detect the 512 mid range of 1024 to do the reversing The range 0 - 512 and the 512 - 1024 is converted to 0-255 by an expression.
Hey @Alan226529,
I agree that combining a DPDT relay for direction switching with PWM speed control in a single direction is an effective way to achieve smooth ramping and prevent sudden speed changes.
Could you share which example code or ramping methods you’ve tried with your current setup? Seeing what you’ve experimented with would help us provide more targeted advice or code suggestions.
Hi Ryan sorry I have not got back to you sooner.
Here is the bit of the script that changes direction (dir1 and dir2) . These two are applied to the H bridge while the motorsp is appled to the PWM input of the bridge.
The reason the zero point is not 512 is because the centre of the detent pot is not exactly half value.
if (Speed < 585) { // drive forward
dir1 = 1;
dir2 = 0;
motorsp = -255. / 512. * Speed + 255.;
} else if (Speed >= 595) { // drive reverse
dir1 = 0;
dir2 = 1;
motorsp = (255. / 512.) * Speed - 255.;
} else if (Speed >= 580 && Speed <= 595) {
motorsp = 0;
I’m trying to ramp the pwm value up or down depending on the change in the pot setting.
All the best Alan
I’m not sure I follow the logic exactly, but I don’t think that code will do what you want.
To implement acceleration then the speed should be changing from the old speed to the new speed at an increasing rate. Your code doesn’t seem to include the old speed (ie, before the new speed reading was taken) so there can’t be any ramping from one to the other. For instance, if the speed changed from 50 to 120 your steps might be something like 55 64 78 97 120
For linear acceleration the step increment is the step number (1 to 5 in the above example) multiplied by the unit increment. The unit increment is the speed difference (eg 120-50, or 70) divided by the sum of the step numbers (eg 1+2+3+4+5 or 15). So the first change is one of those 15 unit increments, the second change is 2 of them, and so on. The actual rate of acceleration then depends on the time delay between each step. The number of steps sets the smoothness.