2. Move by Code

In the previous project, we have tried to control the operation of the motor by using different level signals for the input of the L9110 module.

If we modify the level signals through the program, then we can control the movement of the car in a flexible way.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

3 in 1 Starter Kit

380+

3 in 1 Starter Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

SunFounder R3 Board

BUY

L9110 Motor Driver Module

-

TT Motor

-

Wiring

Connect the wires between the L9110 module and the R3 board according to the diagram below.

L9110 Module

R3 Board

Motor

A-1B

5

A-1A

6

B-1B(B-2A)

9

B-1A

10

OB(B)

Black wire of right motor

OA(B)

Red wire of right motor

OB(A)

Black wire of left motor

OA(A)

Red wire of left motor

../_images/car_2.png

Code

Note

  • Open the 2.move.ino file under the path of 3in1-kit\car_project\2.move.

  • Or copy this code into Arduino IDE.

After the code is uploaded, the car will move forward, backward, left and right for two seconds respectively.

How it works?

This project is essentially the same as the previous one, involving making the car move forward, backward, left, and right, as well as stopping by providing different signal levels to the input pins of the L9110 module.

  1. Initialize the pins of L9110 module.

    const int A_1B = 5;
    const int A_1A = 6;
    const int B_1B = 9;
    const int B_1A = 10;
    
    void setup() {
        pinMode(A_1B, OUTPUT);
        pinMode(A_1A, OUTPUT);
        pinMode(B_1B, OUTPUT);
        pinMode(B_1A, OUTPUT);
    }
    
  2. Set the input pins to different high or low levels to control the rotation of the left and right motors, and then encapsulate them in individual functions.

    void moveForward() {
        digitalWrite(A_1B, LOW);
        digitalWrite(A_1A, HIGH);
        digitalWrite(B_1B, HIGH);
        digitalWrite(B_1A, LOW);
    }
    
    void moveBackward() {
        digitalWrite(A_1B, HIGH);
        digitalWrite(A_1A, LOW);
        digitalWrite(B_1B, LOW);
        digitalWrite(B_1A, HIGH);
    }
    ...
    
  3. Call these functions in loop().

    void loop() {
        moveForward();
        delay(2000);
        stopMove();
        delay(500);
    
        moveBackward();
        delay(2000);
        stopMove();
        delay(500);
    ...
    
  • digitalWrite(pin, value)

    • pin: the Arduino pin number.

    • value: HIGH or LOW.

    Write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

  • pinMode(pin, mode)

    • pin: the Arduino pin number to set the mode of.

    • mode: INPUT, OUTPUT, or INPUT_PULLUP.

    Configures the specified pin to behave either as an input or an output.

  • delay(ms)

    • ms: the number of milliseconds to pause. Allowed data types: unsigned long.

    Pauses the program for the amount of time (in milliseconds) specified as parameter. (There are 1000 milliseconds in a second.)