Welcome to Synthiam!

The easiest way to program the most powerful robots. Use technologies by leading industry experts. ARC is a free-to-use robot programming software that makes servo automation, computer vision, autonomous navigation, and artificial intelligence easy.

Get Started
United Kingdom
Asked — Edited

How To Use H-Bridge With Gradual Turns?

Hey Guys,

I've seen the tutorial video and the sample source code for controlling motors via a H-bridge, but all these have the motors being in 4 fixed states (5 if you count stopped) Fwd, Back, Left and Right. What I want to be able to do though is have a robot turn in a more gradual manor.

If you think of a joystick, pushing forward should get gradually faster and pushing to the side should turn gradually more and more. Combinations of the two should also be possible.

I was wondering then if anyone has any code examples of controlling such a device?

Many thanks



Upgrade to ARC Pro

Become a Synthiam ARC Pro subscriber to unleash the power of easy and powerful robot programming

Hey Matt,

Correct me if i'm wrong, but I think what you are talking about sounds like ramping? Instead of just going forward, it would start off slow and then over a specified time get up to full or a specified rate or speed.
I was just taking a look at the script commands, and i'm wondering if you could just use a Movement Panel and then add a script, using the Forward/Reverse/Left/Right commands it looks like you can set the speed, so maybe you could also add something like a for x = 0 to whatever in a loop?
United Kingdom
hey @charleybot ahh cool, I didn't notice the speed param, I'll give that a try. I guess the only thing I might have to work out is how to handle it moving forward whilst turning, I'm guessing it wouldn't allow me to call forward and left / right at the same time? Thanks for letting me know what it's called too, I didn't know what I should be searching for:)
United Kingdom
PWM on the ports connected to the HBridge will allow you to have for instance 100% on left motor forwards and 25% on right motor forwards, causing a long sweeping arc. 25% on right forward and 25% or left reverse would do a slow turn on the spot. Adjust the values to suit.
United Kingdom
So basically what I am hinting at is something like this.

If the joystick is pushed forwards then Y is 0 to 100 depending on how far forwards, backawards is 0 to -100, if left then X is 0 to -100 and right X is 0 to 100.

So forwards and right, you would have something from 0,0 to 100,100. forwards and left is 0,0 to -100,100. Backwards Right 0,0 to 100,-100 and Backwards Left 0,0 to -100,-100.

Assuming the HBridge is on D15, D16, D17 & D18. If positive then the ports D15 & D16 are used, PWM at the specified percentages. If it's negative then D17 & D18.

That's not quite right with the maths as top right would be 100,100 and go straight forwards but you get the idea I hope.

How to write that in C# is beyond me though... It shouldn't be too difficult.
I think this could be done with EZ-Script, if the forward/reverse/left/right commands have to be used with a movement panel, the something kinda like


For x = 1 to 100
pause 20
Forward [x]
next x

This would allow it to ramp up instead of just going, my omnibot 2000, it's too sudden to just go forward then try to stop. I know the syntax is wrong, but something like this would do it, just how to script it......
I am not a C programmer, but in the EZB script, have looked at doing the same thing. In the EZ-b script it is like this.

Assuming the Hbridge ports are connected as HB1 = D19, HB2 = D18, HB3 = D17, and HB4 = D16 you will also needs a pulse width, this is what controls the actual speed of the motors. In the Hbridge video, DJ solders this pulse width to both pulse width pins on the Hbridge. If you use two seperate wires one from D15 and one From D14, you can now change the individual motor speeds as well as control the 4 primary inputs. Thus we now have the 4 directions and the 2 speed signals to work with. To call them in the EZB script something like this, plus your own controls. I assume in C you dont need a move panel so you could actually refer directly to the HBridge 4 digitals apropriatly and then the 2 pulse signals. In doing this if you dont want to perform "on the spot" turns you could just adjust the two pulse signals to steer.
# --Codes starts here--
# Define the Variables
$MCL = D14 # this is the speed signal digital port to the left motor
$MCR = D15 # this is the speed signal digital port to the right motor
$SPDL = 10 # this sets the actual speed of the left motor as a starting speed of 10
$SPDR = 30 # this sets the actual speed of the right motor as the starting speed of 30
$SPDMAX = 100 # this is the max speed of the H bridge options are 0-100
$SPDRAMP = 250 # Sets the speed ramp to 250 ms in the increase speed loop

# the correct sintax is PWM(Digital port, Speed)
# Set the direction and speed
PWM ($MCL,$SPDL) # Sets the speed of the left to side to 10
PWM ($MCR,$SPDR) # Sets the speed of the right side to 30

# Put your sensory controls here


So now we have set the direction as forward and the right hand wheel to travel faster than the left, thus the droid would slowly ARC left even though we have given a forward command as the right hand wheel is traveling faster than the left. Alternatively set them both to 10 and both wheels will travel at the same speed

So you can see from this all we would need is a set of timers that increase the value in $SPDL and $SPDR every X amount of time
Add this next section to the above and you have a working pulse with HBRIDGE motor control, with full speed ramp capability.

If ($SPDL < $SPDMAX) # this is only looking at left speed so you may need to add code here if they are at different values.
$SPDL = $SPDL +10 # Takes the existing speed and adds a speed increase of 10
$SPDR = $SPDR +10 # Takes the existing speed and adds a speed increase of 10
Sleep($SPDRAMP) # Wait time before adjusting speed
PWM($MCL,$SPDL) # Set the new speed
PWM($MCR,$SPDR) # Set the new speed
goto (Increase_Speed)

I Have tested this, and it works to increase the speed by 10 every 250ms until max speed is reached. It could probably be tidied up a little but its a functional speed ramp.

You can also use the PWM individual outputs if you have a bot that naturally arcs due to slightly different wheel speeds, you can set logic to counter this by fixed values or wheel speed values if you have a wheel speed monitor.

You may need to put extra controls around this for your specific need but this in general works out fine.

Special thanks to Rich for recently informing me you nest a goto back to itself based on n if statement.