Skip to content

Search the documentation

Kit
Firmware

Which way do the wheels turn? — Smart Car

Two blocks, used apart: Set Motor … speed decides how fast, and Set Motor … to Direction 1 / Direction 2 decides which way. This program sets the speed once at the start, then changes only the direction, so you can see that they really are two separate things.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Lift the robot up! The wheels are about to move."
  • wait 2 seconds
  • set Motor 1 speed 50 %
  • set Motor 2 speed 50 %
  • Print to serial "Both wheels: Direction 1"
  • set Motor 1 to Direction 1
  • set Motor 2 to Direction 1
  • wait 3 seconds
  • Print to serial "Both wheels: Direction 2"
  • set Motor 1 to Direction 2
  • set Motor 2 to Direction 2
  • wait 3 seconds
  • Print to serial "Spinning: left one way, right the other"
  • set Motor 1 to Direction 1
  • set Motor 2 to Direction 2
  • wait 3 seconds
  • Print to serial "Stop."
  • set Motor 1 speed 0 %
  • set Motor 2 speed 0 %
  • A Smart Car with both wheels fitted.
  • A USB cable.
  • A book, a mug or a box to stand the robot on so the wheels are off the table. The wheels spin for nine seconds — on the floor the robot drives away.
  1. Stand the robot on the book, wheels in the air.
  2. Open 02-motor-directions.mobot, plug in, press Send to Device.
  3. Watch the wheels and the serial monitor together.
serial line what the wheels do
Lift the robot up! … nothing yet — you have 2 seconds
Both wheels: Direction 1 both wheels turn the same way for 3 seconds
Both wheels: Direction 2 both wheels turn the other way for 3 seconds
Spinning: left one way, right the other the wheels fight each other — on the floor the robot would spin on the spot
Stop. both wheels stop and stay stopped

The whole thing takes about 11 seconds and then the robot is quiet.

The test passes if every line in the serial monitor matches what the wheels actually do, and if Direction 1 and Direction 2 are genuinely opposite on both wheels.

Why “Direction 1” and “Direction 2” and not “forwards”?

Section titled “Why “Direction 1” and “Direction 2” and not “forwards”?”

Because which one is forwards depends on how the motors were wired into your robot. Run this once, write on a sticky note which one drives your robot forwards, and use that from then on. If one wheel goes the wrong way, its two motor wires are swapped — that is a wiring job, not a program bug.

block what it does
mobot_motorspeed 0 to 100 %. 0 stops the wheel.
mobot_motordirection Direction 1 or Direction 2, without touching the speed.
mobot_print, wait so you can follow along.

The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.

02-motor-directions.firmware-3.main.py
# Which way do the wheels turn? — robotics (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 02-motor-directions.mobot and press Send to Device. It is GENERATED from the blocks;
# editing this file changes nothing. Edit the blocks instead.
# The firmware 2.x version is 02-motor-directions.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Lift the robot up! The wheels are about to move.')
time.sleep(2)
robot.left_motor.set_speed(50)
robot.right_motor.set_speed(50)
print("[MOBOT]", 'Both wheels: Direction 1')
robot.left_motor.backward(abs(robot.left_motor.speed))
robot.right_motor.backward(abs(robot.right_motor.speed))
time.sleep(3)
print("[MOBOT]", 'Both wheels: Direction 2')
robot.left_motor.forward(abs(robot.left_motor.speed))
robot.right_motor.forward(abs(robot.right_motor.speed))
time.sleep(3)
print("[MOBOT]", 'Spinning: left one way, right the other')
robot.left_motor.backward(abs(robot.left_motor.speed))
robot.right_motor.forward(abs(robot.right_motor.speed))
time.sleep(3)
print("[MOBOT]", 'Stop.')
robot.left_motor.set_speed(0)
robot.right_motor.set_speed(0)
  • Set the speed to 20 instead of 50 and run it again. What happens depends on your robot. On a standard Smart Car (the DRV8833 motor chip) 20 % really is 20 %, and the wheels may just buzz without moving. On an early Smart Car (the L298N motor chip) the wheels turn slowly but they still turn, because that firmware never lets a moving wheel go below about half power, so 20 % is not as slow as you might expect. Both are correct — 03-speed-ramp explains why.
  • Set only the left motor’s speed to 0 and leave the right at 50: the robot turns instead of driving straight.