Programming M.A.R.S. Rover on Raspberry Pi Zero

Programming the M.A.R.S. Rover with Python

Preparing your Raspberry Pi Zero

Before you can install theRover software, you will need to ensure that your Raspberry Pi Zero is up and running and connected to the internet. You will need to be able to see the desktop or console (either using direct connections, or SSH or VNC over the network).

Setting up a Raspberry Pi is not covered here. It is best to get the information from the source at Raspberry Pi

You will also need to enable SPI and I2C. Do this from raspi-config

sudo raspi-config

Select interfaces and enable both SPI and I2C. Then reboot.

Installing the M.A.R.S. Rover Python Software

First you will need to prepare your Pi for the Fireleds (fully compatible with neopixels). The best way we’ve found to do this is run Pimoroni’s curl script.
With your Pi connected to the internet, run (you don’t need to install the resources and examples):

curl -sS | bash

Download the Python library module and example software for M.A.R.S. Rover with

wget -O


This installs the following, which should be run for a command line (not in Idle etc.)

  • Creates a folder home/pi/marsrover for all the example files and library module (the main library module)
  • to demonstrate driving the motors. Must be run in a terminal, not from an IDE like Idle or Thonny. Use LXTerminal for this
  • to demonstrate controlling the servos. Must be run in a terminal, not from an IDE like Idle or Thonny. Use LXTerminal for this
  • flashes all LEDs through Red, Green, Blue and White. This must be run using sudo. ie: sudo python
  • shows the distance in cm for an obstacle using the ultrasonic distance sensor mounted on the mast head
  • shows the numeric value of each key pressed on th eoptional keypad
  • is a basic driving program using the arrow keys to steer and move

Using the Library Module

To use this module, you must first import it into your program using

import rover

Then, before using any other functions you should call the init ( ) function to initialise all the variables used by the library. The init ( ) function has two optional parameters.

1. Default brightness of the LEDs. If omitted, this defaults to 40. So the following call will initialise the library and set the default brightness to 100:

rover.init (100)

2. Using the PiBit to convert a Microbit Rover to Pi Zero. This parameter is only needed if using a PiBit and causes the pins used to drive the motors to be swapped. For a PiBit, use:

rover.init(PiBit = True), or rover.init(100, PiBit=True)

Hot Tip:
As a special case, if you set the brightness to zero, then the LEDs are not initialised. As this would require use of the sudo command, it can be beneficial to call init (0) so that your program can be run without using sudo.

When your program has finished, it is good practice to close down everything tidily by calling:

rover.cleanup ( )

Motor Functions

  • stop ( ): Stops all motors – coast slowly to a halt
  • brake ( ): Stops all motors – brakes quickly
  • forward (speed): Sets all motors to move forward at speed. 0 <= speed <= 100
  • reverse (speed): Sets all motors to reverse at speed. 0 <= speed <= 100
  • spinLeft (speed): Sets left and right motors to turn opposite directions at speed. 0 <= speed <= 100
  • spinRight (speed): Sets left and right motors to turn opposite directions at speed. 0 <= speed <= 100

Fireled Functions

  • setColor (color): Sets all LEDs to color – requires show()
  • setPixel (ID, color): Sets pixel ID to color – requires show()
  • show ( ): Updates the LEDs with state of LED array
  • clear ( ): Clears all LEDs to off – requires show()
  • rainbow ( ): Sets the LEDs to rainbow colors – requires show()
  • fromRGB (red, green, blue): Creates a color value from R, G and B values
  • toRGB (color): Converts a color value to separate R, G and B
  • wheel (pos): Generates rainbow colors across 0-255 positions

UltraSonic Function

  • getDistance ( ). Returns the distance in cm to the nearest reflecting object. 0 == no object.

    Keypad Functions

  • getKey ( ). Waits for a keypress on the optional touch keypad and returns the value of the keypad pressed. The relevant bit is set for any keys pressed (16 keys, so a 16-bit value is returned)