Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
đ Ready to explore and create with us? Click [here] and join today!
Lesson 4: Mastering the TT Motorď
In the previous lessons, we explored Mars rovers, their suspension systems, and delved into knowledge about Arduino.
In this exciting course, weâll explore the workings of motors, a key component that drives Mars rovers. Weâll understand the principles that power these motors and learn to control them using SunFounder R3 board and a GalaxyRVR Shield.
By the end of this course, youâll have a solid understanding of motor operation and hands-on experience in motor control.
Letâs dive in!
Note
If you are learning this course after fully assembling the GalaxyRVR, you need to move this switch to the right before uploading the code.

Course Objectivesď
Understand the basic principles of motors and the characteristics of the TT motor.
Learn how to control the direction and speed of the TT motor.
Understand how the GalaxyRVR Shield controls six motors.
Course Materialsď
SunFounder R3 Board
TT Motor
GalaxyRVR Shield
Battery
USB Cable
Arduino IDE
Computer
Course Stepsď
Step 1: What is a Motor?
Motors play an integral part in our daily lives. Theyâre everywhere! From the electric fans that cool us on hot days, the mixers that help us make delicious cakes, to the electric cars that whizz by on the streets â motors make things move!

A motor is like the heart of a machine. It converts electrical energy into mechanical energy, making our toys, appliances, and even big vehicles come to life!
The magic behind a motor isnât magic at all - itâs science, specifically the principle of electromagnetic induction. Hereâs how it works: when electricity is supplied to a motor, it generates a magnetic field. This magnetic field then interacts with other magnets within the motor, causing the motor to spin. This spin, like spinning a top, can then be used to move wheels, propellers, or any other moving parts of a machine.

The type of motor weâre focusing on in our GalaxyRVR is a specific kind called a TT Gear Motor.

This is essentially a regular motor combined with a series of gears, all encased within a plastic shell.
As the motor spins, the gears translate this spin to the wheels of our rover. The use of gears provides a crucial benefit - it increases torque, enabling the motor to move larger, heavier loads.

Isnât it fascinating to see how science and engineering principles come to life? Motors are a perfect example of these principles in action. By understanding how motors work, we can dream up and invent a wide array of machines. Letâs dive deeper into the world of motors and unleash our creativity!
Step 2: Exploring Motor Functioning and Operation
Having understood what a motor is and its broad spectrum of applications, itâs time we venture into the heart of motor operation.
In essence, a motor works on the principle of electromagnetism. When an electric current passes through a wire, it generates a magnetic field around it. This magnetic field can interact with other magnetic fields, causing motion.
Consider a simple experiment where we connect a motor directly to a battery. The current from the battery flows into the motor, triggering the internal mechanism of the motor to start spinning. This spinning action is due to the magnetic forces inside the motor.
Interestingly, if you reverse the connections to the battery, the motor spins in the opposite direction! This happens because the direction of current flow changes, altering the direction of the magnetic field and consequently the direction of the motorâs spin.
Now we know that connecting the motor directly to a battery can make it spin, but often we want to control its movement with code, so we include an Arduino board between them. But what would happen if we tried to connect the motor directly to the signal pins on the Arduino board?

If you guessed that the motor would not spin, you are correct! But why is that so?
The answer lies in the current output of the Arduino board. The signal pins on a typical Arduino board can output only about 20mA of current, which is insufficient to drive a motor.
So, how can we control motors using our Arduino? This is where a crucial component comes into the picture - a motor driver. Think of a motor driver as a bridge between the Arduino and the motor. It takes the low-current control signal from the Arduino, amplifies it, and sends it to the motor, thus enabling the motor to spin.

In our next step, weâll dive into the specifics of the motor driver and understand how we can effectively use it with our Arduino board to control a motor. Stay tuned for more exciting learning!
Step 3: How the Motor is controlled by the Motor Driver
Our GalaxyRVR Shield, included in the kit, serves as the control center for our Mars Rover. It is the hub where we connect all our sensors, motors, and power supply. It consists of several components that allow us to control and power our Rover effectively.
On the right side of the shield, youâll notice six motor ports. However, they are grouped into two sets, each controlled by a separate motor drive chip. Three ports marked âLeftâ are controlled by one chip, and the other three ports marked âRightâ are controlled by another.

Letâs learn how these two drive chips control the six motors through hands-on experience:
1. Connecting the Circuit
Plug the GalaxyRVR Shield into the R3 board, connect a motor, and finally plug in the battery to provide power to the expansion board.
The first time you use, it is recommended that you plug in a Type-C USB cable to fully charge the battery first. Then turn the power on.
2. Writing and Uploading Code
Open the Arduino IDE and input the following code:
void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); } void loop() { digitalWrite(2, LOW); digitalWrite(3, HIGH); }
pinMode()
: This function sets a pin as INPUT or OUTPUT, akin to deciding whether a character in our story speaks (OUTPUT) or listens (INPUT).digitalWrite()
: This function can set a pin HIGH (on) or LOW (off), much like switching a magic light on and off.
Once youâve selected the correct board(Arduino Uno) and port, click on the Upload button. Itâs like putting a letter in a mailbox - youâre sending your instructions off to Arduino!
Once the code has been successfully uploaded, you will see the motor start to rotate clockwise.
3. About Circuit Internal Connection
You can plug two more motors into the âLeftâ marked motor ports. You will see them rotate simultaneously.
Now, letâs understand the simple principle of how the two drive chips control the six motors. Pins 2 and 3 on the Arduino board output signals to the motor drive chip, and the other end of the chip is connected to three motors in parallel. Similarly, pins 4 and 5 output signals to another drive chip, which in turn is connected to another three motors in parallel.
If you want to test another drive chip, you just need to change the pins to
4
and5
.const int in3 = 4; const int in4 = 5; void setup() { pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void loop() { digitalWrite(in3, LOW); digitalWrite(in4, HIGH); }
Here, we define two variables to represent pins 4 and 5. By using variables, we can easily manage and adjust our pin assignments throughout our code.
Think of it as if weâre assigning a specific role or duty to each pin number. When we decide to reassign the roles, instead of going through the entire script and changing every instance, we just update the assignment at the beginning of the script (where the variable is initially defined).
4. About Drive Logic
In the previous tests, you would have noticed that the motors all spin in one direction. How do we make it spin in the opposite direction? Someone might suggest swapping the HIGH and LOW of the two pins. Thatâs correct.
const int in3 = 4; const int in4 = 5; void setup() { pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void loop() { digitalWrite(in3, HIGH); digitalWrite(in4, LOW); }
Once youâve written your code and uploaded it to your Arduino board, the motor will behave as instructed.
Letâs now look at the internal driving logic of the drive chip.
INA
INB
Motor
L
L
Standby
L
H
Clockwise
H
L
Counterclockwise
H
H
Brake
Now, letâs try to make the motor rotate clockwise for 2 seconds, counterclockwise for 2 seconds, and then stop.
const int in3 = 4; const int in4 = 5; void setup() { pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void loop() { digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(2000); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); delay(2000); digitalWrite(in3, HIGH); digitalWrite(in4, HIGH); delay(5000); }
Here we use the
delay()
function to make the Arduino pause for a certain amount of time, much like taking a short nap in the middle of our story.In the code, we use the âBrakeâ state to stop the motor, and youâll notice that the motor stops abruptly. Try setting both pins to LOW to test the âStandbyâ state, and youâll find that the motor gradually slows down to a stop.
Now that you should have a better understanding of how the motor driver chip controls the motors through the GalaxyRVR Shield and how we can use Arduino code to manipulate the motorâs movements. Isnât it fascinating how a few lines of code can dictate the behavior of a physical object like our motor?
Consider the following questions as you move forward:
If we move all the code from the
loop()
function into thesetup()
function, how would the behavior of the motor change?How would you modify the code to control six motors simultaneously?
Remember, the more you experiment and play around with your code, the more you learn. Feel free to tweak, modify, and optimize your code as you deem fit. Happy coding!
Step 4: Controlling Motor Speed
In the previous step, we controlled the motorâs direction by simply setting its pins HIGH or LOW. This is like giving the motor full power to drive it, similar to pressing the accelerator pedal to the floor in a car. But in many situations, we might want to adjust the motor speed to suit different scenarios, just like we adjust the speed of a car depending on whether weâre driving in a city or on a highway. This is where Pulse Width Modulation (PWM) comes in.

PWM is a technique used to create the effect of variable voltage output by rapidly switching the output between HIGH and LOW. With PWM, we can simulate the effect of an analogue signal while only actually outputting digital signals.
You might be finding this hard to understand, and thatâs okay! Weâll be learning how to adjust motor speed using PWM through coding in the following sections.
Note that although the SunFounder R3 board has some pins with built-in PWM functionality, we canât use them for our motor because theyâre already serving other functions. Thus, weâre connecting the driver chips to pins 2, 3, 4, and 5, and using the Arduinoâs SoftPWM library to enable PWM on these pins.
Hereâs what weâll do next:
Open Arduino IDE, search for
softpwm
in the LIBRARY MANAGER and install it.Enter the following code into Arduino IDE. After uploading the code successfully, the motor will rotate clockwise.
#include <SoftPWM.h> const int in1 = 2; const int in2 = 3; void setup() { SoftPWMBegin(); } void loop() { SoftPWMSet(in1, 0); SoftPWMSet(in2, 255); }
In the code above, we first add
SoftPWM.h
to the top of the code, enabling us to use the functions in theSoftPWM
library directly.Then, initialize the
SoftPWM
library withSoftPWMBegin()
function.Finally, in the
loop()
function, we useSoftPWMSet()
to assign different values toin1
andin2
, setting the motor in motion. You will notice the effect is similar to directly usingLOW
andHIGH
, but here we use numerical values within a range of0~255
.Remember, in the world of Arduino, speed is expressed as a value between 0 (like a car at a stop sign) and 255 (zooming down the highway!). So, when we say
SoftPWMSet(in2, 255)
, weâre telling that motor to go full speed ahead!
Now, letâs enter other values and observe any differences in motor speed.
#include <SoftPWM.h> const int in1 = 2; const int in2 = 3; void setup() { SoftPWMBegin(); } void loop() { SoftPWMSet(in1, 0); for (int i = 0; i <= 255; i++) { SoftPWMSet(in2, i); delay(100); } delay(1000); }
In the code above, we use a
for
loop to increment a variablei
up to255
. Thefor
loop in C language is used to iterate over a part of the program several times. It consists of three parts:Initialization: This step is executed first and only once when we enter the loop for the first time. It allows us to declare and initialize any loop control variables.
Condition: This is the next step after initialization. If itâs true, the body of the loop is executed. If itâs false, the body of the loop does not execute and the flow of control goes outside of the for loop.
Increment or Decrement: After executing the Initialization and Condition steps and the loop body code, the Increment or Decrement step is executed. This statement allows us to update any loop control variables.
The flowchart for the for loop is shown below:
So, after running the above code, you will see the motor speed gradually increasing. It stops for a second, and then starts again from 0 and gradually increases.
In this step, we have learned about Pulse Width Modulation (PWM), a technique for controlling the speed of our motor. By using the Arduinoâs SoftPWM library, we can adjust the speed of the motor, allowing us to simulate analogue signals while only outputting digital signals. This provides us with finer control over our roverâs movements, and prepares us for more complex maneuvers in the future.
Step 5: Reflect and Improve
Having completed this lesson, you should now be familiar with the working principles of motors, as well as how to control their direction and speed through programming.
Letâs test your understanding with these challenges:
How would you modify the for loop to gradually decrease the motor speed?
How would you control the motor to accelerate or decelerate while rotating counterclockwise?
You can experiment with the provided code to answer these questions. Feel free to adjust the code according to your hypotheses and observe the changes in the motorâs behavior.
Your hands-on experiments and reflections on these questions will deepen your understanding and enhance your problem-solving skills. It is through challenges like these that real learning occurs. Always remember, there is no ârightâ or âwrongâ in your exploratory journey â this is all about learning and discovery!