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 program
Section titled “The program”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 %
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”- Stand the robot on the book, wheels in the air.
- Open 02-motor-directions.mobot, plug in, press Send to Device.
- Watch the wheels and the serial monitor together.
What you should see and hear
Section titled “What you should see and hear”| 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.
The blocks
Section titled “The blocks”| 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 Python code
Section titled “The Python code”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.
# 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 Pinimport 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)# Which way do the wheels turn? — robotics## This is what MoBot puts on the board 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.
from machine import Pinimport time
print("[MOBOT]", 'Lift the robot up! The wheels are about to move.')time.sleep(2)mobotROBOTICS.left_motor.setSpeed(50)mobotROBOTICS.right_motor.setSpeed(50)print("[MOBOT]", 'Both wheels: Direction 1')mobotROBOTICS.left_motor.setDirection(0)mobotROBOTICS.right_motor.setDirection(0)time.sleep(3)print("[MOBOT]", 'Both wheels: Direction 2')mobotROBOTICS.left_motor.setDirection(1)mobotROBOTICS.right_motor.setDirection(1)time.sleep(3)print("[MOBOT]", 'Spinning: left one way, right the other')mobotROBOTICS.left_motor.setDirection(0)mobotROBOTICS.right_motor.setDirection(1)time.sleep(3)print("[MOBOT]", 'Stop.')mobotROBOTICS.left_motor.setSpeed(0)mobotROBOTICS.right_motor.setSpeed(0)Try changing it
Section titled “Try changing it”- 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.