A simple example of a switch controlling a motor

This is an example circuit for motor control using a switch with the MSP430G2553:


[Download editable Inkscape SVG version]

The following program uses the switch input to control the direction of the motor. When the switch is pressed, the motor turns one way. When the switch is not pressed, the motor turns the other way.

Please note that this is not a complete Race to the Wall solution!!!

// Switch motor control example for MSP430G2553
// Written by Ted Burke - Last modified 8-10-2014

#include <msp430.h>

int main( void )
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
    P2DIR = 0b00000011; // P2.0-1 are outputs
    // Main loop repeats forever
        if ((P2IN & 0b00010000) > 0)
            // Motor goes one way
            P2OUT = 0b00000001;
            // Motor goes the other way
            P2OUT = 0b00000010;
    // The program never actually reaches this point
    return 0;
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s