Hi guys
im really new to this and i want to build my first robot.
So i bought a arduino uno and a couple of servo's + wheels but these servo's only have 2 wires instead of 3 (in most build descriptions). How do i connect my servo so that they can be controlled. (i want to build a obstacle avoid robot) using the Hc-sr04)
Hi Wouter,
Yes those are motors not servos, as Grumpy-mike says, you can drive them with just about any motor driver/shield these using the L293/298 are OK, just remember you lose about 2V's across the output transistors, so need at least 4 x AA's. A FET based one is better as the lose is very small, try the Pololu DRV8833 based module, I find this fine.
Beware of the cheap Chinese shield that offers a lot for it's money (It's about the only one with 2xL293's), it's really weird!! only works with it's own library... But there are some good cheap chinese boards too. In fact a lot of my stuff comes from China.
So first get this bit sorted, let us know what you have and I'm sure we can help you more, you could take a look at some of my early bots here: www.melsaunders.co.uk
What part of the world are you in? I might have a few useful links...
Hope it helps, regards.
Mel.
PS. The attached shows an home-made shield(RED) with Pololu DRV8833 driver(GREEN) (Bottom Left).
Hi guys sorry for the late update but i had to wait on some parts
This is what i have so far. it kinda works. Sometimes it seams not to see the objects right in front of him and crashes into them any way. I also added my code i hope you guys can check it out for me?
(sorry the jumperwire cable set i orded doesn't have black wires for the ground.
#include <AFMotor.h>
#define trigPin 18
#define echoPin 19
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 8KHz pwm
AF_DCMotor motor3(3, MOTOR34_64KHZ); // create motor #3, 8KHz pwm
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
motor1.setSpeed(255); // set the speed to 200/255
motor3.setSpeed(255); // set the speed to 200/255
}
int CheckDistance()
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
return distance;
}
void MotorForward(int delaytime)
{
motor1.run(FORWARD);
motor3.run(FORWARD);
delay(delaytime);
}
void MotorBackward(int delaytime)
{
motor1.run(BACKWARD);
motor3.run(BACKWARD);
delay(delaytime);
}
void MotorRelease()
{
motor1.run(RELEASE);
motor3.run(RELEASE);
delay(1000);
}
void MotorLeft()
{
motor1.run(FORWARD);
motor3.run(BACKWARD);
delay(600);
}
void MotorRight()
{
motor1.run(BACKWARD);
motor3.run(FORWARD);
delay(500);
}
void loop()
{
int testDistance = CheckDistance();
Serial.print(testDistance);
Serial.println(" test");
if (testDistance >= 30 || testDistance <= 0){
Serial.println("Out of range");
//go forward and check range again
MotorForward(700);
MotorRelease();
}
else
{
Serial.print(testDistance);
Serial.println(" cm");
//object in path, reverse and turn to avoid
MotorBackward(600);
MotorRelease();
MotorRight();
MotorRelease();
MotorForward(600);
MotorRelease();
MotorLeft();
MotorRelease();
}
delay(500);
}
Hi wouter-,
That's the very motor shield I was thinking of!! If you're using a UNO then where are pins 18 & 19 (according to the code) that you use for the HCSR04? In your picture they seem to be connected to A4 & A5? these are usually used for the i2c interface, or analogue inputs, BUT can be digital too.
I am just wondering how secure some of your wiring is!! Perhaps at this time you don't want to solder anything, so just make sure that everything is connected right and tight!
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line [b][u] 10 uS could be too long, adding to the result.[/b][/u]
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
My code is this:
const int pingPin=7, EchoPin=8;
long duration;
// The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse before to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
pinMode(EchoPin, INPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(2);
digitalWrite(pingPin, LOW);
duration = pulseIn(EchoPin, HIGH);
delay(100);
I find this works very well, with no problems.
What is your power supply? 4 x AA's at least for that shiled!
Well there's something to work on, get back to us.
Thanks for the reply! I really like the motor shield and i get my motors to work with it! so thanks!
Yes i use the A4 and A5 for the Hc SR04 this because the digital pins cant be used without soldering.
I borrowed a code from some one on the internet but i cant find the website anymore.
How can i refer to the a4 and 5 pins? or is it better to use other pins.
You are completely right that my wiring is a bit wierd but i orded headers and i'm waiting for those.
I will try your code thank you!
currently i use a 9v battery for the shield. the uno doesn't seem to require a own powersource.
i also changed the robot a bit it is now way smaller and doesn't use the breadboard anymore.
Hi wouter-,
You can just refer to A4 & A5 as just that! it's just the same as saying D2 or D8, etc.
#define trigPin A4 #define echoPin A5
The only other thing that bothers me is that 9v battery! What kind of battery is it? Please tell me it's NOT one of these tiny squarish things!! These just can't supply the current your motors need, OK perhaps for driving a Uno that's not doing a lot, but completely uselss for motors, servos and relays that demand more current, they die very quicky, as their designed for low current use.
As I said this shield seems to get data from the Uno, sends it to a shift register and outputs it to the L293's etc in a serial/bit format which is why you have to use their AFmotor.LIB So will you have to modify the LIB if you change pins, could be!! AS time goes on I think you will see the limits of this shield and hopefully move on to something else.