Attaching Sensors

I have assembled the DFRobotshop Rover (the most recent version) and have been able to work it with the keyboard w/a/s/d. I want to add some of the sensors I bought separately since the kits were sold out at the time. Where on the Arduino board do I attach the Sharp IR sensor?

The Sharp IR sensor is an analog sensor, so you can connect its output signal to one of the analog input pins on the blue Arduino header, for example pin A0. Here’s a tutorial that shows how to use the sensor with Arduino in general:

Thank you, that helps and I discovered the tutorials section on your site. After reading the tutorial on your site I still am not sure if I the DFRobotshop Rover 2 needs an extra part called a motor controller to make the IR sensor communicate with the motors or if the rover already has something like this built in. I bought the basic rover 2.

You do not need any additional products; the signal from the IR sensor is transmitted to the microcontroller.
Your code should read this analog value, and make a “decision” (stop, move in a different direction etc).
To move, the code should send the appropriate signals to the onboard L298 motor controller to move the drive motors.
No additional “controller” is needed for sensors.
We suggest the Lynxmotion sensor bracket to help you mount the sensor, otherwise use double-sided tape or hot glue.

I am getting closer to making it work, but there must be something I am missing. Going back over what I have done briefly…(1) assembled the DFRobotshop Rover 2 basic kit…(2) downloaded the Arduino software and connected the rover with USB…(3) have been able to program the rover to go forwards at full speed by uploading the “motor” sketch and use w/a/s/d on my keyboard with USB attached (I don’t have wireless components yet)…(4) connected the Sharp IR sensor as shown in the previous post…(5) copy/pasted the code from the link that was provided from the previous post…(6) uploaded that code.

At this point I remove the USB cord and turn on the rover and nothing happens. I was expecting it to move forwards until it got close to an object and then stop and turn another direction. I am pretty sure I have the IR sensor connected the right way because when I open the serial monitor (while USB attached) I see numbers scrolling down and the numbers change if I move my hand in front of the sensor. I am thinking that I am supposed to somehow combine the “motor” sketch with the IR sensor code/sketch?

There is no sample code which integrates the IR sensor. The robot’s motion is dependent on your code, which is likely the issue here.
If you want it to move autonomously and react to distance, it will be up to you to merge the two codes.

I finally have an autonomous rover! I decided to use the hc-sr04 ultrasonic instead of the Sharp IR. For several weeks my son and I learned how to use various sensors apart from the rover (like using the hc-sr04 to activate LEDs on a solderless breadboard). Then after looking at some examples of autonomous code using the hc-sro4 I came up with one that works. So here it is…

The Trig pin goes to digital pin 4
The Echo pin goes to digital pin 3
GND to GND
VCC to 5 volt

The hc-sr04 is attached to the front of the rover with a little duct tape. There is not a panning servo.

[code]#include <NewPing.h>
/* Copy and paste the code below into the Arduino software */

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
#define echoPin 3 // Echo Pin
#define trigPin 4 // Trigger Pin
int maximumRange = 200; // Maximum range needed
int minimumRange = 6; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup()
{
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int leftspeed = 255; //255 is maximum speed
int rightspeed = 255;
analogWrite (E1,255);
digitalWrite(M1,LOW);
analogWrite (E2,255);
digitalWrite(M2,LOW);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(100);

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance > 20) {
analogWrite(E1,255);
digitalWrite(M1,LOW);
analogWrite(E2,255);
digitalWrite(M2,LOW);
delay(100);
} else {
analogWrite (E1,0);
digitalWrite(M1,LOW);
analogWrite (E2,0);
digitalWrite(M2,LOW);
delay(1000);
analogWrite(E1,255);
digitalWrite(M1,HIGH);
analogWrite(E2,255);
digitalWrite(M2,HIGH);
delay(1000);
analogWrite(E1,255);
digitalWrite(M1,HIGH);
analogWrite(E2,255);
digitalWrite(M2,LOW);
delay(550);

/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading.
*/
Serial.println(distance);

}

//Delay 50ms before next reading.
}[/code]

That is the best approach you can take for the Autonomous Rover kit - now that you have “gotten your hands dirty” and seen what it takes to create / modify and compile code, create the necessary connections, you are well on your way to being comfortable creating custom robots. We look forward to seeing your progress. As a hint: if you point your distance sensor at around 45 degrees downward, it can sense cliffs and walls.