Introduction: Controlling a DC Motor With Arduino
Lately, I've been working on a wireless remote controller for a Robotic Arm. Most of everything is planned out, but I don't have all the parts and shields yet, so I decided to begin prototyping with a single motor. I am using two Xbee radios for wireless communication. As there are 5 motors and an LED, there aren't enough digital pins for me to communicate in that way, so am using Serial communication from Arduino 1 > Xbee 1 > Xbee 2 > Arduino 2.
So, I decided to make simulations of this but with one wired Arduino. In this Instructable I will be demonstrating how to change a DC motor's direction, using both Serial Commands and commands from buttons. Let's get started, shall we?
Step 1: Setup
Software
In this step, I will provide a parts list and a link to download the Arduino software. I used Arduino Create for this project. You can do the same thing with the Arduino program, which is available for download here.
Parts List:
- Arduino Uno (other boards may be used)
- Standard DC motor
- L293DNE H-Bridge
- 2 Push Buttons
- Jumper Wires
- Breadboard
All of these items can be easily bought online for a very cheap price.
Step 2: Serial Communication - the Wiring
First, let's work on the Serial communication. Wire up the Arduino as demonstrated.
Step 3: Serial Communication - the Code
Now that you have your Arduino wired up, copy and paste this code into the Arduino IDE. What this code does is read for a signal that you would manually type into the Serial monitor. When either 1 or 2 is entered, the motor would turn either clockwise or counterclockwise for a short period of time. Experiment a little! Type in multiple 1's or 2's and see what happens!
int in1pin = 6;
int in2pin = 7; // connections to H-Bridge, clockwise / counterchar receivedChar; // store info boolean newData = false; // create a true/false statement
void setup() {
pinMode(in1pin, OUTPUT); pinMode(in2pin, OUTPUT); // set pins to OUTPUTS
Serial.begin(9600); // start up serial communication }
void loop() {
recvData(); // read and store data
moveMotor(); // move motor according to data and then reset }
void recvData() {
if (Serial.available() > 0) { // if the serial monitor has a reading
receivedChar = Serial.read(); // set char to be what is read newData = true; // make statement true } }
void moveMotor() {
int motordirection = (receivedChar - '0'); // turn recieved data into usable form and give it a name
while(newData == true) {
Serial.println(motordirection); // print motor direction
if (motordirection == 1) { // if it reads 1...
digitalWrite(in1pin, HIGH); // turn motor one way digitalWrite(in2pin, LOW);
delay(250); }
else if (motordirection == 2) { // if it reads 2...
digitalWrite(in1pin, LOW); // turn motor other way digitalWrite(in2pin, HIGH);
delay(250); }
else { // if nothing is read
digitalWrite(in1pin, LOW); // motor is off digitalWrite(in2pin, LOW); }
newData = false; // reset value to false } }
Step 4: Button Commands - the Wiring
For button communication, add buttons as shown.
Step 5: Button Commands - the Code
Now, make a new sketch and copy and paste this code, and play around.
int in1pin = 6;
int in2pin = 7; // h bridge pinsint leftButton = 8; int rightButton = 9; // buttons
void setup() {
pinMode(in1pin, OUTPUT); pinMode(in2pin, OUTPUT); // outputs
pinMode(leftButton, INPUT_PULLUP); pinMode(rightButton, INPUT_PULLUP); // inputs w internal pullup resistors }
void loop() {
int leftPinState = digitalRead(leftButton); int rightPinState = digitalRead(rightButton); // set value names for read data
if (leftPinState == LOW) { // if left button is pressed ...
digitalWrite(in1pin, HIGH); // make motor go one way digitalWrite(in2pin, LOW); }
else if (rightPinState == LOW) { // if right button is pressed ...
digitalWrite(in1pin, LOW); digitalWrite(in2pin, HIGH); // make motor go other way
}
else { // if neither button is pressed ...
digitalWrite(in1pin, LOW); // nothing happens digitalWrite(in2pin, LOW); } }
Step 6: Useful Links!
Want to learn more about a topic? Check out these links! If you have any questions, comments, or suggestions, feel free to leave them down below!
Serial Communication - http://forum.arduino.cc/index.php?topic=396450
H-Bridges - https://learn.adafruit.com/adafruit-arduino-lesso...
Input Pullup and Constants in general - https://www.arduino.cc/en/Reference/Constants
Using boolean - https://www.arduino.cc/en/Reference/Boolean