Arduino Robot Vacuum

by Kush Gandhi in Circuits > Arduino

2856 Views, 9 Favorites, 0 Comments

Arduino Robot Vacuum

j7_Website_Kitchen_2560x800.jpeg

The purpose of this project is to clean a room floor from small debris and dust/dirt autonomously. A pair of hobby gear motors and a swivel wheel at the front will allow the robot vacuum to move about, and an ultrasonic sensor will detect any objects in front of it. A DC motor with a fan blade will act as a suction system to clean. When the user chooses, the vacuum may also switch to remote control mode, allowing the vacuum to be operated using an IR remote. Two LEDs will show the robot's statuses, one showing whether it is on or off and the other indicating the mode (autonomous or remote-controlled). Finally, a potentiometer will allow the user to control the speed of the 2 gear motors, faster speeds will allow for a quicker clean while slower clean will allow for a more thorough clean.

Components Necessary

This circuit requires...



If you're planning to build the robot vacuum in its entirety, you'll need some extra parts

  • Card Board
  • Wheels compatible with the Hobby Gearmotors x2
  • Swivel Wheel
  • Fan Blade for DC Motor
  • Exacto Knife

Wiring the Circuit

Arduino Final  (1).png
Screen Shot 2022-01-21 at 11.50.14 AM.png
Screen Shot 2022-01-21 at 11.50.24 AM.png

Use the TinkerCad image above to help you throughout this process. I've also attached a schematic view of the circuit to help you further while your wiring it up!


1. Connecting the 2 power sources


Since the Hobby Gearmotor requires more current than the Arduino's 5v(VCC) pin can deliver, we'll also be utilizing a 9v battery to power the two gear motors. The Arduino's 5v will be used to power the remainder of the components. As a result, the 9v battery will be linked to the left side of the breadboard, while the components will be attached to the Arduino's 5v pin on the right side. Remember to ground both sides; you may achieve this with the battery's negative terminal and the Arduino's GND pin.


2. Building the Robot Vacuum Circuit: Part 1(L293D, Hobby Gearmotors, and Potentiometer)


The L293D IC's job is to regulate the gear motors, which includes reversing their rotational direction to allow the vacuum to go backwards to avoid obstacles. As it can flip the polarity of the voltage given to the two gear motors, the L293D IC, also known as the H-Bridge, will assist us. Wiring the H-Bridge may appear difficult at first, but I'll guide you through it pin by pin.


(Left Side of the Chip - Starting from Top)

Enable Pin 1 & 2 -> Digital Pin 9

Input 1 -> Digital Pin 5

Output 1 -> Hobby Gearmotor Positive Terminal

Ground -> Negative Rail

Ground -> Negative Rail

Output 2 -> Hobby Gearmotor Negative Terminal

Input 2 -> Digital Pin 4

Power -> Positive Rail(9v Battery)


(Right Side of the Chip - Starting from Bottom)

Enable Pin 3 & 4 -> Digital Pin 6

Input 3 -> Digital Pin 8

Output 3 -> Hobby Gearmotor(2) Positive Terminal

Ground -> Negative Rail

Ground -> Negative Rail

Output 4 -> Hobby Gearmotor(2) Negative Terminal

Input 4 -> Digital Pin 7

Power -> Positive Rail(9v Battery)


Now that your 2 Hobby Gearmotors and the L293D IC Chip are wired up, it should look like the TinkerCad diagram.


Potentiometer

We'll use the potentiometer to receive a value between 0 and 1023, but we'll map it to 100 to 255 to regulate the hobby gear motors' speed. This enables the user to lower the speed for a more thorough clean or raise the speed for a quicker clean. Since the potentiometer just has three pins, wiring it is simple. Terminal 1 pin will be connected to the 5v power rail while the terminal 2 pin will be connected to the GND pin on either side of the board. Finally, the middle pin called "common" will be connected to the A2 analog pin our Arduino.


3. Building the Robot Vacuum Circuit: Part 2(Switches and LEDs)


The switches are necessary for turning on/off the vacuum and switching between autonomous and remote-controlled modes. Terminal 1, Common, and Terminal 2 are the three pins on the switches. In order to prevent burnouts by lowering the voltage, the LEDs must also be linked to a 330 ohms resistor.


Power Switch

Terminal 1 -> (VCC - 5v through right side power rail)

Common -> Analog Pin A0

Terminal 2 -> Grounded to Negative Rail


Mode Switch

Terminal 1 -> (VCC - 5v through right side power rail)

Common -> Analog Pin A1

Terminal 2 -> Grounded to Negative Rail


Green LED(To Display Power On/Off)

Anode -> Digital Pin 2

Cathode -> 330 Ohms Resistor -> Grounded to Negative Rail


Red LED(To Display Mode : On = Remote Control Mode)

Anode -> Digital Pin 13

Cathode -> 330 Ohms Resistor -> Grounded to Negative Rail


4. Building the Robot Vacuum Circuit: Part 3(IR Sensor)


The IR Sensor is necessary for the remote-controlled mode for the vacuum. Wiring it up is super easy! The sensor has 3 pins called "OUT", "GND", and "POWER". "Power" needs to be connected to the VCC Pin on our Arduino through the right side power rail. "GND" needs to be connected to a negative rail on either side to ground the sensor. Finally, "out" needs to be connected to digital pin 11.


5. Building the Robot Vacuum Circuit: Part 4(NPN Transistor and DC Motor)


The DC Motor which we'll use to create the suction system of the vacuum by using a fan blade attached to it. However, in order to turn it on or off on command, we can use an NPN transistor. The NPN transistor has three pins: "Collector," "Base," and "Emitter." The "collector" will be connected to the VCC pin on the right side of the breadboard via the positive rail, the "base" to digital pin 3, and the "emitter" to the DC motor's positive terminal.


Congrats! You have finished wiring the robot vacuum, on to the coding!

Coding: Part 1 (Variables)

Screen Shot 2022-01-21 at 11.09.50 AM.png

Coding may seem difficult, but I've put comments throughout my attached code to ensure that everything is understood by anyone of all skill levels.


Let's begin coding, the first chunk of my code is just initializing the variables and importing the IR remote library that we will be needed later on in our code. So, first, we import the IRremote library by using the following code: #include <IRremote.h>. Next, we create a receiver object by using the following code: IRrecv rc(11);. The number 11 in that line of code stands for the pin being used for the IR sensor. Lastly for the IR remote, we need to create a variable type "decode_results" with the name "results" by using the following line: decode_results results;. Now that we have our IR remote setup, we can move to initialize the rest of the variables which is shown in the picture above.

Coding: Part 2(Setup Function)

Screen Shot 2022-01-21 at 11.10.04 AM.png

Alright, now that our variables are initialized, we can code our setup function. This function will only run once, so it's important that we set up our pin modes within it. However, we also need to start serial communication between the Arduino and the USB connection at a 9600 baud rate. The PIN modes can either be set to INPUT or OUTPUT depending on what component they are connected to, the picture above displays all the PIN modes for the pins we'll be using. We also need to enable the receiving process for the IR remote using the following line of code: rc.enableIRIn();.

Coding Part 3(Loop Function)

Screen Shot 2022-01-21 at 11.11.28 AM.png
Screen Shot 2022-01-21 at 11.11.43 AM.png

Before we begin this section, remember you can always download my code file with the comments describing each chunk of code or click the images above in case they are cut off to see the entire picture!


The Loop Function is a vital part of our code as that's where the functionality will be and will continue to repeat or "loop" as the function name suggests. We'll first begin with reading our sensors and switches in order to use those values to calculate what the vacuum will do next. The code starts by first receiving the distance in front of the ultrasonic sensor and initializing it to a variable named "distance" with the function "checkdist". Then the code reads the power switch by reading a HIGH or LOW value and initializing it to a variable named "power", this will decide if the motors will be on or off. The code also then reads the mode switch by reading a HIGH or LOW value and initializing it to a variable named "mode", this will decide whether the vacuum is in autonomous or remote-controlled mode.


Moving onto the first conditional statement which decides whether the robot should be on or off using the value we received from the power switch set 0 for off or 1 for on. If the power switch is in the "on" position, the green LED will turn on-off which indicates to the user that the vacuum is on. We move on to the next conditional statement within the first "power" statement, this "sysstart" statement allows us to print to the serial monitor if the system is on.


The next few conditional statements are very important to the functionality of our system, the first conditional statement uses the value from the mode switch to decide whether to execute the autonomous mode code or the IR remote controlled code. Starting off with the autonomous mode statement code, we first turn the DC motor for the suction system on. We also use another conditional system to print to the serial monitor the vacuum's current mode. Using the distance value from the ultrasonic sensor, we use a conditional statement to decide what to do if the vacuum comes within 20 inches of an obstacle in front of it. If it does encounter an obstacle a series of movement functions called "stop", "delay", "backward", and "turnRight" all execute what the name suggests. This allows the robot to autonomously move navigate away from the obstacle and continue cleaning. The conditional statement also includes an "else" statement to allow the robot to keep moving forward with the function "forward" if nothing is within 20inches of the robot.


Moving onto the manual mode conditional statement(if the mode switch is in the off position), we first turn the mode LED on. We also use a conditional statement to print to the serial monitor our current mode. Next, we use a conditional statement to check whether we received a signal from the IR remote. In the case that we did, we take the input and pass through a switch statement. This allows the numerous buttons with a hex code to have a certain function. For example, the power button will execute a conditional statement that turns on and off the DC motor for the suction system. We do a switch "case" for each of the buttons that we want to functionalize, such as the volume up for forward. This allows the vacuum to be controlled in all directions or stopped with the play/pause button.

Coding: Part 4(Functions)

Screen Shot 2022-01-21 at 11.11.55 AM.png

In this section, I'll explain what each of the functions that we previously used does. To start off, let me explain the "distance check" function. This function returns the distance in inches of an obstacle in front of the ultrasonic sensor by applying a HIGH digital output to the trigPin on the ultrasonic sensor for 10 milliseconds which causes the ultrasonic sensor to send out an 8 cycle sonic burst which travels at the speed of sound and then bounces of an obstacle and is received by the Echo Pin. The time it takes for the signal to be received on the echo pin needs to be divided by 74 as we want the distance value in inches. Since there are about 74 microseconds per inch as sound travels 1130 feet per second. However, since the duration is the time it takes the sound waves to reach and come back to the echo pin, we need to dive by 2.


The rest of the functions are for controlling the movement of the robot using the L293D chip while also reading the potentiometer to obtain the speed the motors should have.


Forward();

In this function, inputs 1 and 3 are set to HIGH which causes both motors to turn forwards allowing the robot to move in a straight line forwards.


turnRight();

In this function, inputs 1 and 4 are set HIGH which causes the left motor to turn forwards and the right motor to turn backwards.


turnLeft();

In this function, inputs 2 and 3 are set HIGH which causes the right motor to turn forwards and the left motor to turn backwards.


backwards();

In this function, inputs 2 and 4 are set to HIGH which causes both motors to turn forwards allowing the robot to move in a straight line backwards.


stop();

In this function, all inputs are set to LOW which causes both motors to turn off which stops the robot from moving.


Congratulations! You've completed the coding aspect of the robot vacuum.

Building the Vacuum

maxresdefault.jpeg
vacuum.jpeg

Thus far we've only created the circuit, but the frame of the robot and suction system still needs to be designed. I've uploaded a picture of another design for a robot vacuum made from cardboard. I've also uploaded a picture of a DC Motor vacuum which you can apply upon the first design as the suction system. The design is completely up to you!