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
  • bit_wrangler
    bit_wrangler over 2 years ago in reply to Derek (DCtoDaylight)

    const byte FWD = 0;
    const byte REV = 1;
    
    const byte H1 = 3; // PWM pin 3
    const byte H2 = 5; // PWM pin 5
    const byte L1 = 6; // PWM pin 6
    const byte L2 = 9; // PWM pin 9
    
    const unsigned long DWELLTIME = 2000; // time mS to hold at max speed
    const unsigned long PWMSTPTIME = 10; // mS delay between PWM changes
    const unsigned long STOPTIME = 25; // mS delay between direction changes
    const byte PWMMAXVAL = 255; // 8-bit PWM so 0 - 255
    
    
    byte direction = FWD;
    
    
    void stop() {
      analogWrite(H1, 0);
      analogWrite(H2, 0);
      analogWrite(L1, 0);
      analogWrite(L2, 0);
      delay(STOPTIME);
    }
    
    void motion(const byte dir, const byte duty) {
      static byte current_dir = 2; // keep track of current h-bridge direction, initialized to invalid state
    
      // Check for direction change (or first execution)
      if(dir != current_dir){
        stop();
        current_dir = dir;
      }
      
      switch(dir){
        case FWD:
          analogWrite(H1, duty);
          analogWrite(L2, duty);
         break;
        case REV:
          analogWrite(H2, duty);
          analogWrite(L1, duty);
          break;
      }
    }
    
    void ramp_pwm(const byte start, const byte end){
      if(start == end) return;
      const short step = end > start ? 1 : -1;
      
      // ramp profile
      for(short pwmvalue=start; pwmvalue != end; pwmvalue += step){
        motion(direction, pwmvalue);
        delay(PWMSTPTIME);
      }
    }
    
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
    
      // ramp up profile
      ramp_pwm(0, PWMMAXVAL);
    
      // dwell in plateau
      delay(DWELLTIME);
    
      // ramp down profile
      ramp_pwm(PWMMAXVAL, 0);
    
      // swap direction
      direction ^= 1;
    }

    Something like this, maybe.
    I tried to keep it relatively simple, but my sensibilities scream about using int when it's not necessary (or non-stdint types for that matter, but I compromised).

    • 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

    Ha. I cringed a little when I used int's as well, but it's a fine line - keeping a simple presentation vs. the most efficient way to write code.

    Anyway, I like your solution!

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

    Ha. I cringed a little when I used int's as well, but it's a fine line - keeping a simple presentation vs. the most efficient way to write code.

    Anyway, I like your solution!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
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