Example of the Arduino starter Program DC motor (2015-06-15)

Source: Internet
Author: User

Overview

Demonstrates the control of a DC motor.

Example program PWM control DC Motor

Skip the example of controlling motor turn-off, it is necessary to change the PWM here to digital port output high or low on the line.

// ----------------------------------------------------------------------------//Pwmmotor.ino// //Created 2015-06-11//by Seesea <seesea2517#gmail#com>// //PWM control DC motor Speed//In fact, the previous PWM control LED program, exactly the same, just the hardware plus a transistor used to drive the motor only////11 feet to the base of the NPN transistor, transistor collector motor One foot, the other foot of the motor to connect the external power positive, transistor emitter connected to the external power negative, Arduino GND also received transistor emitter// ----------------------------------------------------------------------------ConstUnsignedCharMotor = One;ConstUnsignedCharSensor =A0;voidSetup () {Pinmode (motor, OUTPUT); Pinmode (sensor, INPUT);}voidLoop () {//map the scope using the map functionAnalogwrite (motor, map (Analogread (sensor),0,1023,0,255));}

Last wiring diagram and:

H-Bridge Control DC motor
// ----------------------------------------------------------------------------//Hbridgemotor.ino// //Created 2015-06-11//by Seesea <seesea2517#gmail#com>// //H-Bridge Control DC motor Demo//control the direction of rotation of the DC motor via H-bridge//Turn clockwise for one second, turn counterclockwise for one second, stop for a second, repeat the aforementioned action////wiring is based on the actual circuit of the H-bridge. There was no ready-made chip, and an experiment with four transistors of rough H-bridge prototypes// ----------------------------------------------------------------------------ConstUnsignedCharPINMOTORCW =Ten;//H-bridge pin for control motor clockwise rotationConstUnsignedCharPINMOTORCCW = One;//H-bridge pin for control motor counterclockwise//Motor stop-turnvoidMotorstop () {Digitalwrite (PINMOTORCW, low); Digitalwrite (PINMOTORCCW, Low);}//Motor Clockwise TurnvoidMOTORCW () {digitalwrite (PINMOTORCW, high); Digitalwrite (PINMOTORCCW, Low);}//Motor Turn counterclockwisevoidMOTORCCW () {Digitalwrite (PINMOTORCW, low); Digitalwrite (PINMOTORCCW, High);}voidSetup () {Pinmode (PINMOTORCW, OUTPUT); Pinmode (PINMOTORCCW, OUTPUT);}voidLoop () {MOTORCW (); Delay ( +);    MOTORCCW (); Delay ( +);    Motorstop (); Delay ( +);}

Hand-built H-bridge, too messy to see an effect bar, it is recommended to use off-the-shelf chip directly.

The red and green two LEDs are then used to visually see the PWM and direction:

PWM + H-Bridge control DC motor

Simultaneous control of the direction and speed of the DC motor

// ----------------------------------------------------------------------------//Pwmhbridgemotor.ino// //Created 2015-06-11//by Seesea <seesea2517#gmail#com>// //PWM control DC motor via H-bridge//Control the speed of the motor and the direction of the motor//control motor clockwise gradually turn fast, then gradually slow, and then counterclockwise from slow turn fast again slow, and back to the clockwise rotation cycle////wiring is based on the actual circuit of the H-bridge. There was no ready-made chip, and an experiment with four transistors of rough H-bridge prototypes// ----------------------------------------------------------------------------ConstUnsignedCharPINMOTORCW =Ten;//H-bridge pin for control motor clockwise rotationConstUnsignedCharPINMOTORCCW = One;//H-bridge pin for control motor counterclockwise//Motor stop-turnvoidMotorstop () {Digitalwrite (PINMOTORCW, low); Digitalwrite (PINMOTORCCW, Low);}//the motor is rotated clockwise with the PWM value set by the parametervoidMOTORCW (unsignedCharPWM)    {Analogwrite (PINMOTORCW, PWM); Digitalwrite (PINMOTORCCW, Low);}//the motor turns counterclockwise with the PWM value set by the parametervoidMOTORCCW (unsignedCharPWM)    {Digitalwrite (PINMOTORCW, low); Analogwrite (PINMOTORCCW, PWM);}voidSetup () {Pinmode (PINMOTORCW, OUTPUT); Pinmode (PINMOTORCCW, OUTPUT);}voidLoop () {unsignedChari; //clockwise from slow to fast     for(i =0; I <255; ++i) {MOTORCW (i); Delay (Ten); }        //clockwise from fast to slow     for(i =255; i >0; --i) {MOTORCW (i); Delay (Ten); }    //counter clockwise from slow to fast     for(i =0; I <255; ++i) {MOTORCCW (i); Delay (Ten); }    //counter clockwise from fast to slow     for(i =255; i >0; --i) {MOTORCCW (i); Delay (Ten); }}

The simple H-bridge with the transistor to see an effect, it is recommended to use the ready-made chip directly.

The red and green two LEDs are then used to visually see the PWM and direction:

Example of the Arduino starter Program DC motor (2015-06-15)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.