Making my Arduino robot aware of the surroundings – part 1

Arduino Robot Sensors2 300x200 Making my Arduino robot aware of the surroundings   part 1Connecting the sensors

This is the first part on connecting the sensors to my arduino robot. It will cover connecting the sensors and the code to test it. In a second article I will then describe the final code sections to make this work with my robot.

Lightsensor

I use 2 standard photosensors for reading the light value.  Each sensor is connected to an analog pin. I also use a 10k Ohm resistor on each photocell. The whole circuit is displayed below.

robot photocell001 300x179 Making my Arduino robot aware of the surroundings   part 1

Testcode

The testcode is simple. I define  2 pins for input values (A3 ist for the right sensos, A4 for the left). Then I also define inputvariables (sensorValueL, sensorValueR) and reset both values initially to 0.

Next as part of the void setup section I declare both pins as input pins so I can read values from the sensor. I also create a serial connection, so I can read the data on the serial monitor. I finish this section by including an initial serial message “Ready”.

In the loop section I basically read the sensor data from each sensor and print it via serial monitor. After each reading cycle I have a 2 second delay, so the data is readable in the serial monitor.

const int RightLightSensor = A3; // This pin is used to read the value of the Right Sensor.
const int LeftLightSensor = A4; // This pin is used to read the value of the Left Sensor.
int sensorValueL = 0;
int sensorValueR = 0;
void setup()
{
pinMode(LeftLightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightLightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
Serial.begin(9600);
Serial.println("Ready");
}
void loop()
{
sensorValueL = analogRead(LeftLightSensor);
sensorValueR = analogRead(RightLightSensor);
Serial.println("Left light value = ");
Serial.println(sensorValueL);
Serial.println("Right light value = ");
Serial.println(sensorValueR);
delay(2000);
}

Follow the light

To create a simple code to identify the direction in which the robot should move I compare booth sensor readings. If one reading is significantly lower than the other, then I will call a function.

const int RightLightSensor = A3; // This pin is used to read the value of the Right Sensor.
const int LeftLightSensor = A4; // This pin is used to read the value of the Left Sensor.
int sensorValueL = 0;
int sensorValueR = 0;
int differencevalue;
void setup()
{
pinMode(LeftLightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
pinMode(RightLightSensor, INPUT); // Defines this pin as an input. The Arduino will read values from this pin.
Serial.begin(9600);
Serial.println("Ready");
}
void loop()
{
sensorValueL = analogRead(LeftLightSensor);
sensorValueR = analogRead(RightLightSensor);
differencevalue = abs(sensorValueR-sensorValueL); //calculate the difference between the two values
if(differencevalue < 50) //if the difference is less than 50 then...
{
//function to move forward
}
else
{
if(sensorValueR > sensorValueL) //it has less light on the right side
{
//i.e. function to turn towards the light
}
else
{
//i.e. function to turn away from the light
}
}
}

Arduino Robot Ultrasonicsensors 012 300x200 Making my Arduino robot aware of the surroundings   part 1Ultrasonic sensor

As an ultrosonic sensor I use a pre assembled component from grove. Since I also use a grove motor shield for my robot the only thing I had to do, was to connected to the arduino via the digital pin 7 via the 4 pin plug that comes with all grove components. For detailed instructions, please have a look ate the grove wiki page.

What I also had to do is to connect the servo to digital pin 5 as shown in the picture below.

robot servo001 300x155 Making my Arduino robot aware of the surroundings   part 1

Testcode

#include "Ultrasonic.h"
Ultrasonic ultrasonic(7);
int targetdistance = 0;
void setup()
{
   slcd.begin();
}
void loop()
{
ultrasonic.MeasureInCentimeters();
targetdistance = ultrasonic.RangeInCentimeters;
delay(100);
}

Moving the sensor

#include <Servo.h>
Servo myservo;
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(5); // attaches the servo on pin 5 to the servo object
}
void loop()
{
for (int i = 0; i < 3; i++) {
sweep();
delay(15); // waits 15ms for the servo to reach the position
}
for (int i = 0; i < 3; i++) {
sweep_fast();
delay(15); // waits 15ms for the servo to reach the position
}
delay(2000);
}

Infrared sensor

As with the ultrasonic sensor I also used preconfigured infrared sensors form grove. If you need detailed information on how to connect them, please have a look at their wiki page.

Testcode

There is no need to come up with a test code. A test and callibration tutorial is available on the grove wiki page.

You may also like...

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

%d bloggers like this: