I've been buying a few cheap pieces off eBay and using what I had around to finally get going with my first robotic project.
After viewing several videos and tutorials I decided to use a L293D chip from a motor controller board I had bought as it allowed more freedom with spare pins as I only needed to control two DC motors.
The first two images show the cheap robotic base I bought from China for £7. It has two 6V DC motors.
On the second two images it shows the full robot that I made. It is being powered by a 9.6v rechargeable pack for a remote control car. This is going through a voltage regulator to put out 5V for the motors. I know that this is not the best way but is certainly the quickest for me to have something working.
To allow the robot to roam without any trailing cables the UNO is being powered by a rechargeable 9V battery.
The fifth image is the schematic of the wiring (looks like a birds nest in the images ).
Further below is the code that I've used.
Now I have a few main issues that hopefully are easy to get over.
Firstly when I was testing all the controls with the robot sitting on a stand so the wheels would spin without it shooting off I would find a couple of things. Sometimes when you set it to go forwards (both wheels travelling at the same time) only one wheel would start up and run at around half speed. If I set it to loop through a couple of commands and run the forward again it ran fine.
Secondly on one of the motors I was unable to get it to run in reverse. This was on pins D1 / D2 where it was as if D1 would go LOW but D2 would not go to HIGH.
Thirdly I also tried to use PWM on pins D10 / D11 to vary the speed of the motors. Sometimes this would work and other times this would not. The code shows everything as digitalWrite but for the leftMotor and rightMotor on pins D10 / D11 these were written as the proper analogWrite so the code is not a mistake just how it was changed to work.
Lastly when I set the robot off on the floor with a simple go forwards and if the sensor picked something up within 50cm do a left turn it all worked fine except that the robot had a slightly quicker drive on the right motor. This results in the robot doing a slight left turn where had it got the space would probably run around in a 3 or 4 meter wide circle.
I'm not sure if these are related to the power I'm using to drive the motors as I'm using a 5V regulator. I'm sure that some of these can easily be resolved.
Hopefully the video / link below will work on the robot running in circles.
The Code.
#include <NewPing.h>
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 6 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// --------------------------------------------------------------------------- Motors
int motor_left[] = {1, 2};
int motor_right[] = {3, 4};
int leftMotor = 10;
int rightMotor = 11;
int motorSpeed = 255;
// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
}
// --------------------------------------------------------------------------- Loop
void loop()
{
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if(uS / US_ROUNDTRIP_CM > 50)
{
drive_forward();
digitalWrite(leftMotor,HIGH);
digitalWrite(rightMotor,HIGH);
}
else
{
motor_stop();
turn_left();
digitalWrite(leftMotor,HIGH);
digitalWrite(rightMotor,HIGH);
}
}
// --------------------------------------------------------------------------- Drive
void motor_stop()
{
digitalWrite(leftMotor,LOW);
digitalWrite(rightMotor,LOW);
}
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);
}