1. Do not share user accounts! Any account that is shared by another person will be blocked and closed. This means: we will close not only the account that is shared, but also the main account of the user who uses another person's account. We have the ability to detect account sharing, so please do not try to cheat the system. This action will take place on 04/18/2023. Read all forum rules.
    Dismiss Notice
  2. For downloading SimTools plugins you need a Download Package. Get it with virtual coins that you receive for forum activity or Buy Download Package - We have a zero Spam tolerance so read our forum rules first.

    Buy Now a Download Plan!
  3. Do not try to cheat our system and do not post an unnecessary amount of useless posts only to earn credits here. We have a zero spam tolerance policy and this will cause a ban of your user account. Otherwise we wish you a pleasant stay here! Read the forum rules
  4. We have a few rules which you need to read and accept before posting anything here! Following these rules will keep the forum clean and your stay pleasant. Do not follow these rules can lead to permanent exclusion from this website: Read the forum rules.
    Are you a company? Read our company rules

Tutorial Arduino code for 4DOF and 1 DIRpin Hbridge board

Discussion in 'DIY Motion Simulator Projects' started by RacingMat, Aug 7, 2013.

  1. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Hello,

    1 PWMpin & 1 DIRpin H-bridge board
    I've purchase H-bridge board to drive my motors in 24V but as you can see, there is 1 pin for pwm and 1 pin for direction!!
    (25$ instead of 80$ for my Motomonster board @sparkfun https://www.sparkfun.com/products/10182 and 12V max).

    Hbrige 1pinDir 1pinPWM.JPG ebay
    (this ebay Hbridge is not good, too trials ended into smoke, choose Pololu instead for example).

    As you can see, there is 1 pin for pwm and 1 pin for direction!!
    -> in this topic you'll find the 2DOF code adapted to 4 DOF and 1 pins Hbridge boards.

    edit 25 aug :
    successfully tested with Pololu 755 single Hbridge driver boards = but 40 $ per motor ...
    5.5 to 30 V voltage range and is efficient enough to deliver a continuous 15 A without a heat sink.
    http://www.pololu.com/catalog/product/755

    0J1022.600.jpg

    0J1025.600.jpg

    if you have a 2 pins Dir HBridge (like Sparkfun Motomonster),
    INA1 INB1 PWM1 -> motor #1 OUTA1, OUTB1
    INA2 INB2 PWM2 -> motor #2 OUTA2, OUTB2
    use the original code
    here in resources section

    Arduino Code for 4 DOF and 1 pins Hbridge boards
    You can download the source code in the resources section!

    You only have to open this arduino sketch (.ino) in the Arduino IDE and upload it into the Arduino board!

    Arduino setup
    - install manually the drivers (see the Arduino's specific documentation for Windows Seven installation).
    - After complete installation, find in the Windows system on which COM Port it has been installed : you will need to put this information in SimTools / Interface Settings.
    20130421140258-RacingMat-port-Com-Arduino-1.PNG

    - Double clic on Arduino's line and adjust the default speed :
    20130421140309-RacingMat-port-Com-Arduino-2.PNG

    - In the Arduino plateform, put the right COM Port : if not, you won't be able to upload your program into the Arduino...
    20130421140317-RacingMat-port-Com-Arduino-3.png

    - Open the Serial Monitor (magnifying lens on top right ).
    20130421140325-RacingMat-port-Com-Arduino-4.png

    - Adjust here as well the same default speed :
    20130421140334-RacingMat-port-Com-Arduino-5.PNG

    Don't mess within the code ;-) or make everything coherent everywhere
    Code:
    ////////////////////////////////////////////////////////////////////////////////
    // INITIALIZATION
    ////////////////////////////////////////////////////////////////////////////////
    void setup()
    {
      // serial initialization
      Serial.begin(115200);
    First hardware tests and WIRING

    First : pot are not mounted onto the motor
    you can see the expected behaviour in this video

    and please find the explanation of the code here :

    The code is written to put the simulator on middle position at start :
    /* init position value*/
    int DataValueL=512; //middle position

    - if your pot is fully turned to max, as Arduino reads potentiometer value as motor position, Arduino believes that motor is in max position.
    - but the order is to be in middle position
    - so the Arduino will ask the motor to go reverse in order to go to middle position
    - your motor is turning
    - but while your pot keeps still, the motor will turn non stop !

    - when you manually turn the pot backward to min, Arduino reads that motor is in min position
    - but the order is still middle position, so Arduino will tell the motor to go forward
    - motor runs non stop until the pot is in middle position

    In the code, you have a tolerance parameter : when the pot is around middle position (<middle+tolerance and >middle-tolerance), the motor stops.
    You can broader Tolerance value to stop your motor more easily manually.

    Second : now checked the wiring
    Once you have played in this manner, now checked the wiring
    simulate the pot mounted onto the motor (back to back or alongside with gears...)
    reduce the speed (pwmMax) to see the direction of motor rotation
    and check if Arduino will behave as expected when you turn the pot

    if your pot is mounted this way :
    20130413154729-RacingMat-montage-potentiomy-tre-2-.JPG

    when you turn in a direction, motor has to go the opposite way.
    If not, invert motor wiring or pot wiring.

    Warning ! if you have gears between pot and motor, you'll have to twist your mind :eek: to check the correct behaviour.

    Third : mount the pot onto the motor, the behaviour will be the following :
    - motor goes to middle position and stop
    - if you send order to Arduino via Serial Monitor (say R7FLAE) the left motor will move and stop at his new position ! right motor didn't move.
    - etc...

    2- calibrating the pot boundaries in the Arduino
    const int potMiniRorL=300;
    const int potMaxiRorL=700;

    these values are convenient as they will always constrain the values whatever comes from the simulator software,
    so the arduino will never send over-range value to the motors :) and your hardware will be protected !

    Now without any game running ! send orders from Arduino serial monitor :

    type the following strings in Hexa and press Enter:
    R7FL7F
    -> it should move the playseat in horizontal position

    increase the angulation
    R64L64
    go farther cautiously
    R3FL3F
    if the playseat moves too little, then the potMini and PotMaxi are too close to middle position
    and maybe go to max now : max backward tilt -> R01L01

    - if it's too litlle check how much you can go further and decrease potMiniRorL, reupload and resend R01L01
    and so on, until you found the pot minimal position

    - do the same for potMaxiRorL, send RFFLFF and increase potMaxiRorL, reupload and resend RFFLFF

    (write somewhere else these values that are associated to your particular playseat hardware).

    3- find your pot amplifying ratio
    - increase now the gearMotorRorLPotRatio (line 17)
    - by default, it's equal to 1
    - if you increase to 1,5 : the range will be increased by 50 %

    no worries, if the amplifying ratio is too high :
    - the playseat will not bump over the limits
    - but the mouvments will be fast or jittering

    Now, you can play with your DOFs :
    type the following strings in Hexa and press Enter:
    R7FL7F
    -> it should move the playseat in horizontal position

    increase the angulation
    R64L64
    go farther
    R3FL3F
    max backward tilt -> R01L01

    R9BL9B
    RC1LC1
    RFFLFF (Max !!)
    -> it should tilt the playseat forward and a bit further

    for rolling movment, you have to mix the orders this way :
    R3FLC1
    R01LFF (Max !!)
    -> roll left

    RC1L3F
    RFFL01 (Max !!)
    -> roll right

    SimTools Setup
    - In SimTools : set the same ComPort and same Speed
    arduino Setup 6.png

    - my Arduino code needs 8bits Hex data : just setup like this for 1 bridge and 2 motors
    Com Output SimTools.JPG

    - startup and stop just send the instructions to come to middle
    - and 7F in hex equals 127 in dec

    if you want to use 4 motors :
    R7FL7FY7FW7F
    R<Axis1>L<Axis2>Y<Axis3>W<Axis4>
    R7FL7FY7FW7F

    if you want to use winds motor :
    R7FL7FY7FW0 (no wind at startup or end !!)
    R<Axis1>L<Axis2>Y<Axis3>W<Axis4>
    R7FL7FY7FW0
    For fun, the code is written in 3 DOF configuration + wind motor... but it works for 4 DOF.

    Wiring Setup
    Here are some high res pictures for plugging !

    1 pins Hbridge and Arduino plugging (1).JPG

    1 pins Hbridge and Arduino plugging (2).JPG

    1 pins Hbridge and Arduino plugging (3).JPG

    1 pins Hbridge and Arduino plugging (4).JPG

    First run
    - between your motors and the HBridge, you have inserted Fuses for safety

    - but if you still wonder about the current needed by your motors under load
    and if you want to protect your HBridge ;D
    you can use the following functionnality inside my Arduino software :
    reduce (scale down in fact) the motors's speed -> it will limit the intensity !

    modify this line and try : 150 instead of 255 !
    #define pwmMax 255 // or less, if you want to lower the maximum motor's speed

    - measure the intensity and increase gradually max speed

    Mat

    With 2 motomonster and 1 arduino UNO or Nano, control 4 motors!!
    arduino et 2 motomonster.png
    • Like Like x 21
    • Informative Informative x 2
    • Optimistic Optimistic x 1
    Last edited: May 10, 2014
  2. RaceRay

    RaceRay Administrator Staff Member SimAxe Beta Tester

    Joined:
    Nov 8, 2006
    Messages:
    4,656
    Occupation:
    Self-employed | Web and application development
    Location:
    Hamburg, Germany
    Balance:
    23,864Coins
    Ratings:
    +2,007 / 13 / -0
    My Motion Simulator:
    2DOF, DC motor, SimAxe, SimforceGT
    Hi Mat,

    thats extremely cheap.
    Do you had luck at fleabay or where? :) Thanks for sharing your source here.

    Would love it also to see your complete project here;)

    Cheers,
    René
  3. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Hello !

    I've updated the post with the links : yes, it's ebay !

    my 3DOF project is only at the beginning now :)
    it will be a traction loss design ! 24V wiper motors and arduino, of course !

    My previous 2DOF design + a traction loss base :
    the classical approach just like the addon Simxperience simxperience.com/products

    Mat
    • Old Old x 1
    Last edited: Sep 16, 2013
  4. aarondc

    aarondc Member

    Joined:
    Jun 12, 2013
    Messages:
    133
    Location:
    Melbourne, Australia
    Balance:
    191Coins
    Ratings:
    +7 / 0 / -0
    What is a wind motor?
  5. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    @aarondc : it's a fan motor for blowing wind while driving !
    As I bought 2 drivers board, I can drive 4 motors but I only need 3 !! So i figured out how to use the fourth :-D

    @Raceray : I open a new post with my complete 2DOF project 2dof-xsim-arduino-wiper-playseat-t4616.html#p44019
    For my new 3DOF project, I wish I could beta-test SimmTools.
  6. aarondc

    aarondc Member

    Joined:
    Jun 12, 2013
    Messages:
    133
    Location:
    Melbourne, Australia
    Balance:
    191Coins
    Ratings:
    +7 / 0 / -0
    A fan! What a great idea :D
  7. ferslash

    ferslash Active Member

    Joined:
    Feb 8, 2011
    Messages:
    495
    Balance:
    4,798Coins
    Ratings:
    +181 / 2 / -0
    wooow seams interesting... where did you get the wipers that runs at 24v? what car are from ?

    how are you planning to feed them? a special power source or how?

    nice project :D
  8. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
  9. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Hi !

    After some PM requests, I have updated the firt post of this topic with Arduino setup screenshots and SimTools Setup screenshot.

    I'm glad ozyy53 has succeeded in using it ! :hi:

    Mat
  10. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,161
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,201Coins
    Ratings:
    +2,164 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF
    Mat, my own Chinese driver boards arrived today (same as yours). I am curious, what are the rest of the commands that you had Simtools send to the arduino?

    Specifically:

    Output range: 8 bits?, Decimal?

    Startup output, Interface output, and Shutdown output?

    Thanks much for working on this.

    I am guessing that Interface-Output would be 1<Axis1>~0<Axis2>~ ?
  11. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Hi Historiker !

    I'm editing my first post to give you the informlation you need ;-)
  12. eaorobbie

    eaorobbie Well-Known Member SimTools Developer Gold Contributor

    Joined:
    May 26, 2009
    Messages:
    2,574
    Occupation:
    CAD Detailer
    Location:
    Ellenbrook, Western Australia
    Balance:
    20,440Coins
    Ratings:
    +1,684 / 23 / -2
    My Motion Simulator:
    2DOF, DC motor, JRK, SimforceGT, 6DOF
    Ya quick RacingMat , did I get it right. :clap:

    So to answer your question
    Start up centres both axis. R7FL7F - Sim Tools R<7F>L<7F>
    Send packet - Sim Tools R<Axis1>L<Axis2>
    End packet is the same as the start no turning off command , mmm

    [strike]I would tend to use 127 instead of 7F which is equal to 127 but not sure havent taken a close look at his code[/strike].
    Didnt read close enough your using hex output for value
    Correct me if wrong, I haven't plugged a Ard in to test it.
  13. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    you're right ! :thbup:

    - startup and stop just send the instructions to come to middle
    - and 7F in hex equals 127 in dec

    if you want to use 4 motors :
    R7FL7FY7FW7F
    R<Axis1>L<Axis2>Y<Axis3>W<Axis4>
    R7FL7FY7FW7F


    if you want to use winds motor :

    R7FL7FY7FW0 (no wind at startup or end !!)
    R<Axis1>L<Axis2>Y<Axis3>W<Axis4>
    R7FL7FY7FW0

    Here are some high res pictures for plugging !
    1 pins Hbridge and Arduino plugging (1).JPG

    1 pins Hbridge and Arduino plugging (4).JPG

    1 pins Hbridge and Arduino plugging (2).JPG

    1 pins Hbridge and Arduino plugging (3).JPG
  14. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,161
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,201Coins
    Ratings:
    +2,164 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF
    Thanks gents! I made a quick trip (ok, 5 hours) and picked up the rest of my wiring and a spare Arduino so tomorrow I will do a test run.
  15. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,161
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,201Coins
    Ratings:
    +2,164 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF
    Mat, I have two of the Chinese motor controllers. Is it possible to split the PWM and DIR and input for one motor to both sides of the motor driver? Then attach a single motor to both motor outputs? Would this allow a higher amperage output to the motor than a single side would?
  16. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Arduino code for 4DOF and 1 PWMpin & 1 DIRpin Hbridge board

    Hum, it is not written in the specs, so i wouldn't advise you to test that !!
    You are worried that much about intensity ?

    Have you tried some more basic tests ?
  17. Historiker

    Historiker Dramamine Adict Gold Contributor

    Joined:
    Dec 16, 2010
    Messages:
    2,161
    Occupation:
    Retired
    Location:
    Michigan USA
    Balance:
    9,201Coins
    Ratings:
    +2,164 / 19 / -1
    My Motion Simulator:
    3DOF, DC motor, Arduino, Motion platform, 6DOF
    Hi Mat, thanks for the quick response.

    Since I have two boards and am only using 2DoF I just wondered if using both outputs from one board per motor would reduce the load on the motor driver. Although I am putting 15amp fuses between the motor drivers and the motors I thought this might give me a further safety cushion.

    Aarondc suggested that I do this when I posted which drivers that I had planned on using. I am just not sure how to wire them in this manner.
  18. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Arduino code for 4DOF and 1 PWMpin & 1 DIRpin Hbridge board

    Keep it simple and safe !
    The KISS idea is :
    - use 1 HBridge
    - use your fuse
    - limit the speed in the arduino program :)

    I have updated my first post with this instruction :

    you can use the following functionnality inside my Arduino software :
    reduce (scale down in fact) the motors's speed -> it will limit the intensity !

    1- modify this line and try : 150 instead of 255 !

    #define pwmMax 255 // or less, if you want to lower the maximum motor's speed

    2- measure the intensity and increase gradually max speed
    Mat
  19. prilad

    prilad Well-Known Member SimAxe Beta Tester SimTools Developer

    Joined:
    Apr 29, 2012
    Messages:
    380
    Location:
    Dubna, Russia
    Balance:
    9,638Coins
    Ratings:
    +512 / 1 / -0
    My Motion Simulator:
    3DOF, DC motor, Arduino, 4DOF
    Sorry to interfere in your discussion, but I strongly recommend not to connect the one motor parallel to two different bridge circuits.
    Since drivers are never exactly equal, there is a greater probability of passing through current, which in turn can lead to failure of the h-bridge mosfets.

    Regards...
    Ale
    • Informative Informative x 1
  20. RacingMat

    RacingMat Well-Known Member Gold Contributor

    Joined:
    Feb 22, 2013
    Messages:
    2,236
    Location:
    Marseille - FRANCE
    Balance:
    20,975Coins
    Ratings:
    +2,089 / 21 / -2
    My Motion Simulator:
    2DOF, DC motor, Arduino
    Arduino code for 4DOF and 1 PWMpin & 1 DIRpin Hbridge board

    Thanks ! You're very welcome with all your electronic knowledge :)
    It's better with your words !