Arduino Obstacle Avoiding Robot

This robotic car is able to sense its surroundings and gauge whether it is appropriate to turn and avoid objects in its path. The car, while able to drive on its own, has built-in voice command as well as remote control, LED, motion detection, and other advanced features.

Engineer

Devan C

Area of Interest

Aerospace Engineering

School

La Jolla High School

Grade

Incoming Junior

Project Overview

Welcome to my BlueStamp Engineering website! For the BSE Remote Program for Summer 2020, I created a multifunctional Arduino Obstacle-Avoiding Robot. As you may have read above, this robotic car has advanced AI features that enable it to do much more than obstacle avoid. The following content below depicts my time here at BlueStamp through a series of videos, graphs, paragraphs, images, and other media. Please scroll to the bottom of the page and work your way up if you wish to experience the process of building this robot!

Functions

  • Obstacle-Avoiding (Self-Drive) Mode
  • Voice Command Mode (via Bluetooth)
  • Remote Control Function
  • Built-in Motion Detection
  • Advanced AI Features
  • LED Function
  • Voice Features
Final Code
Final App Apk
Final App Aia

Final Message

Virtual Demo Night

Over my time at BlueStamp Engineering, I have learned and experienced what it  is truly like to be in the STEM field of engineering. Coming from a background of basic coding skills and architectural design, I came to BSE to pursue an interest in building and creating mechanisms. The first week, I was very frustrated as the remote program presented a tough atmosphere to get engaged and the new field was extremely confusing. However, after overcoming these obstacles, I was able to learn a lot about what I am capable of and what I want do in the future. BSE provided a space where I could problem-solve everyday and gave me important skills such as how to look and analyze a problem by taking many perspectives and then breaking it down to isolate and understand what the problem is. It is always crucial to comprehend your mistakes so you can learn from them and make sure not to repeat them. The more problems I faced and overcame, the more I learned. BlueStamp also taught be how to communicate my thoughts appropriately by presenting to a general audience without any engineering background. Additionally, the speakers BSE provided were absolutely amazing and showed me that I can succeed in this great field. I would recommend BlueStamp to anyone with a minute interest in engineering or one who enjoys situations in which they are forced to solve problems. Overall, this program has shown me that this field is a community, and we all want to pursue the advancement of mankind. As a prospective aerospace engineer, I will take what I have learned here and apply it to more complex and projects to this new found interest of engineering.

Final Milestone

For my final milestone and fourth modification, I eventually settled on attaching a motion detector to the robot. Originally, the plan following the LEDs was to add a camera so that it would send a picture every time it sensed motion around it. However, upon receiving the camera, there were just too many logistical problems including the fact that it needed more parts, the built-in motion detection would not have worked on a moving car, and that there was little to no information online. Eventually, it became apparent that without the necessary pieces, the camera was an unrealistic modification with the little time left I had at BlueStamp. I then decided to add a motion detector and have it send a notification to my phone saying “motion detected.” The set-up of this PIR motion sensor was fairly simple: it included connecting the sensor to the 5V and Ground rails on the breadboard and then one digital pin on the Arduino, which ended up being pin 10. After this, I moved onto the software component which entailed using an initial example code from the Arduino website. From here I tested the sensor out by running the code with established vals (values) and it was very hard to tell whether it was working. To solve this, I used my LED code to be triggered when the motion sensor detects something. So if a person walked by the robot now, the car would flash blue three times. Next was the most frustrating part of this modification was the attempt to send a signal to my app from the motion sensor essentially telling it that there was in fact motion detected. After hours and hours of looking over my code and app, rewiring, research, and collaboration, many BlueStamp instructors and I came to the conclusion that my phone had firewalls that prevented the signal from the Bluetooth module from getting through (more info in separate paragraph). Somehow, however, it worked in the demonstration for Demo Night for reasons that are unknown. What is even more peculiar was that the phone was not connected to the robot when the “Motion Detected” sign showed up. Nonetheless, this modification was very cool and I learned a lot. Now, as I approach the end here at BlueStamp, I have an obstacle-avoiding robot with an app companion; and for this final modification, I also have a new motion detection feature.

PIR Motion Sensor

The PIR motion sensor is a sensor that detects motion through infrared waves. In other words, this sensor is able to detect heat which usually means that it senses a human. It takes very little to power it and doesn’t break easily. Its main purpose on this project is to detect if a person is in the way while in obstacle-avoiding mode. It was added to replace the built-in motion detection on the camera and ended up working really well.

Failure
Today was the very first day at BlueStamp Engineering where I did not overcome a problem with my project. Before discussing why it didn’t work, I will just establish the problem. The goal of this milestone was to get the PIR motion sensor to work by having it send a notification to the phone when it sensed human motion near. While the motion sensor got up and running, it was very difficult to get the notification sent. There were two possible problems, either with the code or with the app. Looking on the Arduino website, I could see that my code was correct as I used the appropriate “write” command, and it also was proofed by BlueStamp instructors. This meant that the problem was in the app. The issue with this was that there was little to no information concerning this type of problem on the internet.  However, I made do with the available examples and attempted to model my app configuration like them. After 42 different versions of the app, changing little details every time, it was clear that the bluetooth module was not able to send data adequately enough to the phone. Upon researching, it was clear why this happened. The Samsung Knox software has a firewall and for whatever reason, the firewall did not let the data from the Bluetooth module get through. This meant that unless I rooted the phone, the motion sensor would not be able to send a notification. Because Demo Night was very soon, I unfortunately could not complete this goal. Nonetheless, this was a great lesson to learn because it shows that Engineering takes dedication and commitment and requires multiple perspectives. I can now take what I have learned from this and move forward and try again until I succeed.

Fifth Milestone

Adafruit LED NeoPixels

These Adafruit LED Neopixels are a simple instrument created to add and extra “wow” factor to the car. The tiny LEDs can be manipulated by code to be any color with described amounts of red, blue, and green. The frequency and colors are changed using for loops and map functions meaning that the colors as well as the speed in which they change can be altered. In this particular project, the NeoPixels increase in brightness as the car approaches 15 centimeters  away from an obstacle and any closer triggers the lights to blink on and off red.

Double-Battery Circuit

With the need for more than 6 Volts of energy, the car was added an extra battery pack in order to compensate for the continuous addition of parts. This diagram essentially explains how this was carried out. The red black wire from the first battery pack remained in the Ground, the red from the first one and the black from the second one were connected, and lastly the red wire from the second battery pack was inserted to the 12 Volt slot on the motor L298N motor driver.

Voice Mode LED Blink (Orange)

Red
Green
Blue

Obstacle Mode LED Blink (Green)

Red
Green
Blue
For my fifth milestone and third modification, I went with the addition of Adafruit LED NeoPixels to the car due to a shipping delay of a camera that will be seen in the next modification. With a slight mix-up of ordering and shipping parts, BlueStamp advised me that I choose a modification with parts already sent to me. Naturally, I turned to the LEDs for this section as they would be easy to set up, cool to see in action,  and be a great addition to an already artificial intelligence based project. The first step in doing so was to simply measure the circumference of the chassis and count the number of LED NeoPixels it would take to equate this measurement. In the end, the total count was 42 lights and it was an easy process to cut the strip and attach it to the perimeter of my robot. From here, it was easy to follow diagrams of completing the circuit which was to connect it to the 5V, Ground, and a digital port on the Arduino, similar to the servo. After choosing port 5, as it needed  to be a PWM pin and the servo library deactivated the available port (10), I began the code. As this was a new instrument, I decided to use the library Adafruit provided and test it. The LEDs all of a sudden began flashing various colors of light: red, green, blue, then rainbow, white, etc. and then repeated at different brightnesses. Moving forward, I now understood how the LED NeoPixels worked and began to implement the necessary for loops, which is essentially a loop that caters to integers, as well as map functions, a new tool specifically designed for controlling NeoPixels, into my app software. The code, while difficult to adapt to, was not even close to as hard as what was to come. When the proper software was added to the robot, it refused to budge and did so blinking the wrong color and number of LEDs. After fixing the LED code, I then was successful in enabling the lights to get brighter as obstacles came from 80 centimeters to the car. When the obstacle was within 15 centimeters rather than 30, the lights then flicked on and off three times and proceeded with the obstacle-avoiding function. The greatest challenge of this entire project was what happened next, The obstacle light function worked great with the chosen color red without motors, however, both circuits could not work simultaneously with only 6 Volts from the battery pack. This seemed to be an easy fix as I would just add another battery pack to increase the voltage. Upon doing so, I discovered that this entire time, the L298N motor driver had been drawing some of its power from the Arduino which was not good news. Then when running the software on the two battery packs, the motor driver received more than 12 Volts and immediately fried. This breakdown was caused by the faulty battery pack, and new pieces were ordered to try again. When achieving this step of establishing the double-battery circuit, the robot’s obstacle avoiding function seemed to continue malfunctioning. From here, this indicated that there was a problem with the new LED software again so the BlueStamp instructors and I looked over the code. We then found that the Arduino was running the code in order thereby prioritizing the LED NeoPixel function over the obstacle function, and thankfully the voice command function was not affected. Once this problem was fixed, it was also determined that the car was getting too heavy, so now it could only be tested on hard surfaces instead of carpet due to its friction. After days of reordering the code to adapt to the new advanced functions, I added a new LED function of them blinking the color of the mode button on the app (Green = Obstacle, Orange = Voice). Now I have a car that has functional LED NeoPixels that further advance the artificial intelligence of this obstacle-avoiding robot.

Fourth Milestone

For my fourth milestone and second modification, I made the decision to add a SG90 Micro Servo 9g to the obstacle-avoid mode for the purpose of increasing the robot’s intelligence. After completing the app and establishing the two modes, I wanted to allow the car to make decisions as opposed to me making them for it. Luckily, BlueStamp had already sent me a servo in the starter kit so no additional parts needed to be ordered. The first step in this process was to create a holder to keep the HC-SR04 Ultrasonic Sensor still while the servo moved left and right. This took a couple of hours as I had to design it based on other models made out of plastic, and then use dull scissors to cut through cardboard. Once this was complete, I attached the sensor to the cardboard holder and used Male-Female jumper wires to complete the circuit to the breadboard. With tape, I then secured the servo to the holder and connected it to its specific ports on the Arduino using various diagrams. The electrical component for this task was mostly done and now it was time to move to editing software. Because I had to adapt the servo to an obstacle-avoid mode, there were very few resources online to assist me with the code. This meant I had to start from scratch and attempt to logic my way through its completion. My initial approach was to have if..else statements and my previous motor code combined so that if the sensor saw something closer on the right or left it would turn the opposite direction. One issue with this was that the sensor could not make the distinction between left and right. The solution I came to was to direct the servo to turn 55 degrees and then set each amount as integers “lookleft” and “lookright.” From there I would create a separate if statement saying “if lookleft > lookright —> turn right, else —> turn left.” This approach proved a success and the individual servo code worked perfectly. The combination with the two modes, similarly, was very successful and yielded very little problems. The greatest challenge of this milestone was to fix the electrical parts. Primarily, the wheels and batteries needed to be replaced as the car was getting too heavy and it was draining the batteries easily. Next, when the combined software was run, half of the L298N motor driver would shut down everytime the servo was in motion. After many hours of deliberating and the collaboration with twelve BlueStamp Engineering instructors, we finally figured out that the servo library deactivates the PWM function on pins 9 and 10 so I had to rewire the motor driver circuit in order for this problem to go away. To our satisfaction, the new function worked well. Now the robot has increased intelligence when in obstacle-avoid mode as it can make turning decisions for itself.

SG90 Micro Servo 9g

The SG90 Micro Servo 9g essentially acts as the neck for the robot. Its purpose is to move the HC-SR04 Ultrasonic Sensor left and right at a dictated degree. This servo sits at the front of the robot and connects to the Arduino using a breadboard and many jumper wires. The sensor was moved from the breadboard to holder and wires were also used to connect it back. It was then attached to the cardboard holder where it currently rotates it approximately 55 degrees when the car detects an obstacle within 30 centimeters away.

Servo Circuit

The SG90 Micro Servo 9g circuit is actually pretty simple. It is connected to the 5V and Ground ports on the Arduino via a breadboard, and is also connected to port 11 instead of port 4 as it was one of the few that remained open. The servo steals power from the Arduino which has the power source of a computer or a portable charger which means it is able to function without the battery switch on. Lastly, the servo was secured to the chassis with tape thereby completing the electrical component and enabling it to work on a flat surface.

Third Milestone

Voice Command / Remote Control Mode

Obstacle-Avoiding Mode

HC-06 Bluetooth Module

The HC-06 Bluetooth module connects the car to the Android. It essentially acts as the middleman between the electrical components of the car and the software components of the car. The module connects to the app created with MIT App Inventor to the Arduino on the chassis. It does this by receiving the integers sent by the app via Bluetooth and feeding it to the Arduino for it to process and carry out different functions.

MIT APP Inventor

MIT App Inventor functions exactly like it sounds. In this project, its primary service was to be a platform for which I would create my app for the voice control command. This was carried out by using previous aia files found online and then inserting them into the site so that I could combine and edit. The website has two main parts, one is for the design and look of the app, and the other is the blocks of code of that app. Both enabled me to create a great app that was great in both design and software.

For my third milestone, I completed my first modification to the Obstacle-Avoiding robot which was to add a voice command function to the car. With the overall base of the car done, the next step in adding voice command was to combine a bluetooth module with the three established circuits. Before even proceeding with that, I needed to learn how exactly I would continue with this function. Upon my research, I realized I should use MIT APP Inventor as it is the easiest/best service to use on Arduino boards. Following this, I added the bluetooth module to the circuits using a reliable diagram online which meant the electrical component of this milestone was complete. The only thing to do was to create the app and download it to an Android and run it (BSE was thoughtful enough to send me a Samsung to help with this). After completing my research on how to use MIT APP Inventor, I began to develop an app using previous apk and aia files online, essentially combining them to make a “super-app.” There were many challenges in creating this app. Primarily, MIT App Inventor was a new platform that was difficult to edit and left me very frustrated. However, many days into the process, I finally was able to make an app that enabled voice command as well as a remote control function. This was completed by having these different commands (left, right, forward, backward) send integers to the Arduino so it could process them and then carry out the functions. In the code, as seen below, I learned a trick which was to create multiple voids and have the loop go to each void based on what integer. This proved very successful and the app and voice command was now flawless. The voice command function essentially had a voice recognition software embedded so it could read what you are saying and then send that command in the form of an integer as well. Last, the hardest part of this entire process was combining the voice command code with the obstacle-avoiding code. This took many days to complete as there was always one function working but not the other. Finally, my instructor and I realized that we needed to add multiple while loops and breaks which essentially lets code go in a loop while/when something is true. The combination of the two functions meant adding two buttons on the app that send more integers, creating two different modes with two different loops. With this new combination, the app was able to pair well with the bluetooth module and send both functions effortlessly. Now the car has a self-drive, obstacle-avoid mode, and a voice control, remote control mode. 
Code
MIT App apk
MIT App aia

Second Milestone

For my second milestone, I managed to complete the entire obstacle avoiding robot project. After proceeding from the basic structure of the robotic car, the next goal was to add the obstacle-avoiding function to the car. The first step in doing so was to add the HC-SR04 Ultrasonic Sensor to the Arduino-motor driver-motor circuit powered by the outside power source and the battery pack. When approaching this, the best thing to gauge the best understanding of the sensor was to create a new circuit between the Arduino, an LED, and a sensor using a breadboard and wires. This proved to be quite challenging as the HC-SR04 needed to be connected to various ports on the Arduino that had been taken up by the motor driver. Moreover, the LED circuit needed extra wires and a resistor as well as adding the entire LED blink code to the HC-SR04 simple version of the code on Arduino C++ (the coding language). Once this was complete, the computer would print distances taken by the ultrasonic sensor and the LED would blink when I placed my hand within 10 centimeters of it. The next challenge was combining the motor driver circuit and the ultrasonic circuit, which was very difficult because both used the 5V port on the Arduino. I solved this problem by connecting the ground port of the motor driver and the GRD port of the sensor to the breadboard and allowed for one continuous current that lets both get to the 5V port. The last step in achieving this milestone was connecting all the pieces and getting the function of obstacle-avoiding in the code. This new and improved code combined all of the circuits (motor driver, LED, ultrasonic sensor) and was achieved by taking from previous code and adding the necessary components of each one. How this works is when the sensor sees something within 30 centimeters, the LED goes on and it sends the information to the computer as well as the Arduino then the motor driver. From there the motors will stop, move backwards, and turn clockwise until the sensor doesn’t see anything. I was able to do this  by using an “if…else” statement in my code. After uploading this to the Arduino, I soldered all the wires to the motors, added the portable charger in the front, and created a switch for the battery pack, allowing for the car to turn on and off with ease.

HC-SR04 Ultrasonic Sensor

The HC-S04 Ultrasonic Sensor essentially acts as the eyes for the obstacle-avoiding robot. It does this by sending ultrasonic sound waves out and that bounce back, acting as an electronic form of echolocation. Once it gets this signal back, it sends the information to the computer to print. When an “if..else” statement is inserted in the code, the car carries out different functions depending on the distance. The sensor has four ports: Gnd, Echo, Trig, and Vcc, connected to the 5V port, digital port 12, digital port 13, and the ground port of the Arduino respectively. They each have a specific role in the code and allows the sensor to carry out its main function: if  the sensor says that objects are within 30 centimeters, the far will back-up and turn, everything else, proceed.

Sensor-Motor Driver-LED Circuit

This circuit is the product of combining the LED, motor driver, and sensor circuits all to a singular Arduino. When building the ultrasonic sensor circuit, I tested it out on an LED before on actual motors. However, I ended up leaving it in because it served as a visual purpose of how and when the robot can detect objects in its path. The next thing to do was add the motor function by writing code that would enable that, but also create a smooth circuit without problems. The issue was that both the sensor and the motor driver needed the 5V port in the Arduino. So a breadboard was used to combine both of them in a row. This circuit is as simple as it is elegant. When the sensor detects that there are obstacles within 30 centimeters, the light goes on and the information is sent to the Arduino (and computer if connected), and then the program tells the motor driver to make the motors stop and allow the car to back-up and turn to where there are no obstacles.

First Milestone

Arduino UNO 3

The Arduino UNO 3 basically the brain of any Arduino-based robot. This board allows for open sourcing meaning various projects can be built off of it. It regulates inputs and outputs in analog and digital functions with the goal of connecting software to firmware. One can simply insert code on a computer and upload the software to the Arduino using a cable and when inserted into a circuit, the board can carry out many different functions.

L298N DC Motor Driver

The L298N DC Motor Driver essentially does exactly what its name indicates. Its main purpose, specifically in this vehicle, is to control speed and rotation direction. The driver first takes the information sent by the Arduino as well as the energy from the battery pack, and regulates the motor(s) it is attached to by wire, thereby completing the circuit. There are two techniques on the driver, both for a different purpose. PWM is utilized for controlling speed by varying voltage input, and H-Bridge for controlling rotation direction by varying polarity of input voltage. This driver is an essential piece to the robot because it allows the Arduino to adapt to a project containing a running motor.

For my first milestone, I was able to assemble the basic structure of the robot, essentially creating a functioning car. The overall base of this obstacle-avoiding robot is called a “chassis.” From the chassis, I began to add the necessary pieces both above and below to conserve space. These parts included the motor holders, wheels, hammer caster, and battery pack, all held together by screws and nuts. Before beginning, the chassis was completely covered in a sticker-like material, which led to the tedious task of removing it, taking almost 45 minutes. After assembling all these parts together on the bottom of the chassis, the next step was to get the DC motors to work by creating a circuit consisting of the Arduino UNO 3, L298N motor driver, motors, and the battery pack. This stage proved to be  difficult as various diagrams had entirely different positions for wire placement. In this situation, the best thing to do was follow the placement according to the C++ code and fix any inconsistencies later. I discovered that we needed to add an extra wire from the motor driver to the 5V slot in the Arduino to give the driver enough power to enable both motors to run which proved successful. Finally, after getting the motors to run, the last step in this first milestone was to attach everything to the chassis and run the vehicle by connecting it to my computer. Another challenge arose as the Arduino kept rejecting the code inserted by my computer. With minor tweaks and the use of a new computer, I was able to get the robot to go backwards and forwards at different speeds dictated by my code.

Arduino-Motor Driver Circuit

The complete circuit here enables the motors to run and the car to move forwards and backwards and different speeds. The main source of power comes from the four AA batteries in the battery pack. This is connected to the motor driver. A separate power source, either from the computer or a portable charger is connected to the Arduino so that it can be powered. Once the information (code) arrives to the Arduino, it is sent to the L298N DC motor driver. From there the motor driver distributes the information between each motor and using its two built-in systems, regulates the motors’ speed and rotation direction. Once this circuit is complete, the process repeats allowing the car to move continuously.

Approach and Inventory

Success Amid COVID-19 Pandemic

In the midst of this COVID-19 Pandemic, BlueStamp Engineering has been a ray of hope and prosperity. With the on-site programs in San Francisco, Palo Alto, and New York cancelled, the new remote version of BSE was moved onto Zoom sessions. This was an entirely new thing for BlueStamp, but for our safety it was the right thing to do. After going through the six week program, I can say for sure that it was still and amazing experience and BSE did an excellent job at adapting to the circumstances. Although not as great as the in-person program, the instructors and leaders were able to teach us a lot and let us apply what we learned each week. We were still able to learn to code on Arduino, make websites, build circuits, solder wires, and communicate ideas. Most of all, BSE has given me a place to go amid this pandemic and has enabled its students to succeed and move forward into the future with the knowledge of skills we will have the rest of our lives.

More Information

If you have any questions or concerns regarding my project or its process, feel free to email me at [email protected].

Leave a Comment

Start typing and press Enter to search

Bluestamp Engineering