element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
DC to Daylight
  • Challenges & Projects
  • element14 presents
  • DC to Daylight
  • More
  • Cancel
DC to Daylight
Documents How to Control a DC Motor using Optocouplers with an H-Bridge -- DC to Daylight 18
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join DC to Daylight to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: cstanton
  • Date Created: 14 Oct 2022 12:04 PM Date Created
  • Last Updated Last Updated: 19 Oct 2022 7:24 AM
  • Views 220177 views
  • Likes 7 likes
  • Comments 11 comments
Related
Recommended

How to Control a DC Motor using Optocouplers with an H-Bridge -- DC to Daylight 18

In this application/project based episode, we're going to put together an H-bridge which will allow us to control the speed and direction of a DC brushed motor. An Arduino will provide the PWM pulses and direction control. We'll also use optocouplers as the interface between the H-bridge and Arduino, which will mitigate noise and potentially dangerous voltages from damaging our IO pins.

Watch the Video

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

Supplemental Content

  • Schematics

element14 presents

element14 presents  | About Derek  |  DC to Daylight

  • h bridge
  • how to control a dc motor
  • h-bridge
  • pwm
  • dc to daylight
  • optocouplers
  • optoisolator
  • pulse with modulation
  • motor speed controller
  • Share
  • History
  • More
  • Cancel
  • Sign in to reply
Parents
  • Derek (DCtoDaylight)
    Derek (DCtoDaylight) over 2 years ago

    Hello All,

    It was brought to my attention in the YT comments that I professed about the need to avoid turning on same-side H/L transistors which would short the + and - rails, but didn't account for PQWM software timing which would cause an overlapping state.

    The function in question:

    void motion(int dir, int pwm)
    {
        if(dir == FWD)
        {
            analogWrite(H1, pwm);
            analogWrite(H2, 0);
            analogWrite(L1, 0);
            analogWrite(L2, pwm);
        }
        if(dir == REV)
        {
            analogWrite(H1, 0);
            analogWrite(H2, pwm);
            analogWrite(L1, pwm);
            analogWrite(L2, 0);
        }
    }

    Now that my error has been revealed,... I have a simple solution in mind. How would you handle it? What would you change about this function?

    -Derek

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • beacon_dave
    beacon_dave over 2 years ago in reply to Derek (DCtoDaylight)

    Do it in hardware. Don't most H bridges have an additional transistor on each side to prevent those 'magic smoke' situations from occurring?  

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Derek (DCtoDaylight)
    Derek (DCtoDaylight) over 2 years ago in reply to beacon_dave

    Yep. I've seen some clever approaches using additional hardware logic to prevent no-no situations... even reducing control lines. However, I didn't want to add another layer of complexity for presentation (though in this case, the software came back to bite me anyway). Maybe those additional transistors would have been a good feature after all. 

    -Derek

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Derek (DCtoDaylight)
    Derek (DCtoDaylight) over 2 years ago in reply to beacon_dave

    Yep. I've seen some clever approaches using additional hardware logic to prevent no-no situations... even reducing control lines. However, I didn't want to add another layer of complexity for presentation (though in this case, the software came back to bite me anyway). Maybe those additional transistors would have been a good feature after all. 

    -Derek

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • beacon_dave
    beacon_dave over 2 years ago in reply to Derek (DCtoDaylight)

    I can't seem to find the lecture that I was thinking of. I thought it was part of this one by Bruce Land at Cornell Uni: 

    https://youtu.be/WlU-uIC2LNk?list=PLD7F7ED1F3505D8D5&t=1218

    but it is missing the part I was thinking of that discussed various protection methods.  

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Derek (DCtoDaylight)
    Derek (DCtoDaylight) over 2 years ago in reply to beacon_dave

    Never control with software! I love it. This is a good tip for designing actual hardware that has the potential to lose the smoke.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • beacon_dave
    beacon_dave over 2 years ago in reply to Derek (DCtoDaylight)

    Perhaps just think of Therac-25 where reliance on software over hardware interlocks was allowed in the design and the horrific consequences that followed.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • bit_wrangler
    bit_wrangler over 2 years ago in reply to Derek (DCtoDaylight)

    I personally try do both - where possible. The software shouldn't tell the hardware to do something that is logically unsafe, and the hardware shouldn't assume that the software won't signal it to do something unsafe. The motion function in the sample I provided is actually a simple example of that. motion() was changed to only accept bytes, so it can rely on pwm never being out of range, and direction is handled in a switch that's completely ignoring invalid directions (the only side effect of such should be causing harmless extra 'stops'). It also doesn't rely on an external variable to keep track of current direction, so from the function's perspective, it can rely on no external actor accidentally modifying that variable.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Derek (DCtoDaylight)
    Derek (DCtoDaylight) over 2 years ago in reply to bit_wrangler

    Any function or method should have proper validation / abstraction / encapsulation - good programming practice... proper input validation for each and every function or method is also key. The demo code in the video is not the best example of 'good programming practice' - the goal was really to just show how the h-bridge works. Though a good takeaway from this discussion thread: Maybe it's better in making simple demos, not with the intent of making them appear more concise on screen, but more inline with how it'd be done in the real world.

    I can't speak for other industries, but at least in semiconductor, hardware has the final say - for Semi (S2?) standards, there must be an exclusion zone and any access points must be interlocked - hardware loop with redundant lines that kills servo if the loop is broken. Both signals must transition within x number of milliseconds or servo is also powered down immediately. One of the unwritten rules is, never enter the 'zone' - because you never know what the software could do - like in the Therac-25 thing, race condition causing unexpected behavior. Good read by the way.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube