Creating a scanning sensor platform

While knowing the distance in front of your robotic project is normally important, you might want to know other distances around the robot as well. One solution is to hook up multiple sensors, which is quite simple. However, there is another solution that may be a bit more cost effective. Instead of multiple sensors, you can place a single sensor on a servo and then use the concepts that you learned in Chapter 7, Controlling Servos with Arduino, to move the servo and allow the sensor to scan a set of different distances.

To create a scanning sensor of this type, take a sensor of your choice (in this case, I'll use the IR sensor) and mount it on a servo. I like to use a servo L bracket for this, which is mounted on the servo Pas follows:

Creating a scanning sensor platform

You'll need to connect both the IR sensor (as described earlier in this chapter) as well as the servo to Arduino (as explained in Chapter 7, Controlling Servos with Arduino).

Now, you will need some Arduino code that will move the servo and also take the sensor readings. The following screenshot illustrates the code:

Creating a scanning sensor platform

The preceding code simply moves the servo to an angle and then prints out the distance value reported by the IR sensor. The specific statements that may be of interest are as follows:

  • servo.attach(servoPin);: This statement attaches the servo control to the pin defined
  • servo.write(angle);: This statement sends the servo to this angle
  • inValue = analogRead(inputPin);: This statement reads the analog input value from this pin
  • distance = 30431 * pow(inValue, -1.169);: This statement translates the reading to distance in centimeters

If you upload the sketch, open Serial Monitor, and enter different angle values, the servo should move and you should see something like the following screenshot:

Creating a scanning sensor platform
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.10