Arduino Projects

Master Robotics with GalaxyRVR Rover

Master Robotics:

Master Robotics with GalaxyRVR- SunFounder’s GalaxyRVR Rover is not just a toy, but it’s a full kit for you and your kids to Master robotics. With the GalaxyRVR Mars Rover, you get 23 lesson codes. You can run these codes on the robot and even change them as you like.

Master Robotics with GalaxyRVR Rover

I have already written an article; on how to build and test this robot.

Today, we will cover the Motors, IR sensors, Ultrasonic sensor, Servo motor, and RGB LED.

Amazon Links:

GalaxyRVR Rover Kit

Other Tools and Components:

ESP32 WiFi + Bluetooth Module (Recommended)

Arduino Nano USB C type (Recommended)

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Master Robotics Lesson 1: Motors

The GalaxyRVR Rover has 6 motors. There are 3 motors on the right side and 3 motors on the left side. These motors are divided into two groups: left side motors and right side motors.

You cannot individually control any motor on the left side and the same goes for the motors on the right side. If you send a signal to turn ON a motor on the right side, all the motors on that side will turn ON, and if you adjust the speed; all the motors will change speed at the same time.

Think of these 6 motors as just 2 motors, one on the left side and one on the right side.

All these motors are connected to a motor driver. Using only 4 pins (2, 3, 4, and 5) on the Arduino Uno, you can control the speed and direction.

So, let’s start with our first example.

GalaxyRVR Move Forward Program:


First, you will need to download the SoftPWM library which allows us to control the speed and direction of the motors.

To install this library, simply copy the library name “SoftPWM”, then go to the sketch menu, then to Include Library, and click on the Manage Libraries.

Paste the Library name in the search box.

SoftPWM library for the Arduino - master robotics

You can see I have already installed this library. You also need to install the same exact library.

Next, we define pins for the motors.

We are telling the controller which pins on the Arduino are going to be used to control two groups of motors” Right Side motors and Left Side Motors”.

in1 is connected to pin 2.

in2 is connected to pin 3.

in3 is connected to pin 4.

in4 is connected to pin 5.

The setup function runs once when the program starts. Here, we start the SoftPWM library with SoftPWMBegin().

The loop function runs over and over again. In this loop:

We set the left motors to rotate counterclockwise. Using the pulse width modulation or PWM to sit the in1 to its full speed and we sit the in2 to 0 which means stop.

And we do the opposite thing to the right side motors. It have set the right motors to rotate clockwise.

We set the right motors to rotate clockwise:

In simple terms, this code makes the left motors spin in one direction and the right motors spin in the opposite direction, both at full speed.

To upload this program,

  • first connect the USB cable.
  • Next, select the Arduino Uno from the Boards list.
  • Next, select the communication port.
  • Finally, click on the upload button.

Turn ON the power switch and you will see the GalaxyRVR will start moving in the forward direction forever.

robot motors forward control - master robotics

Let’s modify the code.

I want the GalaxyRVR to move forward for 3 seconds, then I want it to stop for 2 seconds, and then I want it to move in the reverse direction, again it stops for 2 seconds, and then this repeats again and again.

Motors Speed Control Program:

As you can see, I have modified the code. First, the robot moves forward for 3 seconds.

To stop the robot, I have set all the pins to 0.

For reverse direction, I have set in1 and in4 to 0, and in2 and in3 to 255 for full speed. Then, there is a 3-second delay. So, the robot will go in reverse for 3 seconds and then stop for 2 seconds.

Upload the above program and you will see the Motors on the GalaxyRVR rotating at their full speed.

You can also control the motor speed very easily. You need to remember, 0 means stop and 255 means full speed. So, to control the speed, you can use any value between 0 and 255.

Modify the above program, to reduce the speed, this time, you can replace 255 with 100. After making the necessary changes, again upload the program, and this time you will see decrease in the motors speed.

Motors Left Right control:

You can control the robot’s left-right movement using different techniques. For example, if you turn off the motors on the right side and turn on the motors on the left side, the robot will turn right. If you turn off the motors on the left side and turn on the motors on the right side, the robot will turn left.

If you want the robot to turn quickly, you can make the motors on both sides rotate in opposite directions.

If you want the robot to turn slowly, you can simply reduce the speed. Let’s take a look at the modified code.

To turn the robot to the right side, I simply set the in1 to 255 and all the other pins to 0. Then the robot stops for 2 seconds. For the left side movement I set the in4 to 255, and all the other pins to 0. Again the robot stops for 2 seconds.

For the quick movement we turn ON the motors on both sides.

For the slow movement, we simply reduce the PWM value.

Robot Moves Forward and Left_Right:

You can define your own functions for the robot’s movements. This will make your code more organized, and you can easily make changes.

As you can see, to move the robot forward, we have the function moveForward(). This is a user-defined function. It has no return type and takes only one value as an argument, which is the speed. Now you don’t need to go into the function body to change the speed; you can just change the value inside the parenthesis in the main loop() function.

The same thing applies to all the other functions. You can set any speed you want.

Upload the program and make necessary changes as per your requirements.

Master Robotics, Lesson 1 Video:

Master Robotics Lesson 2: IR Sensors

After controlling the motors forward, reverse, left, and right movements, now we will cover IR sensors. The GalaxyRVR Rover has two IR sensors; one on the right side and one on the left side.

Robot IR sensors - master robotics

The right IR sensor is connected to pin 7 on the Arduino Uno, and the left IR sensor is connected to pin 8.

  • First, you need to set the alignment of these two sensors.
  • Next, you need to set their detection range.

To do this, first turn on the GalaxyRVR Rover, then use your hand or an object to check if both sensors are detecting properly. If the distance is too short or too long, you can adjust the range using the potentiometers on the IR sensors.

After setting the alignment and range of the IR sensors, now we can start with the programming.

Reading IR Sensors:

First, we define the two pins our IR sensors are connected to. There are certain rules when it comes to defining variables, I have already covered it in my Arduino Course, it’s completely free.

I am sure you already know about the setup() function; it’s a special function that executes only once when the Arduino board is powered up or reset.

Since we are going to use IR Sensors for detecting objects and humans so that’s why we need to set the IR Sensor pins to “INPUT” mode, meaning they will receive the signals.

Next, you will need to activate the Serial communication, if you want to send messages to the computer and get them printed on the Serial monitor. We want to check if the IR Sensors detect objects.

Anyway, inside the parenthesis, you can see this number 9600, it’s the Baud Rate.

Loop() is also a special function like the setup() function. But, the loop() function runs repeatedly over and over again.

Since, the IR sensors on the GalaxyRVR Rover, we are using are digital; so, that’s why we are using digitalRead() functions). If we were using analog IR sensors, then we would use analogRead() functions.

So, we use the digitalRead() function to check if the IR sensor is blocked by any object or human. Let me tell you, when there is nothing in front of the IR sensor you get 1, and when it detects anything then it gives 0.

Anyway, after reading the IR sensors, then we print it on the Serial monitor.


IR Sensors on a master Robot

Both the IR sensors, successfully detected my hands. You can see 0s; it means both the IR sensors detected my hands.

Now, let’s turn the GalaxyRVR in an obstacle avoidance robot.

Obstacle Avoidance Robot programming:

We are going to use the same library and the same pins on the Arduino to control all the 6 motors. We can’t change these pins because of the Hardware limitations. They have already defined the connections. So, you will have to use the same pins when it comes to controlling the motors on the GalaxyRVR Rover.

The IR sensors pins definitions and code inside the setup() function remains exactly the same. The softPWMBegin() is for the PWM. If, you remember we used it in the first lesson.

Inside the loop() function, first we check the IR Sensors, and store the pins status in the variables rightValue and leftValue.

Next, we use if conditions to check if only one IR Sensor is blocked or both the IR sensors are blocked. 0 means there is something in front of the IR Sensor and 1 means there is nothing.

Take a look at the first condition; we are telling the controller to backRight, if there is something in front of the right IR sensor.

Then in the 2nd condition we are telling the controller to backLeft, if there is something in front of the Left IR sensor.

And in third condition, we are telling the controller to move the robot backward, you can also stop the robot.


If there is nothing in front of both the IR Sensors, then the controller moves the GalaxyRVR in the forward direction.

obstacle avoid robot using ir sensors- master robotics

Master Robotics, Lesson 2 Video:

Master Robotics Lesson 3: Ultrasonic Sensor

After covering the IR sensors; next, we are going to start with the HC-SR04 Ultrasonic Sensor, installed on the front of the GalaxyRVR Rover.

Ultrasonic Sensor on a robot - master robotics

I won’t talk about its technical specification and how it works; because I have already used in several projects.

Anyway, the Ultrasonic Sensor is connected to the Arduino pin 10. So, let’s first read this Ultrasonic Sensor and afterwards we will do some interesting projects.

Reading the Ultrasonic Sensor:

First, define a pin for the Ultrasonic Sensor. As I said, the ultrasonic sensor is connected to the Arduino pin 10.

In the setup() function, we simply activated the Serial communication and we are using 9600 baud rate.

In the loop() function that runs repeatedly, we started off by adding a delay of 4ms, otherwise the reading may be 0.

We set the Ultrasonic_pin to output to send a singal.

Then we clear the trigger pin.

Then we trigger the Ultrasonic Sensor by sending a high pulse for 10us.

Next, we set the trigger pin back to low.

Then we set it to INPUT to read the signal.

The PulseIn returns the duration of the pulse on the pin.

Then we calculate the distance in cm based on the speed of sound 340 m/s or 0.034 cm/us. And don’t forget to divide it by 2.

Finally we the distance value to the serial monitor.

reading an ultrasonic Sensor - master robotics

Now, let’s turn this into an obstacle avoidance robot using the Ultrasonic Sensor.

Obstacle Avoidance using Ultrasonic Sensor:

Again we are using the same library for the motors.

The same pins definitions for the Ultrasonic Sensor and motors pins.

Inside the setup() function, we simply activated the SoftPWM library.

Rest of the program is pretty straightforward. First we read the ultrasonic sensor. readSensorData() is a user-defined function and its return type is float. You can see it consist of the same set of instructions, I explained in example number1. This time it calculates the distance and then return the final calculated distance.

Then we use some if conditions to control the GalaxyRVR Rover based on the distance reading.

The first condition checks if the distance is greater than 50 then we call the moveForward() function.

And if the distance is less than 15 and greater than 1 then move the rover backward for half a second.

Next we turn the rover by calling the backLeft function.

moveForward(), moveBackward(), and backLeft(), these are user-defined functions; used for controlling the motors. I have already explained all these functions in Lesson 1.

I have talked in much detail about how to control the speed and direction of motors.

obstacle avoidance with ultrasonic sensor - master robotics

Right now, it can only sense objects that are in front of the Ultrasonic sensor. It totally ignores objects on the left and right sides. So, next we are going to use the Ultrasonic Sensor and IR sensors to make the smartest obstacle avoidance system. So, let’s do it

Obstacle avoidance Robot using Ultrasonic & IR Sensors:

This program is just the combination of two projects. The ultrasonic avoidance that you just saw and the IR avoidance system that I explained in lesson 2. You can clearly see, the same set of instructions are used. So, I think there is no need for me to explain anything.

obstacle avoidance robot using ultrasonic sensor and ir sensors - master robotics

Now, it can not only detect objects on the front side, but also on the right and left sides of the rover.

Now, let’s make the GalaxyRVR Rover to follow an object.

Robot follows an Object:

You can see this is the same exact program with some minor changes, this time instead of avoiding any object, it follows that object.

object follower robot using ultrasonic sensor and ir sensors - master robotics

Master Robotics, Lesson 3 Video:

Master Robotics Lesson 4: Servo Motor

After covering the Ultrasonic Sensor, next, we are going to start with this Micro Servo which is used to control the position of the ESP32 Camera module.

Servo motor with a camera - master robotics

Its range is from 0 to 180 degrees. A Servo is one of the most popular and most commonly used components. It is used in applications where precise movement and control are required. As you can see, on the GalaxyRVR Rover, we use it to control the position of this camera. You can also use it in RC Vehicles, in RC planes for controlling the ailerons and Rudder. You can also use it in automated manufacturing systems, and so on. And let me also tell you this servo is connected to the Arduino Uno pin 6.

Now, let’s start with a very basic program; we are going to control the position of this camera.

Servo Angle control:

First we add the Servo library which contains the code to control a servo.

Next, we create a servo object with the name myServo. You can define multiple Servo objects, if you need to control multiple Servo motors.

In the setup() function, we tell the controller that Servo is connected to pin 6 on the Arduino.

In the loop() function, we set the camera position at 45 degrees using myServo.write() function.

Upload the program; and then keep changing this values inside the parenthesis.

Upload the program; each time you change the angle.

Isn’t it so boring, changing the angle, and uploading the program again and again. Let’s use a loop.

Servo with for loop:

I slightly modified this program, you can see this time I added this for loop, to automatically increment the angle from 0 to 130. You might be wondering, why not 180? Because, I have practically tested it, the camera can only go to 130 degrees, otherwise the camera may get damaged by the body.

Upload this program and you will see the Servo; starting from 0 and then moves all the way to 130 degrees.

You can change the speed; by changing the delay value, currently its 100ms, to make it faster you can change this to a lower value let’s say 20ms. Try different values and you will get the idea.

Now, let’s control its position using Ultrasonic Sensor.

By default, I want the camera at zero degree, and when the ultrasonic Sensor detects an object, I want the camera to change its position towards the front. Let’s do it.

Servo and Ultrasonic Sensor:

This code is the combination of the Servo first example and the Ultrasonic Sonic based Avoidance system.

I only added these two conditions.

If the distance is less than or equal to 50 then move the Servo to 100 degrees.

camera angle control using a servo master robotics

and if the distance is greater than 50, or if there is nothing in front of the Ultrasonic Sensor then move the camera to its default position. Rest of the program is exactly the same.

servo for camera angle - master robotics

You can see, it’s working perfectly, but when I touch the robot body; it starts to act in an abnormal way. It may be due to, when I touch the robot body might introduce electrical noise or static electricity, which interferes with the ultrasonic sensor and servo control signals. Or it may be due to the grounding issues. The same thing I also noticed with the IR sensors. To avoid such issues, just keep your hands off the robot.

Master Robotics, Lesson 4 Video:

Master Robotics Lesson 5: Battery and RGB LED Strips

After covering the Servo Motor, next, we are going to start with the RGB LED strips and battery voltage monitoring.

rgb led strips on a robot - master robotics

Let’s first start with the two RGB LED strips attached underneath the robot body. And let me tell you, the two LED strips share the same pins (11, 12, and 13) on the Arduino Uno. Let’s start with our first example which is about turning the LED strips ON and OFF.

RGB LED strip Light up:

As usual for the PWM we have added the SoftPWM library. This is the same library for controlling the speed of all 6 motors.

These are the pins our LED strips are connected to.

In the setup() function, we activate the SoftPWM library.

Now, the actual code is in the loop function that runs repeatedly.

This instruction lights up the RED LEDs with full brightness. While all the other LEDs are OFF.

rgb leds control - master robotics

With this program, we can only turn ON the Red, Green, or Blue LEDs on the strips. To generate any color of our choice, for this, we will need to make some changes in this code.

Generate different colors using LED RGB strips:

This time we are going to use the setColor function to generate any color of our choice. Its just a user-defined function and it takes three inputs as the arguments. Its job is to mix these color, and it’s done by changing the Red, Green, and Blue color values.

Now; let’s control these RGB LED Strips using the Ultrasonic Sensor.

I want the Arduino to turn ON the RED Leds; when there is an obstacle in front of the Ultrasonic Sensor. And I want the Arduino to turn ON the Green LEDs when there is nothing in front of the Ultrasonic Sensor. So, let’s go ahead and take a look at the program.

RGB LED Strips and Ultrasonic Sensor:

This program is the combination of Ultrasonic Avoidance example and the RGB Led strip example. We simply measure the distance using the ultrasonic sensor and then use some if conditions to check the distance and then accordingly control the LED strips. Rest of the code, you can see, is exactly the same.

rgb leds and ultrasonic sensor - master robotics

By default, when there is nothing in front of the Ultrasonic Sensor; the Green LEDs are turned ON.

ultrasonic sensor and rgb leds on a robot- master robotics

And when the Ultrasonic Sensor detects any object then the Red LEDs are turned ON.

Now, let’s go ahead and measure the battery voltage.

Robot Battery Voltage Monitoring:

For the battery voltage monitoring we have this program. You can see the battery pin is connected to the Arduino Analog pin A3.

Then we have this user-defined function batteryGetVoltage(), its return type is float, and it doesn’t take any argument as the input.

We read the battery pin using the analogRead() function and then store the value in variable adcValue.

Next, we convert this value into voltage.

Next, we round the voltage value to two decimal places.

And then finally; we return the batteryVoltage.

We have another user-defined function to calculate the battery percentage based on its voltage.

First, we store the battery voltage in variable voltage.

Next, we map the voltage to a percentage and store it in the variable temp. 6.6 is the minimum battery voltage when it’s fully discharged, and 8.4 is the maximum battery voltage when the battery is fully charged.

This line of code ensures that the variable percentage is within the range of 0 to 100. If temp is less than 0, percentage will be set to 0. If temp is greater than 100, percentage will be set to 100. Otherwise, percentage will be set to the value of temp. You can read more about the min() and max() functions in the article.

This part of the code compares the value of temp with 100.

The min function returns the smaller of the two values.

If temp is greater than 100, min(temp, 100) will return 100.

If temp is less than or equal to 100, min(temp, 100) will return temp.

This part of the code takes the result from min(temp, 100) and compares it with 0.

The max function returns the larger of the two values.

If the result from min(temp, 100) is less than 0, max(min(temp, 100), 0) will return 0.

If the result from min(temp, 100) is greater than or equal to 0, max(min(temp, 100), 0) will return that result.

Finally, we return the percentage.

Inside the setup(), I have activated the serial communication and this time I am using 115200 as the baud rate.

Using the pinMode(), I have set the BATTERY_PIN as the INPUT.

And then in the loop() function, we send the battery actual voltage and percentage to the Serial monitor.

battery voltage and percentage monitoring on a robot-master robotic

Master Robotics, Lesson 5 Video:


Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

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

Back to top button