Servos are cool, but they may not drive your high speed line-follower or help your cardboard box robot get around (I wish I had a video of the one I made... wifi controlled cardboard box... fun!)
Today I am going to make a couple of brushed DC motors do my bidding.
First thing to note is that if you want motors to go forwards and backwards, you need something like an H-Bridge controller. You want to know how an H-Bridge controller works? I am not going to tell you here. I have a very tenous grasp on the secret inner workings of the H-Bridge, so I am not qualified to give a lecture. BUT I will give you a diagram of how to wire one so it works...
The motors I am using are encoded Roomba motors, but I am not using any encoding here. Just the black and red leads are hooked up.
I choose to use the SN74410 chip rather than build out an h-bridge with resistors and crap. It can be done and there are lots of tutorials out there. Nice clean simple chips suit me fine.
The tutorial *Dual Motor Driver* was instrumental in this project. It is specific to the SN74420 chip (interchangable with the L293D h-bridge chip). The code I used came from that guy too.
Diagram:
You might have seen the MPU-6050 from my last project in the video. It is just hanging out, not doing anything... YET.
The black and red power leads are going to a 5V UBEC run off a 3s battery.*My fritzing diagrams do not show the best routing pathways. They are intended only to show the proper connections.*
Code:
// Use this code to test your motor with the Arduino board:
// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command
// ————————————————————————— Motors
int motor_left[] = {4, 3};
int motor_right[] = {7, 8};
int ledPin = 13; // LED connected to digital pin 13
// ————————————————————————— Setup
void setup() {
Serial.begin(9600);
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}
}
// ————————————————————————— Loop
void loop() {
drive_forward();
delay(1000);
motor_stop();
Serial.println("1");
drive_backward();
delay(1000);
motor_stop();
Serial.println("2");
turn_left();
delay(1000);
motor_stop();
Serial.println("3");
turn_right();
delay(1000);
motor_stop();
Serial.println("4");
motor_stop();
delay(1000);
motor_stop();
Serial.println("5");
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
// ————————————————————————— Drive
void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}
void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}
void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}
I don't know about the PWM comments in the code. The motors I am using are 'supposed' to have PWM input, but work with the code as is. *shrug*
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.