Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

šŸ‘‰ Ready to explore and create with us? Click [here] and join today!

Lesson 6: Exploring the Obstacle Avoidance Moduleļƒ

Weā€™re diving into the world of the Infrared Obstacle Avoidance Module. Tucked at the sides of our Mars Rover, these sensors act as the roverā€™s ā€œeyes,ā€ helping it dodge side obstacles and safely navigate the Martian landscape.

Weā€™ll learn how to integrate these modules with our rover, unravel the magic behind their functioning, and develop code to make our rover smartly sidestep any hurdles it comes across.

Get ready to gear up our rover with some Martian obstacle-dodging intelligence! Letā€™s get rolling!

Note

If you are learning this course after fully assembling the GalaxyRVR, you need to move this switch to the right before uploading the code.

_images/camera_upload.png

Learning Objectivesļƒ

  • Understand the working principle and application of the infrared obstacle avoidance module.

  • Learn to use Arduino to control the infrared obstacle avoidance module.

  • Practice designing and building an automatic obstacle avoidance system based on infrared obstacle avoidance.

Materials Neededļƒ

  • Obstacle Avoidance Modules

  • Basic tools and accessories (e.g. screwdriver, screws, wires etc.)

  • Mars Rover Model (Equipped with rocker-bogie system, main boards, motors)

  • USB Cable

  • Arduino IDE

  • Computer

Stepsļƒ

Step 1: Install the Obstacle Avoidance Module

Now we will install the two obstacle avoidance modules onto the rover.

The assembly steps were simple, werenā€™t they? In the following steps, we will learn about the working principle of these modules, and how they help our Mars Rover to avoid obstacles. Stay tuned!

Step 2: Demystifying the Module

Meet the Infrared Obstacle Avoidance Module - our roverā€™s smart sidekick. This little device is a pack of wonders. Letā€™s take a tour:

_images/ir_avoid1.png

Here are the pin definitions:

  • GND: This is like the moduleā€™s anchor, connecting it to the ground or common point in the circuit.

  • +: Hereā€™s where the module gets its energy, needing a power supply of 3.3 to 5V DC.

  • Out: This is the moduleā€™s communicator. By default, it stays high and only goes low when it spots an obstacle.

  • EN: Meet the moduleā€™s controller. This enable pin decides when the module should work. By default, it is connected to GND, meaning the module is always on the job.

Curious about how this tiny module works? Itā€™s quite interesting! It uses a pair of IR components - a transmitter and a receiver. The transmitter is like the moduleā€™s flashlight, emitting infrared light. When an obstacle appears, the infrared light bounces back and gets caught by the receiver. The module then gives a low signal, alerting our rover of the obstacle.

_images/ir_receive1.png

Our little module is quite robust, spotting obstacles within a range of 2-40cm and boasting excellent anti-interference abilities. However, the color of objects does impact its sensing. Darker objects, especially black ones, are detected at a shorter range. Against a white wall, the sensor is most efficient, sensing within the 2-30cm range.

The EN pinā€™s low-level state activates the module, with the jumper cap securing the EN pin to the GND. If you wish to control the EN pin via code, the jumper cap needs to be removed.

_images/ir_cap1.png

There are two potentiometers on the module, one for adjusting the transmitting power and one for adjusting the transmitting frequency, and by adjusting these two potentiometers you can adjust its effective distance.

_images/ir_avoid_pot1.png

Thatā€™s quite a bit about our little module. In the next step, weā€™ll learn how to integrate it with our rover and control it using the Arduino. Stay tuned!

Step 3: Read from the 2 Modules

Just like curious space explorers, letā€™s dive into the universe of codes and sensors!

  1. Our Mars Rover is equipped with two special ā€œAlien-Eyeā€ sensors, perched neatly on pins 7 (right) and 8 (left). These ā€œAlien-Eyeā€ sensors are actually our infrared obstacle avoidance modules, always vigilant to dodge any ā€œspace rocksā€ (obstacles) in our Roverā€™s interstellar journey!

    _images/ir_shield1.png
  2. Next, weā€™ll need to communicate with our Rover using the universal language of Arduino code.

    First things first, letā€™s give a unique name to each eye of the Rover. Letā€™s call them IR_RIGHT and IR_LEFT, this way we wonā€™t mix them up.

    #define IR_RIGHT 7
    #define IR_LEFT 8
    

    Now, we let our Rover know that these are its special eyes - they will feed information from the world outside into the Roverā€™s electronic brain.

    pinMode(IR_RIGHT, INPUT);
    pinMode(IR_LEFT, INPUT);
    

    To make sure our Rover shares its findings with us, we establish a secret line of communication, like spies in a sci-fi movie. This next line kicks off a serial conversation at the speed of 9600 bits per second - thatā€™s lightning fast chatter!

    Serial.begin(9600);
    

    Now, our Rover scans its surroundings with its ā€œAlien-Eyesā€ and relays the findings back to us. If it spots an obstacle, the value will be 0; if the path is clear, the value will be 1. It keeps sending these messages to us, keeping us in the loop.

    int rightValue = digitalRead(IR_RIGHT);
    int leftValue = digitalRead(IR_LEFT);
    Serial.print("Right IR: ");
    Serial.println(rightValue);
    Serial.print("Left IR: ");
    Serial.println(leftValue);
    

    Finally, the Rover pauses for a moment (about 200 milliseconds) after each transmission. This tiny break gives us the chance to interpret the Roverā€™s message before it sends another one.

    delay(200);
    

    Here is the complete code:

  3. Once your code is ready, select the correct board and port, and beam up the code to your Mars Rover. Then, tune into our secret communication line (the Serial Monitor) by clicking on its icon in the top right corner.

    _images/ir_open_serial.png
  4. Before you start receiving the Roverā€™s messages, make sure your secret communication line is tuned at the same speed (9600 baud) as your Rover. And there you have it - live updates from your Mars Rover!

    _images/ir_serial.png
  5. To put our system to the test, wave a ā€œspace rockā€ (your hand) in front of one of the sensors. Youā€™ll see the value flip to 0, and the corresponding LED on the module lights up. Thatā€™s the Rover saying, ā€œLook out, space rock on my right!ā€

    Right IR: 0
    Left IR: 1
    Right IR: 0
    Left IR: 1
    Right IR: 0
    Left IR: 1
    

By now, youā€™ve not just journeyed through space but also deciphered Martian! Canā€™t wait to see what interstellar secrets we unveil in our next mission!

Step 4: Adjusting the Detection Distance

We have arrived at an essential step, which is to adjust the detection distances of our sensors based on our current environment. The factory settings may not be optimal.

If the detection distance of the two infrared modules is too short, the Mars Rover might collide with obstacles. If itā€™s too far, the Rover might start turning while still a significant distance from an obstacle, potentially impacting its movement.

Hereā€™s how you can make adjustments:

  1. Start by adjusting the right obstacle avoidance module. During transportation, collisions may cause the transmitter and receiver on the infrared module to tilt. Therefore, you need to manually straighten them.

  2. Place an obstacle about 20 cm directly in front of the right module. The box in which our Rover kit came is a good choice for this! Now, turn the potentiometer on the module until the indicator light on the module just lights up. Then, keep moving the obstacle back and forth to check if the indicator light comes on at the desired distance. If the light doesnā€™t turn on at the correct distance or if it remains on without going out, youā€™ll need to adjust the other potentiometer.

  3. Repeat the same process for another module.

Now that our sensors are fully prepared, letā€™s embark on the next journey!

Step 5: Design an Automatic Obstacle Avoidance System

Now, letā€™s take a big leap in our space exploration and make use of these messages from the Rover. Weā€™re going to create an automatic obstacle avoidance system!

Hereā€™s our plan: If the right sensor detects an obstacle, the Rover will turn to the back right. If the left sensor detects an obstacle, the Rover will turn to the back left. If both sensors detect an obstacle, the Rover will move backward. If no obstacles are detected, the Rover will continue moving straight ahead.

Letā€™s visualize this in a flowchart to make it even more clear. Flowcharts are a great way to logically outline a plan, especially when it comes to programming!

_images/ir_flowchart.png

Letā€™s whisper this plan to our Rover in its language(Arduino code):

In this code, we are using if...else statement in the loop() function.

The if...else statement is used to execute a block of code among two alternatives. However, when we need to choose among more than two alternatives, we use the if...else if...else statement.

The syntax of the if...else if...else statement is:

if (condition1) {
// code block 1
}
else if (condition2){
// code block 2
}
else if (condition3){
// code block 3
}
else {
// code block 4
}

Here,

  • If condition1 is true, code block 1 is executed.

  • If condition1 is false, then condition2 is evaluated.

  • If condition2 is true, code block 2 is executed.

  • If condition2 is false, then condition3 is evaluated.

  • If condition3 is true, code block 3 is executed.

  • If condition3 is false, code block 4 is executed.

Now that weā€™ve designed our automatic obstacle avoidance system, itā€™s time for the exciting part - putting it to the test!

  • You can observe if the Rover moves as you expected.

  • Or, place it in different lighting conditions to see how its movements change.

By integrating science into our engineering project, weā€™re becoming space detectives, solving the mysteries of our Roverā€™s behavior. This isnā€™t just about correcting errors but optimizing performance, making our Rover the best it can be! Keep up the fantastic work, space detectives!

Step 6: Reflection and Summary

In the testing phase, you might have noticed an interesting behavior of our Mars Rover: while it expertly avoids obstacles to its left and right, it might struggle to detect smaller obstacles straight ahead.

How can we solve this challenge?

Stay tuned for the next lesson, where weā€™ll continue our exploration into the fascinating world of coding, sensors, and obstacle detection.

Remember, every challenge is an opportunity for learning and innovation. And as we continue our space exploration journey, thereā€™s so much more to discover and learn!