Skip to content

Search the documentation

Kit
Firmware

Your first program

On this page you run one real program on your robot. There is one for each kit. Each program is the first part of the first Python example of that kit. The code is copied exactly from the example files. The whole example, with more parts to try, is on its own page.

The code is shown twice, for firmware 3.0 and for firmware 2.x. Pick the tab that matches your robot. If you are not sure, see Firmware versions. The tab you pick is remembered on the other pages of this site.

  1. Connect your robot to the web IDE or the desktop IDE. MoBot Blocks cannot open Python projects.
  2. Choose File ▸ Examples. Pick your board, then switch from Blocks to Python, and open the first example of your kit. The card says For firmware 3.0 or For firmware 2.x. The IDE picks the one for the robot you connected. With no robot connected, it uses the last robot it saw, or 3.0.
  3. Press Run to try the program once without saving it. Or press Save to put it on the robot as main.py. After the save, the IDE starts it. You do not need to press the robot’s reset button.
  4. Read the lines the program prints in the terminal under the editor.

You can also type the program yourself. Open the Code editor, paste it into a file called main.py, and press Run.

The first example is Drive a square. This is part 1 of it: the wheels speed up while you hold the robot in the air. The numbers SPEED, SIDE_SECONDS and TURN_SECONDS at the top are used by the rest of the example.

main.py
import time
# Turn these numbers into your own robot. Bigger SPEED, bigger square.
SPEED = 45 # how hard the wheels push, from 0 to 100
SIDE_SECONDS = 1.2 # how long one side of the square takes
TURN_SECONDS = 0.6 # how long a corner takes - tune this one first
left = robot.left_motor
right = robot.right_motor
def halt():
"""Stop both wheels.
stop() lets the wheels roll to a halt. brake() would hold them still
instead - try it and listen for the difference.
"""
left.stop()
right.stop()
# ---------------------------------------------------------------- part 1 ----
# A speed ramp, with the robot HELD IN THE AIR so it cannot run away.
#
# forward(speed) chooses the way AND the speed, in one go. set_speed(speed)
# only changes how fast: the wheels keep turning the way they already were.
print("Part 1: hold the robot in the air. The wheels will speed up.")
robot.led1.on()
time.sleep(3)
left.forward(20)
right.forward(20)
for speed in (20, 40, 60, 80, 100):
print(" speed", speed)
left.set_speed(speed)
right.set_speed(speed)
time.sleep(0.8)
halt()
robot.led1.off()

What you should see and hear:

  • At once, the terminal says Part 1: hold the robot in the air. and LED 1 comes on.
  • After 3 seconds both wheels start turning forward and get faster five times. You can hear the motors climb. The terminal prints speed 20, 40, 60, 80 and 100.
  • Then the wheels stop and LED 1 goes out.

If the messages appear but the wheels never move, the program is running. The problem is the motors, the battery or the wiring, not your code.

In the 3.0 code, left.forward(20) chooses the way and the speed in one go. left.set_speed(speed) only changes how fast, so the wheel keeps turning the way it was. The 2.x code has to set the direction and the speed separately. See Firmware versions for what changed.

The rest of the example drives a square on the floor. Open Drive a square to run all of it.

The first example is Drive the Mini. This is the first part of it: the wheels go forward, faster, backward, and then roll to a stop.

main.py
import time
left = robot.left_motor
right = robot.right_motor
# -------------------------------------------------------------------- 1 -----
# Hold the Mini in the air for this bit.
print("Part 1: hold the Mini in the air.")
robot.led1.on()
time.sleep(3)
robot.led1.off()
print("Both wheels forward, slowly...")
left.forward(30)
right.forward(30)
time.sleep(1.5)
print(" left wheel says: speed", left.speed)
print(" right wheel says: speed", right.speed)
print("Faster - set_speed() changes the speed and keeps the way...")
left.set_speed(70)
right.set_speed(70)
time.sleep(1.5)
print("Now backwards...")
left.backward(70)
right.backward(70)
time.sleep(1.5)
print(" left wheel says: speed", left.speed, " - negative means backwards")
# -------------------------------------------------------------------- 2 -----
# stop() and brake() are NOT the same thing, and you can hear the difference.
print("stop() - the wheels are let go and roll to a halt.")
left.stop()
right.stop()
time.sleep(2)

What you should see and hear:

  • LED 1 stays on for three seconds. Pick the Mini up.
  • Both wheels turn forward, slowly. The terminal prints what the wheels say about themselves: speed 30 on firmware 3.0, and speed 30 direction 0 on firmware 2.x.
  • Then the wheels go faster in the same direction.
  • Then both wheels go backward. On firmware 3.0 the left wheel now says speed -70. The minus sign means backward.
  • At the end, stop() lets the wheels go, and they coast to a halt. You can still turn them with a finger.

If you save this program and the Mini does nothing, look at the terminal when the Mini starts. A Mini in another mode with a program on it says [MOBOT] The robot is in ... mode, so your program is not running. Choose Programmable in the MoBot app. The program is fine. The Mini never reached it. Set the Mini to Programmable and try again.

Copying a Smart Car program onto a Mini does not work on firmware 2.x. The 2.x Mini uses mobotMINI and different spellings, such as set_direction_and_speed. On firmware 3.0 both robots use robot, and the motor lines are the same.

The rest of the example shows brake(), which stops the wheels harder than stop(), and a wheel that is fitted the wrong way round. Open Drive the Mini to run all of it.

The first example is Hello, house. Run it first on a new Smart Home board. This is part 1 of it: the screen. The line that imports SEQUENCES is for the buzzer part, later in the example.

main.py
import time
from sounds import SEQUENCES
display = home.display
lamp = home.rgb_led
# ------------------------------------------------------------------ 1 -------
# The screen. 128 dots across, 64 down, and the letters are 8 dots wide -
# so a line of text can be 16 letters long and there is room for 8 lines.
print("Part 1: the screen.")
display.clear()
display.show_text("Hello!", 0, 0)
display.show_text("I am your house", 0, 16)
time.sleep(2)
display.clear()
display.draw_rectangle(4, 4, 120, 56) # a frame round the edge
display.draw_line(4, 32, 124, 32) # a horizon
display.draw_circle(32, 18, 10, filled=True) # a sun
display.draw_triangle(70, 30, 100, 30, 85, 10) # a roof - no block for this
time.sleep(3)
# The three built-in faces, big. show_emoji() clears the screen first.
for face in ("happy", "sad", "love"):
display.show_emoji(face, scale=3)
time.sleep(1.2)
display.show_logo() # the MoBot logo
time.sleep(2)

What you should see:

  • The screen says Hello! and I am your house.
  • Then it draws a picture: a frame, a horizon line, a filled sun and a triangle roof.
  • Then three big faces, happy, sad and love, and the MoBot logo.

The colour light, the buzzer, the fan and the servo come in the later parts of the same example. At the very end the colour light blinks green three times and the screen shows the happy face. Open Hello, house to run all of it. If every part of it works, the board is healthy.