Python Based Computer Controlled Mars Rover

Example Description
In this tutorial, you'll discover how to use Python programming to control the Mars Rover. You'll learn how to use different keys on your keyboard to move the rover forward, backward, turn left, and turn right.

In this activity, we will make the computer program that controls the Mars Rover. It’s like a remote-control car. You can press different keys on the keyboard to make the Mars Rover move forward, backward, turn left and turn right.

Motor and Servo Motor

In our Mars rover, there are a total of 6 motors and 5 servo motors. 

The motors provide rotation to the wheels which helps the rover to attain motion in both forward and backward directions. All the left side motors (3 motors) are connected to the left motor port of Quarky and all the right side motors (3 motors) are connected to the right motor port of Quarky using a 3 port wire. This means that to control the Mars rover we have to control only 2 motors – Left and Right.

Also, there are 2 parameters to control – Direction (Forward or Backward) and Speed. With this control, the Mars rover can do all the desired motions.

The servo motors help in providing rotation to the complete wheel assembly so that the rover can change its wheel alignments and its path. These play a major role in turning cases of the Mars Rover.

We will need to turn the servo motors to the Inside Servo Position to make Mars Rover turn left and right.

Python Code

  1. We will make sure we have initialized Quarky and Mars Rover correctly.
  2. We will use the while loop to continuously detect the keys of the keyboard and react according to the way.
  3. We will use the python function of sprite “.iskeypressed()” to know which key has been pressed and act respectively.
  4. When the up arrow key is pressed, we will set all the servos to 90 degree angle with the help of the rover.home() function. We will run the mars rover with the help of quarky.runtimedrobot() function.
  5. With the same set of functions we will create the structure for when the users press the backward key to create the respective motion.
  6. When the user presses the right or left arrow key, we will set the servo angles to a specific degree – 40 to complete the turn successfully.
  7. By this way we will be able to turn the rover according to our needs by using the runtimedrobot()  function.
import time
quarky = Quarky()
rover = MarsRover(4, 1, 7, 2, 6)
while True:
  if sprite.iskeypressed("up arrow"):
  if sprite.iskeypressed("down arrow"):
  if sprite.iskeypressed("right arrow"):
  if sprite.iskeypressed("left arrow"):


Forward-Backward Motions

Normal Right-Left Motions

Table of Contents